From 1920b806e3e3921a3142b678935d1aea8ca94506 Mon Sep 17 00:00:00 2001 From: Daniel Chew Date: Mon, 10 Jun 2024 11:07:34 +0900 Subject: [PATCH 1/4] fix(apps/hermes/client): use camelCase for streaming params --- apps/hermes/client/js/package.json | 2 +- apps/hermes/client/js/src/HermesClient.ts | 22 ++++++++++++++++--- .../client/js/src/examples/HermesClient.ts | 7 +++++- package-lock.json | 4 ++-- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/apps/hermes/client/js/package.json b/apps/hermes/client/js/package.json index 2033430775..77c54215ca 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.0", + "version": "1.0.1", "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 fd3cc2ffbc..3e8a9a22a7 100644 --- a/apps/hermes/client/js/src/HermesClient.ts +++ b/apps/hermes/client/js/src/HermesClient.ts @@ -30,6 +30,20 @@ export type HermesClientConfig = { httpRetries?: number; }; +function camelToSnakeCase(str: string): string { + return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`); +} + +function camelToSnakeCaseObject( + obj: Record +): Record { + const result: Record = {}; + Object.keys(obj).forEach((key) => { + result[camelToSnakeCase(key)] = obj[key]; + }); + return result; +} + export class HermesClient { private baseURL: string; private timeout: DurationInMs; @@ -193,8 +207,8 @@ export class HermesClient { options?: { encoding?: EncodingType; parsed?: boolean; - allow_unordered?: boolean; - benchmarks_only?: boolean; + allowUnordered?: boolean; + benchmarksOnly?: boolean; } ): Promise { const url = new URL("/v2/updates/price/stream", this.baseURL); @@ -203,8 +217,10 @@ export class HermesClient { }); if (options) { - this.appendUrlSearchParams(url, options); + const transformedOptions = camelToSnakeCaseObject(options); + this.appendUrlSearchParams(url, transformedOptions); } + console.log(url.toString()); return new EventSource(url.toString()); } diff --git a/apps/hermes/client/js/src/examples/HermesClient.ts b/apps/hermes/client/js/src/examples/HermesClient.ts index 1fa6c44c97..7d286ce149 100644 --- a/apps/hermes/client/js/src/examples/HermesClient.ts +++ b/apps/hermes/client/js/src/examples/HermesClient.ts @@ -45,7 +45,12 @@ async function run() { console.log(priceUpdates); // Streaming price updates - const eventSource = await connection.getPriceUpdatesStream(priceIds); + const eventSource = await connection.getPriceUpdatesStream(priceIds, { + encoding: "base64", + parsed: true, + allowUnordered: true, + benchmarksOnly: true, + }); eventSource.onmessage = (event) => { console.log("Received price update:", event.data); diff --git a/package-lock.json b/package-lock.json index a1143e44a0..619cc7500f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,7 +48,7 @@ }, "apps/hermes/client/js": { "name": "@pythnetwork/hermes-client", - "version": "1.0.0", + "version": "1.0.1", "license": "Apache-2.0", "dependencies": { "eventsource": "^2.0.2", @@ -54903,7 +54903,7 @@ }, "target_chains/ethereum/sdk/js": { "name": "@pythnetwork/pyth-evm-js", - "version": "1.51.0", + "version": "1.52.0", "license": "Apache-2.0", "dependencies": { "@pythnetwork/price-service-client": "*", From 62757210616b1023bf00e6848a9eb2c0b52be2c6 Mon Sep 17 00:00:00 2001 From: Daniel Chew Date: Mon, 10 Jun 2024 11:08:18 +0900 Subject: [PATCH 2/4] remove console log --- apps/hermes/client/js/src/HermesClient.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/hermes/client/js/src/HermesClient.ts b/apps/hermes/client/js/src/HermesClient.ts index 3e8a9a22a7..890a3b8815 100644 --- a/apps/hermes/client/js/src/HermesClient.ts +++ b/apps/hermes/client/js/src/HermesClient.ts @@ -220,7 +220,6 @@ export class HermesClient { const transformedOptions = camelToSnakeCaseObject(options); this.appendUrlSearchParams(url, transformedOptions); } - console.log(url.toString()); return new EventSource(url.toString()); } From a589fbf0680295857b353d541e0e61d3f22b6a83 Mon Sep 17 00:00:00 2001 From: Daniel Chew Date: Mon, 10 Jun 2024 11:10:23 +0900 Subject: [PATCH 3/4] refactor --- apps/hermes/client/js/src/HermesClient.ts | 15 +-------------- apps/hermes/client/js/src/utils.ts | 13 +++++++++++++ 2 files changed, 14 insertions(+), 14 deletions(-) create mode 100644 apps/hermes/client/js/src/utils.ts diff --git a/apps/hermes/client/js/src/HermesClient.ts b/apps/hermes/client/js/src/HermesClient.ts index 890a3b8815..12475dadd3 100644 --- a/apps/hermes/client/js/src/HermesClient.ts +++ b/apps/hermes/client/js/src/HermesClient.ts @@ -1,6 +1,7 @@ import EventSource from "eventsource"; import { schemas } from "./zodSchemas"; import { z } from "zod"; +import { camelToSnakeCaseObject } from "./utils"; // Accessing schema objects export type AssetType = z.infer; @@ -30,20 +31,6 @@ export type HermesClientConfig = { httpRetries?: number; }; -function camelToSnakeCase(str: string): string { - return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`); -} - -function camelToSnakeCaseObject( - obj: Record -): Record { - const result: Record = {}; - Object.keys(obj).forEach((key) => { - result[camelToSnakeCase(key)] = obj[key]; - }); - return result; -} - export class HermesClient { private baseURL: string; private timeout: DurationInMs; diff --git a/apps/hermes/client/js/src/utils.ts b/apps/hermes/client/js/src/utils.ts new file mode 100644 index 0000000000..bac6b59f43 --- /dev/null +++ b/apps/hermes/client/js/src/utils.ts @@ -0,0 +1,13 @@ +function camelToSnakeCase(str: string): string { + return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`); +} + +export function camelToSnakeCaseObject( + obj: Record +): Record { + const result: Record = {}; + Object.keys(obj).forEach((key) => { + result[camelToSnakeCase(key)] = obj[key]; + }); + return result; +} From db86be0a4f13a4642a4eed1aed8888f6e796ec20 Mon Sep 17 00:00:00 2001 From: Daniel Chew Date: Mon, 10 Jun 2024 11:24:00 +0900 Subject: [PATCH 4/4] use hex as example --- apps/hermes/client/js/src/examples/HermesClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/hermes/client/js/src/examples/HermesClient.ts b/apps/hermes/client/js/src/examples/HermesClient.ts index 7d286ce149..3965006f30 100644 --- a/apps/hermes/client/js/src/examples/HermesClient.ts +++ b/apps/hermes/client/js/src/examples/HermesClient.ts @@ -46,7 +46,7 @@ async function run() { // Streaming price updates const eventSource = await connection.getPriceUpdatesStream(priceIds, { - encoding: "base64", + encoding: "hex", parsed: true, allowUnordered: true, benchmarksOnly: true,