JSPM

@injectivelabs/sdk-ts

1.0.125
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 36237
    • Score
      100M100P100Q149305F
    • License MIT

    SDK in TypeScript for building Injective applications in a Node environment.

    Package Exports

    • @injectivelabs/sdk-ts
    • @injectivelabs/sdk-ts/dist/client
    • @injectivelabs/sdk-ts/dist/client/chain
    • @injectivelabs/sdk-ts/dist/client/chain/index.js
    • @injectivelabs/sdk-ts/dist/client/exchange
    • @injectivelabs/sdk-ts/dist/client/exchange/index.js
    • @injectivelabs/sdk-ts/dist/client/exchange/types/derivatives
    • @injectivelabs/sdk-ts/dist/client/exchange/types/derivatives.js
    • @injectivelabs/sdk-ts/dist/client/exchange/types/spot
    • @injectivelabs/sdk-ts/dist/client/exchange/types/spot.js
    • @injectivelabs/sdk-ts/dist/client/index.js
    • @injectivelabs/sdk-ts/dist/client/indexer/types/derivatives
    • @injectivelabs/sdk-ts/dist/client/indexer/types/derivatives.js
    • @injectivelabs/sdk-ts/dist/client/indexer/types/spot
    • @injectivelabs/sdk-ts/dist/client/indexer/types/spot.js
    • @injectivelabs/sdk-ts/dist/core
    • @injectivelabs/sdk-ts/dist/core/index.js
    • @injectivelabs/sdk-ts/dist/index.js
    • @injectivelabs/sdk-ts/dist/utils
    • @injectivelabs/sdk-ts/dist/utils/index.js
    • @injectivelabs/sdk-ts/dist/utils/pagination
    • @injectivelabs/sdk-ts/dist/utils/pagination.js
    • @injectivelabs/sdk-ts/dist/utils/transaction
    • @injectivelabs/sdk-ts/dist/utils/transaction.js

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@injectivelabs/sdk-ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    ๐ŸŒŸ Injective Protocol - SDK TS

    downloads npm-version license

    Accessing decentralized finance through TypeScript (for Web and Node environment)

    @injectivelabs/sdk-ts is a TypeScript SDK for writing applications on top of the Injective chain in both a Node.js and a browser environment.

    Documentation ยท Examples ยท API Reference ยท NPM Package ยท GitHub

    โœจ Features

    • Written in TypeScript, with type definitions,
    • Works in Node.js and in the browser,
    • Exposes on-chain data and the exchange-api data,
    • Parses responses into native JavaScript types
    • much more ...

    We highly suggest using the @injectivelabs/sdk-ts with TypeScript, or JavaScript in a code editor that has support for type declarations, so you can take advantage of the helpful type hints that are included with the package.

    ๐Ÿ“š Installation

    yarn add @injectivelabs/sdk-ts

    ๐Ÿ“– Documentation

    There are two pieces of the sdk-ts - querying a data source and making transactions.

    Querying a data source

    There are 2 data sources that can be accessed through the sdk-ts:

    • The Injective chain itself through a sentry node,
    • The Indexer API (indexer of events from the Injective chain to a MongoDB),

    For each of the data sources there are two ways that they can be queried:

    • using the gRPC protocol,
    • using REST

    We also have a GraphQL consumer of the Peggy subgraph on Ethereum (used only for tracking deposits and withdrawals on Ethereum).

    For the 2 main data sources, there are abstraction classes that developers can use to access specific modules of the Injective Chain or specific modules within the Exchange API. The responses of these requests are always mapped into normal JavaScript objects (regardless of the data source type) and served to the end user.

    Making Transactions

    To interact with Injective, the user has to issue a transaction. Each transaction that is broadcasted to Injective can hold multiple messages that can change the state of the Injective chain. Within the core folder, developers can find a convenient way to make these messages with methods that can convert these messages in a way that they can be used by developers in different scenarios (broadcasting using the normal cosmos way or converted to EIP712 to be signed using Ethereum wallets).

    Every message extends the MsgBase interface, which has couple of mapping functionalities:

    • toData -> Converts the Message to a simple Object representation,
    • toProto -> Returns a proto representation of the Message,
    • toDirectSign -> Converts the Message to a proto representation (ready to be used in the normal Cosmos way of handling transactions),
    • toAmino -> Converts the Message to a amino representation + type (usually used to covert the message to EIP712 typed data to be signable in Ethereum native wallets),
    • toWeb3 -> same as toAmino (deprecated)

    There are also some utility classes and functions that are exposed from the package. There is also a local folder that exposes some utility classes that can be used to make developers life easier in a Node environment.


    ๐ŸŽ’ Usage

    Let's go through couple of use-cases of the sdk-ts so developers can have a reference codebase that they can look at.

    Consuming data

    • Fetching user's inj balance from the chain
    // Importing only the needed API
    import { ChainGrpcBankApi, Network } from '@injectivelabs/sdk-ts'
    
    const network = Network.testnet()
    const injectiveAddress = 'inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r'
    const denom = 'inj'
    const chainGrpcBankApi = new ChainGrpcBankApi(network.sentryGrpcApi)
    console.log(await chainGrpcBankApi.fetchBalance({ injectiveAddress, denom }))
    // Using the client
    import { ChainGrpcClient, Network } from '@injectivelabs/sdk-ts'
    
    const network = Network.testnet()
    const injectiveAddress = 'inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r'
    const denom = 'inj'
    const chainGrpcClient = new ChainGrpcClient(network.sentryGrpcApi)
    console.log(await chainGrpcClient.bank.fetchBalance({ injectiveAddress, denom }))
    • Fetching all derivative markets from the exchange (indexer) API
    // Importing only the needed API
    import { IndexerGrpcDerivativesApi, Network } from '@injectivelabs/sdk-ts'
    
    const network = Network.testnet()
    const exchangeGrpcDerivativesApi = new IndexerGrpcDerivativesApi(network.indexerApi)
    console.log(await exchangeGrpcDerivativesApi.fetchMarkets())
    // Using the client
    import { IndexerGrpcClient, Network } from '@injectivelabs/sdk-ts'
    
    const network = Network.testnet()
    const exchangeGrpcClient = new IndexerGrpcClient(network.indexerApi)
    console.log(await exchangeGrpcClient.derivatives.fetchMarkets())

    Broadcasting Transactions

    • Sending INJ to another address
    import { getNetworkInfo, Network } from "@injectivelabs/networks";
    import { ChainRestAuthApi } from "@injectivelabs/sdk-ts";
    import { PrivateKey } from "@injectivelabs/sdk-ts/dist/local";
    import {
      privateKeyToPublicKeyBase64,
      MsgSend,
      DEFAULT_STD_FEE,
    } from "@injectivelabs/sdk-ts";
    import { createTransaction } from "@injectivelabs/tx-ts";
    import { TxGrpcClient, TxClient } from "@injectivelabs/tx-ts/dist/client";
    import { BigNumberInBase } from "@injectivelabs/utils";
    
    /** MsgSend Example */
    (async () => {
      const network = getNetworkInfo(Network.Public);
      const privateKeyHash =
        "f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3";
      const privateKey = PrivateKey.fromPrivateKey(privateKeyHash);
      const injectiveAddress = privateKey.toBech32();
      const publicKey = privateKeyToPublicKeyBase64(
        Buffer.from(privateKeyHash, "hex")
      );
    
      /** Account Details **/
      const accountDetails = await new ChainRestAuthApi(
        network.sentryHttpApi
      ).fetchAccount(injectiveAddress);
    
      /** Prepare the Message */
      const amount = {
        amount: new BigNumberInBase(0.01).toWei().toFixed(),
        denom: "inj",
      };
    
      const msg = MsgSend.fromJSON({
        amount,
        srcInjectiveAddress: injectiveAddress,
        dstInjectiveAddress: injectiveAddress,
      });
    
      /** Prepare the Transaction **/
      const { signBytes, txRaw } = createTransaction({
        message: msg.toDirectSign(),
        memo: "",
        fee: DEFAULT_STD_FEE,
        pubKey: Buffer.from(publicKey).toString("base64"),
        sequence: parseInt(accountDetails.account.base_account.sequence, 10),
        accountNumber: parseInt(
          accountDetails.account.base_account.account_number,
          10
        ),
        chainId: network.chainId,
      });
    
      /** Sign transaction */
      const signature = await privateKey.sign(signBytes);
    
      /** Append Signatures */
      txRaw.setSignaturesList([signature]);
    
      /** Calculate hash of the transaction */
      console.log(`Transaction Hash: ${await TxClient.hash(txRaw)}`);
    
      const txService = new TxGrpcClient({
        txRaw,
        endpoint: network.sentryGrpcApi,
      });
    
      /** Simulate transaction */
      const simulationResponse = await txService.simulate();
      console.log(
        `Transaction simulation response: ${JSON.stringify(
          simulationResponse.gasInfo
        )}`
      );
    
      /** Broadcast transaction */
      const txResponse = await txService.broadcast();
      console.log(
        `Broadcasted transaction hash: ${JSON.stringify(txResponse.txhash)}`
      );
    })();

    Streaming Data

    • Streaming users subaccount balances from the indexer API
    import { getNetworkInfo, Network } from "@injectivelabs/networks";
    import { IndexerGrpcStreamClient } from "@injectivelabs/sdk-ts/dist/client/indexer/IndexerGrpcStreamClient";
    
    (async () => {
      const network = getNetworkInfo(Network.TestnetK8s);
    
      const subaccountId =
        "0xaf79152ac5df276d9a8e1e2e22822f9713474902000000000000000000000000";
    
      const exchangeClient = new IndexerGrpcStreamClient(
        network.indexerApi
      );
    
      await exchangeClient.account.streamSubaccountBalance({
        subaccountId,
        callback: (subaccountBalance) => {
          console.log(subaccountBalance);
        },
        onEndCallback: (status) => {
          console.log("Stream has ended with status: " + status);
        },
      });
    })();

    ๐Ÿ“œ Contribution

    Contribution guides and practices will be available once there is a stable foundation of the whole package set within the injective-ts repo.


    โ›‘ Support

    Reach out to us at one of the following places!


    ๐Ÿ”“ License

    This software is licensed under the MIT license. See LICENSE for full disclosure.

     

    Powering the future of decentralized finance.