Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Commit 5cf8e66

Browse files
authored
[common] Add binary request config + bump v (#83)
1 parent abb345c commit 5cf8e66

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

pyth-common-js/package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyth-common-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/pyth-common-js",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"description": "Pyth Network Common Utils in JS",
55
"author": {
66
"name": "Pyth Data Association"
@@ -41,7 +41,7 @@
4141
"yargs": "^17.4.1"
4242
},
4343
"dependencies": {
44-
"@pythnetwork/pyth-sdk-js": "^1.1.0",
44+
"@pythnetwork/pyth-sdk-js": "^1.2.0",
4545
"@types/ws": "^8.5.3",
4646
"axios": "^0.26.1",
4747
"axios-retry": "^3.2.4",

pyth-common-js/src/PriceServiceConnection.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import { makeWebsocketUrl, removeLeading0xIfExists } from "./utils";
88

99
export type DurationInMs = number;
1010

11+
export type PriceFeedRequestConfig = {
12+
/* Optional verbose to request for verbose information from the service */
13+
verbose?: boolean;
14+
/* Optional binary to include the price feeds binary update data */
15+
binary?: boolean;
16+
};
17+
1118
export type PriceServiceConnectionConfig = {
1219
/* Timeout of each request (for all of retries). Default: 5000ms */
1320
timeout?: DurationInMs;
@@ -20,14 +27,17 @@ export type PriceServiceConnectionConfig = {
2027
httpRetries?: number;
2128
/* Optional logger (e.g: console or any logging library) to log internal events */
2229
logger?: Logger;
23-
/* Optional verbose to request for verbose information from the service */
30+
/* Deprecated: please use priceFeedRequestConfig.verbose instead */
2431
verbose?: boolean;
32+
/* Configuration for the price feed requests */
33+
priceFeedRequestConfig?: PriceFeedRequestConfig;
2534
};
2635

2736
type ClientMessage = {
2837
type: "subscribe" | "unsubscribe";
2938
ids: HexString[];
3039
verbose?: boolean;
40+
binary?: boolean;
3141
};
3242

3343
type ServerResponse = {
@@ -54,7 +64,7 @@ export class PriceServiceConnection {
5464

5565
private logger: undefined | Logger;
5666

57-
private verbose: boolean;
67+
private priceFeedRequestConfig: PriceFeedRequestConfig;
5868

5969
/**
6070
* Custom handler for web socket errors (connection and message parsing).
@@ -79,9 +89,13 @@ export class PriceServiceConnection {
7989
retryDelay: axiosRetry.exponentialDelay,
8090
});
8191

82-
this.verbose = config?.verbose || false;
92+
this.priceFeedRequestConfig = {
93+
binary: config?.priceFeedRequestConfig?.binary,
94+
verbose: config?.priceFeedRequestConfig?.verbose ?? config?.verbose,
95+
};
8396

8497
this.priceFeedCallbacks = new Map();
98+
8599
this.logger = config?.logger;
86100
this.onWsError = (error: Error) => {
87101
this.logger?.error(error);
@@ -107,7 +121,8 @@ export class PriceServiceConnection {
107121
const response = await this.httpClient.get("/api/latest_price_feeds", {
108122
params: {
109123
ids: priceIds,
110-
verbose: this.verbose,
124+
verbose: this.priceFeedRequestConfig.verbose,
125+
binary: this.priceFeedRequestConfig.binary,
111126
},
112127
});
113128
const priceFeedsJson = response.data as any[];
@@ -180,7 +195,8 @@ export class PriceServiceConnection {
180195
const message: ClientMessage = {
181196
ids: newPriceIds,
182197
type: "subscribe",
183-
verbose: this.verbose,
198+
verbose: this.priceFeedRequestConfig.verbose,
199+
binary: this.priceFeedRequestConfig.binary,
184200
};
185201

186202
await this.wsClient?.send(JSON.stringify(message));
@@ -262,7 +278,8 @@ export class PriceServiceConnection {
262278
const message: ClientMessage = {
263279
ids: Array.from(this.priceFeedCallbacks.keys()),
264280
type: "subscribe",
265-
verbose: this.verbose,
281+
verbose: this.priceFeedRequestConfig.verbose,
282+
binary: this.priceFeedRequestConfig.binary,
266283
};
267284

268285
this.logger?.info("Resubscribing to existing price feeds.");

pyth-common-js/src/examples/PriceServiceClient.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const argv = yargs(hideBin(process.argv))
3131
async function run() {
3232
const connection = new PriceServiceConnection(argv.endpoint, {
3333
logger: console, // Providing logger will allow the connection to log it's events.
34+
priceFeedRequestConfig: {
35+
binary: true,
36+
},
3437
});
3538

3639
const priceIds = argv.priceIds as string[];
@@ -46,6 +49,7 @@ async function run() {
4649
priceFeed.getPriceNoOlderThan(60)
4750
)}.`
4851
);
52+
console.log(priceFeed.getVAA());
4953
});
5054

5155
await sleep(600000);

0 commit comments

Comments
 (0)