Injective | TS & dApps Docs
DocumentationInjective TS
  • Overview
  • Getting Started
    • Technical Concepts
    • Application Concepts
      • Calculations
        • Min Price Tick Size
        • Min Quantity Tick Size
      • Networks
      • CosmJs Support
    • Assets
      • Creating Tokens
      • Denom Client (deprecated)
      • Injective Lists
    • Running examples
  • Wallets
    • Getting Started
    • Accounts
    • Wallet Connections
    • Wallet Strategy
    • Offchain (Arbitrary) Data
  • Querying
    • Getting Started
    • Chain
      • Auction
      • Auth
      • Bank
      • Distribution
      • Exchange
      • Governance
      • IBC
      • Mint
      • Insurance Funds
      • Oracle
      • Peggy
      • Permissions
      • Staking
      • Tendermint
      • Wasm
      • WasmX
      • Token Factory
    • Indexer
      • Account
      • Auction
      • Derivatives
      • Explorer
      • Insurance Funds
      • Markets
      • Leaderboard
      • Mito
      • Oracle
      • Portfolio
      • Spot
      • Web3Gw Transactions
      • Streaming
        • Account
        • Auction
        • Derivatives
        • Oracle
        • Portfolio
        • Spot
        • Explorer
    • Ethereum (GraphQL)
  • Transactions
    • Getting Started
    • Cosmos
      • Ledger through Keplr Wallet
    • Ethereum
      • Ethereum Ledger
    • MsgBroadcaster
    • Private Key
    • Web3 Gateway
  • Core Modules (& examples)
    • Getting Started
    • Auction
    • AuthZ
    • Bank
    • Distribution
    • Exchange
    • Feegrant
    • Governance
    • IBC
    • Insurance
    • Peggy
    • Permissions
    • Staking
    • Tokenfactory
    • Wasm
  • Smart Contracts
    • Cosmwasm
      • Injective Name Service
      • Neptune Service
      • CW20 to Bank & Market Order in One Transaction
  • Bridges
    • Getting Started
    • Ethereum
    • IBC
    • Wormhole
  • Building Dapps
    • Getting Started
    • Configuring Nuxt
    • Configuring React
    • dApps Examples
      • Smart Contract
      • DEX
      • Bridge
      • Simple HTML example with Webpack
Powered by GitBook
On this page
  1. Querying
  2. Indexer
  3. Streaming

Derivatives

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

Using gRPC Stream

Stream derivatives orderbook

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

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcDerivativesStream = new IndexerGrpcDerivativesStream(
  endpoints.indexer,
)

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

const streamFn = indexerGrpcDerivativesStream.streamDerivativeOrderbookV2.bind(
  indexerGrpcDerivativesStream,
)

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

const streamFnArgs = {
  marketIds,
  callback,
}

streamFn(streamFnArgs)

Stream derivative orders

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

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcDerivativesStream = new IndexerGrpcDerivativesStream(
  endpoints.indexer,
)

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

const streamFn = indexerGrpcDerivativesStream.streamDerivativeOrders.bind(
  indexerGrpcDerivativesStream,
)

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

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

streamFn(streamFnArgs)

Stream derivative order history

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

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcDerivativesStream = new IndexerGrpcDerivativesStream(
  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 = indexerGrpcDerivativesStream.streamDerivativeOrderHistory.bind(
  indexerGrpcDerivativesStream,
)

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

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

streamFn(streamFnArgs)

Stream derivative trades

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

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcDerivativesStream = new IndexerGrpcDerivativesStream(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 = indexerGrpcDerivativesStream.streamDerivativeTrades.bind(indexerGrpcDerivativesStream)

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

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

streamFn(streamFnArgs)

Stream derivative positions

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

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcDerivativesStream = new IndexerGrpcDerivativesStream(
  endpoints.indexer,
)

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

const streamFn = indexerGrpcDerivativesStream.streamDerivativePositions.bind(
  indexerGrpcDerivativesStream,
)

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

const streamFnArgs = {
  marketId,
  subaccountId,
  callback,
}

streamFn(streamFnArgs)

Stream markets

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

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcDerivativesStream = new IndexerGrpcDerivativesStream(
  endpoints.indexer,
)

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

const streamFn = indexerGrpcDerivativesStream.streamDerivativeMarket.bind(
  indexerGrpcDerivativesStream,
)

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

const streamFnArgs = {
  marketIds,
  callback,
}

streamFn(streamFnArgs)

Stream orderbook updates

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

const endpoints = getNetworkEndpoints(Network.Testnet)
const indexerGrpcDerivativesStream = new IndexerGrpcDerivativesStream(
  endpoints.indexer,
)

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

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

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

const streamFnArgs = {
  marketIds,
  callback,
}

streamFn(streamFnArgs)
PreviousAuctionNextOracle

Last updated 1 year ago