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 IDataFeed {
    function requestPrices(
        bytes32[] calldata _assets,
        function(uint8[] memory, uint256[] memory) external _callback
    ) external payable;

    function feePerAsset() external view returns (uint256);
}

Interface Functions

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

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.

contract DataFeedInteractor  {
    address public constant PRICE_FEED_PROXY = 0x30B3731d5fE29E768Ab282dBF2c79D9A70776Ad0; // Data Feed Address on Arbitrum Sepolia
    IDataFeed public priceFeed;

    uint8[] public lastDecimals;
    uint256[] public lastPrices;

    event PricesReceived(uint8[] decimals, uint256[] prices);

    constructor() {
        priceFeed = IDataFeed(PRICE_FEED_PROXY);
    }

    function requestPrices(bytes32[] calldata _assets) external payable {
        uint256 totalFee = priceFeed.feePerAsset() * _assets.length;
        require(msg.value >= totalFee, "Insufficient fee");

        priceFeed.requestPrices{value: totalFee}(_assets, this.dataCallback);

        // Refund excess fee
        if (msg.value > totalFee) {
            payable(msg.sender).transfer(msg.value - totalFee);
        }
    }

    // Callback to handle the response
    function dataCallback(uint8[] memory _decimals, uint256[] memory _prices) external {
        require(msg.sender == PRICE_FEED_PROXY, "Unauthorized callback");

        lastDecimals = _decimals;
        lastPrices = _prices;

        emit PricesReceived(_decimals, _prices);
    }

    function getLastPrices() external view returns (uint8[] memory, uint256[] memory) {
        return (lastDecimals, lastPrices);
    }

    // Receive function to handle refunds in case of excess funds
    receive() external payable {}
}

Explanation of 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