|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity >=0.8.0 <0.9.0; |
| 3 | + |
| 4 | +import {FtsoV2Interface} from "@flarenetwork/flare-periphery-contracts/coston2/FtsoV2Interface.sol"; |
| 5 | +import {ContractRegistry} from "@flarenetwork/flare-periphery-contracts/coston2/ContractRegistry.sol"; |
| 6 | +import {TestFtsoV2Interface} from "@flarenetwork/flare-periphery-contracts/coston2/TestFtsoV2Interface.sol"; |
| 7 | + |
| 8 | +/** |
| 9 | + * THIS IS AN EXAMPLE CONTRACT. |
| 10 | + * DO NOT USE THIS CODE IN PRODUCTION. |
| 11 | + */ |
| 12 | +contract FtsoV2AnchorFeedConsumer { |
| 13 | + bytes21 private constant FLR_USD_ID = |
| 14 | + 0x01464c522f55534400000000000000000000000000; |
| 15 | + bytes21 private constant BTC_USD_ID = |
| 16 | + 0x014254432f55534400000000000000000000000000; |
| 17 | + bytes21 private constant ETH_USD_ID = |
| 18 | + 0x014554482f55534400000000000000000000000000; |
| 19 | + mapping(uint32 => mapping(bytes21 => TestFtsoV2Interface.FeedData)) |
| 20 | + public provenFeeds; |
| 21 | + |
| 22 | + // Track which feeds have been proven per round for easy enumeration |
| 23 | + mapping(uint32 => bytes21[]) private _provenFeedIdsByRound; |
| 24 | + mapping(uint32 => mapping(bytes21 => bool)) private _isProven; |
| 25 | + |
| 26 | + event FeedProven( |
| 27 | + uint32 indexed votingRoundId, |
| 28 | + bytes21 indexed id, |
| 29 | + int32 value, |
| 30 | + uint16 turnoutBIPS, |
| 31 | + int8 decimals |
| 32 | + ); |
| 33 | + |
| 34 | + function savePrice( |
| 35 | + TestFtsoV2Interface.FeedDataWithProof calldata data |
| 36 | + ) external { |
| 37 | + /* THIS IS A TEST METHOD, in production use: ftsoV2 = ContractRegistry.getFtsoV2(); */ |
| 38 | + TestFtsoV2Interface ftsoV2 = ContractRegistry.getTestFtsoV2(); |
| 39 | + // Step 1: Verify the proof |
| 40 | + require(ftsoV2.verifyFeedData(data), "Invalid proof"); |
| 41 | + |
| 42 | + // Step 2: Ensure the proof is for the desired feedId to avoid manipulation |
| 43 | + require( |
| 44 | + data.body.id == FLR_USD_ID || |
| 45 | + data.body.id == BTC_USD_ID || |
| 46 | + data.body.id == ETH_USD_ID, |
| 47 | + "Proof is not for desired feedId" |
| 48 | + ); |
| 49 | + // Step 3: Use the feed data with app specific logic |
| 50 | + // Here the feed data is saved |
| 51 | + uint32 roundId = data.body.votingRoundId; |
| 52 | + bytes21 id = data.body.id; |
| 53 | + provenFeeds[roundId][id] = data.body; |
| 54 | + |
| 55 | + // Record id for enumeration if first time proven in this round |
| 56 | + if (!_isProven[roundId][id]) { |
| 57 | + _isProven[roundId][id] = true; |
| 58 | + _provenFeedIdsByRound[roundId].push(id); |
| 59 | + } |
| 60 | + |
| 61 | + emit FeedProven( |
| 62 | + roundId, |
| 63 | + id, |
| 64 | + data.body.value, |
| 65 | + data.body.turnoutBIPS, |
| 66 | + data.body.decimals |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + // Returns whether a given feed id has been proven for the round |
| 71 | + function isProven( |
| 72 | + uint32 votingRoundId, |
| 73 | + bytes21 id |
| 74 | + ) external view returns (bool) { |
| 75 | + return _isProven[votingRoundId][id]; |
| 76 | + } |
| 77 | + |
| 78 | + // Returns all feed ids proven for a voting round |
| 79 | + function getProvenFeedIds( |
| 80 | + uint32 votingRoundId |
| 81 | + ) external view returns (bytes21[] memory) { |
| 82 | + return _provenFeedIdsByRound[votingRoundId]; |
| 83 | + } |
| 84 | + |
| 85 | + // Returns all proven feeds (full FeedData) for a voting round |
| 86 | + function getProvenFeeds( |
| 87 | + uint32 votingRoundId |
| 88 | + ) external view returns (TestFtsoV2Interface.FeedData[] memory) { |
| 89 | + bytes21[] memory ids = _provenFeedIdsByRound[votingRoundId]; |
| 90 | + TestFtsoV2Interface.FeedData[] |
| 91 | + memory out = new TestFtsoV2Interface.FeedData[](ids.length); |
| 92 | + for (uint256 i = 0; i < ids.length; i++) { |
| 93 | + out[i] = provenFeeds[votingRoundId][ids[i]]; |
| 94 | + } |
| 95 | + return out; |
| 96 | + } |
| 97 | +} |
0 commit comments