@pythnetwork/hermes-client
Advanced tools
@@ -21,2 +21,3 @@ /* eslint-disable @typescript-eslint/require-await */ /* eslint-disable @typescript-eslint/no-misused-spread */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ "use strict"; | ||
| headers; | ||
| accessToken; | ||
| /** | ||
@@ -32,5 +33,11 @@ * Constructs a new Connection. | ||
| this.headers = config?.headers ?? {}; | ||
| this.accessToken = config?.accessToken; | ||
| } | ||
| async httpRequest(url, schema, options, retries = this.httpRetries, backoff = 100 + Math.floor(Math.random() * 100)) { | ||
| try { | ||
| // Build auth headers if access token is provided | ||
| const authHeaders = {}; | ||
| if (this.accessToken !== undefined) { | ||
| authHeaders.Authorization = `Bearer ${this.accessToken}`; | ||
| } | ||
| const response = await fetch(url, { | ||
@@ -45,2 +52,3 @@ ...options, | ||
| headers: { | ||
| ...authHeaders, | ||
| ...this.headers, | ||
@@ -174,2 +182,7 @@ ...options?.headers | ||
| } | ||
| // Build auth headers for SSE fetch | ||
| const authHeaders = {}; | ||
| if (this.accessToken !== undefined) { | ||
| authHeaders.Authorization = `Bearer ${this.accessToken}`; | ||
| } | ||
| return new _eventsource.EventSource(url.toString(), { | ||
@@ -180,2 +193,3 @@ fetch: (input, init)=>fetch(input, { | ||
| ...init?.headers, | ||
| ...authHeaders, | ||
| ...this.headers | ||
@@ -186,27 +200,2 @@ } | ||
| } | ||
| /** | ||
| * Fetch the latest TWAP (time weighted average price) for a set of price feed IDs. | ||
| * This endpoint can be customized by specifying the encoding type and whether the results should also return the calculated TWAP using the options object. | ||
| * This will throw an error if there is a network problem or the price service returns a non-ok response. | ||
| * | ||
| * @param ids - Array of hex-encoded price feed IDs for which updates are requested. | ||
| * @param window_seconds - The time window in seconds over which to calculate the TWAP, ending at the current time. | ||
| * For example, a value of 300 would return the most recent 5 minute TWAP. Must be greater than 0 and less than or equal to 600 seconds (10 minutes). | ||
| * @param options - Optional parameters: | ||
| * - encoding: Encoding type. If specified, return the TWAP binary data in the encoding specified by the encoding parameter. Default is hex. | ||
| * - parsed: Boolean to specify if the calculated TWAP should be included in the response. Default is false. | ||
| * - ignoreInvalidPriceIds: Boolean to specify if invalid price IDs should be ignored instead of returning an error. Default is false. | ||
| * | ||
| * @returns TwapsResponse object containing the latest TWAPs. | ||
| */ async getLatestTwaps(ids, window_seconds, options, fetchOptions) { | ||
| const url = this.buildURL(`updates/twap/${window_seconds.toString()}/latest`); | ||
| for (const id of ids){ | ||
| url.searchParams.append("ids[]", id); | ||
| } | ||
| if (options) { | ||
| const transformedOptions = (0, _utils.camelToSnakeCaseObject)(options); | ||
| this.appendUrlSearchParams(url, transformedOptions); | ||
| } | ||
| return this.httpRequest(url.toString(), _zodSchemas.schemas.TwapsResponse, fetchOptions); | ||
| } | ||
| appendUrlSearchParams(url, params) { | ||
@@ -213,0 +202,0 @@ for (const [key, value] of Object.entries(params)){ |
@@ -10,3 +10,2 @@ import { EventSource } from "eventsource"; | ||
| export type PriceUpdate = z.infer<typeof schemas.PriceUpdate>; | ||
| export type TwapsResponse = z.infer<typeof schemas.TwapsResponse>; | ||
| export type PublisherCaps = z.infer<typeof schemas.LatestPublisherStakeCapsUpdateDataResponse>; | ||
@@ -30,2 +29,9 @@ export type UnixTimestamp = number; | ||
| headers?: HeadersInit; | ||
| /** | ||
| * Optional API access token for authentication. | ||
| * When provided, this token will be included in all requests either: | ||
| * - As a Bearer token in the Authorization header (for HTTP requests) | ||
| * - As an ACCESS_TOKEN query parameter (for WebSocket/SSE connections) | ||
| */ | ||
| accessToken?: string; | ||
| }; | ||
@@ -37,2 +43,3 @@ export declare class HermesClient { | ||
| private headers; | ||
| private accessToken; | ||
| /** | ||
@@ -139,24 +146,4 @@ * Constructs a new Connection. | ||
| }): Promise<EventSource>; | ||
| /** | ||
| * Fetch the latest TWAP (time weighted average price) for a set of price feed IDs. | ||
| * This endpoint can be customized by specifying the encoding type and whether the results should also return the calculated TWAP using the options object. | ||
| * This will throw an error if there is a network problem or the price service returns a non-ok response. | ||
| * | ||
| * @param ids - Array of hex-encoded price feed IDs for which updates are requested. | ||
| * @param window_seconds - The time window in seconds over which to calculate the TWAP, ending at the current time. | ||
| * For example, a value of 300 would return the most recent 5 minute TWAP. Must be greater than 0 and less than or equal to 600 seconds (10 minutes). | ||
| * @param options - Optional parameters: | ||
| * - encoding: Encoding type. If specified, return the TWAP binary data in the encoding specified by the encoding parameter. Default is hex. | ||
| * - parsed: Boolean to specify if the calculated TWAP should be included in the response. Default is false. | ||
| * - ignoreInvalidPriceIds: Boolean to specify if invalid price IDs should be ignored instead of returning an error. Default is false. | ||
| * | ||
| * @returns TwapsResponse object containing the latest TWAPs. | ||
| */ | ||
| getLatestTwaps(ids: HexString[], window_seconds: number, options?: { | ||
| encoding?: EncodingType; | ||
| parsed?: boolean; | ||
| ignoreInvalidPriceIds?: boolean; | ||
| }, fetchOptions?: RequestInit): Promise<TwapsResponse>; | ||
| private appendUrlSearchParams; | ||
| private buildURL; | ||
| } |
@@ -1,2 +0,2 @@ | ||
| /* eslint-disable @typescript-eslint/no-non-null-assertion */ "use strict"; | ||
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { | ||
@@ -11,3 +11,3 @@ value: true | ||
| }); | ||
| function camelToSnakeCase(str) { | ||
| /* eslint-disable @typescript-eslint/no-non-null-assertion */ function camelToSnakeCase(str) { | ||
| return str.replaceAll(/[A-Z]/g, (letter)=>`_${letter.toLowerCase()}`); | ||
@@ -14,0 +14,0 @@ } |
@@ -33,3 +33,5 @@ "use strict"; | ||
| "crypto_index", | ||
| "crypto_nav" | ||
| "crypto_nav", | ||
| "eco", | ||
| "kalshi" | ||
| ]); | ||
@@ -83,13 +85,2 @@ const asset_type = AssetType.nullish(); | ||
| }).passthrough(); | ||
| const ParsedPriceFeedTwap = _zod.z.object({ | ||
| down_slots_ratio: _zod.z.string(), | ||
| end_timestamp: _zod.z.number().int(), | ||
| id: RpcPriceIdentifier, | ||
| start_timestamp: _zod.z.number().int(), | ||
| twap: RpcPrice | ||
| }).passthrough(); | ||
| const TwapsResponse = _zod.z.object({ | ||
| binary: BinaryUpdate, | ||
| parsed: _zod.z.array(ParsedPriceFeedTwap).nullish() | ||
| }).passthrough(); | ||
| const schemas = { | ||
@@ -109,5 +100,3 @@ AssetType, | ||
| ParsedPublisherStakeCapsUpdate, | ||
| LatestPublisherStakeCapsUpdateDataResponse, | ||
| ParsedPriceFeedTwap, | ||
| TwapsResponse | ||
| LatestPublisherStakeCapsUpdateDataResponse | ||
| }; | ||
@@ -302,49 +291,2 @@ const endpoints = (0, _core.makeApi)([ | ||
| response: LatestPublisherStakeCapsUpdateDataResponse | ||
| }, | ||
| { | ||
| method: "get", | ||
| path: "/v2/updates/twap/:window_seconds/latest", | ||
| alias: "latest_twaps", | ||
| description: `Get the latest TWAP by price feed id with a custom time window. | ||
| Given a collection of price feed ids, retrieve the latest Pyth TWAP price for each price feed.`, | ||
| requestFormat: "json", | ||
| parameters: [ | ||
| { | ||
| name: "window_seconds", | ||
| type: "Path", | ||
| schema: _zod.z.number().int().gte(0) | ||
| }, | ||
| { | ||
| name: "ids[]", | ||
| type: "Query", | ||
| schema: _zod.z.array(PriceIdInput) | ||
| }, | ||
| { | ||
| name: "encoding", | ||
| type: "Query", | ||
| schema: _zod.z.enum([ | ||
| "hex", | ||
| "base64" | ||
| ]).optional() | ||
| }, | ||
| { | ||
| name: "parsed", | ||
| type: "Query", | ||
| schema: _zod.z.boolean().optional() | ||
| }, | ||
| { | ||
| name: "ignore_invalid_price_ids", | ||
| type: "Query", | ||
| schema: _zod.z.boolean().optional() | ||
| } | ||
| ], | ||
| response: TwapsResponse, | ||
| errors: [ | ||
| { | ||
| status: 404, | ||
| description: `Price ids not found`, | ||
| schema: _zod.z.void() | ||
| } | ||
| ] | ||
| } | ||
@@ -351,0 +293,0 @@ ]); |
@@ -10,3 +10,2 @@ import { EventSource } from "eventsource"; | ||
| export type PriceUpdate = z.infer<typeof schemas.PriceUpdate>; | ||
| export type TwapsResponse = z.infer<typeof schemas.TwapsResponse>; | ||
| export type PublisherCaps = z.infer<typeof schemas.LatestPublisherStakeCapsUpdateDataResponse>; | ||
@@ -30,2 +29,9 @@ export type UnixTimestamp = number; | ||
| headers?: HeadersInit; | ||
| /** | ||
| * Optional API access token for authentication. | ||
| * When provided, this token will be included in all requests either: | ||
| * - As a Bearer token in the Authorization header (for HTTP requests) | ||
| * - As an ACCESS_TOKEN query parameter (for WebSocket/SSE connections) | ||
| */ | ||
| accessToken?: string; | ||
| }; | ||
@@ -37,2 +43,3 @@ export declare class HermesClient { | ||
| private headers; | ||
| private accessToken; | ||
| /** | ||
@@ -139,24 +146,4 @@ * Constructs a new Connection. | ||
| }): Promise<EventSource>; | ||
| /** | ||
| * Fetch the latest TWAP (time weighted average price) for a set of price feed IDs. | ||
| * This endpoint can be customized by specifying the encoding type and whether the results should also return the calculated TWAP using the options object. | ||
| * This will throw an error if there is a network problem or the price service returns a non-ok response. | ||
| * | ||
| * @param ids - Array of hex-encoded price feed IDs for which updates are requested. | ||
| * @param window_seconds - The time window in seconds over which to calculate the TWAP, ending at the current time. | ||
| * For example, a value of 300 would return the most recent 5 minute TWAP. Must be greater than 0 and less than or equal to 600 seconds (10 minutes). | ||
| * @param options - Optional parameters: | ||
| * - encoding: Encoding type. If specified, return the TWAP binary data in the encoding specified by the encoding parameter. Default is hex. | ||
| * - parsed: Boolean to specify if the calculated TWAP should be included in the response. Default is false. | ||
| * - ignoreInvalidPriceIds: Boolean to specify if invalid price IDs should be ignored instead of returning an error. Default is false. | ||
| * | ||
| * @returns TwapsResponse object containing the latest TWAPs. | ||
| */ | ||
| getLatestTwaps(ids: HexString[], window_seconds: number, options?: { | ||
| encoding?: EncodingType; | ||
| parsed?: boolean; | ||
| ignoreInvalidPriceIds?: boolean; | ||
| }, fetchOptions?: RequestInit): Promise<TwapsResponse>; | ||
| private appendUrlSearchParams; | ||
| private buildURL; | ||
| } |
@@ -11,2 +11,3 @@ /* eslint-disable @typescript-eslint/require-await */ /* eslint-disable @typescript-eslint/no-misused-spread */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ import { EventSource } from "eventsource"; | ||
| headers; | ||
| accessToken; | ||
| /** | ||
@@ -22,5 +23,11 @@ * Constructs a new Connection. | ||
| this.headers = config?.headers ?? {}; | ||
| this.accessToken = config?.accessToken; | ||
| } | ||
| async httpRequest(url, schema, options, retries = this.httpRetries, backoff = 100 + Math.floor(Math.random() * 100)) { | ||
| try { | ||
| // Build auth headers if access token is provided | ||
| const authHeaders = {}; | ||
| if (this.accessToken !== undefined) { | ||
| authHeaders.Authorization = `Bearer ${this.accessToken}`; | ||
| } | ||
| const response = await fetch(url, { | ||
@@ -35,2 +42,3 @@ ...options, | ||
| headers: { | ||
| ...authHeaders, | ||
| ...this.headers, | ||
@@ -164,2 +172,7 @@ ...options?.headers | ||
| } | ||
| // Build auth headers for SSE fetch | ||
| const authHeaders = {}; | ||
| if (this.accessToken !== undefined) { | ||
| authHeaders.Authorization = `Bearer ${this.accessToken}`; | ||
| } | ||
| return new EventSource(url.toString(), { | ||
@@ -170,2 +183,3 @@ fetch: (input, init)=>fetch(input, { | ||
| ...init?.headers, | ||
| ...authHeaders, | ||
| ...this.headers | ||
@@ -176,27 +190,2 @@ } | ||
| } | ||
| /** | ||
| * Fetch the latest TWAP (time weighted average price) for a set of price feed IDs. | ||
| * This endpoint can be customized by specifying the encoding type and whether the results should also return the calculated TWAP using the options object. | ||
| * This will throw an error if there is a network problem or the price service returns a non-ok response. | ||
| * | ||
| * @param ids - Array of hex-encoded price feed IDs for which updates are requested. | ||
| * @param window_seconds - The time window in seconds over which to calculate the TWAP, ending at the current time. | ||
| * For example, a value of 300 would return the most recent 5 minute TWAP. Must be greater than 0 and less than or equal to 600 seconds (10 minutes). | ||
| * @param options - Optional parameters: | ||
| * - encoding: Encoding type. If specified, return the TWAP binary data in the encoding specified by the encoding parameter. Default is hex. | ||
| * - parsed: Boolean to specify if the calculated TWAP should be included in the response. Default is false. | ||
| * - ignoreInvalidPriceIds: Boolean to specify if invalid price IDs should be ignored instead of returning an error. Default is false. | ||
| * | ||
| * @returns TwapsResponse object containing the latest TWAPs. | ||
| */ async getLatestTwaps(ids, window_seconds, options, fetchOptions) { | ||
| const url = this.buildURL(`updates/twap/${window_seconds.toString()}/latest`); | ||
| for (const id of ids){ | ||
| url.searchParams.append("ids[]", id); | ||
| } | ||
| if (options) { | ||
| const transformedOptions = camelToSnakeCaseObject(options); | ||
| this.appendUrlSearchParams(url, transformedOptions); | ||
| } | ||
| return this.httpRequest(url.toString(), schemas.TwapsResponse, fetchOptions); | ||
| } | ||
| appendUrlSearchParams(url, params) { | ||
@@ -203,0 +192,0 @@ for (const [key, value] of Object.entries(params)){ |
@@ -12,3 +12,5 @@ import { makeApi, Zodios } from "@zodios/core"; | ||
| "crypto_index", | ||
| "crypto_nav" | ||
| "crypto_nav", | ||
| "eco", | ||
| "kalshi" | ||
| ]); | ||
@@ -62,13 +64,2 @@ const asset_type = AssetType.nullish(); | ||
| }).passthrough(); | ||
| const ParsedPriceFeedTwap = z.object({ | ||
| down_slots_ratio: z.string(), | ||
| end_timestamp: z.number().int(), | ||
| id: RpcPriceIdentifier, | ||
| start_timestamp: z.number().int(), | ||
| twap: RpcPrice | ||
| }).passthrough(); | ||
| const TwapsResponse = z.object({ | ||
| binary: BinaryUpdate, | ||
| parsed: z.array(ParsedPriceFeedTwap).nullish() | ||
| }).passthrough(); | ||
| export const schemas = { | ||
@@ -88,5 +79,3 @@ AssetType, | ||
| ParsedPublisherStakeCapsUpdate, | ||
| LatestPublisherStakeCapsUpdateDataResponse, | ||
| ParsedPriceFeedTwap, | ||
| TwapsResponse | ||
| LatestPublisherStakeCapsUpdateDataResponse | ||
| }; | ||
@@ -281,49 +270,2 @@ const endpoints = makeApi([ | ||
| response: LatestPublisherStakeCapsUpdateDataResponse | ||
| }, | ||
| { | ||
| method: "get", | ||
| path: "/v2/updates/twap/:window_seconds/latest", | ||
| alias: "latest_twaps", | ||
| description: `Get the latest TWAP by price feed id with a custom time window. | ||
| Given a collection of price feed ids, retrieve the latest Pyth TWAP price for each price feed.`, | ||
| requestFormat: "json", | ||
| parameters: [ | ||
| { | ||
| name: "window_seconds", | ||
| type: "Path", | ||
| schema: z.number().int().gte(0) | ||
| }, | ||
| { | ||
| name: "ids[]", | ||
| type: "Query", | ||
| schema: z.array(PriceIdInput) | ||
| }, | ||
| { | ||
| name: "encoding", | ||
| type: "Query", | ||
| schema: z.enum([ | ||
| "hex", | ||
| "base64" | ||
| ]).optional() | ||
| }, | ||
| { | ||
| name: "parsed", | ||
| type: "Query", | ||
| schema: z.boolean().optional() | ||
| }, | ||
| { | ||
| name: "ignore_invalid_price_ids", | ||
| type: "Query", | ||
| schema: z.boolean().optional() | ||
| } | ||
| ], | ||
| response: TwapsResponse, | ||
| errors: [ | ||
| { | ||
| status: 404, | ||
| description: `Price ids not found`, | ||
| schema: z.void() | ||
| } | ||
| ] | ||
| } | ||
@@ -330,0 +272,0 @@ ]); |
+2
-2
| { | ||
| "name": "@pythnetwork/hermes-client", | ||
| "version": "2.1.0", | ||
| "version": "3.1.0", | ||
| "description": "Pyth Hermes Client", | ||
@@ -43,3 +43,3 @@ "author": { | ||
| "engines": { | ||
| "node": ">=22.14.0" | ||
| "node": "^24.0.0" | ||
| }, | ||
@@ -46,0 +46,0 @@ "type": "module", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
6
-14.29%457473
-14.1%9604
-15.59%