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.
function requestPrices(
bytes calldata payload, // callback
uint256 expiration, // expire
uint256 maxGasPrice, // limit gas price
uint256 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.
function xOracleCall(
uint256 reqId, // request id
bool priceUpdate, // must priceUpdate is true
bytes memory 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.
function requestPrices() external {
// allowance req fee
IERC20(weth).approve(xOracle, type(uint256).max);
// make payload and call
bytes memory payload = ""; // no payload
uint256 expired = 0; // no expiration
uint256 maxGasPrice = 10e9; // 10 gwei
uint256 callbackMaxGasLimit = 5000000; // 5M
IXOracle(xOracle).requestPrices(payload, expired, maxGasPrice, callbackMaxGasLimit);
}
Implement the xOracleCall function to handle the callback from xOracle. In this example we do nothing in callback.