Skip to content

02QueryingIndexerDerivatives

Bojan Angjelkoski edited this page Apr 2, 2023 · 5 revisions

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

Using gRPC

  • fetch markets
import { IndexerGrpcDerivativesApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(endpoints.indexer)

const markets = await indexerGrpcDerivativesApi.fetchMarkets()

console.log(markets)
  • fetch market based on a market id
import { IndexerGrpcDerivativesApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(endpoints.indexer)

const marketId = '0x...'

const market = await indexerGrpcDerivativesApi.fetchMarket(marketId)

console.log(market)
  • fetch binary options markets
import { IndexerGrpcDerivativesApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(endpoints.indexer)


const binaryOptionsMarket = await indexerGrpcDerivativesApi.fetchBinaryOptionsMarkets()

console.log(binaryOptionsMarket)
  • fetch binary options market based on market id
import { IndexerGrpcDerivativesApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(endpoints.indexer)

const marketId = '0x...'

const binaryOptionsMarket = await indexerGrpcDerivativesApi.fetchBinaryOptionsMarket(marketId)

console.log(binaryOptionsMarket)
  • fetch a market's orderbook based on market id
import { IndexerGrpcDerivativesApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(endpoints.indexer)

const marketId = '0x...'

const orderbook = await indexerGrpcDerivativesApi.fetchOrderbook(marketId)

console.log(orderbook)
  • fetch a market's orders
import { PaginationOption, DerivativeOrderSide, IndexerGrpcDerivativesApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(endpoints.indexer)

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

const orders = await indexerGrpcDerivativesApi.fetchOrders({
  marketId,
  orderSide,
  subaccountId,
  paginationOption
})

console.log(orders)
  • fetch a market's order history
import {
  TradeDirection,
  PaginationOption,
  DerivativeOrderSide,
  TradeExecutionType,
  IndexerGrpcDerivativesApi
} from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(endpoints.indexer)

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

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

console.log(orderHistory)
  • fetch a market's positions
import {
  TradeDirection,
  PaginationOption,
  IndexerGrpcDerivativesApi
} from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(endpoints.indexer)

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

const positions = await indexerGrpcDerivativesApi.fetchPositions({
  marketIds,
  direction,
  subaccountId,
  paginationOption
})

console.log(positions)
  • fetch a market's trades
import {
  TradeDirection,
  PaginationOption,
  TradeExecutionType,
  IndexerGrpcDerivativesApi
} from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(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 paginationOption = {...} as PaginationOption /* optional param */

const trades = await indexerGrpcDerivativesApi.fetchTrades({
  marketId,
  executionTypes,
  direction,
  subaccountId,
  paginationOption
})

console.log(trades)
  • get funding payments for a market
import {
  PaginationOption,
  IndexerGrpcDerivativesApi
} from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(endpoints.indexer)

const marketIds = ['0x...'] /* optional param */
const paginationOption = {...} as PaginationOption /* optional param */

const fundingPayments = await indexerGrpcDerivativesApi.fetchFundingPayments({
  marketIds,
  paginationOption
})

console.log(fundingPayments)
  • get funding rates for a market
import {
  PaginationOption,
  IndexerGrpcDerivativesApi
} from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(endpoints.indexer)

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

const fundingRates = await indexerGrpcDerivativesApi.fetchFundingRates({
  marketId,
  paginationOption
})

console.log(fundingRates)
  • get a list of subaccount orders
import {
  PaginationOption,
  IndexerGrpcDerivativesApi
} from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(endpoints.indexer)

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

const subaccountOrders = await indexerGrpcDerivativesApi.fetchSubaccountOrdersList({
  marketId,
  subaccountId,
  paginationOption
})

console.log(subaccountOrders)
  • get a list of subaccount trades
import {
  TradeDirection,
  TradeExecutionType,
  PaginationOption,
  IndexerGrpcDerivativesApi
} from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(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 paginationOption = {...} as PaginationOption /* optional param */

const subaccountTrades = await indexerGrpcDerivativesApi.fetchSubaccountTradesList({
  marketId,
  subaccountId,
  executionType,
  direction,
  paginationOption
})

console.log(subaccountTrades)
  • get orderbooks for multiple markets
import { IndexerGrpcDerivativesApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(endpoints.indexer)

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

const orderbooks = await indexerGrpcDerivativesApi.fetchOrderbooksV2(marketIds)

console.log(orderbooks)
  • get orderbook for a market
import { IndexerGrpcDerivativesApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(endpoints.indexer)

const marketId = '0x...'

const orderbook = await indexerGrpcDerivativesApi.fetchOrderbookV2(marketId)

console.log(orderbook)

Using HTTP REST

  • get market summary, such as a history of prices and 24 hr volume
import { IndexerRestDerivativesChronosApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerRestDerivativesChronosApi = new IndexerRestDerivativesChronosApi(`${endpoints.chronos}/api/chronos/v1/derivative`)

const marketId = '0x...'

const marketSummary = await indexerRestDerivativesChronosApi.fetchMarketSummary(marketId)

console.log(marketSummary)
  • get all markets' summaries, such as a history of prices and 24 hr volume
import { IndexerRestDerivativesChronosApi } from '@injectivelabs/sdk-ts'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'

const endpoints = getNetworkEndpoints(Network.TestnetK8s)
const indexerRestDerivativesChronosApi = new IndexerRestDerivativesChronosApi(`${endpoints.chronos}/api/chronos/v1/derivative`)

const marketSummaries = await indexerRestDerivativesChronosApi.fetchMarketsSummary(marketId)

console.log(marketSummaries)

⚠️ DOCUMENTATION ⚠️

Clone this wiki locally