{"version":3,"file":"priceFeedQueries.mjs","names":["#ctx","#hermesEndpoint","#connection"],"sources":["../../src/queries/priceFeedQueries.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { Transaction } from '@mysten/sui/transactions';\n\nimport { PriceInfoObject } from '../contracts/pyth/price_info.js';\nimport { SuiPriceServiceConnection, SuiPythClient } from '../pyth/pyth.js';\nimport { PRICE_INFO_OBJECT_MAX_AGE_MS } from '../utils/config.js';\nimport { DEEPBOOK_HERMES_PROXY, PYTH_UPGRADED_HERMES } from '../utils/constants.js';\nimport { ConfigurationError } from '../utils/errors.js';\nimport type { QueryContext } from './context.js';\n\nexport class PriceFeedQueries {\n\t#ctx: QueryContext;\n\n\tconstructor(ctx: QueryContext) {\n\t\tthis.#ctx = ctx;\n\t}\n\n\t/**\n\t * The Hermes endpoint serving the Pyth deployment margin is configured against.\n\t *\n\t * There are two routes, chosen by whether the caller brought\n\t * credentials. With `accessToken` set, the SDK talks to Pyth directly and no DeepBook\n\t * infrastructure is in the path — the token is sent to Pyth's own host, so a token\n\t * minted for some other endpoint must be paired with an explicit `hermesEndpoint`.\n\t *\n\t * Note the converse too: an explicit `hermesEndpoint` wins over everything, and the\n\t * token is sent to whatever host it names. Point it at a mirror or staging proxy while\n\t * a production token is still configured and the credential goes there.\n\t *\n\t * Without a token it falls back to the DeepBook-operated proxy, which supplies\n\t * credentials server-side; that proxy is not deployed yet, so today this path throws a\n\t * `ConfigurationError` instead. An explicit `hermesEndpoint` overrides both.\n\t */\n\t#hermesEndpoint(): string {\n\t\tconst { hermesEndpoint, accessToken } = this.#ctx.config.pyth;\n\t\tif (hermesEndpoint) {\n\t\t\treturn hermesEndpoint;\n\t\t}\n\n\t\tif (accessToken) {\n\t\t\treturn PYTH_UPGRADED_HERMES;\n\t\t}\n\t\tif (DEEPBOOK_HERMES_PROXY) {\n\t\t\treturn DEEPBOOK_HERMES_PROXY;\n\t\t}\n\n\t\tthrow new ConfigurationError(\n\t\t\t\"Pushing price updates against Pyth's upgraded Core needs credentials: its Hermes answers 401 unauthenticated. Set the client's `pythAccessToken` option (or `pyth.accessToken`), or point `pyth.hermesEndpoint` at an endpoint that supplies credentials itself.\",\n\t\t);\n\t}\n\n\t/**\n\t * A Hermes connection for the active Pyth deployment, carrying any configured auth\n\t * headers. The endpoint serving the upgraded Core requires an `Authorization` header\n\t * and answers 401 without one.\n\t */\n\t#connection(): SuiPriceServiceConnection {\n\t\tconst { accessToken } = this.#ctx.config.pyth;\n\t\treturn new SuiPriceServiceConnection(\n\t\t\tthis.#hermesEndpoint(),\n\t\t\taccessToken ? { accessToken } : undefined,\n\t\t);\n\t}\n\n\tasync getPriceInfoObject(tx: Transaction, coinKey: string): Promise<string> {\n\t\tthis.#ctx.config.requirePyth();\n\t\tconst currentTime = Date.now();\n\t\tconst priceInfoObjectAge = await this.getPriceInfoObjectAge(coinKey);\n\t\tif (\n\t\t\tpriceInfoObjectAge &&\n\t\t\tcurrentTime - priceInfoObjectAge * 1000 < PRICE_INFO_OBJECT_MAX_AGE_MS\n\t\t) {\n\t\t\treturn this.#ctx.config.getPriceInfoObjectId(coinKey);\n\t\t}\n\n\t\tconst connection = this.#connection();\n\n\t\tconst priceIDs = [this.#ctx.config.getFeedId(coinKey)];\n\n\t\tconst priceUpdateData = await connection.getPriceFeedsUpdateData(priceIDs);\n\n\t\tconst { pythStateId, wormholeStateId } = this.#ctx.config.pyth;\n\n\t\tconst client = new SuiPythClient(this.#ctx.client, pythStateId, wormholeStateId);\n\n\t\treturn (await client.updatePriceFeeds(tx, priceUpdateData, priceIDs))[0];\n\t}\n\n\tasync getPriceInfoObjects(tx: Transaction, coinKeys: string[]): Promise<Record<string, string>> {\n\t\tthis.#ctx.config.requirePyth();\n\t\tif (coinKeys.length === 0) {\n\t\t\treturn {};\n\t\t}\n\n\t\tconst currentTime = Date.now();\n\n\t\tconst coinToObjectId: Record<string, string> = {};\n\t\tconst objectIds: string[] = [];\n\t\tfor (const coinKey of coinKeys) {\n\t\t\tconst priceInfoObjectId = this.#ctx.config.getPriceInfoObjectId(coinKey);\n\t\t\tcoinToObjectId[coinKey] = priceInfoObjectId;\n\t\t\tobjectIds.push(priceInfoObjectId);\n\t\t}\n\n\t\tconst res = await this.#ctx.client.core.getObjects({\n\t\t\tobjectIds,\n\t\t\tinclude: { content: true },\n\t\t});\n\n\t\tconst staleCoinKeys: string[] = [];\n\t\tconst result: Record<string, string> = {};\n\n\t\tfor (let i = 0; i < coinKeys.length; i++) {\n\t\t\tconst coinKey = coinKeys[i];\n\t\t\tconst obj = res.objects[i];\n\n\t\t\tif (obj instanceof Error || !obj?.content) {\n\t\t\t\tstaleCoinKeys.push(coinKey);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst priceInfoObject = PriceInfoObject.parse(obj.content);\n\t\t\tconst arrivalTime = Number(priceInfoObject.price_info.arrival_time);\n\t\t\tconst age = currentTime - arrivalTime * 1000;\n\n\t\t\tif (age >= PRICE_INFO_OBJECT_MAX_AGE_MS) {\n\t\t\t\tstaleCoinKeys.push(coinKey);\n\t\t\t} else {\n\t\t\t\tresult[coinKey] = coinToObjectId[coinKey];\n\t\t\t}\n\t\t}\n\n\t\tif (staleCoinKeys.length === 0) {\n\t\t\treturn result;\n\t\t}\n\n\t\t// Distinct coins can share a feed. No shipped map has a collision — every configured\n\t\t// coin is on its own feed — but `coins` is a public constructor option, and coins\n\t\t// tracking the same underlying (a wrapped asset priced off its reference, say) are\n\t\t// the normal reason to supply one. Deduplicate before building the update:\n\t\t// a feed listed twice would emit two `update_single_price_feed` calls against the same\n\t\t// object off one hot-potato vector, and pay two update fees for it. One feed can also\n\t\t// map to several coins, so the reverse index holds a list, not a single key.\n\t\tconst staleFeedIds: string[] = [];\n\t\tconst feedIdToCoinKeys: Record<string, string[]> = {};\n\t\tfor (const coinKey of staleCoinKeys) {\n\t\t\tconst feedId = this.#ctx.config.getFeedId(coinKey);\n\t\t\tif (!feedIdToCoinKeys[feedId]) {\n\t\t\t\tfeedIdToCoinKeys[feedId] = [];\n\t\t\t\tstaleFeedIds.push(feedId);\n\t\t\t}\n\t\t\tfeedIdToCoinKeys[feedId].push(coinKey);\n\t\t}\n\n\t\tconst connection = this.#connection();\n\n\t\tconst priceUpdateData = await connection.getPriceFeedsUpdateData(staleFeedIds);\n\n\t\tconst { pythStateId, wormholeStateId } = this.#ctx.config.pyth;\n\t\tconst pythClient = new SuiPythClient(this.#ctx.client, pythStateId, wormholeStateId);\n\n\t\tconst updatedObjectIds = await pythClient.updatePriceFeeds(tx, priceUpdateData, staleFeedIds);\n\n\t\tfor (let i = 0; i < staleFeedIds.length; i++) {\n\t\t\tfor (const coinKey of feedIdToCoinKeys[staleFeedIds[i]]) {\n\t\t\t\tresult[coinKey] = updatedObjectIds[i];\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tasync getPriceInfoObjectAge(coinKey: string): Promise<number> {\n\t\tconst priceInfoObjectId = this.#ctx.config.getPriceInfoObjectId(coinKey);\n\t\tconst res = await this.#ctx.client.core.getObject({\n\t\t\tobjectId: priceInfoObjectId,\n\t\t\tinclude: {\n\t\t\t\tcontent: true,\n\t\t\t},\n\t\t});\n\n\t\tif (!res.object?.content) {\n\t\t\tthrow new Error(`Price info object not found for ${coinKey}`);\n\t\t}\n\n\t\tconst priceInfoObject = PriceInfoObject.parse(res.object.content);\n\t\treturn Number(priceInfoObject.price_info.arrival_time);\n\t}\n}\n"],"mappings":";;;;;;;AAYA,IAAa,mBAAb,MAA8B;CAC7B;CAEA,YAAY,KAAmB;AAC9B,QAAKA,MAAO;;;;;;;;;;;;;;;;;;CAmBb,kBAA0B;EACzB,MAAM,EAAE,gBAAgB,gBAAgB,MAAKA,IAAK,OAAO;AACzD,MAAI,eACH,QAAO;AAGR,MAAI,YACH,QAAO;AAER,MAAI,sBACH,QAAO;AAGR,QAAM,IAAI,mBACT,mQACA;;;;;;;CAQF,cAAyC;EACxC,MAAM,EAAE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