Skip to main content
Example code snippets to query the indexer for oracle module related data.

Using gRPC

Fetch list of oracles

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

const endpoints = getNetworkEndpoints(Network.Testnet);
const indexerGrpcOracleApi = new IndexerGrpcOracleApi(endpoints.indexer);

const oracleList = await indexerGrpcOracleApi.fetchOracleList();

console.log(oracleList);

Fetch price from the oracle

Base and Quote oracle symbols are always fetched from the market itself. They can be in a different representation than plain symbols (i.e hashes for pyth oracle).
import { getNetworkEndpoints, Network } from "@injectivelabs/networks";
import {
  IndexerGrpcOracleApi,
  IndexerGrpcDerivativesApi,
} from "@injectivelabs/sdk-ts/client/indexer";

const endpoints = getNetworkEndpoints(Network.Testnet);
const indexerGrpcDerivativesApi = new IndexerGrpcDerivativesApi(endpoints.indexer);
const indexerGrpcOracleApi = new IndexerGrpcOracleApi(endpoints.indexer);

// Fetch the list of derivative markets
const markets = await indexerGrpcDerivativesApi.fetchMarkets();

// Find the specific market by ticker
const market = markets.find((market) => market.ticker === "INJ/USDT PERP");

if (!market) {
  throw new Error("Market not found");
}

// These values are a part of the market object
// fetched from the indexer i.e `oracleBase` and `oracleQuote`
const baseSymbol = market.oracleBase;
const quoteSymbol = market.oracleQuote;
const oracleType = market.oracleType;

const oraclePrice = await indexerGrpcOracleApi.fetchOraclePriceNoThrow({
  baseSymbol,
  quoteSymbol,
  oracleType,
});

console.log(oraclePrice);