๐ŸงฉOff-chain Examples

Integrating off-chain data into your workflow is essential for creating robust and dynamic decentralized applications. This section provides a detailed guide on how to access eOracle's API off-chain.

import { ethers } from 'ethers';

// ABI of the IEOFeedManager interface
const IEOFeedManagerAbi = [
  "function getLatestPriceFeed(uint16 symbol) external view returns (tuple(uint256 value, uint256 timestamp))",
  "function getLatestPriceFeeds(uint16[] calldata symbols) external view returns (tuple(uint256 value, uint256 timestamp)[])"
];

// Address of the deployed IEOFeedManager contract on Holesky network
const IEOFeedManagerAddress = "0x723BD409703EF60d6fB9F8d986eb90099A170fd0";

async function main() {
  // Connect to the Ethereum network (Holesky in this case)
  const provider = new ethers.providers.JsonRpcProvider('https://some.holesky.rpc');

  // Create a contract instance
  const feedManagerContract = new ethers.Contract(IEOFeedManagerAddress, IEOFeedManagerAbi, provider);

  // Example to get the latest price feed for a single symbol (e.g., BTC:USD with symbol ID 1)
  async function getPrice(symbol: number) {
    try {
      const priceFeed = await feedManagerContract.getLatestPriceFeed(symbol);
      console.log(`Price: ${priceFeed.value.toString()}, Timestamp: ${priceFeed.timestamp.toString()}`);
      return priceFeed;
    } catch (error) {
      console.error('Error fetching price feed:', error);
    }
  }

  // Example to get the latest price feeds for multiple symbols
  async function getPrices(symbols: number[]) {
    try {
      const priceFeeds = await feedManagerContract.getLatestPriceFeeds(symbols);
      priceFeeds.forEach((feed: { value: ethers.BigNumber, timestamp: ethers.BigNumber }, index: number) => {
        console.log(`Symbol: ${symbols[index]}, Price: ${feed.value.toString()}, Timestamp: ${feed.timestamp.toString()}`);
      });
      return priceFeeds;
    } catch (error) {
      console.error('Error fetching price feeds:', error);
    }
  }

  // Call the functions with example symbol(s)
  await getPrice(1); // For BTC:USD
  await getPrices([1, 2]); // Example for multiple symbols (e.g., BTC:USD and ETH:USD)
}

main().catch(console.error);

Last updated