Spot

Example code snippets to query the indexer for spot market module related data.

Using gRPC

Fetch markets

import { IndexerGrpcSpotApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotApi = new IndexerGrpcSpotApi(endpoints.indexer)

const markets = await indexerGrpcSpotApi.fetchMarkets()

console.log(markets)

Fetch market based on market id

import { IndexerGrpcSpotApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotApi = new IndexerGrpcSpotApi(endpoints.indexer)

const marketId = '0x...'

const market = await indexerGrpcSpotApi.fetchMarket(marketId)

console.log(market)

Fetch market's orders

import { PaginationOption, IndexerGrpcSpotApi } from '@injectivelabs/sdk-ts'
import { OrderSide } from '@injectivelabs/ts-types'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotApi = new IndexerGrpcSpotApi(endpoints.indexer)

const marketId = '0x...' /* optional param */
const orderSide = OrderSide.Buy /* optional param */
const subaccountId = '0x...' /* optional param */
const pagination = {...} as PaginationOption /* optional param */

const orders = await indexerGrpcSpotApi.fetchOrders({
  marketId,
  orderSide,
  subaccountId,
  pagination
})

console.log(orders)

Fetch market's order history

import {
  TradeDirection,
  PaginationOption,
  TradeExecutionType,
  IndexerGrpcSpotApi
} from '@injectivelabs/sdk-ts'
import { OrderSide } from '@injectivelabs/ts-types'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotApi = new IndexerGrpcSpotApi(endpoints.indexer)

const marketIds = ['0x...'] /* optional param */
const executionTypes = [TradeExecutionType.Market] /* optional param */
const orderTypes = OrderSide.Buy /* optional param */
const direction = TradeDirection.Buy /* optional param */
const subaccountId = '0x...' /* optional param */
const pagination = {...} as PaginationOption /* optional param */

const orderHistory = await indexerGrpcSpotApi.fetchOrderHistory({
  marketIds,
  executionTypes,
  orderTypes,
  direction,
  subaccountId,
  pagination
})

console.log(orderHistory)

Fetch market's trades

import {
  TradeDirection,
  PaginationOption,
  TradeExecutionType,
  IndexerGrpcSpotApi
} from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotApi = new IndexerGrpcSpotApi(endpoints.indexer)

const marketId = '0x...' /* optional param */
const executionTypes = [TradeExecutionType.Market] /* optional param */
const direction = TradeDirection.Buy /* optional param */
const subaccountId = '0x...'/* optional param */
const pagination = {...} as PaginationOption /* optional param */

const trades = await indexerGrpcSpotApi.fetchTrades({
  marketId,
  executionTypes,
  direction,
  subaccountId,
  pagination
})

console.log(trades)

Fetch list of subaccount orders

import {
  PaginationOption,
  IndexerGrpcSpotApi
} from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotApi = new IndexerGrpcSpotApi(endpoints.indexer)

const marketId = '0x...' /* optional param */
const subaccountId = '0x...' /* optional param */
const pagination = {...} as PaginationOption /* optional param */

const subaccountOrders = await indexerGrpcSpotApi.fetchSubaccountOrdersList({
  marketId,
  subaccountId,
  pagination
})

console.log(subaccountOrders)

Fetch list of subaccount trades

import {
  TradeDirection,
  TradeExecutionType,
  PaginationOption,
  IndexerGrpcSpotApi
} from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotApi = new IndexerGrpcSpotApi(endpoints.indexer)

const marketId = '0x...' /* optional param */
const subaccountId = '0x...' /* optional param */
const executionType = TradeExecutionType.LimitFill /* optional param */
const direction = TradeDirection.Sell /* optional param */
const pagination = {...} as PaginationOption /* optional param */

const subaccountTrades = await indexerGrpcSpotApi.fetchSubaccountTradesList({
  marketId,
  subaccountId,
  executionType,
  direction,
  pagination
})

console.log(subaccountTrades)

Fetch orderbooks for multiple markets

import { IndexerGrpcSpotApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotApi = new IndexerGrpcSpotApi(endpoints.indexer)

const marketIds = ['0x...']

const orderbooks = await indexerGrpcSpotApi.fetchOrderbooksV2(marketIds)

console.log(orderbooks)

Fetch orderbook for a market

import { IndexerGrpcSpotApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotApi = new IndexerGrpcSpotApi(endpoints.indexer)

const marketId = '0x...'

const orderbook = await indexerGrpcSpotApi.fetchOrderbookV2(marketId)

console.log(orderbook)

Using HTTP REST

Fetch market summary, such as a history of prices and 24 hr volume

import { IndexerRestSpotChronosApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerRestSpotChronosApi = new IndexerRestSpotChronosApi(
  `${endpoints.chronos}/api/chronos/v1/spot`,
)

const marketId = '0x...'

const marketSummary = await indexerRestSpotChronosApi.fetchMarketSummary(
  marketId,
)

console.log(marketSummary)

Fetch all markets summaries, such as a history of prices and 24 hr volume

import { IndexerRestSpotChronosApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerRestSpotChronosApi = new IndexerRestSpotChronosApi(
  `${endpoints.chronos}/api/chronos/v1/spot`,
)

const marketSummaries = await indexerRestSpotChronosApi.fetchMarketsSummary(
  marketId,
)

console.log(marketSummaries)

Last updated