JSPM

@injectivelabs/sdk-ts

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

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

    Package Exports

    • @injectivelabs/sdk-ts
    • @injectivelabs/sdk-ts/dist/index.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)


    📚 Installation

    yarn add @injectivelabs/sdk-ts

    📖 Documentation

    There are couple of components within this package. This package offers clients that can be easily instantiated so you can access all functionalities easily. Also, it offer decoupled exports so you can instantiate classes that you only need so you can utilize tree-shaking in the build process and reduce the file-size of the imports.

    We are going to go through all of the components of the package and explain them in depth.

    Classes

    This package contains some utility classes that can enhance the developer experience.

    • Address class -> Can be used to get details about an injective address,
    • Network class -> Can be used to get a pre-defined set of endpoints (sentry, api, etc) for easier usage,
    • PrivateKey class -> Can be used to sign transactions, etc

    Client

    This package contains utility classes which ease the way we consume data from data sources.

    There are two data sources available:

    • Chain -> Consuming data directly from the chain,
    • Exchange -> Consuming data directly from the exchange (indexer) API,

    At the moment, only the gRPC method of consuming the data is supported, we might introduce REST as well in the future.

    Core

    This package contains utility message classes that can help generate messages which can be packed into a transaction and broadcasted to the chain. Messages are separated into folders that correspond to a module on the Injective chain.

    Every message is represented as a MsgBase class, which has couple of mapping functionalities:

    • toData -> Converts the Message to a simple Object representation,
    • toProto -> Returns a proto representation of the message,
    • toAmino -> Converts the Message to a proto representation + type (ready for client side usage),
    • toWeb3 -> Converts the Message to a web3 representation + type (ready for web3-gateway usage i.e browser),

    UI

    This package contains utility classes that can help transform (map) data from a gRPC (or REST) API to a human readable format that's easy to consume on the UI.

    It has two parts:

    • Chain -> Utility classes (and types) related to the chain modules,
    • Exchange -> Utility classes (and types) related to the exchange (indexer) api,

    Utils

    This package contains some utility functions and constants.

    Local

    This package contains some utility functions and constants for client-side usage (local, node environment)

    • InjectiveTx class -> Can be used to prepare a transaction for signing/broadcasting on the client side,
    • TxService class -> Can be used for simulating or broadcasting a transaction on the client side

    🎒 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 balance from the chain
    // Using the client
    import { ChainClient, Network } from '@injectivelabs/sdk-ts'
    
    const network = Network.testnet()
    const injectiveAddress = 'inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r'
    const denom = 'inj'
    const chainGrpcClient = new ChainClient.GrpcClient(network.sentryGrpcApi)
    console.log(await chainGrpcClient.bankApi.balance({ injectiveAddress, denom }))
    // Importing only the needed API
    import { ChainClient, Network } from '@injectivelabs/sdk-ts'
    
    const network = Network.testnet()
    const injectiveAddress = 'inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r'
    const denom = 'inj'
    const bankApi = new ChainClient.BankApi(network.sentryGrpcApi)
    console.log(await bankApi.balance({ injectiveAddress, denom }))
    • Fetching all derivative markets from the exchange (indexer) API
    // Using the client
    import { ExchangeClient, Network } from '@injectivelabs/sdk-ts'
    
    const network = Network.testnet()
    const exchangeGrpcClient = new ExchangeClient.GrpcClient(network.exchangeApi)
    console.log(await exchangeGrpcClient.derivativesApi.markets())
    // Importing only the needed API
    import { ExchangeClient, Network } from '@injectivelabs/sdk-ts'
    
    const network = Network.testnet()
    const derivativesApi = new ExchangeClient.DerivativesApi(network.exchangeApi)
    console.log(await derivativesApi.markets())

    Broadcasting Transactions

    • Creating a spot limit order transaction and broadcasting it to the chain
    import { Network, ExchangeCore, ExchangeClient, PrivateKey, InjectiveTx, TxService } from '@injectivelabs/sdk-ts'
    
    const network = Network.testnet()
    const privateKey = PrivateKey.fromPrivateKey('f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3')
    
    /** Account Details **/
    const injectiveAddress = "inj1ql0alrq4e4ec6rv9svqjwer0k6ewfjkaay9lne";
    const authApi = new ExchangeClient.AuthApi(network.sentryGrpcApi)
    const accountDetails = await authApi.account(injectiveAddress)
    
    /** Limit Order Details */
    const price = 5;
    const quantity = 10;
    const baseDecimals = 18; // INJ has 18 decimals
    const quoteDecimals = 6; // USDT has 6 decimals
    const marketId =
      "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"; // INJ/USDT on testnet;
    const subaccountId =
      "0x07dfdf8c15cd738d0d85830127646fb6b2e4cadd000000000000000000000000";
    const orderType = 1; /* Buy, 2 for Sale */
    
    /** Preparing the transaction */
    const msg = new ExchangeCore.MsgCreateSpotLimitOrder({
      marketId,
      subaccountId,
      injectiveAddress,
      orderType,
      price,
      quantity,
      triggerPrice: '0',
      feeRecipient: injectiveAddress
    })
    const injectiveTx = new InjectiveTx({
      accountDetails,
      tx: {
        msgs: [msg],
        chainId: network.chainId,
        address: injectiveAddress
      }
    })
    const signature = privateKey.sign(injectiveTx.signDoc.serializeBinary())
    const txRaw = injectiveTx.toTxRaw(signature)
    console.log(`Transaction Hash: ${InjectiveTx.getTxHash(txRaw)}`);
    
    /** Simulating and Broadcasting a transaction */
    const txService = new TxService({ txRaw, endpoint: network.sentryGrpcApi })
    const simulationResponse = await txService.simulate()
    console.log(
      `Transaction simulation response: ${JSON.stringify(simulationResponse.gasInfo)}`
    );
    
    const txResponse = await txService.broadcast()
    console.log(
      `Broadcasted transaction hash: ${JSON.stringify(txResponse.txhash)}`
    );

    Streaming Data


    🖱️ Examples

    To run an example, cd into the examples folder and execute the desired example by running:

    yarn ts-node pathToExample
    
    ## Example: yarn ts-node ./core/MsgBid.ts

    Don't forget to do yarn and install dependencies before executing any example.


    ⛑ Support

    Reach out to us at one of the following places!


    🔓 License