EVM
This guide provides instructions on how to interact with the data feed contract to retrieve real-time price data directly into your Solidity smart contracts for all EVM chains.
Interface Definition
To read the data feeds
into your smart contract, implement the following interface:
Interface Functions
requestPrices
requestPrices
This function has the following parameters:
_assets
: An array of the data feeds required for your smart contract. You can request up to 100 data feeds in a single request._callback
: The callback function to handle the response data. You should embed the logic for processing the response data here.
feePerAsset
feePerAsset
This function returns the fee required per asset.
Implementation Example
The smart contract example below demonstrates how to use the IDataFeed
interface in your contracts on the desired chain.
Explanation of dataCallback
dataCallback
The dataCallback
function is used to process the response from requestPrices
. Here's how it works:
The function accepts the response from
requestPrices
, which includes arrays of prices and decimals. These values are separated because Solidity does not support floating-point numbers.The response can be handled according to the user's needs. For simplicity, this example stores the values in state variables and emits an event.
Key Points
Fee Calculation: The
requestPrices
function calculates the total fee based on the number of assets requested and the fee per asset.Refund Handling: If the user sends more than the required fee, the excess amount is refunded.
Data Storage: The last received prices and decimals are stored in state variables and can be retrieved using the
getLastPrices
function.
By following these steps, you can effectively integrate real-time price data into your smart contracts on any EVM-compatible chain.
Last updated