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.
Import the ERC20 interface and the xOracle interface.
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.
Implement the xOracleCall function to handle the callback from xOracle. In this example we do nothing in callback.
Note: onlyXOracle is a modifier that should be defined to restrict access to this function to the xOracle contract only.
Read the Latest Price. Create a function to get the latest price of a specific token using the getLastPrice function from the xOracle contract.
Full Code
Here is the full example code, including the necessary imports and functions.