/** biome-ignore-all lint/style/noProcessEnv: this is an example file, so using process.env is okay for demonstration */ import type { PriceUpdate } from "@pythnetwork/hermes-client"; import { HermesClient } from "@pythnetwork/hermes-client"; import { arrayify, Contract, hexlify, Provider, Wallet } from "fuels"; import { FUEL_ETH_ASSET_ID, PYTH_CONTRACT_ABI, PYTH_CONTRACT_ADDRESS_SEPOLIA, } from "../index"; async function main() { // Create a provider for interacting with Fuel RPC const provider = await Provider.create( "https://testnet.fuel.network/v1/graphql", ); const privateKey = process.env.ACCOUNT_PRIVATE_KEY; if (privateKey === undefined) { throw new Error("Missing ACCOUNT_PRIVATE_KEY env var"); } const wallet = Wallet.fromPrivateKey(privateKey, provider); // Create a `Contract` instance to interact with the Pyth contract on Fuel const contract = new Contract( PYTH_CONTRACT_ADDRESS_SEPOLIA, PYTH_CONTRACT_ABI, wallet, ); const _priceFeedSymbol = "Crypto.ETH/USD"; const priceFeedId = "0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace"; // Pyth ETH/USD price feed id const _previousPrice = ( await contract.functions.price_unsafe(priceFeedId).get() ).value; // Create a client for pulling price updates from Hermes. const hermesClient = new HermesClient("https://hermes.pyth.network"); // Get the latest values of the price feeds as json objects. const priceUpdates: PriceUpdate = await hermesClient.getLatestPriceUpdates([ priceFeedId, ]); const priceFeedUpdateData = arrayify( Buffer.from(priceUpdates.binary.data[0], "hex"), ); const updateFee: number = ( await contract.functions.update_fee([priceFeedUpdateData]).get() ).value; const _tx = await contract.functions .update_price_feeds([priceFeedUpdateData]) .callParams({ forward: [updateFee, hexlify(FUEL_ETH_ASSET_ID)], }) .call(); const _newPrice = ( await contract.functions.price_no_older_than(60, priceFeedId).get() ).value; } main();