freeradiantbunny.org

freeradiantbunny.org/blog

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.

function getAssetPrice(address asset) public view returns (uint256) {
                                                                  uint256 price = priceFeed.getPrice(asset);
                                                                  return price;
                                                                  }
                                                                  

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;
                                                                  }
                                                                  

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.

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:

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:

Example Flow:

  1. User Action: A user calls a function on a smart contract, such as a trade or loan operation.
  2. 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.
  3. FTSO Data Aggregation: The FTSO aggregates price data from validators and returns the latest price.
  4. 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