@@ -118,12 +118,12 @@ The code snippet below provides an example of how to fetch price updates using N
118
118
import { TestContractAbi__factory } from " @/sway-api" ;
119
119
import PYTH_CONTRACT_ABI from " ../abi/pyth-contract-abi.json" ;
120
120
import { arrayify , Contract , hexlify } from " fuels" ;
121
+ import { HermesClient } from " @pythnetwork/hermes-client" ;
121
122
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/" ;
124
124
const FUEL_ETH_BASE_ASSET_ID =
125
125
" 0xf8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07" ;
126
- const PRICE_FEED_ID =
126
+ const ETH_USD_PRICE_FEED_ID =
127
127
" 0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace" ; // ETH/USD
128
128
129
129
const contractId =
@@ -135,15 +135,14 @@ const pythContractId = process.env
135
135
136
136
async function updateAndGetPrice() {
137
137
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" );
147
146
return buffer ;
148
147
};
149
148
0 commit comments