Solidity

Solidity SDK Fetching Integration

Follow these steps to fetch price feeds on-chain as part of your workflow. All price feeds are automatically updated every 30 minutes.

  1. Clone this repo and execute the following commands

git clone https://github.com/Eoracle/Eoracle-SDK.git
cd Eoracle-sdk
  1. Import the eoracle-solidity-sdk folder into your smart contract environment

  2. Inherit the EoracleConsumer contract in your contract

import "./eoracle-solidity-sdk/EoracleConsumer";
contract MyContract is EoracleConsumer {
    // ... your contract code 
}
  1. Integrate fetching into your contract function

function MyFunction(
    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;
        emit PriceFeedUsage(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';
const API_BASE_URL = 'https://api.testnet.eoracle.network/api/v1/get_rate';
const EORACLE_ADDRESS = '0xE2912fc186A1dD7997aC41ccCD053D794e9845D3'; 
const RPC_URL = 'https://rpc.ankr.com/eth_goerli';

interface IProvableQuote {
    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[];
}

async function fetchQuote(symbol: string): Promise<IProvableQuote> {
    const response = await axios.get(`${API_BASE_URL}?symbol=${symbol}`);
    const quote : 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;
}

async function main() {
    const provider = new ethers.JsonRpcProvider("https://Your_RPC_URL");
    const user = new ethers.Wallet("0xYour_Private_Key", provider);
    const contractAbi = [
        "function updateAndGetPriceFeed(string calldata symbol, uint256 value, uint256 timestamp, bytes memory proof)",
    ];
    const contract = new ethers.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) 
    const quote = await fetchQuote(symbol);
    try {
        // You can override the transaction gas price in order to ensure high latency updates by providing another argument {gasprice: 1000000000 }
        const tx = await contract.updateAndGetPriceFeed(symbol, quote.rate, quote.timestamp, quote.proof);
        await tx.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 script
const EORACLE_CONSUMER_ADDRESS = '0xYourConsumerContractAddress'; 
// ...
const contractAbi = [
    "function updateAndGetFeed(string calldata symbol, uint256 value, uint256 timestamp, bytes memory proof)"
];
const contract = new ethers.Contract(EORACLE_CONSUMER_ADDRESS, contractAbi, user);
// ...
const tx = await contract.updateAndGetPriceFeed(symbol, quote.rate, quote.timestamp, quote.hash, quote.proof);
// .. 

Last updated