@pythnetwork/hermes-client 2.0.0 → 3.1.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/README.md +6 -5
- package/dist/cjs/hermes-client.cjs +209 -0
- package/{lib/HermesClient.d.ts → dist/cjs/hermes-client.d.ts} +20 -34
- package/dist/cjs/index.cjs +18 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/utils.cjs +22 -0
- package/{lib → dist/cjs}/utils.d.ts +0 -1
- package/dist/cjs/zodSchemas.cjs +293 -0
- package/{lib → dist/cjs}/zodSchemas.d.ts +20 -827
- package/dist/esm/hermes-client.d.ts +145 -0
- package/dist/esm/hermes-client.mjs +199 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.mjs +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/utils.d.ts +1 -0
- package/dist/esm/utils.mjs +12 -0
- package/dist/esm/zodSchemas.d.ts +4146 -0
- package/dist/esm/zodSchemas.mjs +272 -0
- package/package.json +70 -23
- package/lib/HermesClient.d.ts.map +0 -1
- package/lib/HermesClient.js +0 -216
- package/lib/examples/HermesClient.d.ts +0 -2
- package/lib/examples/HermesClient.d.ts.map +0 -1
- package/lib/examples/HermesClient.js +0 -86
- package/lib/utils.d.ts.map +0 -1
- package/lib/utils.js +0 -13
- package/lib/zodSchemas.d.ts.map +0 -1
- package/lib/zodSchemas.js +0 -322
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { makeApi, Zodios } from "@zodios/core";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
const AssetType = z.enum([
|
|
4
|
+
"crypto",
|
|
5
|
+
"fx",
|
|
6
|
+
"equity",
|
|
7
|
+
"metal",
|
|
8
|
+
"rates",
|
|
9
|
+
"crypto_redemption_rate",
|
|
10
|
+
"commodities",
|
|
11
|
+
"crypto_index",
|
|
12
|
+
"crypto_nav",
|
|
13
|
+
"eco",
|
|
14
|
+
"kalshi"
|
|
15
|
+
]);
|
|
16
|
+
const asset_type = AssetType.nullish();
|
|
17
|
+
const RpcPriceIdentifier = z.string();
|
|
18
|
+
const PriceFeedMetadata = z.object({
|
|
19
|
+
attributes: z.record(z.string()),
|
|
20
|
+
id: RpcPriceIdentifier
|
|
21
|
+
}).passthrough();
|
|
22
|
+
const PriceIdInput = z.string();
|
|
23
|
+
const EncodingType = z.enum([
|
|
24
|
+
"hex",
|
|
25
|
+
"base64"
|
|
26
|
+
]);
|
|
27
|
+
const BinaryUpdate = z.object({
|
|
28
|
+
data: z.array(z.string()),
|
|
29
|
+
encoding: EncodingType
|
|
30
|
+
}).passthrough();
|
|
31
|
+
const RpcPrice = z.object({
|
|
32
|
+
conf: z.string(),
|
|
33
|
+
expo: z.number().int(),
|
|
34
|
+
price: z.string(),
|
|
35
|
+
publish_time: z.number().int()
|
|
36
|
+
}).passthrough();
|
|
37
|
+
const RpcPriceFeedMetadataV2 = z.object({
|
|
38
|
+
prev_publish_time: z.number().int().nullable(),
|
|
39
|
+
proof_available_time: z.number().int().nullable(),
|
|
40
|
+
slot: z.number().int().gte(0).nullable()
|
|
41
|
+
}).partial().passthrough();
|
|
42
|
+
const ParsedPriceUpdate = z.object({
|
|
43
|
+
ema_price: RpcPrice,
|
|
44
|
+
id: RpcPriceIdentifier,
|
|
45
|
+
metadata: RpcPriceFeedMetadataV2,
|
|
46
|
+
price: RpcPrice
|
|
47
|
+
}).passthrough();
|
|
48
|
+
const PriceUpdate = z.object({
|
|
49
|
+
binary: BinaryUpdate,
|
|
50
|
+
parsed: z.array(ParsedPriceUpdate).nullish()
|
|
51
|
+
}).passthrough();
|
|
52
|
+
const ParsedPublisherStakeCap = z.object({
|
|
53
|
+
cap: z.number().int().gte(0),
|
|
54
|
+
publisher: z.string()
|
|
55
|
+
}).passthrough();
|
|
56
|
+
const ParsedPublisherStakeCapsUpdate = z.object({
|
|
57
|
+
publisher_stake_caps: z.array(ParsedPublisherStakeCap)
|
|
58
|
+
}).passthrough();
|
|
59
|
+
const LatestPublisherStakeCapsUpdateDataResponse = z.object({
|
|
60
|
+
binary: BinaryUpdate,
|
|
61
|
+
parsed: z.array(ParsedPublisherStakeCapsUpdate).nullish()
|
|
62
|
+
}).passthrough();
|
|
63
|
+
export const schemas = {
|
|
64
|
+
AssetType,
|
|
65
|
+
asset_type,
|
|
66
|
+
RpcPriceIdentifier,
|
|
67
|
+
PriceFeedMetadata,
|
|
68
|
+
PriceIdInput,
|
|
69
|
+
EncodingType,
|
|
70
|
+
BinaryUpdate,
|
|
71
|
+
RpcPrice,
|
|
72
|
+
RpcPriceFeedMetadataV2,
|
|
73
|
+
ParsedPriceUpdate,
|
|
74
|
+
PriceUpdate,
|
|
75
|
+
ParsedPublisherStakeCap,
|
|
76
|
+
ParsedPublisherStakeCapsUpdate,
|
|
77
|
+
LatestPublisherStakeCapsUpdateDataResponse
|
|
78
|
+
};
|
|
79
|
+
const endpoints = makeApi([
|
|
80
|
+
{
|
|
81
|
+
method: "get",
|
|
82
|
+
path: "/v2/price_feeds",
|
|
83
|
+
alias: "price_feeds_metadata",
|
|
84
|
+
description: `Get the set of price feeds.
|
|
85
|
+
|
|
86
|
+
This endpoint fetches all price feeds from the Pyth network. It can be filtered by asset type
|
|
87
|
+
and query string.`,
|
|
88
|
+
requestFormat: "json",
|
|
89
|
+
parameters: [
|
|
90
|
+
{
|
|
91
|
+
name: "query",
|
|
92
|
+
type: "Query",
|
|
93
|
+
schema: z.string().nullish()
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
name: "asset_type",
|
|
97
|
+
type: "Query",
|
|
98
|
+
schema: asset_type
|
|
99
|
+
}
|
|
100
|
+
],
|
|
101
|
+
response: z.array(PriceFeedMetadata)
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
method: "get",
|
|
105
|
+
path: "/v2/updates/price/:publish_time",
|
|
106
|
+
alias: "timestamp_price_updates",
|
|
107
|
+
description: `Get the latest price updates by price feed id.
|
|
108
|
+
|
|
109
|
+
Given a collection of price feed ids, retrieve the latest Pyth price for each price feed.`,
|
|
110
|
+
requestFormat: "json",
|
|
111
|
+
parameters: [
|
|
112
|
+
{
|
|
113
|
+
name: "publish_time",
|
|
114
|
+
type: "Path",
|
|
115
|
+
schema: z.number().int()
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
name: "ids[]",
|
|
119
|
+
type: "Query",
|
|
120
|
+
schema: z.array(PriceIdInput)
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
name: "encoding",
|
|
124
|
+
type: "Query",
|
|
125
|
+
schema: z.enum([
|
|
126
|
+
"hex",
|
|
127
|
+
"base64"
|
|
128
|
+
]).optional()
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
name: "parsed",
|
|
132
|
+
type: "Query",
|
|
133
|
+
schema: z.boolean().optional()
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
name: "ignore_invalid_price_ids",
|
|
137
|
+
type: "Query",
|
|
138
|
+
schema: z.boolean().optional()
|
|
139
|
+
}
|
|
140
|
+
],
|
|
141
|
+
response: PriceUpdate,
|
|
142
|
+
errors: [
|
|
143
|
+
{
|
|
144
|
+
status: 404,
|
|
145
|
+
description: `Price ids not found`,
|
|
146
|
+
schema: z.void()
|
|
147
|
+
}
|
|
148
|
+
]
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
method: "get",
|
|
152
|
+
path: "/v2/updates/price/latest",
|
|
153
|
+
alias: "latest_price_updates",
|
|
154
|
+
description: `Get the latest price updates by price feed id.
|
|
155
|
+
|
|
156
|
+
Given a collection of price feed ids, retrieve the latest Pyth price for each price feed.`,
|
|
157
|
+
requestFormat: "json",
|
|
158
|
+
parameters: [
|
|
159
|
+
{
|
|
160
|
+
name: "ids[]",
|
|
161
|
+
type: "Query",
|
|
162
|
+
schema: z.array(PriceIdInput)
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
name: "encoding",
|
|
166
|
+
type: "Query",
|
|
167
|
+
schema: z.enum([
|
|
168
|
+
"hex",
|
|
169
|
+
"base64"
|
|
170
|
+
]).optional()
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
name: "parsed",
|
|
174
|
+
type: "Query",
|
|
175
|
+
schema: z.boolean().optional()
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
name: "ignore_invalid_price_ids",
|
|
179
|
+
type: "Query",
|
|
180
|
+
schema: z.boolean().optional()
|
|
181
|
+
}
|
|
182
|
+
],
|
|
183
|
+
response: PriceUpdate,
|
|
184
|
+
errors: [
|
|
185
|
+
{
|
|
186
|
+
status: 404,
|
|
187
|
+
description: `Price ids not found`,
|
|
188
|
+
schema: z.void()
|
|
189
|
+
}
|
|
190
|
+
]
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
method: "get",
|
|
194
|
+
path: "/v2/updates/price/stream",
|
|
195
|
+
alias: "price_stream_sse_handler",
|
|
196
|
+
description: `SSE route handler for streaming price updates.
|
|
197
|
+
|
|
198
|
+
The connection will automatically close after 24 hours to prevent resource leaks.
|
|
199
|
+
Clients should implement reconnection logic to maintain continuous price updates.`,
|
|
200
|
+
requestFormat: "json",
|
|
201
|
+
parameters: [
|
|
202
|
+
{
|
|
203
|
+
name: "ids[]",
|
|
204
|
+
type: "Query",
|
|
205
|
+
schema: z.array(PriceIdInput)
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
name: "encoding",
|
|
209
|
+
type: "Query",
|
|
210
|
+
schema: z.enum([
|
|
211
|
+
"hex",
|
|
212
|
+
"base64"
|
|
213
|
+
]).optional()
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
name: "parsed",
|
|
217
|
+
type: "Query",
|
|
218
|
+
schema: z.boolean().optional()
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
name: "allow_unordered",
|
|
222
|
+
type: "Query",
|
|
223
|
+
schema: z.boolean().optional()
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
name: "benchmarks_only",
|
|
227
|
+
type: "Query",
|
|
228
|
+
schema: z.boolean().optional()
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
name: "ignore_invalid_price_ids",
|
|
232
|
+
type: "Query",
|
|
233
|
+
schema: z.boolean().optional()
|
|
234
|
+
}
|
|
235
|
+
],
|
|
236
|
+
response: PriceUpdate,
|
|
237
|
+
errors: [
|
|
238
|
+
{
|
|
239
|
+
status: 404,
|
|
240
|
+
description: `Price ids not found`,
|
|
241
|
+
schema: z.void()
|
|
242
|
+
}
|
|
243
|
+
]
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
method: "get",
|
|
247
|
+
path: "/v2/updates/publisher_stake_caps/latest",
|
|
248
|
+
alias: "latest_publisher_stake_caps",
|
|
249
|
+
description: `Get the most recent publisher stake caps update data.`,
|
|
250
|
+
requestFormat: "json",
|
|
251
|
+
parameters: [
|
|
252
|
+
{
|
|
253
|
+
name: "encoding",
|
|
254
|
+
type: "Query",
|
|
255
|
+
schema: z.enum([
|
|
256
|
+
"hex",
|
|
257
|
+
"base64"
|
|
258
|
+
]).optional()
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
name: "parsed",
|
|
262
|
+
type: "Query",
|
|
263
|
+
schema: z.boolean().optional()
|
|
264
|
+
}
|
|
265
|
+
],
|
|
266
|
+
response: LatestPublisherStakeCapsUpdateDataResponse
|
|
267
|
+
}
|
|
268
|
+
]);
|
|
269
|
+
export const api = new Zodios(endpoints);
|
|
270
|
+
export function createApiClient(baseUrl, options) {
|
|
271
|
+
return new Zodios(baseUrl, endpoints, options);
|
|
272
|
+
}
|
package/package.json
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pythnetwork/hermes-client",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Pyth Hermes Client",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Pyth Data Association"
|
|
7
7
|
},
|
|
8
8
|
"homepage": "https://pyth.network",
|
|
9
|
-
"main": "lib/HermesClient.js",
|
|
10
|
-
"types": "lib/HermesClient.d.ts",
|
|
11
9
|
"files": [
|
|
12
|
-
"
|
|
10
|
+
"dist/**/*"
|
|
13
11
|
],
|
|
14
12
|
"repository": {
|
|
15
13
|
"type": "git",
|
|
@@ -19,34 +17,21 @@
|
|
|
19
17
|
"publishConfig": {
|
|
20
18
|
"access": "public"
|
|
21
19
|
},
|
|
22
|
-
"scripts": {
|
|
23
|
-
"build:typescript": "tsc",
|
|
24
|
-
"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",
|
|
26
|
-
"example": "node lib/examples/HermesClient.js",
|
|
27
|
-
"format": "prettier --write \"src/**/*.ts\"",
|
|
28
|
-
"test:lint": "eslint src/",
|
|
29
|
-
"prepublishOnly": "pnpm run build:typescript && pnpm run test:lint",
|
|
30
|
-
"preversion": "pnpm run test:lint",
|
|
31
|
-
"version": "pnpm run format && git add -A src"
|
|
32
|
-
},
|
|
33
20
|
"keywords": [
|
|
34
21
|
"pyth",
|
|
35
22
|
"oracle"
|
|
36
23
|
],
|
|
37
24
|
"license": "Apache-2.0",
|
|
38
25
|
"devDependencies": {
|
|
26
|
+
"@cprussin/eslint-config": "^4.0.2",
|
|
27
|
+
"@pythnetwork/jest-config": "",
|
|
39
28
|
"@types/jest": "^29.4.0",
|
|
40
29
|
"@types/node": "^20.14.2",
|
|
41
30
|
"@types/yargs": "^17.0.10",
|
|
42
|
-
"
|
|
43
|
-
"@typescript-eslint/parser": "^5.21.0",
|
|
44
|
-
"eslint": "^8.14.0",
|
|
31
|
+
"eslint": "^9.23.0",
|
|
45
32
|
"jest": "^29.4.0",
|
|
46
33
|
"openapi-zod-client": "^1.18.1",
|
|
47
|
-
"prettier": "^
|
|
48
|
-
"ts-jest": "^29.0.5",
|
|
49
|
-
"typescript": "catalog:",
|
|
34
|
+
"prettier": "^3.5.3",
|
|
50
35
|
"yargs": "^17.4.1"
|
|
51
36
|
},
|
|
52
37
|
"dependencies": {
|
|
@@ -54,5 +39,67 @@
|
|
|
54
39
|
"eventsource": "^3.0.5",
|
|
55
40
|
"zod": "^3.23.8"
|
|
56
41
|
},
|
|
57
|
-
"
|
|
58
|
-
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": "^24.0.0"
|
|
44
|
+
},
|
|
45
|
+
"type": "module",
|
|
46
|
+
"exports": {
|
|
47
|
+
"./hermes-client": {
|
|
48
|
+
"require": {
|
|
49
|
+
"types": "./dist/cjs/hermes-client.d.ts",
|
|
50
|
+
"default": "./dist/cjs/hermes-client.cjs"
|
|
51
|
+
},
|
|
52
|
+
"import": {
|
|
53
|
+
"types": "./dist/esm/hermes-client.d.ts",
|
|
54
|
+
"default": "./dist/esm/hermes-client.mjs"
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
".": {
|
|
58
|
+
"require": {
|
|
59
|
+
"types": "./dist/cjs/index.d.ts",
|
|
60
|
+
"default": "./dist/cjs/index.cjs"
|
|
61
|
+
},
|
|
62
|
+
"import": {
|
|
63
|
+
"types": "./dist/esm/index.d.ts",
|
|
64
|
+
"default": "./dist/esm/index.mjs"
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
"./utils": {
|
|
68
|
+
"require": {
|
|
69
|
+
"types": "./dist/cjs/utils.d.ts",
|
|
70
|
+
"default": "./dist/cjs/utils.cjs"
|
|
71
|
+
},
|
|
72
|
+
"import": {
|
|
73
|
+
"types": "./dist/esm/utils.d.ts",
|
|
74
|
+
"default": "./dist/esm/utils.mjs"
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
"./zodSchemas": {
|
|
78
|
+
"require": {
|
|
79
|
+
"types": "./dist/cjs/zodSchemas.d.ts",
|
|
80
|
+
"default": "./dist/cjs/zodSchemas.cjs"
|
|
81
|
+
},
|
|
82
|
+
"import": {
|
|
83
|
+
"types": "./dist/esm/zodSchemas.d.ts",
|
|
84
|
+
"default": "./dist/esm/zodSchemas.mjs"
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
"./package.json": "./package.json"
|
|
88
|
+
},
|
|
89
|
+
"module": "./dist/esm/index.mjs",
|
|
90
|
+
"types": "./dist/cjs/index.d.ts",
|
|
91
|
+
"main": "./dist/cjs/index.cjs",
|
|
92
|
+
"scripts": {
|
|
93
|
+
"build": "ts-duality",
|
|
94
|
+
"build:schemas": "openapi-zod-client ./schema.json --output src/zodSchemas.ts",
|
|
95
|
+
"pull:schema": "curl -o schema.json -z schema.json https://hermes.pyth.network/docs/openapi.json",
|
|
96
|
+
"example": "node lib/examples/HermesClient.js",
|
|
97
|
+
"fix:lint": "eslint src/ --fix --max-warnings 0",
|
|
98
|
+
"fix:format": "prettier --write \"src/**/*.ts\"",
|
|
99
|
+
"test:lint": "eslint src/ --max-warnings 0",
|
|
100
|
+
"test:format": "prettier --check \"src/**/*.ts\"",
|
|
101
|
+
"preversion": "pnpm run test:lint",
|
|
102
|
+
"version": "pnpm run format && git add -A src",
|
|
103
|
+
"clean": "rm -rf ./dist"
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
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,CAAC,KAAK,CAAC,OAAO,OAAO,CAAC,SAAS,CAAC,CAAC;AAC1D,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,OAAO,CAAC,YAAY,CAAC,CAAC;AACrE,MAAM,MAAM,YAAY,GAAG,CA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;;;;;;;;;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
DELETED
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HermesClient = void 0;
|
|
4
|
-
const eventsource_1 = require("eventsource");
|
|
5
|
-
const zodSchemas_1 = require("./zodSchemas");
|
|
6
|
-
const utils_1 = require("./utils");
|
|
7
|
-
const DEFAULT_TIMEOUT = 5000;
|
|
8
|
-
const DEFAULT_HTTP_RETRIES = 3;
|
|
9
|
-
class HermesClient {
|
|
10
|
-
baseURL;
|
|
11
|
-
timeout;
|
|
12
|
-
httpRetries;
|
|
13
|
-
headers;
|
|
14
|
-
/**
|
|
15
|
-
* Constructs a new Connection.
|
|
16
|
-
*
|
|
17
|
-
* @param endpoint endpoint URL to the price service. Example: https://website/example/
|
|
18
|
-
* @param config Optional HermesClientConfig for custom configurations.
|
|
19
|
-
*/
|
|
20
|
-
constructor(endpoint, config) {
|
|
21
|
-
this.baseURL = endpoint;
|
|
22
|
-
this.timeout = config?.timeout ?? DEFAULT_TIMEOUT;
|
|
23
|
-
this.httpRetries = config?.httpRetries ?? DEFAULT_HTTP_RETRIES;
|
|
24
|
-
this.headers = config?.headers ?? {};
|
|
25
|
-
}
|
|
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
|
-
) {
|
|
28
|
-
try {
|
|
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
|
-
});
|
|
37
|
-
if (!response.ok) {
|
|
38
|
-
const errorBody = await response.text();
|
|
39
|
-
throw new Error(`HTTP error! status: ${response.status}${errorBody ? `, body: ${errorBody}` : ""}`);
|
|
40
|
-
}
|
|
41
|
-
const data = await response.json();
|
|
42
|
-
return schema.parse(data);
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
if (retries > 0 &&
|
|
46
|
-
!(error instanceof Error && error.name === "AbortError")) {
|
|
47
|
-
// Wait for a backoff period before retrying
|
|
48
|
-
await new Promise((resolve) => setTimeout(resolve, backoff));
|
|
49
|
-
return this.httpRequest(url, schema, options, retries - 1, backoff * 2); // Exponential backoff
|
|
50
|
-
}
|
|
51
|
-
throw error;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Fetch the set of available price feeds.
|
|
56
|
-
* This endpoint can be filtered by asset type and query string.
|
|
57
|
-
* This will throw an error if there is a network problem or the price service returns a non-ok response.
|
|
58
|
-
*
|
|
59
|
-
* @param options Optional parameters:
|
|
60
|
-
* - query: String to filter the price feeds. If provided, the results will be filtered to all price feeds whose symbol contains the query string. Query string is case insensitive. Example: "bitcoin".
|
|
61
|
-
* - assetType: String to filter the price feeds by asset type. Possible values are "crypto", "equity", "fx", "metal", "rates", "crypto_redemption_rate". Filter string is case insensitive.
|
|
62
|
-
*
|
|
63
|
-
* @returns Array of PriceFeedMetadata objects.
|
|
64
|
-
*/
|
|
65
|
-
async getPriceFeeds({ fetchOptions, ...options } = {}) {
|
|
66
|
-
const url = this.buildURL("price_feeds");
|
|
67
|
-
if (options) {
|
|
68
|
-
const transformedOptions = (0, utils_1.camelToSnakeCaseObject)(options);
|
|
69
|
-
this.appendUrlSearchParams(url, transformedOptions);
|
|
70
|
-
}
|
|
71
|
-
return await this.httpRequest(url.toString(), zodSchemas_1.schemas.PriceFeedMetadata.array(), fetchOptions);
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Fetch the latest publisher stake caps.
|
|
75
|
-
* This endpoint can be customized by specifying the encoding type and whether the results should also return the parsed publisher caps.
|
|
76
|
-
* This will throw an error if there is a network problem or the price service returns a non-ok response.
|
|
77
|
-
*
|
|
78
|
-
* @param options Optional parameters:
|
|
79
|
-
* - encoding: Encoding type. If specified, return the publisher caps in the encoding specified by the encoding parameter. Default is hex.
|
|
80
|
-
* - parsed: Boolean to specify if the parsed publisher caps should be included in the response. Default is false.
|
|
81
|
-
*
|
|
82
|
-
* @returns PublisherCaps object containing the latest publisher stake caps.
|
|
83
|
-
*/
|
|
84
|
-
async getLatestPublisherCaps({ fetchOptions, ...options } = {}) {
|
|
85
|
-
const url = this.buildURL("updates/publisher_stake_caps/latest");
|
|
86
|
-
if (options) {
|
|
87
|
-
this.appendUrlSearchParams(url, options);
|
|
88
|
-
}
|
|
89
|
-
return await this.httpRequest(url.toString(), zodSchemas_1.schemas.LatestPublisherStakeCapsUpdateDataResponse, fetchOptions);
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Fetch the latest price updates for a set of price feed IDs.
|
|
93
|
-
* 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.
|
|
94
|
-
* This will throw an error if there is a network problem or the price service returns a non-ok response.
|
|
95
|
-
*
|
|
96
|
-
* @param ids Array of hex-encoded price feed IDs for which updates are requested.
|
|
97
|
-
* @param options Optional parameters:
|
|
98
|
-
* - encoding: Encoding type. If specified, return the price update in the encoding specified by the encoding parameter. Default is hex.
|
|
99
|
-
* - parsed: Boolean to specify if the parsed price update should be included in the response. Default is false.
|
|
100
|
-
* - ignoreInvalidPriceIds: Boolean to specify if invalid price IDs should be ignored instead of returning an error. Default is false.
|
|
101
|
-
*
|
|
102
|
-
* @returns PriceUpdate object containing the latest updates.
|
|
103
|
-
*/
|
|
104
|
-
async getLatestPriceUpdates(ids, options, fetchOptions) {
|
|
105
|
-
const url = this.buildURL("updates/price/latest");
|
|
106
|
-
for (const id of ids) {
|
|
107
|
-
url.searchParams.append("ids[]", id);
|
|
108
|
-
}
|
|
109
|
-
if (options) {
|
|
110
|
-
const transformedOptions = (0, utils_1.camelToSnakeCaseObject)(options);
|
|
111
|
-
this.appendUrlSearchParams(url, transformedOptions);
|
|
112
|
-
}
|
|
113
|
-
return this.httpRequest(url.toString(), zodSchemas_1.schemas.PriceUpdate, fetchOptions);
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Fetch the price updates for a set of price feed IDs at a given timestamp.
|
|
117
|
-
* This endpoint can be customized by specifying the encoding type and whether the results should also return the parsed price update.
|
|
118
|
-
* This will throw an error if there is a network problem or the price service returns a non-ok response.
|
|
119
|
-
*
|
|
120
|
-
* @param publishTime Unix timestamp in seconds.
|
|
121
|
-
* @param ids Array of hex-encoded price feed IDs for which updates are requested.
|
|
122
|
-
* @param options Optional parameters:
|
|
123
|
-
* - encoding: Encoding type. If specified, return the price update in the encoding specified by the encoding parameter. Default is hex.
|
|
124
|
-
* - parsed: Boolean to specify if the parsed price update should be included in the response. Default is false.
|
|
125
|
-
* - ignoreInvalidPriceIds: Boolean to specify if invalid price IDs should be ignored instead of returning an error. Default is false.
|
|
126
|
-
*
|
|
127
|
-
* @returns PriceUpdate object containing the updates at the specified timestamp.
|
|
128
|
-
*/
|
|
129
|
-
async getPriceUpdatesAtTimestamp(publishTime, ids, options, fetchOptions) {
|
|
130
|
-
const url = this.buildURL(`updates/price/${publishTime}`);
|
|
131
|
-
for (const id of ids) {
|
|
132
|
-
url.searchParams.append("ids[]", id);
|
|
133
|
-
}
|
|
134
|
-
if (options) {
|
|
135
|
-
const transformedOptions = (0, utils_1.camelToSnakeCaseObject)(options);
|
|
136
|
-
this.appendUrlSearchParams(url, transformedOptions);
|
|
137
|
-
}
|
|
138
|
-
return this.httpRequest(url.toString(), zodSchemas_1.schemas.PriceUpdate, fetchOptions);
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Fetch streaming price updates for a set of price feed IDs.
|
|
142
|
-
* This endpoint can be customized by specifying the encoding type, whether the results should include parsed updates,
|
|
143
|
-
* and if unordered updates or only benchmark updates are allowed.
|
|
144
|
-
* This will return an EventSource that can be used to listen to streaming updates.
|
|
145
|
-
* If an invalid hex-encoded ID is passed, it will throw an error.
|
|
146
|
-
*
|
|
147
|
-
* @param ids Array of hex-encoded price feed IDs for which streaming updates are requested.
|
|
148
|
-
* @param options Optional parameters:
|
|
149
|
-
* - encoding: Encoding type. If specified, updates are returned in the specified encoding. Default is hex.
|
|
150
|
-
* - parsed: Boolean to specify if the parsed price update should be included in the response. Default is false.
|
|
151
|
-
* - allowUnordered: Boolean to specify if unordered updates are allowed to be included in the stream. Default is false.
|
|
152
|
-
* - benchmarksOnly: Boolean to specify if only benchmark prices should be returned. Default is false.
|
|
153
|
-
* - ignoreInvalidPriceIds: Boolean to specify if invalid price IDs should be ignored instead of returning an error. Default is false.
|
|
154
|
-
*
|
|
155
|
-
* @returns An EventSource instance for receiving streaming updates.
|
|
156
|
-
*/
|
|
157
|
-
async getPriceUpdatesStream(ids, options) {
|
|
158
|
-
const url = this.buildURL("updates/price/stream");
|
|
159
|
-
ids.forEach((id) => {
|
|
160
|
-
url.searchParams.append("ids[]", id);
|
|
161
|
-
});
|
|
162
|
-
if (options) {
|
|
163
|
-
const transformedOptions = (0, utils_1.camelToSnakeCaseObject)(options);
|
|
164
|
-
this.appendUrlSearchParams(url, transformedOptions);
|
|
165
|
-
}
|
|
166
|
-
return new eventsource_1.EventSource(url.toString(), {
|
|
167
|
-
fetch: (input, init) => fetch(input, {
|
|
168
|
-
...init,
|
|
169
|
-
headers: {
|
|
170
|
-
...init?.headers,
|
|
171
|
-
...this.headers,
|
|
172
|
-
},
|
|
173
|
-
}),
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Fetch the latest TWAP (time weighted average price) for a set of price feed IDs.
|
|
178
|
-
* This endpoint can be customized by specifying the encoding type and whether the results should also return the calculated TWAP using the options object.
|
|
179
|
-
* This will throw an error if there is a network problem or the price service returns a non-ok response.
|
|
180
|
-
*
|
|
181
|
-
* @param ids Array of hex-encoded price feed IDs for which updates are requested.
|
|
182
|
-
* @param window_seconds The time window in seconds over which to calculate the TWAP, ending at the current time.
|
|
183
|
-
* 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).
|
|
184
|
-
* @param options Optional parameters:
|
|
185
|
-
* - encoding: Encoding type. If specified, return the TWAP binary data in the encoding specified by the encoding parameter. Default is hex.
|
|
186
|
-
* - parsed: Boolean to specify if the calculated TWAP should be included in the response. Default is false.
|
|
187
|
-
* - ignoreInvalidPriceIds: Boolean to specify if invalid price IDs should be ignored instead of returning an error. Default is false.
|
|
188
|
-
*
|
|
189
|
-
* @returns TwapsResponse object containing the latest TWAPs.
|
|
190
|
-
*/
|
|
191
|
-
async getLatestTwaps(ids, window_seconds, options, fetchOptions) {
|
|
192
|
-
const url = this.buildURL(`updates/twap/${window_seconds}/latest`);
|
|
193
|
-
for (const id of ids) {
|
|
194
|
-
url.searchParams.append("ids[]", id);
|
|
195
|
-
}
|
|
196
|
-
if (options) {
|
|
197
|
-
const transformedOptions = (0, utils_1.camelToSnakeCaseObject)(options);
|
|
198
|
-
this.appendUrlSearchParams(url, transformedOptions);
|
|
199
|
-
}
|
|
200
|
-
return this.httpRequest(url.toString(), zodSchemas_1.schemas.TwapsResponse, fetchOptions);
|
|
201
|
-
}
|
|
202
|
-
appendUrlSearchParams(url, params) {
|
|
203
|
-
Object.entries(params).forEach(([key, value]) => {
|
|
204
|
-
if (value !== undefined) {
|
|
205
|
-
url.searchParams.append(key, String(value));
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
buildURL(endpoint) {
|
|
210
|
-
return new URL(`./v2/${endpoint}`,
|
|
211
|
-
// We ensure the `baseURL` ends with a `/` so that URL doesn't resolve the
|
|
212
|
-
// path relative to the parent.
|
|
213
|
-
`${this.baseURL}${this.baseURL.endsWith("/") ? "" : "/"}`);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
exports.HermesClient = HermesClient;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"HermesClient.d.ts","sourceRoot":"","sources":["../../src/examples/HermesClient.ts"],"names":[],"mappings":""}
|