Skip to main content

Getting Started

You can use FTSOv2 to connect your smart contracts with real-world data feeds. FTSOv2 leverages Flare's network of 100 independent data providers to fetch offchain data and deliver it onchain. This section demonstrates how to consume FTSOv2's block-latency feeds on Flare using an onchain Solidity contract.

New to smart contract development?

Learn how to deploy your first smart contract before you start this guide.

Sample contract

This example smart contract queries the latest feed values for FLR/USD, BTC/USD, and ETH/USD from FTSOv2 on Flare Testnet Coston2.

FtsoV2FeedConsumer.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

import {ContractRegistry} from "@flarenetwork/flare-periphery-contracts/coston2/ContractRegistry.sol";
/* THIS IS A TEST IMPORT, in production use: import {FtsoV2Interface} from "@flarenetwork/flare-periphery-contracts/coston2/FtsoV2Interface.sol"; */
import {TestFtsoV2Interface} from "@flarenetwork/flare-periphery-contracts/coston2/TestFtsoV2Interface.sol";

/**
* THIS IS AN EXAMPLE CONTRACT.
* DO NOT USE THIS CODE IN PRODUCTION.
*/
contract FtsoV2FeedConsumer {
TestFtsoV2Interface internal ftsoV2;
// Feed IDs, see https://dev.flare.network/ftso/feeds for full list
bytes21[] public feedIds = [
bytes21(0x01464c522f55534400000000000000000000000000), // FLR/USD
bytes21(0x014254432f55534400000000000000000000000000), // BTC/USD
bytes21(0x014554482f55534400000000000000000000000000) // ETH/USD
];

/**
* Constructor initializes the FTSOv2 contract.
* The contract registry is used to fetch the FtsoV2 contract address.
*/
constructor() {
/* THIS IS A TEST METHOD, in production use: ftsoV2 = ContractRegistry.getFtsoV2(); */
ftsoV2 = ContractRegistry.getTestFtsoV2();
}

/**
* Get the current value of the feeds.
*/
function getFtsoV2CurrentFeedValues()
external
view
returns (
uint256[] memory _feedValues,
int8[] memory _decimals,
uint64 _timestamp
)
{
/* Your custom feed consumption logic. In this example the values are just returned. */
return ftsoV2.getFeedsById(feedIds);
}
}

Don't use test interfaces in production

The TestFtsoV2Interface is for testing only, with all methods as view to allow rapid development without gas costs or state changes. For production, use FtsoV2Interface, which includes payable methods required for real transactions and state modifications.

Make the following changes to FtsoV2FeedConsumer.sol:

- import {TestFtsoV2Interface} from "@flarenetwork/flare-periphery-contracts/coston2/TestFtsoV2Interface.sol";
+ import {FtsoV2Interface} from "@flarenetwork/flare-periphery-contracts/coston2/FtsoV2Interface.sol";

contract FtsoV2FeedConsumer {
constructor() {
- ftsoV2 = ContractRegistry.getTestFtsoV2();
+ ftsoV2 = ContractRegistry.getFtsoV2();
}
}
Breaking down the contract.
  • Purpose: Interacts with the Flare Network to fetch current feed values for specific cryptocurrency pairs (FLR/USD, BTC/USD, ETH/USD).

  • Dependencies:

    • ContractRegistry.sol: Used to get the addresses of various contracts on the Flare network.
    • TestFtsoV2Interface.sol: This interface allows interaction with the FTSOv2 contract, which provides real-time price feeds for various assets.
  • State Variables:

    • ftsoV2: This is a state variable of type TestFtsoV2Interface. It will hold the address of the FTSO V2 contract once initialized.
    • feedIndexes: An array of indexes corresponding to different price feeds. In this example:
      • Index 0 corresponds to FLR/USD
      • Index 2 corresponds to BTC/USD
      • Index 9 corresponds to ETH/USD
  • Constructor:

    • The constructor is a special function that runs only once when the contract is deployed. It initializes the ftsoV2 state variable by fetching the FTSO V2 contract address using the ContractRegistry.
  • Function getFtsoV2CurrentFeedValues:

    • It is marked as external, meaning it can be called from outside the contract.
    • It is also marked as payable, allowing it to receive Ether when called, though in this example, the Ether is not used.
    • It returns three values:
      • _feedValues: The latest price values for the specified feeds.
      • _decimals: The decimal precision of each feed value.
      • _timestamp: The timestamp when the prices were fetched.

    Inside the function:

    • The ftsoV2.getFeedsByIndex(feedIndexes) call retrieves the latest prices for the indexes specified in feedIndexes.
    • These values are then returned to the caller.

Compile and deploy the contract

Configure and fund your wallet

If you have not already configured your MetaMask wallet to support Flare Testnet Coston2 and funded it with testnet C2FLR, learn how to deploy your first smart contract. You can get testnet C2FLR from the Coston2 Faucet.

  1. Click on FtsoV2FeedConsumer.sol in the file explorer to open the contract in the editor.

  2. On the left side of Remix, click the Solidity Compiler tab to view the compiler settings.

  3. Expand the Advanced Configurations section and make sure the EVM Version is set to london.

  4. Click the Compile FtsoV2FeedConsumer.sol button to compile the contract.

  5. On the left side of Remix, click the Deploy & Run transactions tab to view the deployment settings.

  6. Select the Injected Provider - MetaMask environment.

  7. Click Deploy to deploy the contract to Flare Testnet Coston2. MetaMask opens and asks you to confirm payment for deploying the contract. Make sure MetaMask is set to Flare Testnet Coston2 before you confirm the transaction.

  8. In the MetaMask prompt, click Confirm to approve the transaction and spend your testnet C2FLR required to deploy the contract.

  9. After a few seconds, the transaction completes and your contract appears under the Deployed/Unpinned Contracts list in Remix. Click the contract dropdown to view its variables and functions. Click on getFtsoV2CurrentFeedValues to show the latest feed values and decimals.

Function Returns

  • _feedValues: Current integer values of FLR/USD, BTC/USD, and ETH/USD. The returns are in the same order as the input feedIndexes array.
  • _decimals: Decimal places for FLR/USD, BTC/USD, and ETH/USD.
  • _timestamp: Timestamp of the last feed update.

The floating point price of a feed can be calculated by dividing the feedValue by 10^decimals. For example, if the feed value of BTC/USD is 6900420 and the decimal is 2, the floating point feed value is 69004.20.

What's next?

Build your first FTSOv2 app using Foundry, or read feeds offchain in languages like JavaScript, Python, Rust, and Go.