ftso price feed
Smart contracts on Flare access the FTSO price data by calling specific functions provided by price feed contracts. These contracts interact with the FTSO oracle, which aggregates price information from decentralized validators. The retrieved price data is used by the smart contract to make decisions, trigger transactions, or calculate values based on real-world market conditions. This decentralized price oracle system ensures that smart contracts on the Flare Network can reliably access accurate and up-to-date price information.
1. Price Feed Contract Call
Smart contracts on Flare access the price data via special price feed functions. The Flare Network defines contracts that interact with the FTSO to retrieve asset prices. These contracts expose functions that can be called by other smart contracts to request up-to-date price information.
- The smart contract interacts with the FTSO contract by invoking a specific function like
getPrice()
orgetAssetPrice()
. - The function call may require the asset's identifier (ticker symbol) as an argument, so the smart contract can query the specific price data it needs.
function getAssetPrice(address asset) public view returns (uint256) {
uint256 price = priceFeed.getPrice(asset);
return price;
}
- priceFeed in this example is a reference to the FTSO contract.
- getPrice() is a function in the FTSO contract that returns the current price of the asset.
- asset is the unique identifier (such as the ticker symbol or address) for the asset whose price is being requested.
2. Data Request
When a smart contract needs the price of a given asset (say, the price of XLM or BTC), it will request the price feed from the FTSO. This is typically done by calling a price retrieval function provided by the Flare contract.
For example:
function getAssetPrice(address asset) public view returns (uint256) {
uint256 price = priceFeed.getPrice(asset);
return price;
}
- priceFeed in this example is a reference to the FTSO contract.
- getPrice() is a function in the FTSO contract that returns the current price of the asset.
- asset is the unique identifier (such as the ticker symbol or address) for the asset whose price is being requested.
3. FTSO Node Participation
When a smart contract makes this request, the FTSO will aggregate the data reported by the decentralized validators (oracles) on the Flare Network. These validators submit their price estimates for the requested asset at regular intervals, and the FTSO contract will then return the aggregated result.
- Each validator submits a price for the asset.
- The FTSO contract aggregates these price submissions, potentially applying a weighted average or other mechanism to ensure the most accurate price is returned.
- The aggregated price is then made available to any smart contract that requests it.
4. Price Data Delivery to Smart Contract
Once the FTSO contract has aggregated and validated the price data, it returns the price information to the calling smart contract. The data could be in the form of an unsigned integer representing the asset's price (for example, in terms of its smallest unit, like satoshis for BTC).
The smart contract can now use this data for further logic:
- Triggering a transaction if the price crosses a certain threshold.
- Performing calculations or determining if certain conditions have been met for the execution of a function.
Example of a smart contract receiving and utilizing price data:
function triggerActionIfPriceIsHigh(address asset, uint256 threshold) public {
uint256 currentPrice = getAssetPrice(asset);
if (currentPrice >= threshold) {
// Perform some action, such as transferring tokens
transferFunds();
}
}
5. Price Data Freshness
The FTSO continuously updates the price data, and the smart contract may request the most recent price whenever needed. This helps ensure that the smart contract is always working with fresh data. Depending on the use case, the smart contract may also cache price data temporarily to reduce the frequency of calls to the FTSO contract.
6. Interfacing with dApps
For decentralized applications (dApps) on the Flare Network, smart contracts can be written to consume price data in real time. For example:
- A decentralized exchange (DEX) could trigger trades when the price of a token reaches a specific value.
- A lending protocol could check asset prices to determine collateral value and adjust loan-to-value ratios automatically.
Example Flow:
- User Action: A user calls a function on a smart contract, such as a trade or loan operation.
- Smart Contract Call to FTSO: The smart contract needs to know the current price of an asset (say, XLM). It calls the
getAssetPrice()
function of the FTSO contract. - FTSO Data Aggregation: The FTSO aggregates price data from validators and returns the latest price.
- Smart Contract Logic: The smart contract receives the price and checks if it meets certain conditions (e.g., if it's above a certain threshold). If conditions are met, the smart contract executes the next action (e.g., triggering a trade or loan adjustment).
more
script running on a server (off-chain) can access the price data provided by the Flare Time Series Oracle (FTSO) by interacting with the FTSO's smart contracts on the Flare Network.
This is typically achieved using a library like CCXT, which allows the script to fetch price data from various exchanges and then submit this data to the FTSO via the PriceSubmitter contract. This process involves the script collecting price information from selected exchanges and sending it to the FTSO system, where it is aggregated and made available for smart contracts on the Flare Network.
FTSO (Flare Time Series Oracle) | Learn how to Query Prices on Flare | Oracle & Working
Flare Developer Hub: Read feeds offchain