Skip to content

Commit 0d3eb84

Browse files
committed
update client code to use HermesClient
1 parent a3421c6 commit 0d3eb84

File tree

1 file changed

+11
-12
lines changed
  • pages/price-feeds/use-real-time-data

1 file changed

+11
-12
lines changed

pages/price-feeds/use-real-time-data/fuel.mdx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ The code snippet below provides an example of how to fetch price updates using N
118118
import { TestContractAbi__factory } from "@/sway-api";
119119
import PYTH_CONTRACT_ABI from "../abi/pyth-contract-abi.json";
120120
import { arrayify, Contract, hexlify } from "fuels";
121+
import { HermesClient } from "@pythnetwork/hermes-client";
121122

122-
const HERMES_V2_LATEST_PRICE_UPDATES_ENDPOINT =
123-
"https://hermes.pyth.network/v2/updates/price/latest?ids[]=";
123+
const HERMES_ENDPOINT = "https://hermes.pyth.network/";
124124
const FUEL_ETH_BASE_ASSET_ID =
125125
"0xf8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07";
126-
const PRICE_FEED_ID =
126+
const ETH_USD_PRICE_FEED_ID =
127127
"0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace"; // ETH/USD
128128

129129
const contractId =
@@ -135,15 +135,14 @@ const pythContractId = process.env
135135

136136
async function updateAndGetPrice() {
137137
const fetchPriceUpdateData = async () => {
138-
const response = await fetch(
139-
HERMES_V2_LATEST_PRICE_UPDATES_ENDPOINT + PRICE_FEED_ID
140-
);
141-
if (!response.ok) {
142-
throw new Error("Failed to fetch price");
143-
}
144-
const data = await response.json();
145-
const binaryData = data.binary.data[0];
146-
const buffer = Buffer.from(binaryData, "hex");
138+
const connection = new HermesClient(HERMES_ENDPOINT);
139+
140+
// Latest price updates
141+
const priceUpdates = await connection.getLatestPriceUpdates([
142+
ETH_USD_PRICE_FEED_ID,
143+
]);
144+
145+
const buffer = Buffer.from(priceUpdates.binary.data[0], "hex");
147146
return buffer;
148147
};
149148

0 commit comments

Comments
 (0)