@pythnetwork/hermes-client 2.1.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/dist/cjs/hermes-client.cjs +14 -25
- package/dist/cjs/hermes-client.d.ts +8 -21
- package/dist/cjs/utils.cjs +2 -2
- package/dist/cjs/zodSchemas.cjs +4 -62
- package/dist/cjs/zodSchemas.d.ts +18 -824
- package/dist/esm/hermes-client.d.ts +8 -21
- package/dist/esm/hermes-client.mjs +14 -25
- package/dist/esm/zodSchemas.d.ts +18 -824
- package/dist/esm/zodSchemas.mjs +4 -62
- package/package.json +2 -2
|
@@ -18,6 +18,7 @@ class HermesClient {
|
|
|
18
18
|
timeout;
|
|
19
19
|
httpRetries;
|
|
20
20
|
headers;
|
|
21
|
+
accessToken;
|
|
21
22
|
/**
|
|
22
23
|
* Constructs a new Connection.
|
|
23
24
|
*
|
|
@@ -28,9 +29,15 @@ class HermesClient {
|
|
|
28
29
|
this.timeout = config?.timeout ?? DEFAULT_TIMEOUT;
|
|
29
30
|
this.httpRetries = config?.httpRetries ?? DEFAULT_HTTP_RETRIES;
|
|
30
31
|
this.headers = config?.headers ?? {};
|
|
32
|
+
this.accessToken = config?.accessToken;
|
|
31
33
|
}
|
|
32
34
|
async httpRequest(url, schema, options, retries = this.httpRetries, backoff = 100 + Math.floor(Math.random() * 100)) {
|
|
33
35
|
try {
|
|
36
|
+
// Build auth headers if access token is provided
|
|
37
|
+
const authHeaders = {};
|
|
38
|
+
if (this.accessToken !== undefined) {
|
|
39
|
+
authHeaders.Authorization = `Bearer ${this.accessToken}`;
|
|
40
|
+
}
|
|
34
41
|
const response = await fetch(url, {
|
|
35
42
|
...options,
|
|
36
43
|
signal: AbortSignal.any([
|
|
@@ -40,6 +47,7 @@ class HermesClient {
|
|
|
40
47
|
AbortSignal.timeout(this.timeout)
|
|
41
48
|
]),
|
|
42
49
|
headers: {
|
|
50
|
+
...authHeaders,
|
|
43
51
|
...this.headers,
|
|
44
52
|
...options?.headers
|
|
45
53
|
}
|
|
@@ -168,41 +176,22 @@ class HermesClient {
|
|
|
168
176
|
const transformedOptions = (0, _utils.camelToSnakeCaseObject)(options);
|
|
169
177
|
this.appendUrlSearchParams(url, transformedOptions);
|
|
170
178
|
}
|
|
179
|
+
// Build auth headers for SSE fetch
|
|
180
|
+
const authHeaders = {};
|
|
181
|
+
if (this.accessToken !== undefined) {
|
|
182
|
+
authHeaders.Authorization = `Bearer ${this.accessToken}`;
|
|
183
|
+
}
|
|
171
184
|
return new _eventsource.EventSource(url.toString(), {
|
|
172
185
|
fetch: (input, init)=>fetch(input, {
|
|
173
186
|
...init,
|
|
174
187
|
headers: {
|
|
175
188
|
...init?.headers,
|
|
189
|
+
...authHeaders,
|
|
176
190
|
...this.headers
|
|
177
191
|
}
|
|
178
192
|
})
|
|
179
193
|
});
|
|
180
194
|
}
|
|
181
|
-
/**
|
|
182
|
-
* Fetch the latest TWAP (time weighted average price) for a set of price feed IDs.
|
|
183
|
-
* This endpoint can be customized by specifying the encoding type and whether the results should also return the calculated TWAP using the options object.
|
|
184
|
-
* This will throw an error if there is a network problem or the price service returns a non-ok response.
|
|
185
|
-
*
|
|
186
|
-
* @param ids - Array of hex-encoded price feed IDs for which updates are requested.
|
|
187
|
-
* @param window_seconds - The time window in seconds over which to calculate the TWAP, ending at the current time.
|
|
188
|
-
* 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).
|
|
189
|
-
* @param options - Optional parameters:
|
|
190
|
-
* - encoding: Encoding type. If specified, return the TWAP binary data in the encoding specified by the encoding parameter. Default is hex.
|
|
191
|
-
* - parsed: Boolean to specify if the calculated TWAP should be included in the response. Default is false.
|
|
192
|
-
* - ignoreInvalidPriceIds: Boolean to specify if invalid price IDs should be ignored instead of returning an error. Default is false.
|
|
193
|
-
*
|
|
194
|
-
* @returns TwapsResponse object containing the latest TWAPs.
|
|
195
|
-
*/ async getLatestTwaps(ids, window_seconds, options, fetchOptions) {
|
|
196
|
-
const url = this.buildURL(`updates/twap/${window_seconds.toString()}/latest`);
|
|
197
|
-
for (const id of ids){
|
|
198
|
-
url.searchParams.append("ids[]", id);
|
|
199
|
-
}
|
|
200
|
-
if (options) {
|
|
201
|
-
const transformedOptions = (0, _utils.camelToSnakeCaseObject)(options);
|
|
202
|
-
this.appendUrlSearchParams(url, transformedOptions);
|
|
203
|
-
}
|
|
204
|
-
return this.httpRequest(url.toString(), _zodSchemas.schemas.TwapsResponse, fetchOptions);
|
|
205
|
-
}
|
|
206
195
|
appendUrlSearchParams(url, params) {
|
|
207
196
|
for (const [key, value] of Object.entries(params)){
|
|
208
197
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
@@ -7,7 +7,6 @@ export type EncodingType = z.infer<typeof schemas.EncodingType>;
|
|
|
7
7
|
export type PriceFeedMetadata = z.infer<typeof schemas.PriceFeedMetadata>;
|
|
8
8
|
export type PriceIdInput = z.infer<typeof schemas.PriceIdInput>;
|
|
9
9
|
export type PriceUpdate = z.infer<typeof schemas.PriceUpdate>;
|
|
10
|
-
export type TwapsResponse = z.infer<typeof schemas.TwapsResponse>;
|
|
11
10
|
export type PublisherCaps = z.infer<typeof schemas.LatestPublisherStakeCapsUpdateDataResponse>;
|
|
12
11
|
export type UnixTimestamp = number;
|
|
13
12
|
export type DurationInSeconds = number;
|
|
@@ -26,12 +25,20 @@ export type HermesClientConfig = {
|
|
|
26
25
|
* Optional headers to be included in every request.
|
|
27
26
|
*/
|
|
28
27
|
headers?: HeadersInit;
|
|
28
|
+
/**
|
|
29
|
+
* Optional API access token for authentication.
|
|
30
|
+
* When provided, this token will be included in all requests either:
|
|
31
|
+
* - As a Bearer token in the Authorization header (for HTTP requests)
|
|
32
|
+
* - As an ACCESS_TOKEN query parameter (for WebSocket/SSE connections)
|
|
33
|
+
*/
|
|
34
|
+
accessToken?: string;
|
|
29
35
|
};
|
|
30
36
|
export declare class HermesClient {
|
|
31
37
|
private baseURL;
|
|
32
38
|
private timeout;
|
|
33
39
|
private httpRetries;
|
|
34
40
|
private headers;
|
|
41
|
+
private accessToken;
|
|
35
42
|
/**
|
|
36
43
|
* Constructs a new Connection.
|
|
37
44
|
*
|
|
@@ -133,26 +140,6 @@ export declare class HermesClient {
|
|
|
133
140
|
benchmarksOnly?: boolean;
|
|
134
141
|
ignoreInvalidPriceIds?: boolean;
|
|
135
142
|
}): Promise<EventSource>;
|
|
136
|
-
/**
|
|
137
|
-
* Fetch the latest TWAP (time weighted average price) for a set of price feed IDs.
|
|
138
|
-
* This endpoint can be customized by specifying the encoding type and whether the results should also return the calculated TWAP using the options object.
|
|
139
|
-
* This will throw an error if there is a network problem or the price service returns a non-ok response.
|
|
140
|
-
*
|
|
141
|
-
* @param ids - Array of hex-encoded price feed IDs for which updates are requested.
|
|
142
|
-
* @param window_seconds - The time window in seconds over which to calculate the TWAP, ending at the current time.
|
|
143
|
-
* 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).
|
|
144
|
-
* @param options - Optional parameters:
|
|
145
|
-
* - encoding: Encoding type. If specified, return the TWAP binary data in the encoding specified by the encoding parameter. Default is hex.
|
|
146
|
-
* - parsed: Boolean to specify if the calculated TWAP should be included in the response. Default is false.
|
|
147
|
-
* - ignoreInvalidPriceIds: Boolean to specify if invalid price IDs should be ignored instead of returning an error. Default is false.
|
|
148
|
-
*
|
|
149
|
-
* @returns TwapsResponse object containing the latest TWAPs.
|
|
150
|
-
*/
|
|
151
|
-
getLatestTwaps(ids: HexString[], window_seconds: number, options?: {
|
|
152
|
-
encoding?: EncodingType;
|
|
153
|
-
parsed?: boolean;
|
|
154
|
-
ignoreInvalidPriceIds?: boolean;
|
|
155
|
-
}, fetchOptions?: RequestInit): Promise<TwapsResponse>;
|
|
156
143
|
private appendUrlSearchParams;
|
|
157
144
|
private buildURL;
|
|
158
145
|
}
|
package/dist/cjs/utils.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", {
|
|
3
3
|
value: true
|
|
4
4
|
});
|
|
@@ -8,7 +8,7 @@ Object.defineProperty(exports, "camelToSnakeCaseObject", {
|
|
|
8
8
|
return camelToSnakeCaseObject;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
-
function camelToSnakeCase(str) {
|
|
11
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */ function camelToSnakeCase(str) {
|
|
12
12
|
return str.replaceAll(/[A-Z]/g, (letter)=>`_${letter.toLowerCase()}`);
|
|
13
13
|
}
|
|
14
14
|
function camelToSnakeCaseObject(obj) {
|
package/dist/cjs/zodSchemas.cjs
CHANGED
|
@@ -30,7 +30,9 @@ const AssetType = _zod.z.enum([
|
|
|
30
30
|
"crypto_redemption_rate",
|
|
31
31
|
"commodities",
|
|
32
32
|
"crypto_index",
|
|
33
|
-
"crypto_nav"
|
|
33
|
+
"crypto_nav",
|
|
34
|
+
"eco",
|
|
35
|
+
"kalshi"
|
|
34
36
|
]);
|
|
35
37
|
const asset_type = AssetType.nullish();
|
|
36
38
|
const RpcPriceIdentifier = _zod.z.string();
|
|
@@ -79,17 +81,6 @@ const LatestPublisherStakeCapsUpdateDataResponse = _zod.z.object({
|
|
|
79
81
|
binary: BinaryUpdate,
|
|
80
82
|
parsed: _zod.z.array(ParsedPublisherStakeCapsUpdate).nullish()
|
|
81
83
|
}).passthrough();
|
|
82
|
-
const ParsedPriceFeedTwap = _zod.z.object({
|
|
83
|
-
down_slots_ratio: _zod.z.string(),
|
|
84
|
-
end_timestamp: _zod.z.number().int(),
|
|
85
|
-
id: RpcPriceIdentifier,
|
|
86
|
-
start_timestamp: _zod.z.number().int(),
|
|
87
|
-
twap: RpcPrice
|
|
88
|
-
}).passthrough();
|
|
89
|
-
const TwapsResponse = _zod.z.object({
|
|
90
|
-
binary: BinaryUpdate,
|
|
91
|
-
parsed: _zod.z.array(ParsedPriceFeedTwap).nullish()
|
|
92
|
-
}).passthrough();
|
|
93
84
|
const schemas = {
|
|
94
85
|
AssetType,
|
|
95
86
|
asset_type,
|
|
@@ -104,9 +95,7 @@ const schemas = {
|
|
|
104
95
|
PriceUpdate,
|
|
105
96
|
ParsedPublisherStakeCap,
|
|
106
97
|
ParsedPublisherStakeCapsUpdate,
|
|
107
|
-
LatestPublisherStakeCapsUpdateDataResponse
|
|
108
|
-
ParsedPriceFeedTwap,
|
|
109
|
-
TwapsResponse
|
|
98
|
+
LatestPublisherStakeCapsUpdateDataResponse
|
|
110
99
|
};
|
|
111
100
|
const endpoints = (0, _core.makeApi)([
|
|
112
101
|
{
|
|
@@ -296,53 +285,6 @@ Clients should implement reconnection logic to maintain continuous price updates
|
|
|
296
285
|
}
|
|
297
286
|
],
|
|
298
287
|
response: LatestPublisherStakeCapsUpdateDataResponse
|
|
299
|
-
},
|
|
300
|
-
{
|
|
301
|
-
method: "get",
|
|
302
|
-
path: "/v2/updates/twap/:window_seconds/latest",
|
|
303
|
-
alias: "latest_twaps",
|
|
304
|
-
description: `Get the latest TWAP by price feed id with a custom time window.
|
|
305
|
-
|
|
306
|
-
Given a collection of price feed ids, retrieve the latest Pyth TWAP price for each price feed.`,
|
|
307
|
-
requestFormat: "json",
|
|
308
|
-
parameters: [
|
|
309
|
-
{
|
|
310
|
-
name: "window_seconds",
|
|
311
|
-
type: "Path",
|
|
312
|
-
schema: _zod.z.number().int().gte(0)
|
|
313
|
-
},
|
|
314
|
-
{
|
|
315
|
-
name: "ids[]",
|
|
316
|
-
type: "Query",
|
|
317
|
-
schema: _zod.z.array(PriceIdInput)
|
|
318
|
-
},
|
|
319
|
-
{
|
|
320
|
-
name: "encoding",
|
|
321
|
-
type: "Query",
|
|
322
|
-
schema: _zod.z.enum([
|
|
323
|
-
"hex",
|
|
324
|
-
"base64"
|
|
325
|
-
]).optional()
|
|
326
|
-
},
|
|
327
|
-
{
|
|
328
|
-
name: "parsed",
|
|
329
|
-
type: "Query",
|
|
330
|
-
schema: _zod.z.boolean().optional()
|
|
331
|
-
},
|
|
332
|
-
{
|
|
333
|
-
name: "ignore_invalid_price_ids",
|
|
334
|
-
type: "Query",
|
|
335
|
-
schema: _zod.z.boolean().optional()
|
|
336
|
-
}
|
|
337
|
-
],
|
|
338
|
-
response: TwapsResponse,
|
|
339
|
-
errors: [
|
|
340
|
-
{
|
|
341
|
-
status: 404,
|
|
342
|
-
description: `Price ids not found`,
|
|
343
|
-
schema: _zod.z.void()
|
|
344
|
-
}
|
|
345
|
-
]
|
|
346
288
|
}
|
|
347
289
|
]);
|
|
348
290
|
const api = new _core.Zodios(endpoints);
|