Integration Examples

Using ethers.js to query Avilom's AI oracle:

import { ethers } from "ethers";
const provider = new ethers.providers.JsonRpcProvider("https://rpc.avilom.net");

const oracleAbi = [
  "function getPrice(bytes32) view returns (uint256, uint256)"
];
const aiOracle = new ethers.Contract(ORACLE_ADDRESS, oracleAbi, provider);

async function fetchPrice(symbol) {
  const key = ethers.utils.formatBytes32String(symbol);
  const [priceX1e18, timestamp] = await aiOracle.getPrice(key);

  if (Date.now()/1000 - timestamp > 300) {
    throw new Error("Stale price data");
  }

  return priceX1e18.div(ethers.BigNumber.from("10").pow(18));
}

💡 Ensure freshness of oracle data by validating timestamps.

Last updated