@pythnetwork/hermes-client 1.3.1 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/HermesClient.d.ts +8 -6
- package/lib/HermesClient.d.ts.map +1 -1
- package/lib/HermesClient.js +30 -29
- package/lib/utils.js +1 -2
- package/lib/zodSchemas.js +2 -2
- package/package.json +4 -5
package/lib/HermesClient.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import EventSource from "eventsource";
|
|
1
|
+
import { EventSource } from "eventsource";
|
|
2
2
|
import { schemas } from "./zodSchemas";
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
export type AssetType = z.infer<typeof schemas.AssetType>;
|
|
@@ -51,9 +51,10 @@ export declare class HermesClient {
|
|
|
51
51
|
*
|
|
52
52
|
* @returns Array of PriceFeedMetadata objects.
|
|
53
53
|
*/
|
|
54
|
-
getPriceFeeds(options?: {
|
|
54
|
+
getPriceFeeds({ fetchOptions, ...options }?: {
|
|
55
55
|
query?: string;
|
|
56
56
|
filter?: string;
|
|
57
|
+
fetchOptions?: RequestInit;
|
|
57
58
|
}): Promise<PriceFeedMetadata[]>;
|
|
58
59
|
/**
|
|
59
60
|
* Fetch the latest publisher stake caps.
|
|
@@ -66,9 +67,10 @@ export declare class HermesClient {
|
|
|
66
67
|
*
|
|
67
68
|
* @returns PublisherCaps object containing the latest publisher stake caps.
|
|
68
69
|
*/
|
|
69
|
-
getLatestPublisherCaps(options?: {
|
|
70
|
+
getLatestPublisherCaps({ fetchOptions, ...options }?: {
|
|
70
71
|
encoding?: EncodingType;
|
|
71
72
|
parsed?: boolean;
|
|
73
|
+
fetchOptions?: RequestInit;
|
|
72
74
|
}): Promise<PublisherCaps>;
|
|
73
75
|
/**
|
|
74
76
|
* Fetch the latest price updates for a set of price feed IDs.
|
|
@@ -87,7 +89,7 @@ export declare class HermesClient {
|
|
|
87
89
|
encoding?: EncodingType;
|
|
88
90
|
parsed?: boolean;
|
|
89
91
|
ignoreInvalidPriceIds?: boolean;
|
|
90
|
-
}): Promise<PriceUpdate>;
|
|
92
|
+
}, fetchOptions?: RequestInit): Promise<PriceUpdate>;
|
|
91
93
|
/**
|
|
92
94
|
* Fetch the price updates for a set of price feed IDs at a given timestamp.
|
|
93
95
|
* This endpoint can be customized by specifying the encoding type and whether the results should also return the parsed price update.
|
|
@@ -106,7 +108,7 @@ export declare class HermesClient {
|
|
|
106
108
|
encoding?: EncodingType;
|
|
107
109
|
parsed?: boolean;
|
|
108
110
|
ignoreInvalidPriceIds?: boolean;
|
|
109
|
-
}): Promise<PriceUpdate>;
|
|
111
|
+
}, fetchOptions?: RequestInit): Promise<PriceUpdate>;
|
|
110
112
|
/**
|
|
111
113
|
* Fetch streaming price updates for a set of price feed IDs.
|
|
112
114
|
* This endpoint can be customized by specifying the encoding type, whether the results should include parsed updates,
|
|
@@ -150,7 +152,7 @@ export declare class HermesClient {
|
|
|
150
152
|
encoding?: EncodingType;
|
|
151
153
|
parsed?: boolean;
|
|
152
154
|
ignoreInvalidPriceIds?: boolean;
|
|
153
|
-
}): Promise<TwapsResponse>;
|
|
155
|
+
}, fetchOptions?: RequestInit): Promise<TwapsResponse>;
|
|
154
156
|
private appendUrlSearchParams;
|
|
155
157
|
private buildURL;
|
|
156
158
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HermesClient.d.ts","sourceRoot":"","sources":["../src/HermesClient.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"HermesClient.d.ts","sourceRoot":"","sources":["../src/HermesClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,MAAM,MAAM,SAAS,GAAG,CAAC,CAA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,EAAE,OAAO,CAAC;QACjB,cAAc,CAAC,EAAE,OAAO,CAAC;QACzB,cAAc,CAAC,EAAE,OAAO,CAAC;QACzB,qBAAqB,CAAC,EAAE,OAAO,CAAC;KACjC,GACA,OAAO,CAAC,WAAW,CAAC;IAuBvB;;;;;;;;;;;;;;OAcG;IACG,cAAc,CAClB,GAAG,EAAE,SAAS,EAAE,EAChB,cAAc,EAAE,MAAM,EACtB,OAAO,CAAC,EAAE;QACR,QAAQ,CAAC,EAAE,YAAY,CAAC;QACxB,MAAM,CAAC,EAAE,OAAO,CAAC;QACjB,qBAAqB,CAAC,EAAE,OAAO,CAAC;KACjC,EACD,YAAY,CAAC,EAAE,WAAW,GACzB,OAAO,CAAC,aAAa,CAAC;IAkBzB,OAAO,CAAC,qBAAqB;IAW7B,OAAO,CAAC,QAAQ;CAQjB"}
|
package/lib/HermesClient.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.HermesClient = void 0;
|
|
7
|
-
const eventsource_1 =
|
|
4
|
+
const eventsource_1 = require("eventsource");
|
|
8
5
|
const zodSchemas_1 = require("./zodSchemas");
|
|
9
6
|
const utils_1 = require("./utils");
|
|
10
7
|
const DEFAULT_TIMEOUT = 5000;
|
|
@@ -26,20 +23,17 @@ class HermesClient {
|
|
|
26
23
|
this.httpRetries = config?.httpRetries ?? DEFAULT_HTTP_RETRIES;
|
|
27
24
|
this.headers = config?.headers ?? {};
|
|
28
25
|
}
|
|
29
|
-
async httpRequest(url, schema, options, retries = this.httpRetries, backoff = 100 + Math.floor(Math.random() * 100)
|
|
30
|
-
|
|
31
|
-
const controller = externalAbortController ?? new AbortController();
|
|
32
|
-
const { signal } = controller;
|
|
33
|
-
options = {
|
|
34
|
-
...options,
|
|
35
|
-
signal,
|
|
36
|
-
headers: { ...this.headers, ...options?.headers },
|
|
37
|
-
}; // Merge any existing options with the signal and headers
|
|
38
|
-
// Set a timeout to abort the request if it takes too long
|
|
39
|
-
const timeout = setTimeout(() => controller.abort(), this.timeout);
|
|
26
|
+
async httpRequest(url, schema, options, retries = this.httpRetries, backoff = 100 + Math.floor(Math.random() * 100) // Adding randomness to the initial backoff to avoid "thundering herd" scenario where a lot of clients that get kicked off all at the same time (say some script or something) and fail to connect all retry at exactly the same time too
|
|
27
|
+
) {
|
|
40
28
|
try {
|
|
41
|
-
const response = await fetch(url,
|
|
42
|
-
|
|
29
|
+
const response = await fetch(url, {
|
|
30
|
+
...options,
|
|
31
|
+
signal: AbortSignal.any([
|
|
32
|
+
...(options?.signal ? [options.signal] : []),
|
|
33
|
+
AbortSignal.timeout(this.timeout),
|
|
34
|
+
]),
|
|
35
|
+
headers: { ...this.headers, ...options?.headers },
|
|
36
|
+
});
|
|
43
37
|
if (!response.ok) {
|
|
44
38
|
const errorBody = await response.text();
|
|
45
39
|
throw new Error(`HTTP error! status: ${response.status}${errorBody ? `, body: ${errorBody}` : ""}`);
|
|
@@ -48,7 +42,6 @@ class HermesClient {
|
|
|
48
42
|
return schema.parse(data);
|
|
49
43
|
}
|
|
50
44
|
catch (error) {
|
|
51
|
-
clearTimeout(timeout);
|
|
52
45
|
if (retries > 0 &&
|
|
53
46
|
!(error instanceof Error && error.name === "AbortError")) {
|
|
54
47
|
// Wait for a backoff period before retrying
|
|
@@ -69,12 +62,12 @@ class HermesClient {
|
|
|
69
62
|
*
|
|
70
63
|
* @returns Array of PriceFeedMetadata objects.
|
|
71
64
|
*/
|
|
72
|
-
async getPriceFeeds(options) {
|
|
65
|
+
async getPriceFeeds({ fetchOptions, ...options } = {}) {
|
|
73
66
|
const url = this.buildURL("price_feeds");
|
|
74
67
|
if (options) {
|
|
75
68
|
this.appendUrlSearchParams(url, options);
|
|
76
69
|
}
|
|
77
|
-
return await this.httpRequest(url.toString(), zodSchemas_1.schemas.PriceFeedMetadata.array());
|
|
70
|
+
return await this.httpRequest(url.toString(), zodSchemas_1.schemas.PriceFeedMetadata.array(), fetchOptions);
|
|
78
71
|
}
|
|
79
72
|
/**
|
|
80
73
|
* Fetch the latest publisher stake caps.
|
|
@@ -87,12 +80,12 @@ class HermesClient {
|
|
|
87
80
|
*
|
|
88
81
|
* @returns PublisherCaps object containing the latest publisher stake caps.
|
|
89
82
|
*/
|
|
90
|
-
async getLatestPublisherCaps(options) {
|
|
83
|
+
async getLatestPublisherCaps({ fetchOptions, ...options } = {}) {
|
|
91
84
|
const url = this.buildURL("updates/publisher_stake_caps/latest");
|
|
92
85
|
if (options) {
|
|
93
86
|
this.appendUrlSearchParams(url, options);
|
|
94
87
|
}
|
|
95
|
-
return await this.httpRequest(url.toString(), zodSchemas_1.schemas.LatestPublisherStakeCapsUpdateDataResponse);
|
|
88
|
+
return await this.httpRequest(url.toString(), zodSchemas_1.schemas.LatestPublisherStakeCapsUpdateDataResponse, fetchOptions);
|
|
96
89
|
}
|
|
97
90
|
/**
|
|
98
91
|
* Fetch the latest price updates for a set of price feed IDs.
|
|
@@ -107,7 +100,7 @@ class HermesClient {
|
|
|
107
100
|
*
|
|
108
101
|
* @returns PriceUpdate object containing the latest updates.
|
|
109
102
|
*/
|
|
110
|
-
async getLatestPriceUpdates(ids, options) {
|
|
103
|
+
async getLatestPriceUpdates(ids, options, fetchOptions) {
|
|
111
104
|
const url = this.buildURL("updates/price/latest");
|
|
112
105
|
for (const id of ids) {
|
|
113
106
|
url.searchParams.append("ids[]", id);
|
|
@@ -116,7 +109,7 @@ class HermesClient {
|
|
|
116
109
|
const transformedOptions = (0, utils_1.camelToSnakeCaseObject)(options);
|
|
117
110
|
this.appendUrlSearchParams(url, transformedOptions);
|
|
118
111
|
}
|
|
119
|
-
return this.httpRequest(url.toString(), zodSchemas_1.schemas.PriceUpdate);
|
|
112
|
+
return this.httpRequest(url.toString(), zodSchemas_1.schemas.PriceUpdate, fetchOptions);
|
|
120
113
|
}
|
|
121
114
|
/**
|
|
122
115
|
* Fetch the price updates for a set of price feed IDs at a given timestamp.
|
|
@@ -132,7 +125,7 @@ class HermesClient {
|
|
|
132
125
|
*
|
|
133
126
|
* @returns PriceUpdate object containing the updates at the specified timestamp.
|
|
134
127
|
*/
|
|
135
|
-
async getPriceUpdatesAtTimestamp(publishTime, ids, options) {
|
|
128
|
+
async getPriceUpdatesAtTimestamp(publishTime, ids, options, fetchOptions) {
|
|
136
129
|
const url = this.buildURL(`updates/price/${publishTime}`);
|
|
137
130
|
for (const id of ids) {
|
|
138
131
|
url.searchParams.append("ids[]", id);
|
|
@@ -141,7 +134,7 @@ class HermesClient {
|
|
|
141
134
|
const transformedOptions = (0, utils_1.camelToSnakeCaseObject)(options);
|
|
142
135
|
this.appendUrlSearchParams(url, transformedOptions);
|
|
143
136
|
}
|
|
144
|
-
return this.httpRequest(url.toString(), zodSchemas_1.schemas.PriceUpdate);
|
|
137
|
+
return this.httpRequest(url.toString(), zodSchemas_1.schemas.PriceUpdate, fetchOptions);
|
|
145
138
|
}
|
|
146
139
|
/**
|
|
147
140
|
* Fetch streaming price updates for a set of price feed IDs.
|
|
@@ -169,7 +162,15 @@ class HermesClient {
|
|
|
169
162
|
const transformedOptions = (0, utils_1.camelToSnakeCaseObject)(options);
|
|
170
163
|
this.appendUrlSearchParams(url, transformedOptions);
|
|
171
164
|
}
|
|
172
|
-
return new eventsource_1.
|
|
165
|
+
return new eventsource_1.EventSource(url.toString(), {
|
|
166
|
+
fetch: (input, init) => fetch(input, {
|
|
167
|
+
...init,
|
|
168
|
+
headers: {
|
|
169
|
+
...init?.headers,
|
|
170
|
+
...this.headers,
|
|
171
|
+
},
|
|
172
|
+
}),
|
|
173
|
+
});
|
|
173
174
|
}
|
|
174
175
|
/**
|
|
175
176
|
* Fetch the latest TWAP (time weighted average price) for a set of price feed IDs.
|
|
@@ -186,7 +187,7 @@ class HermesClient {
|
|
|
186
187
|
*
|
|
187
188
|
* @returns TwapsResponse object containing the latest TWAPs.
|
|
188
189
|
*/
|
|
189
|
-
async getLatestTwaps(ids, window_seconds, options) {
|
|
190
|
+
async getLatestTwaps(ids, window_seconds, options, fetchOptions) {
|
|
190
191
|
const url = this.buildURL(`updates/twap/${window_seconds}/latest`);
|
|
191
192
|
for (const id of ids) {
|
|
192
193
|
url.searchParams.append("ids[]", id);
|
|
@@ -195,7 +196,7 @@ class HermesClient {
|
|
|
195
196
|
const transformedOptions = (0, utils_1.camelToSnakeCaseObject)(options);
|
|
196
197
|
this.appendUrlSearchParams(url, transformedOptions);
|
|
197
198
|
}
|
|
198
|
-
return this.httpRequest(url.toString(), zodSchemas_1.schemas.TwapsResponse);
|
|
199
|
+
return this.httpRequest(url.toString(), zodSchemas_1.schemas.TwapsResponse, fetchOptions);
|
|
199
200
|
}
|
|
200
201
|
appendUrlSearchParams(url, params) {
|
|
201
202
|
Object.entries(params).forEach(([key, value]) => {
|
package/lib/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.camelToSnakeCaseObject =
|
|
3
|
+
exports.camelToSnakeCaseObject = camelToSnakeCaseObject;
|
|
4
4
|
function camelToSnakeCase(str) {
|
|
5
5
|
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
|
|
6
6
|
}
|
|
@@ -11,4 +11,3 @@ function camelToSnakeCaseObject(obj) {
|
|
|
11
11
|
});
|
|
12
12
|
return result;
|
|
13
13
|
}
|
|
14
|
-
exports.camelToSnakeCaseObject = camelToSnakeCaseObject;
|
package/lib/zodSchemas.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.api = exports.schemas = void 0;
|
|
4
|
+
exports.createApiClient = createApiClient;
|
|
4
5
|
const core_1 = require("@zodios/core");
|
|
5
6
|
const zod_1 = require("zod");
|
|
6
7
|
const AssetType = zod_1.z.enum([
|
|
@@ -319,4 +320,3 @@ exports.api = new core_1.Zodios(endpoints);
|
|
|
319
320
|
function createApiClient(baseUrl, options) {
|
|
320
321
|
return new core_1.Zodios(baseUrl, endpoints, options);
|
|
321
322
|
}
|
|
322
|
-
exports.createApiClient = createApiClient;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pythnetwork/hermes-client",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Pyth Hermes Client",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Pyth Data Association"
|
|
@@ -36,7 +36,6 @@
|
|
|
36
36
|
],
|
|
37
37
|
"license": "Apache-2.0",
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@types/eventsource": "^1.1.15",
|
|
40
39
|
"@types/jest": "^29.4.0",
|
|
41
40
|
"@types/node": "^20.14.2",
|
|
42
41
|
"@types/yargs": "^17.0.10",
|
|
@@ -47,13 +46,13 @@
|
|
|
47
46
|
"openapi-zod-client": "^1.18.1",
|
|
48
47
|
"prettier": "^2.6.2",
|
|
49
48
|
"ts-jest": "^29.0.5",
|
|
50
|
-
"typescript": "
|
|
49
|
+
"typescript": "catalog:",
|
|
51
50
|
"yargs": "^17.4.1"
|
|
52
51
|
},
|
|
53
52
|
"dependencies": {
|
|
54
53
|
"@zodios/core": "^10.9.6",
|
|
55
|
-
"eventsource": "^
|
|
54
|
+
"eventsource": "^3.0.5",
|
|
56
55
|
"zod": "^3.23.8"
|
|
57
56
|
},
|
|
58
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "f5c4b3df87829b926d59a5058be74917eec05b90"
|
|
59
58
|
}
|