@pythnetwork/hermes-client
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -51,2 +51,3 @@ "use strict"; | ||
// Get price feeds | ||
console.log(`Price feeds matching "btc" with asset type "crypto":`); | ||
const priceFeeds = await connection.getPriceFeeds({ | ||
@@ -58,5 +59,11 @@ query: "btc", | ||
// Latest price updates | ||
console.log(`Latest price updates for price IDs ${priceIds}:`); | ||
const priceUpdates = await connection.getLatestPriceUpdates(priceIds); | ||
console.log(priceUpdates); | ||
// Get the latest 5 second TWAPs | ||
console.log(`Latest 5 second TWAPs for price IDs ${priceIds}`); | ||
const twapUpdates = await connection.getLatestTwaps(priceIds, 5); | ||
console.log(twapUpdates); | ||
// Streaming price updates | ||
console.log(`Streaming latest prices for price IDs ${priceIds}...`); | ||
const eventSource = await connection.getPriceUpdatesStream(priceIds, { | ||
@@ -70,2 +77,3 @@ encoding: "hex", | ||
console.log("Received price update:", event.data); | ||
const _priceUpdate = JSON.parse(event.data); | ||
}; | ||
@@ -72,0 +80,0 @@ eventSource.onerror = (error) => { |
@@ -10,2 +10,3 @@ import EventSource from "eventsource"; | ||
export type PriceUpdate = z.infer<typeof schemas.PriceUpdate>; | ||
export type TwapsResponse = z.infer<typeof schemas.TwapsResponse>; | ||
export type PublisherCaps = z.infer<typeof schemas.LatestPublisherStakeCapsUpdateDataResponse>; | ||
@@ -134,4 +135,24 @@ export type UnixTimestamp = number; | ||
}): Promise<EventSource>; | ||
/** | ||
* Fetch the latest TWAP (time weighted average price) for a set of price feed IDs. | ||
* This endpoint can be customized by specifying the encoding type and whether the results should also return the calculated TWAP using the options object. | ||
* This will throw an error if there is a network problem or the price service returns a non-ok response. | ||
* | ||
* @param ids Array of hex-encoded price feed IDs for which updates are requested. | ||
* @param window_seconds The time window in seconds over which to calculate the TWAP, ending at the current time. | ||
* 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). | ||
* @param options Optional parameters: | ||
* - encoding: Encoding type. If specified, return the TWAP binary data in the encoding specified by the encoding parameter. Default is hex. | ||
* - parsed: Boolean to specify if the calculated TWAP should be included in the response. Default is false. | ||
* - ignoreInvalidPriceIds: Boolean to specify if invalid price IDs should be ignored instead of returning an error. Default is false. | ||
* | ||
* @returns TwapsResponse object containing the latest TWAPs. | ||
*/ | ||
getLatestTwaps(ids: HexString[], window_seconds: number, options?: { | ||
encoding?: EncodingType; | ||
parsed?: boolean; | ||
ignoreInvalidPriceIds?: boolean; | ||
}): Promise<TwapsResponse>; | ||
private appendUrlSearchParams; | ||
} | ||
//# sourceMappingURL=HermesClient.d.ts.map |
@@ -44,3 +44,4 @@ "use strict"; | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
const errorBody = await response.text(); | ||
throw new Error(`HTTP error! status: ${response.status}${errorBody ? `, body: ${errorBody}` : ""}`); | ||
} | ||
@@ -174,2 +175,28 @@ const data = await response.json(); | ||
} | ||
/** | ||
* Fetch the latest TWAP (time weighted average price) for a set of price feed IDs. | ||
* This endpoint can be customized by specifying the encoding type and whether the results should also return the calculated TWAP using the options object. | ||
* This will throw an error if there is a network problem or the price service returns a non-ok response. | ||
* | ||
* @param ids Array of hex-encoded price feed IDs for which updates are requested. | ||
* @param window_seconds The time window in seconds over which to calculate the TWAP, ending at the current time. | ||
* 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). | ||
* @param options Optional parameters: | ||
* - encoding: Encoding type. If specified, return the TWAP binary data in the encoding specified by the encoding parameter. Default is hex. | ||
* - parsed: Boolean to specify if the calculated TWAP should be included in the response. Default is false. | ||
* - ignoreInvalidPriceIds: Boolean to specify if invalid price IDs should be ignored instead of returning an error. Default is false. | ||
* | ||
* @returns TwapsResponse object containing the latest TWAPs. | ||
*/ | ||
async getLatestTwaps(ids, window_seconds, options) { | ||
const url = new URL(`v2/updates/twap/${window_seconds}/latest`, this.baseURL); | ||
for (const id of ids) { | ||
url.searchParams.append("ids[]", id); | ||
} | ||
if (options) { | ||
const transformedOptions = (0, utils_1.camelToSnakeCaseObject)(options); | ||
this.appendUrlSearchParams(url, transformedOptions); | ||
} | ||
return this.httpRequest(url.toString(), zodSchemas_1.schemas.TwapsResponse); | ||
} | ||
appendUrlSearchParams(url, params) { | ||
@@ -176,0 +203,0 @@ Object.entries(params).forEach(([key, value]) => { |
@@ -66,2 +66,17 @@ "use strict"; | ||
.passthrough(); | ||
const ParsedPriceFeedTwap = zod_1.z | ||
.object({ | ||
down_slots_ratio: zod_1.z.string(), | ||
end_timestamp: zod_1.z.number().int(), | ||
id: RpcPriceIdentifier, | ||
start_timestamp: zod_1.z.number().int(), | ||
twap: RpcPrice, | ||
}) | ||
.passthrough(); | ||
const TwapsResponse = zod_1.z | ||
.object({ | ||
binary: BinaryUpdate, | ||
parsed: zod_1.z.array(ParsedPriceFeedTwap).nullish(), | ||
}) | ||
.passthrough(); | ||
exports.schemas = { | ||
@@ -82,2 +97,4 @@ AssetType, | ||
LatestPublisherStakeCapsUpdateDataResponse, | ||
ParsedPriceFeedTwap, | ||
TwapsResponse, | ||
}; | ||
@@ -258,2 +275,46 @@ const endpoints = (0, core_1.makeApi)([ | ||
}, | ||
{ | ||
method: "get", | ||
path: "/v2/updates/twap/:window_seconds/latest", | ||
alias: "latest_twaps", | ||
description: `Get the latest TWAP by price feed id with a custom time window. | ||
Given a collection of price feed ids, retrieve the latest Pyth TWAP price for each price feed.`, | ||
requestFormat: "json", | ||
parameters: [ | ||
{ | ||
name: "window_seconds", | ||
type: "Path", | ||
schema: zod_1.z.number().int().gte(0), | ||
}, | ||
{ | ||
name: "ids[]", | ||
type: "Query", | ||
schema: zod_1.z.array(PriceIdInput), | ||
}, | ||
{ | ||
name: "encoding", | ||
type: "Query", | ||
schema: zod_1.z.enum(["hex", "base64"]).optional(), | ||
}, | ||
{ | ||
name: "parsed", | ||
type: "Query", | ||
schema: zod_1.z.boolean().optional(), | ||
}, | ||
{ | ||
name: "ignore_invalid_price_ids", | ||
type: "Query", | ||
schema: zod_1.z.boolean().optional(), | ||
}, | ||
], | ||
response: TwapsResponse, | ||
errors: [ | ||
{ | ||
status: 404, | ||
description: `Price ids not found`, | ||
schema: zod_1.z.void(), | ||
}, | ||
], | ||
}, | ||
]); | ||
@@ -260,0 +321,0 @@ exports.api = new core_1.Zodios(endpoints); |
{ | ||
"name": "@pythnetwork/hermes-client", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Pyth Hermes Client", | ||
@@ -58,3 +58,3 @@ "author": { | ||
}, | ||
"gitHead": "733971809a4415c420922477a162aff3789a7774" | ||
"gitHead": "512f6375bbedf78ff8e0d651c606701c0f7fb2ac" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
278450
5736