Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hermes): add publisher caps endpoint to js client #1879

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/hermes/client/js/package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
30 changes: 29 additions & 1 deletion apps/hermes/client/js/src/HermesClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { camelToSnakeCaseObject } from "./utils";

// Accessing schema objects
export type AssetType = z.infer<typeof schemas.AssetType>;
export type BinaryPriceUpdate = z.infer<typeof schemas.BinaryPriceUpdate>;
export type BinaryPriceUpdate = z.infer<typeof schemas.BinaryUpdate>;
export type EncodingType = z.infer<typeof schemas.EncodingType>;
export type PriceFeedMetadata = z.infer<typeof schemas.PriceFeedMetadata>;
export type PriceIdInput = z.infer<typeof schemas.PriceIdInput>;
export type PriceUpdate = z.infer<typeof schemas.PriceUpdate>;
export type PublisherCaps = z.infer<
typeof schemas.LatestPublisherStakeCapsUpdateDataResponse
>;

const DEFAULT_TIMEOUT: DurationInMs = 5000;
const DEFAULT_HTTP_RETRIES = 3;
Expand Down Expand Up @@ -120,6 +123,31 @@ 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 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.
*/
async getLatestPublisherCaps(options?: {
encoding?: EncodingType;
parsed?: boolean;
}): Promise<PublisherCaps> {
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.LatestPublisherStakeCapsUpdateDataResponse
);
}

/**
* 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.
Expand Down
41 changes: 38 additions & 3 deletions apps/hermes/client/js/src/zodSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,10 +38,22 @@ const ParsedPriceUpdate = z
.passthrough();
const PriceUpdate = z
.object({
binary: BinaryPriceUpdate,
binary: BinaryUpdate,
parsed: z.array(ParsedPriceUpdate).nullish(),
})
.passthrough();
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,
Expand All @@ -50,11 +62,14 @@ export const schemas = {
PriceFeedMetadata,
PriceIdInput,
EncodingType,
BinaryPriceUpdate,
BinaryUpdate,
RpcPrice,
RpcPriceFeedMetadataV2,
ParsedPriceUpdate,
PriceUpdate,
ParsedPublisherStakeCap,
ParsedPublisherStakeCapsUpdate,
LatestPublisherStakeCapsUpdateDataResponse,
};

const endpoints = makeApi([
Expand Down Expand Up @@ -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: LatestPublisherStakeCapsUpdateDataResponse,
},
]);

export const api = new Zodios(endpoints);
Expand Down
Loading