FtsoV2Interface
Primary interface for interacting with FTSOv2. This is a long-term support (LTS) interface, designed to ensure continuity even as underlying contracts evolve or protocols migrate to new versions.
Sourced from FtsoV2Interface.sol
on GitHub.
Functions
getFeedById
Returns stored data of a feed. A fee (calculated by the FeeCalculator contract) may need to be paid.
function getFeedById(
bytes21 _feedId
) external payable returns (
uint256 _value,
int8 _decimals,
uint64 _timestamp
);
Parameters
_feedId
: The id of the feed.
Returns
_value
: The value for the requested feed._decimals
: The decimal places for the requested feed._timestamp
: The timestamp of the last update.
Sample contract usage
// 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 FtsoV2FeedConsumerById {
TestFtsoV2Interface internal ftsoV2;
// Example Feed ID for FLR/USD
bytes21 public feedId = bytes21(0x01464c522f55534400000000000000000000000000); // FLR/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 a specific feed by its ID.
* @return _feedValue The latest price value of the feed.
* @return _decimals The decimal precision of the feed value.
* @return _timestamp The timestamp of the last feed update.
*/
function getFtsoV2FeedValueById()
external
payable
returns (
uint256 _feedValue,
int8 _decimals,
uint64 _timestamp
)
{
/* Retrieves the latest value, decimals, and timestamp for the specified feed ID. */
return ftsoV2.getFeedById(feedId);
}
}
getFeedByIdInWei
Returns value in wei and timestamp of a feed. A fee (calculated by the FeeCalculator contract) may need to be paid.
function getFeedByIdInWei(
bytes21 _feedId
) external payable returns (
uint256 _value,
uint64 _timestamp
);
Parameters
_feedId
: The id of the feed.
Returns
_value
: The value for the requested feed in wei (i.e. with 18 decimal places)._timestamp
: The timestamp of the last update.
Sample contract usage
// 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 FtsoV2FeedTracker {
TestFtsoV2Interface internal ftsoV2;
// Feed data structure to store multiple feed values with timestamps
struct FeedInfo {
uint256 valueInWei;
uint64 timestamp;
}
// Mapping to store the latest feed data for a given index
mapping(uint256 => FeedInfo) public feedDataByIndex;
uint256 public highestValueIndex;
uint256 public highestValueInWei;
// Event to track feed retrieval
event FeedFetched(uint256 index, uint256 valueInWei, uint64 timestamp);
/**
* 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 a specific feed by its index, in wei.
* @param _index The index of the feed to retrieve.
* @return _feedValue The latest price value of the feed in wei.
* @return _timestamp The timestamp of the last update for the feed.
*/
function getFeedValueByIndexInWei(uint256 _index)
external
payable
returns (uint256 _feedValue, uint64 _timestamp)
{
// Retrieve feed value and timestamp from the FtsoV2 contract
(_feedValue, _timestamp) = ftsoV2.getFeedByInWei{value: msg.value}(_index);
// Store the feed value and timestamp in the contract's storage
feedDataByIndex[_index] = FeedInfo({
valueInWei: _feedValue,
timestamp: _timestamp
});
// Update the highest value feed if applicable
if (_feedValue > highestValueInWei) {
highestValueInWei = _feedValue;
highestValueIndex = _index;
}
// Emit an event to log the feed retrieval
emit FeedFetched(_index, _feedValue, _timestamp);
}
}
getFeedByIndex
Returns stored data of a feed. A fee (calculated by the FeeCalculator contract) may need to be paid.
function getFeedByIndex(
uint256 _index
) external payable returns (
uint256 _value,
int8 _decimals,
uint64 _timestamp
);
Parameters
_index
: The index of the feed, corresponding to feed id in the FastUpdatesConfiguration contract.
Returns
_value
: The value for the requested feed._decimals
: The decimal places for the requested feed._timestamp
: The timestamp of the last update.
Sample contract usage
// 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 FtsoV2FeedConsumerByIndex {
TestFtsoV2Interface internal ftsoV2;
// Example index for a feed (corresponding to a feed id)
uint256 public feedIndex = 1; // Example: FLR/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 a specific feed by its index.
* @return _feedValue The latest price value of the feed.
* @return _decimals The decimal precision of the feed value.
* @return _timestamp The timestamp of the last feed update.
*/
function getFtsoV2FeedValueByIndex()
external
payable
returns (
uint256 _feedValue,
int8 _decimals,
uint64 _timestamp
)
{
/* Retrieves the latest value, decimals, and timestamp for the specified feed index. */
return ftsoV2.getFeedByIndex(feedIndex);
}
}
getFeedByIndexInWei
Returns value in wei and timestamp of a feed. A fee (calculated by the FeeCalculator contract) may need to be paid.
function getFeedByIndexInWei(
uint256 _index
) external payable returns (
uint256 _value,
uint64 _timestamp
);
Parameters
_index
: The index of the feed, corresponding to feed id in the FastUpdatesConfiguration contract.
Returns
_value
: The value for the requested feed in wei (i.e. with 18 decimal places)._timestamp
: The timestamp of the last update.
Sample contract usage
// 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 FtsoV2FeedTracker {
TestFtsoV2Interface internal ftsoV2;
// Feed data structure to store multiple feed values with timestamps
struct FeedInfo {
uint256 valueInWei;
uint64 timestamp;
}
// Mapping to store the latest feed data for a given index
mapping(uint256 => FeedInfo) public feedDataByIndex;
uint256 public highestValueIndex;
uint256 public highestValueInWei;
// Event to track feed retrieval
event FeedFetched(uint256 index, uint256 valueInWei, uint64 timestamp);
/**
* 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 a specific feed by its index, in wei.
* @param _index The index of the feed to retrieve.
* @return _feedValue The latest price value of the feed in wei.
* @return _timestamp The timestamp of the last update for the feed.
*/
function getFeedValueByIndexInWei(uint256 _index)
external
payable
returns (uint256 _feedValue, uint64 _timestamp)
{
// Retrieve feed value and timestamp from the FtsoV2 contract
(_feedValue, _timestamp) = ftsoV2.getFeedByInWei{value: msg.value}(_index);
// Store the feed value and timestamp in the contract's storage
feedDataByIndex[_index] = FeedInfo({
valueInWei: _feedValue,
timestamp: _timestamp
});
// Update the highest value feed if applicable
if (_feedValue > highestValueInWei) {
highestValueInWei = _feedValue;
highestValueIndex = _index;
}
// Emit an event to log the feed retrieval
emit FeedFetched(_index, _feedValue, _timestamp);
}
}
getFeedId
Returns the feed id at a given index. Removed (unused) feed index will return bytes21(0).
function getFeedId(
uint256 _index
) external view returns (
bytes21 _feedId
);
Parameters
_index
: The index.
Returns
_feedId
: The feed id.
Sample contract usage
// 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 FtsoV2FeedIdManager {
TestFtsoV2Interface internal ftsoV2;
// Mapping to store the feed IDs for each index
mapping(uint256 => bytes21) public feedIdByIndex;
// Mapping to track the number of times each feed index was accessed
mapping(uint256 => uint256) public feedAccessCount;
// State variables to track the most frequently accessed feed
uint256 public mostAccessedFeedIndex;
uint256 public mostAccessedFeedCount;
// Event to track feed ID retrieval
event FeedIdFetched(uint256 indexed index, bytes21 feedId);
/**
* 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 feed ID for a given index and track usage statistics.
* @param _index The index of the feed.
* @return _feedId The feed ID for the given index.
*/
function getFeedIdForIndex(uint256 _index)
external
view
returns (bytes21 _feedId)
{
// Retrieve feed ID from the FtsoV2 contract
_feedId = ftsoV2.getFeedId(_index);
// If the feed ID is not zero (i.e., it is valid), store it and update access count
if (_feedId != bytes21(0)) {
// Update access count for this feed index
feedAccessCount[_index]++;
// Check if this feed index is now the most accessed
if (feedAccessCount[_index] > mostAccessedFeedCount) {
mostAccessedFeedCount = feedAccessCount[_index];
mostAccessedFeedIndex = _index;
}
// Emit an event for external monitoring
emit FeedIdFetched(_index, _feedId);
}
}
/**
* Fetch feed IDs for multiple indices and store them.
* @param _indices An array of feed indices.
* @return _feedIds An array of corresponding feed IDs.
*/
function getMultipleFeedIds(uint256[] calldata _indices)
external
view
returns (bytes21[] memory _feedIds)
{
_feedIds = new bytes21[](_indices.length);
for (uint256 i = 0; i < _indices.length; i++) {
_feedIds[i] = getFeedIdForIndex(_indices[i]);
}
}
/**
* Get the most accessed feed's index and the access count.
* @return _mostAccessedIndex The index of the most accessed feed.
* @return _mostAccessedCount The number of times the most accessed feed has been queried.
*/
function getMostAccessedFeed()
external
view
returns (uint256 _mostAccessedIndex, uint256 _mostAccessedCount)
{
return (mostAccessedFeedIndex, mostAccessedFeedCount);
}
/**
* View the number of times a particular feed has been accessed.
* @param _index The index of the feed.
* @return _accessCount The number of times the feed has been accessed.
*/
function getFeedAccessCount(uint256 _index)
external
view
returns (uint256 _accessCount)
{
return feedAccessCount[_index];
}
}
getFeedIndex
Returns the index of a feed.
function getFeedIndex(
bytes21 _feedId
) external view returns (
uint256 _index
);
Parameters
_feedId
: The feed id.
Returns
_index
: The index of the feed.
getFeedsById
Returns stored data of each feed. A fee (calculated by the FeeCalculator contract) may need to be paid.
function getFeedsById(
bytes21[] _feedIds
) external payable returns (
uint256[] _values,
int8[] _decimals,
uint64 _timestamp
);
Parameters
_feedIds
: The list of feed ids.
Returns
_values
: The list of values for the requested feeds._decimals
: The list of decimal places for the requested feeds._timestamp
: The timestamp of the last update.
Sample contract usage
// 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);
}
}
getFeedsByIdInWei
Returns value of each feed and a timestamp. For some feeds, a fee (calculated by the FeeCalculator contract) may need to be paid.
function getFeedsByIdInWei(
bytes21[] _feedIds
) external payable returns (
uint256[] _values,
uint64 _timestamp
);
Parameters
_feedIds
: Ids of the feeds.
Returns
_values
: The list of values for the requested feeds in wei (i.e. with 18 decimal places)._timestamp
: The timestamp of the last update.
Sample contract usage
// 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
];
// Event to log feed values retrieval
event FeedValuesRetrieved(bytes21[] indexed feedIds, uint256[] values, uint64 timestamp);
/**
* 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 in wei.
* A fee may be required for certain feeds, calculated by the FeeCalculator contract.
* @param _feedIds The IDs of the feeds to retrieve values for.
* @return _values The list of values for the requested feeds in wei.
* @return _timestamp The timestamp of the last update.
*/
function getFeedsByIdInWei(bytes21[] calldata _feedIds)
external
payable
returns (uint256[] memory _values, uint64 _timestamp)
{
// Ensure that the length of the feed IDs is non-zero
require(_feedIds.length > 0, "No feed IDs provided");
// Retrieve the feed values and timestamp from the FTSOv2 contract
(_values, _timestamp) = ftsoV2.getFeedsByIdInWei(_feedIds);
// Emit an event to log the retrieved feed values
emit FeedValuesRetrieved(_feedIds, _values, _timestamp);
}
}
getFeedsByIndex
Returns stored data of each feed. A fee (calculated by the FeeCalculator contract) may need to be paid.
function getFeedsByIndex(
uint256[] _indices
) external payable returns (
uint256[] _values,
int8[] _decimals,
uint64 _timestamp
);
Parameters
_indices
: Indices of the feeds, corresponding to feed ids in the FastUpdatesConfiguration contract.
Returns
_values
: The list of values for the requested feeds._decimals
: The list of decimal places for the requested feeds._timestamp
: The timestamp of the last update.
Sample contract usage
// 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;
// Event to log feed retrievals
event FeedsRetrieved(uint256[] indices, uint256[] values, int8[] decimals, uint64 timestamp);
/**
* 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 values and decimals of feeds by their indices.
* A fee may be required for certain feeds, calculated by the FeeCalculator contract.
* @param _indices The indices of the feeds to retrieve values for.
* @return _values The list of values for the requested feeds.
* @return _decimals The list of decimal places for the requested feeds.
* @return _timestamp The timestamp of the last update.
*/
function getFeedsByIndex(uint256[] calldata _indices)
external
payable
returns (uint256[] memory _values, int8[] memory _decimals, uint64 _timestamp)
{
// Ensure that the length of the indices is non-zero
require(_indices.length > 0, "No indices provided");
// Retrieve the feed values, decimals, and timestamp from the FTSOv2 contract
(_values, _decimals, _timestamp) = ftsoV2.getFeedsByIndex(_indices);
// Emit an event to log the retrieved feed values
emit FeedsRetrieved(_indices, _values, _decimals, _timestamp);
}
/**
* Get the current feed values for a specific index.
* This function is helpful to verify values before making a payment for feeds.
*/
function previewFeedValues(uint256[] calldata _indices)
external
view
returns (uint256[] memory _values, int8[] memory _decimals)
{
// Ensure that the length of the indices is non-zero
require(_indices.length > 0, "No indices provided");
// Call the FTSOv2 contract to get the feed values and decimals
(_values, _decimals) = ftsoV2.getFeedsByIndex(_indices);
}
}
getFeedsByIndexInWei
Returns value in wei of each feed and a timestamp. For some feeds, a fee (calculated by the FeeCalculator contract) may need to be paid.
function getFeedsByIndexInWei(
uint256[] _indices
) external payable returns (
uint256[] _values,
uint64 _timestamp
);
Parameters
_indices
: Indices of the feeds, corresponding to feed ids in the FastUpdatesConfiguration contract.
Returns
_values
: The list of values for the requested feeds in wei (i.e. with 18 decimal places)._timestamp
: The timestamp of the last update.
Sample contract usage
// 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;
// Fee percentage that will be deducted from the payment
uint256 public feePercentage = 1; // 1%
/**
* 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 values of feeds in wei by their indices.
* A fee may need to be paid for certain feeds, calculated by the FeeCalculator contract.
* @param _indices The indices of the feeds to retrieve values for.
* @return _values The list of values for the requested feeds in wei.
* @return _timestamp The timestamp of the last update.
*/
function getFeedsByIndexInWei(uint256[] calldata _indices)
external
payable
returns (uint256[] memory _values, uint64 _timestamp)
{
// Ensure that the length of the indices is non-zero
require(_indices.length > 0, "No indices provided");
// Calculate total fee based on the number of indices
uint256 totalFee = calculateFee(_indices.length);
require(msg.value >= totalFee, "Insufficient fee paid");
// Retrieve the feed values and timestamp from the FTSOv2 contract
(_values, _timestamp) = ftsoV2.getFeedsByIndexInWei(_indices);
// Refund excess Ether paid (if any)
if (msg.value > totalFee) {
payable(msg.sender).transfer(msg.value - totalFee);
}
}
/**
* Calculate the fee based on the number of feeds requested.
* @param _numFeeds The number of feeds requested.
* @return The calculated fee in wei.
*/
function calculateFee(uint256 _numFeeds) internal view returns (uint256) {
return (_numFeeds * 1 ether * feePercentage) / 100; // Example: 1% of the total requested amount
}
}
verifyFeedData
Checks if the feed data is valid (i.e. is part of the confirmed Merkle tree).
function verifyFeedData(
struct FtsoV2Interface.FeedDataWithProof _feedData
) external view returns (
bool
);
Parameters
_feedData
: Structure containing data about the feed (FeedData structure) and Merkle proof.
Returns
_0
: true if the feed data is valid.
Sample contract usage
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;
import {ContractRegistry} from "@flarenetwork/flare-periphery-contracts/coston2/ContractRegistry.sol";
import {FtsoV2Interface} from "@flarenetwork/flare-periphery-contracts/coston2/FtsoV2Interface.sol";
import {MerkleProof} from "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
/**
* @title FtsoV2FeedVerifier
* @notice A contract to verify the validity of feed data against a confirmed Merkle tree.
*/
contract FtsoV2FeedVerifier {
FtsoV2Interface internal ftsoV2;
/**
* Constructor initializes the FTSOv2 contract.
*/
constructor() {
// THIS IS A TEST METHOD, in production use: ftsoV2 = ContractRegistry.getFtsoV2();
ftsoV2 = ContractRegistry.getTestFtsoV2();
}
/**
* @notice Verify if the provided feed data is valid based on the Merkle proof.
* @param _feedData The FeedDataWithProof structure containing feed data and proof.
* @return isValid Returns true if the feed data is valid, false otherwise.
*/
function verifyFeedData(FtsoV2Interface.FeedDataWithProof calldata _feedData)
external
view
returns (bool isValid)
{
// Pack the relevant feed data fields into a hash
bytes32 leaf = keccak256(abi.encodePacked(
_feedData.body.votingRoundId,
_feedData.body.id,
_feedData.body.value,
_feedData.body.turnoutBIPS,
_feedData.body.decimals
));
// Verify the Merkle proof against the leaf and root
isValid = MerkleProof.verify(_feedData.proof, _feedData.merkleRoot, leaf);
}
}
Structures
FeedData
Feed data structure
struct FeedData {
uint32 votingRoundId;
bytes21 id;
int32 value;
uint16 turnoutBIPS;
int8 decimals;
}
FeedDataWithProof
Feed data with proof structure
struct FeedDataWithProof {
bytes32[] proof;
struct FtsoV2Interface.FeedData body;
}