Skip to content

Commit 61c370e

Browse files
authored
fix(price_feeds/fuel): use HermesClient to fetch update data (#22)
1 parent 17fd499 commit 61c370e

File tree

4 files changed

+106
-9
lines changed

4 files changed

+106
-9
lines changed

price_feeds/fuel/fetch-and-update-btc-price/.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ NEXT_PUBLIC_HAS_SCRIPT=true
44
NEXT_PUBLIC_DAPP_ENVIRONMENT=testnet
55
NEXT_PUBLIC_TESTNET_CONTRACT_ID=0x657ed30041e69a9138caf8ccb27e51be7ff306082a7d7cd2092bad08b8e20d1f
66
NEXT_PUBLIC_PYTH_TESTNET_CONTRACT_ID=0x273172b23903a5587d034173f9c607c6473dc55afecec4b9efa3a1f9da5f27f6
7-
NEXT_PUBLIC_HERMES_BTC_URL="https://hermes.pyth.network/v2/updates/price/latest?ids[]="
7+
NEXT_PUBLIC_HERMES_URL="https://hermes.pyth.network/"

price_feeds/fuel/fetch-and-update-btc-price/package-lock.json

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

price_feeds/fuel/fetch-and-update-btc-price/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
"dependencies": {
1414
"@fuels/connectors": "^0.2.2",
1515
"@fuels/react": "^0.18.1",
16-
"fuels": "^0.92.1",
16+
"@pythnetwork/hermes-client": "^1.0.4",
1717
"@tanstack/react-query": "^5.29.2",
1818
"dotenv": "^16.4.5",
19+
"fuels": "^0.92.1",
1920
"next": "14.2.2",
2021
"react": "^18.2",
2122
"react-dom": "^18.2",

price_feeds/fuel/fetch-and-update-btc-price/src/pages/index.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useActiveWallet } from "@/hooks/useActiveWallet";
1111
import useAsync from "react-use/lib/useAsync";
1212
import { CURRENT_ENVIRONMENT } from "@/lib";
1313
import { PriceOutput } from "@/sway-api/contracts/TestContractAbi";
14+
import { HermesClient } from "@pythnetwork/hermes-client";
1415

1516
const FUEL_ETH_BASE_ASSET_ID =
1617
"0xf8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07";
@@ -37,13 +38,14 @@ export default function Home() {
3738
const [price, setPrice] = useState<PriceOutput>();
3839

3940
const fetchPriceUpdateData = async () => {
40-
const response = await fetch(hermesUrl + PRICE_FEED_ID);
41-
if (!response.ok) {
42-
throw new Error("Failed to fetch price");
43-
}
44-
const data = await response.json();
45-
const binaryData = data.binary.data[0];
46-
const buffer = Buffer.from(binaryData, "hex");
41+
const connection = new HermesClient(hermesUrl);
42+
43+
// Latest price updates
44+
const priceUpdates = await connection.getLatestPriceUpdates([
45+
PRICE_FEED_ID,
46+
]);
47+
48+
const buffer = Buffer.from(priceUpdates.binary.data[0], "hex");
4749
return buffer;
4850
};
4951

0 commit comments

Comments
 (0)