UNPKG

@d8x/perpetuals-sdk

Version:

Node TypeScript SDK for D8X Perpetual Futures

163 lines (162 loc) 7.52 kB
import type { IdxPriceInfo, PriceFeedConfig, PriceFeedEndpoints, PriceFeedEndpointsOptionalWrite, PriceFeedFormat, PriceFeedSubmission } from "./nodeSDKTypes"; import PerpetualDataHandler from "./perpetualDataHandler"; /** * This class communicates with the REST API that provides price-data that is * to be submitted to the smart contracts for certain functions such as * trader liquidations, trade executions, change of trader margin amount. */ export default class PriceFeeds { private config; private priceFeedConfigNetwork; feedEndpoints: Array<string>; writeFeedEndpoints: Array<string>; private feedInfo; private dataHandler; private triangulations; private THRESHOLD_MARKET_CLOSED_SEC; private cache; private onChainPxFeeds; private PYTH; private polyMktsPxFeed; constructor(dataHandler: PerpetualDataHandler, priceFeedConfigNetwork: string); /** * initialization function. Gathers config from config-hub if url * specified */ init(): Promise<void>; getConfig(): PriceFeedConfig; static overridePriceEndpointsOfSameType(configEndpoints: PriceFeedEndpoints, userProvidedEndpoints: PriceFeedEndpointsOptionalWrite): PriceFeedEndpoints; /** * We cut last / or legacy url format /api/ if any * @param endp endpoint string * @returns trimmed string */ static trimEndpoint(endp: string): string; /** * Pre-processing of triangulations for symbols, given the price feeds * @param symbols set of symbols we want to triangulate from price feeds */ initializeTriangulations(symbols: Set<string>): void; /** * Returns computed triangulation map * @returns Triangulation map */ getTriangulations(): Map<string, [string[], boolean[]]>; /** * Set pre-computed triangulation map */ setTriangulations(triangulation: Map<string, [string[], boolean[]]>): void; /** * Get required information to be able to submit a blockchain transaction with * price-update such as trade execution, liquidation. Uses write price feed endpoints. * @param symbol symbol of perpetual, e.g., BTC-USD-MATIC * @returns PriceFeedSubmission, index prices, market closed information */ fetchFeedPriceInfoAndIndicesForPerpetual(symbol: string): Promise<{ submission: PriceFeedSubmission; pxS2S3: [number, number]; mktClosed: [boolean, boolean]; }>; /** * Get all prices/isMarketClosed for the provided symbols via * "latest_price_feeds" and triangulation. Triangulation must be defined in * config, unless it is a direct price feed. Uses read endpoints. * @param symbol perpetual symbol of the form BTC-USD-MATIC * @returns map of feed-price symbol to price/isMarketClosed */ fetchPrices(symbols: string[]): Promise<Map<string, [number, boolean]>>; /** * Get index prices and market closed information for the given perpetual * @param symbol perpetual symbol such as ETH-USD-MATIC * @returns Index prices and market closed information; for prediction markets also * ema, confidence, and order book parameters. */ fetchPricesForPerpetual(symbol: string): Promise<IdxPriceInfo>; /** * Fetch the provided feed prices and bool whether market is closed or open * - requires the feeds to be defined in priceFeedConfig.json * - vaas are not of interest here, therefore only readonly price feed * endpoints are used * @param symbols array of feed-price symbols (e.g., [btc-usd, eth-usd]) * @returns mapping symbol-> [price, isMarketClosed], also has an entry * <symbol>:ema for each polymarket symbol that maps to the ema price */ fetchFeedPrices(symbols: string[]): Promise<Map<string, [number, boolean]>>; fetchFeedPriceResponses(symbols: string[]): Promise<Map<string, [number, boolean, PriceFeedFormat | undefined, PriceFeedFormat | undefined]>>; private queryOnChainPxFeeds; private queryPolyMktsPxFeeds; /** * Get all required feed prices via "latest_price_feeds". * 'required' means part of perpetuals/triangulations * @returns map of feed-price symbol to price/isMarketClosed */ fetchAllFeedPrices(): Promise<Map<string, [number, boolean]>>; private _buildQuery; /** * Get the latest prices for a given perpetual from the offchain oracle * networks. Uses write price feed endpoints. * @param symbol perpetual symbol of the form BTC-USD-MATIC * @returns array of price feed updates that can be submitted to the smart * contract and corresponding price information */ fetchLatestFeedPriceInfoForPerpetual(symbol: string): Promise<PriceFeedSubmission>; /** * Extract pair-prices from underlying price feeds via triangulation * The function either needs a direct price feed or a defined triangulation to succesfully * return a triangulated price * @param symbols array of pairs for which we want prices, e.g., [BTC-USDC, ETH-USD] * @param feeds data obtained via fetchLatestFeedPriceInfo or fetchLatestFeedPrices * @returns array of prices with same order as symbols */ calculateTriangulatedPricesFromFeedInfo(symbols: string[], feeds: PriceFeedSubmission): [number[], boolean[]]; /** * Extract pair-prices from underlying price feeds via triangulation * The function either needs a direct price feed or a defined triangulation to succesfully * return a triangulated price * @param symbols array of pairs for which we want prices, e.g., [BTC-USDC, ETH-USD] * @param feeds data obtained via fetchLatestFeedPriceInfo or fetchLatestFeedPrices * @returns array of prices with same order as symbols */ triangulatePricesFromFeedPrices(symbols: string[], feedPriceMap: Map<string, [number, boolean]>): [number[], boolean[]]; /** * Queries the REST endpoint and returns parsed VAA price data * We expect one single id in the query, * otherwise the VAA is a compressed VAA for all prices which is not suited * for the smart contracts * @param query query price-info from endpoint * @returns vaa and price info */ private fetchVAAQuery; /** * Queries the REST endpoint and returns parsed price data * @param query query price-info from endpoint * @returns array of vaa and price info */ fetchPriceQuery(query: string): Promise<[string[], PriceFeedFormat[]]>; /** * Queries the REST endpoint and returns parsed price data * @param query query price-info from endpoint * @returns object with vaa and price info (both spot and ema) */ fetchFullPriceQuery(query: string): Promise<{ updateData: string[]; price: PriceFeedFormat[]; emaPrice: PriceFeedFormat[]; }>; /** * Searches for configuration for given network * @param configs pricefeed configuration from json * @param network e.g. testnet * @returns selected configuration */ static _selectConfig(configs: PriceFeedConfig[], network: string): PriceFeedConfig; /** * Wraps configuration into convenient data-structure * @param config configuration for the selected network * @returns feedInfo-map and endPoints-array */ static _constructFeedInfo(config: PriceFeedConfig, shuffleEndpoints: boolean): [Map<string, { symbol: string; endpointId: number; }[]>, string[], string[]]; }