Skip to main content
Example code snippets to query the indexer for transaction module related data. Used only when interacting with the Web3Gateway

Using gRPC

Fetch response for preparing a transaction

import { EvmChainId } from '@injectivelabs/ts-types'
import { Msgs } from '@injectivelabs/sdk-ts/core/modules'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'
import { IndexerGrpcTransactionApi } from '@injectivelabs/sdk-ts/client/indexer'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcTransactionApi = new IndexerGrpcTransactionApi(endpoints.indexer)

const address = '0x...' // ethereum address
const chainId = EvmChainId.Sepolia
const message = { ... } as Msgs
const memo = '...'

const prepareTxResponse = await indexerGrpcTransactionApi.prepareTxRequest({
  address,
  chainId,
  message,
  memo
})

console.log(prepareTxResponse)

Fetch response for preparing a cosmos transaction

import { getNetworkEndpoints, Network } from '@injectivelabs/networks'
import { IndexerGrpcTransactionApi } from '@injectivelabs/sdk-ts/client/indexer'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcTransactionApi = new IndexerGrpcTransactionApi(endpoints.indexer)

const address = 'inj...'
const message = { ... }

const prepareCosmosTxResponse = await indexerGrpcTransactionApi.prepareCosmosTxRequest({
  address,
  message
})

console.log(prepareCosmosTxResponse)

Fetch response for broadcasting transactions using the Web3Gateway

Use MsgBroadcasterWithPk to broadcast transactions within a node/CLI environment, which can be found in @injectivelabs/sdk-ts. Use @injectivelabs/wallet-core’s MsgBroadcaster class for more details on broadcasting a transactions in a browser environment.
import { Msgs } from '@injectivelabs/sdk-ts/core/modules'
import { ChainId, EvmChainId } from '@injectivelabs/ts-types'
import { WalletStrategy } from '@injectivelabs/wallet-strategy'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'
import { IndexerGrpcTransactionApi } from '@injectivelabs/sdk-ts/client/indexer'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcTransactionApi = new IndexerGrpcTransactionApi(endpoints.indexer)

const chainId = ChainId.Testnet // The Injective Testnet Chain ID
const evmChainId = EvmChainId.TestnetEvm // The Injective Evm Testnet Chain ID

export const evmRpcEndpoint = `https://eth-sepolia.g.alchemy.com/v2/${process.env.APP_EVM_RPC_KEY}`

const walletStrategy = new WalletStrategy({
  chainId,
  evmOptions: {
    evmChainId,
    rpcUrl: evmRpcEndpoint,
  },
})

const address = '0x...' // ethereum address
const message = { ... } as Msgs
const memo = '...'
const response = { ... } // response from  prepareTxRequest
const signature = await walletStrategy.signEip712TypedData(
      response.getData(),
      address,
    ) /* see @injectivelabs/wallet-strategy implementation of WalletStrategy. Essentially, you use the signEip712TypedData method of the wallet, if the wallet supports signing ethereum transactions */

const broadcastTxResponse = await indexerGrpcTransactionApi.broadcastTxRequest({
  signature,
  chainId,
  message,
  txResponse: response
})

console.log(broadcastTxResponse)

Fetch response for broadcasting a cosmos transactions.

import { TxRaw } from '@injectivelabs/sdk-ts/types'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'
import { IndexerGrpcTransactionApi } from '@injectivelabs/sdk-ts/client/indexer'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcTransactionApi = new IndexerGrpcTransactionApi(endpoints.indexer)

const address = 'inj...' // ethereum address
const signature = '...' // base64
const txRaw = { ... } as TxRaw
const pubKey = {
  type: string,
  value: string // base64
}

const broadcastCosmosTxResponse = await indexerGrpcTransactionApi.broadcastCosmosTxRequest({
  address,
  signature,
  txRaw,
  pubKey
})

console.log(broadcastCosmosTxResponse)

Fetch Web3Gateway Fee Payer

import { getNetworkEndpoints, Network } from "@injectivelabs/networks";
import { IndexerGrpcTransactionApi } from "@injectivelabs/sdk-ts/client/indexer";

const endpoints = getNetworkEndpoints(Network.Testnet);
const indexerGrpcTransactionApi = new IndexerGrpcTransactionApi(
  endpoints.indexer
);

const feePayer = await indexerGrpcTransactionApi.fetchFeePayer();

console.log(feePayer);