@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
|
@@ -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
|
}
|
|
@@ -8,6 +8,7 @@ export class HermesClient {
|
|
|
8
8
|
timeout;
|
|
9
9
|
httpRetries;
|
|
10
10
|
headers;
|
|
11
|
+
accessToken;
|
|
11
12
|
/**
|
|
12
13
|
* Constructs a new Connection.
|
|
13
14
|
*
|
|
@@ -18,9 +19,15 @@ export class HermesClient {
|
|
|
18
19
|
this.timeout = config?.timeout ?? DEFAULT_TIMEOUT;
|
|
19
20
|
this.httpRetries = config?.httpRetries ?? DEFAULT_HTTP_RETRIES;
|
|
20
21
|
this.headers = config?.headers ?? {};
|
|
22
|
+
this.accessToken = config?.accessToken;
|
|
21
23
|
}
|
|
22
24
|
async httpRequest(url, schema, options, retries = this.httpRetries, backoff = 100 + Math.floor(Math.random() * 100)) {
|
|
23
25
|
try {
|
|
26
|
+
// Build auth headers if access token is provided
|
|
27
|
+
const authHeaders = {};
|
|
28
|
+
if (this.accessToken !== undefined) {
|
|
29
|
+
authHeaders.Authorization = `Bearer ${this.accessToken}`;
|
|
30
|
+
}
|
|
24
31
|
const response = await fetch(url, {
|
|
25
32
|
...options,
|
|
26
33
|
signal: AbortSignal.any([
|
|
@@ -30,6 +37,7 @@ export class HermesClient {
|
|
|
30
37
|
AbortSignal.timeout(this.timeout)
|
|
31
38
|
]),
|
|
32
39
|
headers: {
|
|
40
|
+
...authHeaders,
|
|
33
41
|
...this.headers,
|
|
34
42
|
...options?.headers
|
|
35
43
|
}
|
|
@@ -158,41 +166,22 @@ export class HermesClient {
|
|
|
158
166
|
const transformedOptions = camelToSnakeCaseObject(options);
|
|
159
167
|
this.appendUrlSearchParams(url, transformedOptions);
|
|
160
168
|
}
|
|
169
|
+
// Build auth headers for SSE fetch
|
|
170
|
+
const authHeaders = {};
|
|
171
|
+
if (this.accessToken !== undefined) {
|
|
172
|
+
authHeaders.Authorization = `Bearer ${this.accessToken}`;
|
|
173
|
+
}
|
|
161
174
|
return new EventSource(url.toString(), {
|
|
162
175
|
fetch: (input, init)=>fetch(input, {
|
|
163
176
|
...init,
|
|
164
177
|
headers: {
|
|
165
178
|
...init?.headers,
|
|
179
|
+
...authHeaders,
|
|
166
180
|
...this.headers
|
|
167
181
|
}
|
|
168
182
|
})
|
|
169
183
|
});
|
|
170
184
|
}
|
|
171
|
-
/**
|
|
172
|
-
* Fetch the latest TWAP (time weighted average price) for a set of price feed IDs.
|
|
173
|
-
* This endpoint can be customized by specifying the encoding type and whether the results should also return the calculated TWAP using the options object.
|
|
174
|
-
* This will throw an error if there is a network problem or the price service returns a non-ok response.
|
|
175
|
-
*
|
|
176
|
-
* @param ids - Array of hex-encoded price feed IDs for which updates are requested.
|
|
177
|
-
* @param window_seconds - The time window in seconds over which to calculate the TWAP, ending at the current time.
|
|
178
|
-
* 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).
|
|
179
|
-
* @param options - Optional parameters:
|
|
180
|
-
* - encoding: Encoding type. If specified, return the TWAP binary data in the encoding specified by the encoding parameter. Default is hex.
|
|
181
|
-
* - parsed: Boolean to specify if the calculated TWAP should be included in the response. Default is false.
|
|
182
|
-
* - ignoreInvalidPriceIds: Boolean to specify if invalid price IDs should be ignored instead of returning an error. Default is false.
|
|
183
|
-
*
|
|
184
|
-
* @returns TwapsResponse object containing the latest TWAPs.
|
|
185
|
-
*/ async getLatestTwaps(ids, window_seconds, options, fetchOptions) {
|
|
186
|
-
const url = this.buildURL(`updates/twap/${window_seconds.toString()}/latest`);
|
|
187
|
-
for (const id of ids){
|
|
188
|
-
url.searchParams.append("ids[]", id);
|
|
189
|
-
}
|
|
190
|
-
if (options) {
|
|
191
|
-
const transformedOptions = camelToSnakeCaseObject(options);
|
|
192
|
-
this.appendUrlSearchParams(url, transformedOptions);
|
|
193
|
-
}
|
|
194
|
-
return this.httpRequest(url.toString(), schemas.TwapsResponse, fetchOptions);
|
|
195
|
-
}
|
|
196
185
|
appendUrlSearchParams(url, params) {
|
|
197
186
|
for (const [key, value] of Object.entries(params)){
|
|
198
187
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|