# EVM Oracle
# Overview
The Nibiru EVM Oracle precompile provides smart contracts with access to exchange rate data and ChainLink price feeds. This allows developers to query real-time price information for various asset pairs directly on-chain. From the Nibiru codebase itself, IOracle.sol (opens new window) provides the contract interface used to interact with the Oracle.
The precompile is accessible at a fixed address:
# Interface
The Oracle interface provides two main functions:
# 1. queryExchangeRate
This function returns the latest exchange rate for a given asset pair.
# Parameters
pair
(string memory
): The asset pair to query. Example values:"ubtc:uusd"
- USD price of BTC"unibi:uusd"
- USD price of NIBI
# Returns
price
(uint256
): The exchange rate for the given pair.blockTimeMs
(uint64
): The timestamp (in milliseconds) when the price was last updated.blockHeight
(uint64
): The block height when the price was last updated.
# Example Usage
# 2. chainLinkLatestRoundData
Fetches the latest price data from ChainLink for a given asset pair.
# Parameters
pair
(string memory
): The asset pair to query.
# Returns
roundId
(uint80
): The ID of the price round.answer
(int256
): The latest price of the asset pair.startedAt
(uint256
): Timestamp when the round started.updatedAt
(uint256
): Timestamp when the price was last updated.answeredInRound
(uint80
): The round in which the answer was computed.
# Example Usage
# ChainLink Interface
The Oracle precompile integrates with ChainLink's AggregatorV3Interface, allowing access to decentralized price feeds.
# Interface Methods
# Fetching Active Oracle Pairs
The Nibiru Oracle supports multiple asset pairs, which can be queried dynamically. The currently active markets can be retrieved using the Nibiru CLI with the following command:
# Example Response
Any of these asset pairs can be used as input to the queryExchangeRate
function of the Oracle precompile. If the pair is valid, the function returns the exchange rate; otherwise, it returns an error.
# Full Contract Example
Below is a complete smart contract that interacts with the Nibiru EVM Oracle precompile:
# Full Example with Direct Communication to the 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:
# Explanation of Key Components
# Provider Setup
The script uses the Nibiru Testnet RPC URL (https://evm-rpc.testnet-2.nibiru.fi/
) to connect to the Ethereum-compatible chain.
# ABI Definition
The ABI defines the available functions for interacting with the Oracle precompile (queryExchangeRate
and chainLinkLatestRoundData
).
# Contract Interaction
The ethers.Contract
is used to create an instance of the Oracle contract, allowing you to call its functions.
# Asset Pairs
The pairs (e.g., uatom:uusd
, ubtc:uusd
) are the asset pairs supported by the Oracle for querying exchange rates or ChainLink prices.
# Error Handling
The script uses try-catch
blocks to handle errors in case the Oracle or ChainLink query fails for any asset pair.
# Formatted Output
Prices are formatted to 18 decimals using ethers.formatUnits()
to handle the precision typically used in Ethereum-based tokens.
# How This Script Works
- The script connects to the Nibiru EVM Oracle precompile using the predefined address and ABI.
- It queries the
queryExchangeRate
function to get the price, block time, and block height for each asset pair. - It also queries the
chainLinkLatestRoundData
function to fetch the latest ChainLink price data. - The results are logged to the console for each pair, with appropriate formatting for clarity.
This approach directly communicates with the Nibiru Oracle precompile, and you can extend this to include other queries or add additional functionality as needed.