Fast Pricefeed is a price feed service that allows decentralized applications (dApps) to request and receive current price information efficiently. The process involves requesting prices and receiving prices through the following steps.
Step-by-Step Guide
Send request Prices. Your contract can request prices from the xOracle contract using the requestPrices function. The request fee must be paid in WETH.
functionrequestPrices(bytescalldata payload,// callbackuint256 expiration,// expireuint256 maxGasPrice,// limit gas priceuint256 callbackGasLimit // limit gas limit)
Implementing the callback function to handle the response from xOracle, implement the xOracleCall function. xOracle will callback this function to fulfill the price request and you can execute some logic here.
functionxOracleCall(uint256 reqId,// request idbool priceUpdate,// must priceUpdate is truebytesmemory payload // callback payload from request)
Retrieving the latest price. You can retrieve the latest price using the getLastPrice function from the xOracle contract. This function returns several details including the latest round, current price, latest price, and the timestamp of the price update.
Create a function to call requestPrices to the xOracle contract. This function approves the required fee in WETH and makes the request with specific parameters.
functionrequestPrices() external {// allowance req feeIERC20(weth).approve(xOracle, type(uint256).max);// make payload and callbytesmemory payload =""; // no payloaduint256 expired =0; // no expirationuint256 maxGasPrice =10e9; // 10 gweiuint256 callbackMaxGasLimit =5000000; // 5MIXOracle(xOracle).requestPrices(payload, expired, maxGasPrice, callbackMaxGasLimit);}
Implement the xOracleCall function to handle the callback from xOracle. In this example we do nothing in callback.