JSPM

@injectivelabs/sdk-ts

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

    SDK in TypeScript for building Injective applications in a browser, node, and react native 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/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/eip712
    • @injectivelabs/sdk-ts/dist/core/eip712/index.js
    • @injectivelabs/sdk-ts/dist/core/index.js
    • @injectivelabs/sdk-ts/dist/core/transaction
    • @injectivelabs/sdk-ts/dist/core/transaction/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 Total alerts Language grade: JavaScript license

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

    @injectivelabs/sdk-ts is a TypeScript SDK for writing applications on top of the Injective chain in both a Node.js, browser and react native 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

    Read more and find example usages on our Querying Wiki

    Making Transactions

    Read more and find example usages on our Transactions Wiki


    ๐ŸŽ’ 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,
      privateKeyToPublicKeyBase64,
      MsgSend,
      DEFAULT_STD_FEE,
    } from "@injectivelabs/sdk-ts";
    import { createTransaction, TxGrpcClient, TxClient } from "@injectivelabs/sdk-ts/dist/core/transaction";
    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.replace("0x", ""), "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: publicKey,
        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(network.sentryGrpcApi);
    
      /** Simulate transaction */
      const simulationResponse = await txService.simulate(txRaw);
      console.log(
        `Transaction simulation response: ${JSON.stringify(
          simulationResponse.gasInfo
        )}`
      );
    
      /** Broadcast transaction */
      const txResponse = await txService.broadcast(txRaw);
      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.