{"version":3,"file":"PriceServiceConnection.mjs","names":[],"sources":["../../src/pyth/PriceServiceConnection.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\nimport axios from 'axios';\nimport type { AxiosInstance } from 'axios';\nimport axiosRetry from 'axios-retry';\n\nexport type HexString = string;\nexport type PriceFeedRequestConfig = {\n\tverbose?: boolean;\n\tbinary?: boolean;\n};\nexport type PriceServiceConnectionConfig = {\n\ttimeout?: number;\n\thttpRetries?: number;\n\t/**\n\t * Bearer token for Hermes deployments that authenticate. The endpoint serving Pyth's\n\t * upgraded Core answers 401 without one.\n\t *\n\t * `accessToken` is chosen to converge with the in-flight `@mysten/suins` Pyth migration\n\t * (ts-sdks#1158), which takes the same credential under this name. That is not yet\n\t * published, so this is a convergence target rather than an existing convention.\n\t */\n\taccessToken?: string;\n};\nexport class PriceServiceConnection {\n\tprivate httpClient: AxiosInstance;\n\t/**\n\t * Constructs a new Connection.\n\t *\n\t * @param endpoint endpoint URL to the price service.\n\t * @param config Optional configuration for custom setups.\n\t */\n\tconstructor(endpoint: string, config?: PriceServiceConnectionConfig) {\n\t\tthis.httpClient = axios.create({\n\t\t\tbaseURL: endpoint,\n\t\t\ttimeout: config?.timeout || 5000,\n\t\t\theaders: config?.accessToken ? { Authorization: `Bearer ${config.accessToken}` } : undefined,\n\t\t});\n\t\taxiosRetry(this.httpClient, {\n\t\t\tretries: config?.httpRetries || 3,\n\t\t\tretryDelay: axiosRetry.exponentialDelay,\n\t\t});\n\t}\n\t/**\n\t * Fetch the latest price update data for the given price IDs.\n\t *\n\t * Uses Hermes v2 (`/v2/updates/price/latest`). The v1 endpoint (`/api/latest_vaas`)\n\t * is deprecated; it returned the same payload this reads out of `binary.data`.\n\t *\n\t * Hermes returns one accumulator message covering every requested feed, not one per\n\t * feed, so the result is normally a single element regardless of `priceIds.length`.\n\t *\n\t * @param priceIds Array of hex-encoded price IDs.\n\t * @returns Array of base64-encoded update messages.\n\t */\n\tasync getLatestVaas(priceIds: HexString[]): Promise<string[]> {\n\t\tlet response;\n\t\ttry {\n\t\t\tresponse = await this.httpClient.get('/v2/updates/price/latest', {\n\t\t\t\tparams: {\n\t\t\t\t\t// Serialized explicitly rather than relying on axios's array encoding, which is\n\t\t\t\t\t// what Hermes expects and what a future axios major could change under us.\n\t\t\t\t\t'ids[]': priceIds,\n\t\t\t\t\tencoding: 'base64',\n\t\t\t\t\tparsed: false,\n\t\t\t\t},\n\t\t\t});\n\t\t} catch (error) {\n\t\t\t// An axios error carries `config.headers` — including `Authorization` — and both\n\t\t\t// `JSON.stringify(err)` and `err.toJSON()` serialize it. Callers log failed requests,\n\t\t\t// so letting the raw error escape would put the bearer token in their logs. Re-throw\n\t\t\t// a plain error carrying only what is useful for diagnosis.\n\t\t\tthrow new Error(`Hermes request failed: ${describeRequestError(error)}`, {\n\t\t\t\tcause: undefined,\n\t\t\t});\n\t\t}\n\n\t\tconst data = response.data?.binary?.data;\n\t\tif (!Array.isArray(data)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Unexpected Hermes response: expected 'binary.data' array from /v2/updates/price/latest, got ${JSON.stringify(\n\t\t\t\t\tresponse.data,\n\t\t\t\t)?.slice(0, 200)}`,\n\t\t\t);\n\t\t}\n\n\t\treturn data;\n\t}\n}\n\n/**\n * A diagnosable one-line summary of a failed request that cannot contain the access token:\n * status and response body only, never the request config or its headers.\n */\nfunction describeRequestError(error: unknown): string {\n\tconst e = error as {\n\t\tresponse?: { status?: number; statusText?: string; data?: unknown };\n\t\tcode?: string;\n\t\tmessage?: string;\n\t};\n\tif (e?.response) {\n\t\tconst body =\n\t\t\ttypeof e.response.data === 'string' ? e.response.data : JSON.stringify(e.response.data);\n\t\treturn `${e.response.status ?? '?'} ${e.response.statusText ?? ''} ${body?.slice(0, 200) ?? ''}`.trim();\n\t}\n\t// No response: a transport failure. `message` is a fixed axios string (\"timeout of 5000ms\n\t// exceeded\", \"Network Error\") that embeds no header material.\n\treturn e?.code ? `${e.code} ${e.message ?? ''}`.trim() : (e?.message ?? 'unknown error');\n}\n"],"mappings":";;;;AAwBA,IAAa,yBAAb,MAAoC;;;;;;;CAQnC,YAAY,UAAkB,QAAuC;AACpE,OAAK,aAAa,MAAM,OAAO;GAC9B,SAAS;GACT,SAAS,QAAQ,WAAW;GAC5B,SAAS,QAAQ,cAAc,EAAE,eAAe,UAAU,OAAO,eAAe,GAAG;GACnF,CAAC;AACF,aAAW,KAAK,YAAY;GAC3B,SAAS,QAAQ,eAAe;GAChC,YAAY,WAAW;GACvB,CAAC;;;;;;;;;;;;;;CAcH,MAAM,cAAc,UAA0C;EAC7D,IAAI;AACJ,MAAI;AACH,cAAW,MAAM,KAAK,WAAW,IAAI,4BAA4B,EAChE,QAAQ;IAGP,SAAS;IACT,UAAU;IACV,QAAQ;IACR,EACD,CAAC;WACM,OAAO;AAKf,SAAM,IAAI,MAAM,0BAA0B,qBAAqB,MAAM,IAAI,EACxE,OAAO,QACP,CAAC;;EAGH,MAAM,OAAO,SAAS,MAAM,QAAQ;AACpC,MAAI,CAAC,MAAM,QAAQ,KAAK,CACvB,OAAM,IAAI,MACT,+FAA+F,KAAK,UACnG,SAAS,KACT,EAAE,MAAM,GAAG,IAAI,GAChB;AAGF,SAAO;;;;;;;AAQT,SAAS,qBAAqB,OAAwB;CACrD,MAAM,IAAI;AAKV,KAAI,GAAG,UAAU;EAChB,MAAM,OACL,OAAO,EAAE,SAAS,SAAS,WAAW,EAAE,SAAS,OAAO,KAAK,UAAU,EAAE,SAAS,KAAK;AACxF,SAAO,GAAG,EAAE,SAAS,UAAU,IAAI,GAAG,EAAE,SAAS,cAAc,GAAG,GAAG,MAAM,MAAM,GAAG,IAAI,IAAI,KAAK,MAAM;;AAIxG,QAAO,GAAG,OAAO,GAAG,EAAE,KAAK,GAAG,EAAE,WAAW,KAAK,MAAM,GAAI,GAAG,WAAW"}