Skip to content

Commit f57248d

Browse files
authored
docs(ftso): clarify on-chain anchor verification (#815)
2 parents cb6e0f3 + 0a9f2e0 commit f57248d

File tree

3 files changed

+101
-53
lines changed

3 files changed

+101
-53
lines changed

docs/ftso/solidity-reference/FtsoV2Interface.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import FTSOV2FeedById from "!!raw-loader!/examples/developer-hub-solidity/FTSOV2
1010
import FTSOV2FeedByIdWei from "!!raw-loader!/examples/developer-hub-solidity/FTSOV2FeedByIdWei.sol";
1111
import FTSOV2FeedsById from "!!raw-loader!/examples/developer-hub-solidity/FTSOV2FeedsById.sol";
1212
import FTSOV2FeedsByIdWei from "!!raw-loader!/examples/developer-hub-solidity/FTSOV2FeedsByIdWei.sol";
13-
import FTSOV2VerifyProof from "!!raw-loader!/examples/developer-hub-solidity/FTSOV2VerifyProof.sol";
13+
import FTSOV2AnchorConsumer from "!!raw-loader!/examples/developer-hub-solidity/FTSOV2AnchorConsumer.sol";
1414

1515
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.
1616

@@ -181,13 +181,13 @@ function verifyFeedData(
181181
<details>
182182
<summary>Sample contract usage</summary>
183183

184-
<CodeBlock language="solidity" title="FTSOV2VerifyProof.sol">
185-
{FTSOV2VerifyProof}
184+
<CodeBlock language="solidity" title="FTSOV2AnchorConsumer.sol">
185+
{FTSOV2AnchorConsumer}
186186
</CodeBlock>
187187

188188
</details>
189189

190-
<Remix fileName="FTSOV2VerifyProof.sol">Open sample in Remix</Remix>
190+
<Remix fileName="FTSOV2AnchorConsumer.sol">Open sample in Remix</Remix>
191191

192192
## Structures
193193

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

examples/developer-hub-solidity/FTSOV2VerifyProof.sol

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)