Skip to content

Commit e9a078c

Browse files
committed
add latest twap endpoint
1 parent 65baa14 commit e9a078c

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

apps/hermes/client/js/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/hermes-client",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "Pyth Hermes Client",
55
"author": {
66
"name": "Pyth Data Association"
@@ -22,7 +22,7 @@
2222
"scripts": {
2323
"build:typescript": "tsc",
2424
"build:schemas": "openapi-zod-client ./schema.json --output src/zodSchemas.ts",
25-
"pull:schema": "curl -o schema.json -z schema.json https://hermes.pyth.network/docs/openapi.json",
25+
"pull:schema": "curl -o schema.json -z schema.json http://localhost:33999/docs/openapi.json",
2626
"example": "node lib/examples/HermesClient.js",
2727
"format": "prettier --write \"src/**/*.ts\"",
2828
"test:lint": "eslint src/",

apps/hermes/client/js/src/HermesClient.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type EncodingType = z.infer<typeof schemas.EncodingType>;
1010
export type PriceFeedMetadata = z.infer<typeof schemas.PriceFeedMetadata>;
1111
export type PriceIdInput = z.infer<typeof schemas.PriceIdInput>;
1212
export type PriceUpdate = z.infer<typeof schemas.PriceUpdate>;
13+
export type TwapsResponse = z.infer<typeof schemas.TwapsResponse>;
1314
export type PublisherCaps = z.infer<
1415
typeof schemas.LatestPublisherStakeCapsUpdateDataResponse
1516
>;
@@ -258,6 +259,46 @@ export class HermesClient {
258259
return new EventSource(url.toString(), { headers: this.headers });
259260
}
260261

262+
/**
263+
* Fetch the latest TWAP (time weighted average price) for a set of price feed IDs.
264+
* This endpoint can be customized by specifying the encoding type and whether the results should also return the calculated TWAP using the options object.
265+
* This will throw an error if there is a network problem or the price service returns a non-ok response.
266+
*
267+
* @param ids Array of hex-encoded price feed IDs for which updates are requested.
268+
* @param window_seconds The time window in seconds over which to calculate the TWAP, ending at the current time.
269+
* 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).
270+
* @param options Optional parameters:
271+
* - encoding: Encoding type. If specified, return the TWAP binary data in the encoding specified by the encoding parameter. Default is hex.
272+
* - parsed: Boolean to specify if the calculated TWAP should be included in the response. Default is false.
273+
* - ignoreInvalidPriceIds: Boolean to specify if invalid price IDs should be ignored instead of returning an error. Default is false.
274+
*
275+
* @returns TwapsResponse object containing the latest TWAPs.
276+
*/
277+
async getLatestTwap(
278+
ids: HexString[],
279+
window_seconds: number,
280+
options?: {
281+
encoding?: EncodingType;
282+
parsed?: boolean;
283+
ignoreInvalidPriceIds?: boolean;
284+
}
285+
): Promise<TwapsResponse> {
286+
const url = new URL(
287+
`v2/updates/twap/${window_seconds}/latest`,
288+
this.baseURL
289+
);
290+
for (const id of ids) {
291+
url.searchParams.append("ids[]", id);
292+
}
293+
294+
if (options) {
295+
const transformedOptions = camelToSnakeCaseObject(options);
296+
this.appendUrlSearchParams(url, transformedOptions);
297+
}
298+
299+
return this.httpRequest(url.toString(), schemas.TwapsResponse);
300+
}
301+
261302
private appendUrlSearchParams(
262303
url: URL,
263304
params: Record<string, string | boolean>

0 commit comments

Comments
 (0)