๐ฅ Query Examples
This section provides practical examples of how to query the QIE Oracle from both JavaScript (frontend/backend using ethers.js
) and Solidity (on-chain smart contracts). These examples help developers easily integrate the oracle into dApps and smart contracts.
๐จ Solidity Example
Use this example to read the latest price from the oracle inside a smart contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract PriceConsumer {
AggregatorV3Interface public priceFeed;
constructor(address oracleAddress) {
priceFeed = AggregatorV3Interface(oracleAddress);
}
function getLatestPrice() public view returns (int256) {
(
, // roundId
int256 price,
, // startedAt
, // updatedAt
// answeredInRound
) = priceFeed.latestRoundData();
return price;
}
}
Replace
oracleAddress
with the deployed address of the QIE Oracle contract on your network.
๐ฆ JavaScript Example (ethers.js)
Use this example in your frontend or backend to query the oracle using ethers.js.
const { ethers } = require("ethers");
const provider = new ethers.JsonRpcProvider("<rpc_url>");
// Price Feed contract address (example: BNB/USD on QIE Mainnet)
const priceFeedAddress = "0xYourOracleAddress"; // BNB/USD (8 decimals)
// ABI fragment to read latestRoundData
const aggregatorV3InterfaceABI = [
{
"inputs": [],
"name": "latestRoundData",
"outputs": [
{ "internalType": "uint80", "name": "roundId", "type": "uint80" },
{ "internalType": "int256", "name": "answer", "type": "int256" },
{ "internalType": "uint256", "name": "startedAt", "type": "uint256" },
{ "internalType": "uint256", "name": "updatedAt", "type": "uint256" },
{ "internalType": "uint80", "name": "answeredInRound", "type": "uint80" }
],
"stateMutability": "view",
"type": "function"
}
];
async function getPrice() {
const priceFeed = new ethers.Contract(priceFeedAddress, aggregatorV3InterfaceABI, provider);
const roundData = await priceFeed.latestRoundData();
const price = roundData.answer;
// Normalize 8 decimals
const normalizedPrice = Number(price) / 1e8;
console.log(`BNB/USD Price: $${normalizedPrice}`);
}
getPrice().catch(console.error);
โ ๏ธ Donโt forget to replace
0xYourOracleAddress
andyour_rpc_url
with the correct values for your deployment.
๐ Additional Query: Get Historical Data
You can also query a specific round by ID to fetch past data.
Solidity:
function getHistoricalPrice(uint80 roundId) public view returns (int256) {
(
,
int256 price,
,
,
) = priceFeed.getRoundData(roundId);
return price;
}
JavaScript:
async function fetchHistoricalPrice(roundId) {
const [ , price ] = await priceFeed.getRoundData(roundId);
const decimals = await priceFeed.decimals();
const normalizedPrice = Number(price) / (10 ** decimals);
console.log(`Price at round ${roundId}: ${normalizedPrice}`);
}
๐งช Tip: Mocking in Local Development
For local testing, you can deploy your own instance of the AggregatorV3
contract and call updateAnswer(int256)
to simulate real-world updates.
Last updated