Import the eoracle-solidity-sdk folder into your smart contract environment
Inherit the EoracleConsumer contract in your contract
import"./eoracle-solidity-sdk/EoracleConsumer";contractMyContractisEoracleConsumer {// ... your contract code }
Integrate fetching into your contract function
functionMyFunction(uint256 arg1,uint256 arg2,string[] symbols){Eoracle.PriceFeed memory priceFeeds = Eoracle.getLatestPriceFeeds(symbols);for (uint256 i =0; i < priceFeeds.length; i++) {//unpack the feed and use it. Eoracle.PriceFeed memory priceFeed = priceFeeds[i];uint256 value = priceFeed.value;uint256 timestamp = priceFeed.timestamp;emitPriceFeedUsage(symbols[i], value, timestamp); }}MyFunction(1,1,["eth","btc"])
Pushing Feeds On Demand Using the HTTP API
Eoracle provides http REST api , which makes it possible to fetch and prove quotes for on-chain for any supported symbol.
The following is a minimal Typescript http implementation on Goerli:
import axios from'axios';import { ethers } from'ethers';constAPI_BASE_URL='https://api.testnet.eoracle.network/api/v1/get_rate';constEORACLE_ADDRESS='0xE2912fc186A1dD7997aC41ccCD053D794e9845D3'; constRPC_URL='https://rpc.ankr.com/eth_goerli';interfaceIProvableQuote { symbol:string; rate:BigInt; timestamp:number; signature:BigInt[]; bitmap:string; unhashedLeaf:string; leafIndex:number; epochNumber:BigInt; blockNumber:number; blockHash:string; blockRound:BigInt; currentValidatorSetHash:string; eventRoot:string; proof:string[];}asyncfunctionfetchQuote(symbol:string):Promise<IProvableQuote> {constresponse=awaitaxios.get(`${API_BASE_URL}?symbol=${symbol}`);constquote:IProvableQuote= { symbol:response.data.symbol, rate:response.data.rate, timestamp:response.data.timestamp, signature:response.data.signature, bitmap:response.data.bitmap, unhashedLeaf:response.data.unhashedLeaf, leafIndex:response.data.leafIndex, epochNumber:response.data.epochNumber, blockNumber:response.data.blockNumber, blockHash:response.data.blockHash, blockRound:response.data.blockRound, currentValidatorSetHash:response.data.currentValidatorSetHash, eventRoot:response.data.eventRoot, proof:response.data.proof }return quote;}asyncfunctionmain() {constprovider=newethers.JsonRpcProvider("https://Your_RPC_URL");constuser=newethers.Wallet("0xYour_Private_Key", provider);constcontractAbi= ["function updateAndGetPriceFeed(string calldata symbol, uint256 value, uint256 timestamp, bytes memory proof)", ];constcontract=newethers.Contract(EORACLE_CONSUMER_ADDRESS, contractAbi, user); const symbol = "eth" //place your desired symbol here (you can get all symbols using the /api/v1/get_symbols endpoint)
constquote=awaitfetchQuote(symbol);try { // You can override the transaction gas price in order to ensure high latency updates by providing another argument {gasprice: 1000000000 }
consttx=awaitcontract.updateAndGetPriceFeed(symbol,quote.rate,quote.timestamp,quote.proof);awaittx.wait(); console.log(`${symbol} sucessfully updated with transaction ${tx.hash}`); } catch (error) {console.error('Error updating feeds:', error); }}main().then(() =>process.exit(0)).catch(error => {console.error(error);process.exit(1);});
Similarly , using /api/typescript/http/consume_feed.ts one can update and consume the price feed by invoking the "updateAndGetPriceFeed" function in EOracleConsumerExample, as a reference.
// ... same as the previous scriptconstEORACLE_CONSUMER_ADDRESS='0xYourConsumerContractAddress'; // ...constcontractAbi= ["function updateAndGetFeed(string calldata symbol, uint256 value, uint256 timestamp, bytes memory proof)"];constcontract=newethers.Contract(EORACLE_CONSUMER_ADDRESS, contractAbi, user);// ...consttx=awaitcontract.updateAndGetPriceFeed(symbol,quote.rate,quote.timestamp,quote.hash,quote.proof);// ..