File tree Expand file tree Collapse file tree 4 files changed +106
-9
lines changed
price_feeds/fuel/fetch-and-update-btc-price Expand file tree Collapse file tree 4 files changed +106
-9
lines changed Original file line number Diff line number Diff line change @@ -4,4 +4,4 @@ NEXT_PUBLIC_HAS_SCRIPT=true
4
4
NEXT_PUBLIC_DAPP_ENVIRONMENT = testnet
5
5
NEXT_PUBLIC_TESTNET_CONTRACT_ID = 0x657ed30041e69a9138caf8ccb27e51be7ff306082a7d7cd2092bad08b8e20d1f
6
6
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/"
Original file line number Diff line number Diff line change 13
13
"dependencies" : {
14
14
"@fuels/connectors" : " ^0.2.2" ,
15
15
"@fuels/react" : " ^0.18.1" ,
16
- "fuels " : " ^0.92.1 " ,
16
+ "@pythnetwork/hermes-client " : " ^1.0.4 " ,
17
17
"@tanstack/react-query" : " ^5.29.2" ,
18
18
"dotenv" : " ^16.4.5" ,
19
+ "fuels" : " ^0.92.1" ,
19
20
"next" : " 14.2.2" ,
20
21
"react" : " ^18.2" ,
21
22
"react-dom" : " ^18.2" ,
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import { useActiveWallet } from "@/hooks/useActiveWallet";
11
11
import useAsync from "react-use/lib/useAsync" ;
12
12
import { CURRENT_ENVIRONMENT } from "@/lib" ;
13
13
import { PriceOutput } from "@/sway-api/contracts/TestContractAbi" ;
14
+ import { HermesClient } from "@pythnetwork/hermes-client" ;
14
15
15
16
const FUEL_ETH_BASE_ASSET_ID =
16
17
"0xf8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07" ;
@@ -37,13 +38,14 @@ export default function Home() {
37
38
const [ price , setPrice ] = useState < PriceOutput > ( ) ;
38
39
39
40
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" ) ;
47
49
return buffer ;
48
50
} ;
49
51
You can’t perform that action at this time.
0 commit comments