Spot

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

Using gRPC Stream

Stream the spot orderbook

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

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotStream = new IndexerGrpcSpotStream(endpoints.indexer)

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

const streamFn = indexerGrpcSpotStream.streamSpotOrderbookV2.bind(
  indexerGrpcSpotStream,
)

const callback = (orderbooks) => {
  console.log(orderbooks)
}

const streamFnArgs = {
  marketIds,
  callback,
}

streamFn(streamFnArgs)

Stream spot orders

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

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotStream = new IndexerGrpcSpotsStream(endpoints.indexer)

const marketId = '0x...'
const subaccountId = '0x...' /* optional param */
const orderSide = OrderSide.Buy /* optional param */

const streamFn = indexerGrpcSpotStream.streamSpotOrders.bind(
  indexerGrpcSpotStream,
)

const callback = (orders) => {
  console.log(orders)
}

const streamFnArgs = {
  marketId,
  subaccountId,
  orderside,
  callback,
}

streamFn(streamFnArgs)

Stream spot order history

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

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotStream = new IndexerGrpcSpotStream(endpoints.indexer)

const marketId = '0x...' /* optional param */
const subaccountId = '0x...' /* optional param */
const orderTypes = [OrderSide.Buy] /* optional param */
const executionTypes = [TradeExecutionType.Market] /* optional param */
const direction = TradeDirection.Buy /* optional param*/

const streamFn = indexerGrpcSpotStream.streamSpotOrderHistory.bind(
  indexerGrpcSpotStream,
)

const callback = (orderHistory) => {
  console.log(orderHistory)
}

const streamFnArgs = {
  marketId,
  subaccountId,
  orderTypes,
  executionTypes,
  direction,
  callback,
}

streamFn(streamFnArgs)

Stream spot trades

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

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotStream = new IndexerGrpcSpotStream(endpoints.indexer)

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

const streamFn = indexerGrpcSpotStream.streamSpotTrades.bind(indexerGrpcSpotStream)

const callback = (trades) => {
  console.log(trades)
}

const streamFnArgs = {
  marketIds,
  subaccountId,
  orderTypes,
  direction,
  pagination,
  callback
}

streamFn(streamFnArgs)

Stream markets

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

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotStream = new IndexerGrpcSpotStream(endpoints.indexer)

const marketIds = ['0x...'] /* optional param */

const streamFn = indexerGrpcSpotStream.streamSpotMarket.bind(
  indexerGrpcSpotStream,
)

const callback = (markets) => {
  console.log(markets)
}

const streamFnArgs = {
  marketIds,
  callback,
}

streamFn(streamFnArgs)

Stream orderbook updates

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

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcSpotStream = new IndexerGrpcSpotStream(endpoints.indexer)

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

const streamFn = indexerGrpcSpotStream.streamDerivativeOrderbookUpdate.bind(
  indexerGrpcSpotStream,
)

const callback = (orderbookUpdates) => {
  console.log(orderbookUpdates)
}

const streamFnArgs = {
  marketIds,
  callback,
}

streamFn(streamFnArgs)

Last updated