Nibiru EVM includes a precompile for Nibiru's oracle mechanism, providing access
to exchange rate data and ChainLink-like oracle smart contracts. This allows
developers to query real-time price information for various markets directly
on-chain.
What is the smart contract implementation for these oracle feeds?
These feeds are deployments of NibiruOracleChainLinkLike.sol(opens new window), a contract that sources data from the Nibiru Oracle Mechanism and exposes the data in the ChainLink AggregatorV3Interface format.
# Example: TypeScript, Ethers v6, and the Oracle Precompile
To communicate directly with the Nibiru EVM Oracle precompile, you can use the ethers.js library to interact with the contract. The script you provided already demonstrates how to query the Oracle for exchange rates and Chainlink prices using the correct contract address and ABI.
Here's a breakdown of the necessary steps to ensure a smooth communication with the precompile, along with some refinements:
Copy
const{ ethers }=require("hardhat");asyncfunctionmain(){const provider =newethers.JsonRpcProvider("https://evm-rpc.testnet-2.nibiru.fi/"// Nibiru Testnet RPC URL);const precompileAddress ="0x0000000000000000000000000000000000000801";// Oracle Precompile Address// Define the IOracle ABI for querying the exchange rate and ChainLink dataconst abi =["function queryExchangeRate(string pair) view returns (uint256 price, uint64 blockTimeMs, uint64 blockHeight)","function chainLinkLatestRoundData(string pair) view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)",];// Connect to the Oracle precompile contract using the ABI and providerconst oracle =newethers.Contract(precompileAddress, abi, provider);// Define asset pairs to query the exchange ratesconst pairs =["uatom:uusd","ubtc:uusd","ueth:uusd","uusdc:uusd","uusdt:uusd",];// Loop through the pairs and query the Oracle for exchange rates and ChainLink pricesfor(const pair of pairs){try{// Query the exchange rate for the asset pair
console.log(`Querying exchange rate for ${pair}...`);const[price, blockTimeMs, blockHeight]=await oracle.queryExchangeRate(pair);
console.log(`Pair: ${pair}`);
console.log(`Exchange Rate: ${ethers.formatUnits(price,18)}`);// Format the price to 18 decimals
console.log(`Block Time (ms): ${blockTimeMs.toString()}`);
console.log(`Block Height: ${blockHeight.toString()}`);}catch(error){
console.error(`Error querying exchange rate for ${pair}:`, error);}try{// Query the ChainLink price for the asset pair
console.log(`Querying ChainLink price for ${pair}...`);const[roundId, answer, startedAt, updatedAt, answeredInRound]=await oracle.chainLinkLatestRoundData(pair);
console.log(`Pair: ${pair}`);
console.log(`ChainLink Price: ${ethers.formatUnits(answer,18)}`);// Format the price to 18 decimals
console.log(`Round ID: ${roundId.toString()}`);
console.log(`Started At: ${startedAt.toString()}`);
console.log(`Updated At: ${updatedAt.toString()}`);
console.log(`Answered In Round: ${answeredInRound.toString()}`);}catch(error){
console.error(`Error querying ChainLink price for ${pair}:`, error);}}}// Execute the scriptmain().catch(console.error);
ABI Definition: Specifies the functions available for interaction with the Oracle smart contracts, allowing querying of prices and exchange rates.
Supported Asset Pairs: Lists the asset pairs (e.g., "ueth:uusd", "unibi:uusd") supported by Nibiru’s Oracle system for querying price data.
Error Handling: Implements robust error management using try-catch statements to handle potential failures gracefully during queries to the Oracle or ChainLink-like feeds.
Formatted Output: Provides clear and precise price data formatted to 18 decimals, ensuring readability and ease of interpretation.
This approach directly communicates with the Nibiru Oracle precompile, and you
can extend this to include other queries or add additional functionality as
needed.