From ec74391d8dd446b9778affb6113dce6b46321287 Mon Sep 17 00:00:00 2001 From: keyvan Date: Mon, 9 Sep 2024 19:48:40 -0700 Subject: [PATCH 1/3] feat(hermes): add publisher caps endpoint to js client --- apps/hermes/client/js/package.json | 2 +- apps/hermes/client/js/src/HermesClient.ts | 25 +++++++++++++- apps/hermes/client/js/src/zodSchemas.ts | 41 +++++++++++++++++++++-- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/apps/hermes/client/js/package.json b/apps/hermes/client/js/package.json index dc609edfec..c5ac8aaa47 100644 --- a/apps/hermes/client/js/package.json +++ b/apps/hermes/client/js/package.json @@ -1,6 +1,6 @@ { "name": "@pythnetwork/hermes-client", - "version": "1.0.4", + "version": "1.1.0", "description": "Pyth Hermes Client", "author": { "name": "Pyth Data Association" diff --git a/apps/hermes/client/js/src/HermesClient.ts b/apps/hermes/client/js/src/HermesClient.ts index f4a12eaed6..537d3aa92f 100644 --- a/apps/hermes/client/js/src/HermesClient.ts +++ b/apps/hermes/client/js/src/HermesClient.ts @@ -5,11 +5,12 @@ import { camelToSnakeCaseObject } from "./utils"; // Accessing schema objects export type AssetType = z.infer; -export type BinaryPriceUpdate = z.infer; +export type BinaryPriceUpdate = z.infer; export type EncodingType = z.infer; export type PriceFeedMetadata = z.infer; export type PriceIdInput = z.infer; export type PriceUpdate = z.infer; +export type PublisherCaps = z.infer; const DEFAULT_TIMEOUT: DurationInMs = 5000; const DEFAULT_HTTP_RETRIES = 3; @@ -120,6 +121,28 @@ export class HermesClient { ); } + /** + * Fetch the latest publisher stake caps. + * This endpoint can be customized by specifying the encoding type and whether the results should also return the parsed publisher caps. + * This will throw an error if there is a network problem or the price service returns a non-ok response. + * + * @param options Optional parameters: + * - encoding: Encoding type. If specified, return the price update in the encoding specified by the encoding parameter. Default is hex. + * - parsed: Boolean to specify if the parsed price update should be included in the response. Default is false. + * + * @returns PublisherCaps object containing the latest publisher stake caps. + */ + async getLatestPublisherCaps(options?: { + encoding?: EncodingType; + parsed?: boolean; + }): Promise { + const url = new URL("v2/updates/publisher_stake_caps/latest", this.baseURL); + if (options) { + this.appendUrlSearchParams(url, options); + } + return await this.httpRequest(url.toString(), schemas.PublisherCaps); + } + /** * Fetch the latest price updates 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 parsed price update using the options object. diff --git a/apps/hermes/client/js/src/zodSchemas.ts b/apps/hermes/client/js/src/zodSchemas.ts index 5745eabdce..ce00bcbefe 100644 --- a/apps/hermes/client/js/src/zodSchemas.ts +++ b/apps/hermes/client/js/src/zodSchemas.ts @@ -9,7 +9,7 @@ const PriceFeedMetadata = z .passthrough(); const PriceIdInput = z.string(); const EncodingType = z.enum(["hex", "base64"]); -const BinaryPriceUpdate = z +const BinaryUpdate = z .object({ data: z.array(z.string()), encoding: EncodingType }) .passthrough(); const RpcPrice = z @@ -38,10 +38,24 @@ const ParsedPriceUpdate = z .passthrough(); const PriceUpdate = z .object({ - binary: BinaryPriceUpdate, + binary: BinaryUpdate, parsed: z.array(ParsedPriceUpdate).nullish(), }) .passthrough(); +const ParsedPublisherCap = z.object({ + publisher: z.string(), + cap: z.number().int(), +}); + +const PublisherCaps = z.object({ + binary: BinaryUpdate, + parsed: z + .object({ + publisher_stake_caps: z.array(ParsedPublisherCap), + }) + .array() + .nullish(), +}); export const schemas = { AssetType, @@ -50,11 +64,12 @@ export const schemas = { PriceFeedMetadata, PriceIdInput, EncodingType, - BinaryPriceUpdate, + BinaryUpdate, RpcPrice, RpcPriceFeedMetadataV2, ParsedPriceUpdate, PriceUpdate, + PublisherCaps, }; const endpoints = makeApi([ @@ -196,6 +211,26 @@ Given a collection of price feed ids, retrieve the latest Pyth price for each pr }, ], }, + { + method: "get", + path: "/v2/updates/publisher_stake_caps/latest", + alias: "latest_publisher_stake_caps", + description: `Get the most recent publisher stake caps update data.`, + requestFormat: "json", + parameters: [ + { + name: "encoding", + type: "Query", + schema: z.enum(["hex", "base64"]).optional(), + }, + { + name: "parsed", + type: "Query", + schema: z.boolean().optional(), + }, + ], + response: z.array(PublisherCaps), + }, ]); export const api = new Zodios(endpoints); From 18070309e026469fd69949395b5578d071d2390f Mon Sep 17 00:00:00 2001 From: keyvan Date: Wed, 11 Sep 2024 07:50:24 -0700 Subject: [PATCH 2/3] fix typo --- apps/hermes/client/js/src/HermesClient.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/hermes/client/js/src/HermesClient.ts b/apps/hermes/client/js/src/HermesClient.ts index 537d3aa92f..38bb9bf461 100644 --- a/apps/hermes/client/js/src/HermesClient.ts +++ b/apps/hermes/client/js/src/HermesClient.ts @@ -127,8 +127,8 @@ export class HermesClient { * This will throw an error if there is a network problem or the price service returns a non-ok response. * * @param options Optional parameters: - * - encoding: Encoding type. If specified, return the price update in the encoding specified by the encoding parameter. Default is hex. - * - parsed: Boolean to specify if the parsed price update should be included in the response. Default is false. + * - encoding: Encoding type. If specified, return the publisher caps in the encoding specified by the encoding parameter. Default is hex. + * - parsed: Boolean to specify if the parsed publisher caps should be included in the response. Default is false. * * @returns PublisherCaps object containing the latest publisher stake caps. */ From c4d6989609d64bff5a180a11462ef50d4e6437a2 Mon Sep 17 00:00:00 2001 From: keyvan Date: Wed, 11 Sep 2024 09:05:11 -0700 Subject: [PATCH 3/3] fix schema --- apps/hermes/client/js/src/HermesClient.ts | 9 +++++-- apps/hermes/client/js/src/zodSchemas.ts | 32 +++++++++++------------ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/apps/hermes/client/js/src/HermesClient.ts b/apps/hermes/client/js/src/HermesClient.ts index 38bb9bf461..64882fe86c 100644 --- a/apps/hermes/client/js/src/HermesClient.ts +++ b/apps/hermes/client/js/src/HermesClient.ts @@ -10,7 +10,9 @@ export type EncodingType = z.infer; export type PriceFeedMetadata = z.infer; export type PriceIdInput = z.infer; export type PriceUpdate = z.infer; -export type PublisherCaps = z.infer; +export type PublisherCaps = z.infer< + typeof schemas.LatestPublisherStakeCapsUpdateDataResponse +>; const DEFAULT_TIMEOUT: DurationInMs = 5000; const DEFAULT_HTTP_RETRIES = 3; @@ -140,7 +142,10 @@ export class HermesClient { if (options) { this.appendUrlSearchParams(url, options); } - return await this.httpRequest(url.toString(), schemas.PublisherCaps); + return await this.httpRequest( + url.toString(), + schemas.LatestPublisherStakeCapsUpdateDataResponse + ); } /** diff --git a/apps/hermes/client/js/src/zodSchemas.ts b/apps/hermes/client/js/src/zodSchemas.ts index ce00bcbefe..b64f334c77 100644 --- a/apps/hermes/client/js/src/zodSchemas.ts +++ b/apps/hermes/client/js/src/zodSchemas.ts @@ -42,20 +42,18 @@ const PriceUpdate = z parsed: z.array(ParsedPriceUpdate).nullish(), }) .passthrough(); -const ParsedPublisherCap = z.object({ - publisher: z.string(), - cap: z.number().int(), -}); - -const PublisherCaps = z.object({ - binary: BinaryUpdate, - parsed: z - .object({ - publisher_stake_caps: z.array(ParsedPublisherCap), - }) - .array() - .nullish(), -}); +const ParsedPublisherStakeCap = z + .object({ cap: z.number().int().gte(0), publisher: z.string() }) + .passthrough(); +const ParsedPublisherStakeCapsUpdate = z + .object({ publisher_stake_caps: z.array(ParsedPublisherStakeCap) }) + .passthrough(); +const LatestPublisherStakeCapsUpdateDataResponse = z + .object({ + binary: BinaryUpdate, + parsed: z.array(ParsedPublisherStakeCapsUpdate).nullish(), + }) + .passthrough(); export const schemas = { AssetType, @@ -69,7 +67,9 @@ export const schemas = { RpcPriceFeedMetadataV2, ParsedPriceUpdate, PriceUpdate, - PublisherCaps, + ParsedPublisherStakeCap, + ParsedPublisherStakeCapsUpdate, + LatestPublisherStakeCapsUpdateDataResponse, }; const endpoints = makeApi([ @@ -229,7 +229,7 @@ Given a collection of price feed ids, retrieve the latest Pyth price for each pr schema: z.boolean().optional(), }, ], - response: z.array(PublisherCaps), + response: LatestPublisherStakeCapsUpdateDataResponse, }, ]);