| slug | title | tags | description | keywords | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
adapters |
Migrate an app to FTSO |
|
Migrate your dApp from other oracles to FTSO. |
|
import CodeBlock from "@theme/CodeBlock"; import ChainlinkExample from "!!raw-loader!/examples/developer-hub-solidity/ChainlinkExample.sol"; import chainlinkExample from "!!raw-loader!/examples/developer-hub-javascript/chainlinkExample.ts"; import PythExample from "!!raw-loader!/examples/developer-hub-solidity/PythExample.sol"; import pythExample from "!!raw-loader!/examples/developer-hub-javascript/pythExample.ts"; import Api3Example from "!!raw-loader!/examples/developer-hub-solidity/Api3Example.sol"; import api3Example from "!!raw-loader!/examples/developer-hub-javascript/api3Example.ts"; import BandExample from "!!raw-loader!/examples/developer-hub-solidity/BandExample.sol"; import bandExample from "!!raw-loader!/examples/developer-hub-javascript/bandExample.ts"; import ChronicleExample from "!!raw-loader!/examples/developer-hub-solidity/ChronicleExample.sol"; import chronicleExample from "!!raw-loader!/examples/developer-hub-javascript/chronicleExample.ts";
FTSO Adapters, provided by the @flarenetwork/ftso-adapters library, allow decentralized applications (dApps) built for other popular oracle interfaces to integrate with Flare's FTSO with minimal code changes.
The library provides adapters for Pyth, Chainlink, API3, Band Protocol, and Chronicle.
These adapters act as a compatibility layer, translating the FTSO's data structure into the format expected by each respective oracle's interface.
This enables a seamless migration path for projects looking to leverage the speed, decentralization, and cost-effectiveness of Flare's native oracle. This guide focuses on the specific code modifications required to migrate your existing dApp.
All code examples can be found in our hardhat-starter-kit.
Migrating to a Flare FTSO adapter requires a different approach to handling oracle data. Instead of your contract calling an external oracle, it will now manage price data internally by using an adapter library. This process involves two main changes: modifying your smart contract and setting up a new offchain keeper process.
The main changes happen within your smart contract. You will modify it to store, update, and serve the FTSO price data itself.
-
State Variables: Instead of storing an address to an external oracle, you add state variables to your contract to manage the FTSO feed and cache the price data.
- Before:
AggregatorV3Interface internal dataFeed; - After:
bytes21 public immutable ftsoFeedId; FtsoChainlinkAdapterLibrary.Round private _latestPriceData;
- Before:
-
Constructor: Your constructor no longer needs an oracle's address. Instead, it takes FTSO-specific information, such as the
ftsoFeedIdand any other parameters the adapter needs (likechainlinkDecimals).- Before:
constructor(address _dataFeedAddress) { dataFeed = AggregatorV3Interface(_dataFeedAddress); } - After:
constructor(bytes21 _ftsoFeedId, uint8 _chainlinkDecimals) { ftsoFeedId = _ftsoFeedId; chainlinkDecimals = _chainlinkDecimals; }
- Before:
-
Implement
refresh(): You must add a publicrefresh()function. This function's only job is to call the adapter library'srefreshlogic, which updates your contract's state variables with the latest FTSO price. -
Implement the Oracle Interface: You then add the standard
viewfunction for the oracle you are migrating from (e.g.,latestRoundData()for Chainlink). This function calls the corresponding logic from the adapter library, reading directly from your contract's cached state. -
No Change to Core Logic: Your dApp's internal logic that uses the price data remains unchanged. It continues to call the same standard oracle functions as before (e.g.,
latestRoundData()), but now it's calling a function implemented directly within your own contract.
Since your contract now manages its own price updates, you need an external process to trigger them.
- Create a Keeper Script: This is a simple script that connects to the network and periodically calls the public
refresh()function on your deployed contract. - Run the Keeper: This script ensures the price cached in your contract stays fresh. It replaces the need to rely on the oracle provider's keepers, giving you direct control over how often your prices are updated and how much you spend on gas.
The FtsoChainlinkAdapter implements Chainlink's AggregatorV3Interface. The example is an AssetVault contract that uses the FTSO price to value collateral for borrowing and lending.
The FtsoPythAdapter implements Pyth's IPyth interface. The example is a PythNftMinter contract that dynamically calculates a $1 minting fee based on the live FTSO price.
The FtsoApi3Adapter implements the IApi3ReaderProxy interface. The example is a PriceGuesser prediction market that uses the FTSO price to settle bets.
The FtsoBandAdapter implements Band Protocol's IStdReference interface. The example is a PriceTriggeredSafe that locks withdrawals during high market volatility, detected by checking a basket of FTSO prices.
The FtsoChronicleAdapter implements the IChronicle interface. The example is a DynamicNftMinter that mints NFTs of different tiers based on the live FTSO asset price.