Skip to content

Commit 731ac68

Browse files
committed
fic(docs): use getFeedByIdInWei properly
1 parent b52885f commit 731ac68

File tree

3 files changed

+48
-41
lines changed

3 files changed

+48
-41
lines changed

docs/ftso/2-feeds.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The resulting string is then prefixed with `0x`.
3232
- Feed IDs are **not** addresses. They are `bytes21` structured encodings that combine the category and feed name to ensure each feed has a unique identifier.
3333
- **Do not** hardcode the number of decimals for a feed in your smart contract, as these can change as the feed value changes. You can use either of the following solutions:
3434
- Check the number of decimal places every query ([`getFeedById`](/ftso/solidity-reference/FtsoV2Interface#getfeedbyid)).
35-
- Use the feed value in Wei ([`getFeedsByIdInWei`](/ftso/solidity-reference/FtsoV2Interface#getfeedsbyidinwei)).
35+
- Use the feed value in Wei ([`getFeedByIdInWei`](/ftso/solidity-reference/FtsoV2Interface#getfeedbyidinwei)).
3636

3737
:::
3838

docs/ftso/solidity-reference/FtsoV2Interface.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Remix from "@site/src/components/remix";
88
import CodeBlock from "@theme/CodeBlock";
99
import FTSOV2FeedById from "!!raw-loader!/examples/developer-hub-solidity/FTSOV2FeedById.sol";
1010
import FTSOV2FeedByIndex from "!!raw-loader!/examples/developer-hub-solidity/FTSOV2FeedByIndex.sol";
11+
import FTSOV2FeedByIdWei from "!!raw-loader!/examples/developer-hub-solidity/FTSOV2FeedByIdWei.sol";
1112
import FTSOV2FeedsById from "!!raw-loader!/examples/developer-hub-solidity/FTSOV2FeedsById.sol";
1213
import FTSOV2FeedsByIdWei from "!!raw-loader!/examples/developer-hub-solidity/FTSOV2FeedsByIdWei.sol";
1314
import FTSOV2FeedsByIndexWei from "!!raw-loader!/examples/developer-hub-solidity/FTSOV2FeedsByIndexWei.sol";
@@ -56,6 +57,41 @@ function getFeedById(
5657
<Remix fileName="FTSOV2FeedById.sol">Open sample in Remix</Remix>
5758
<br></br>
5859

60+
### getFeedByIdInWei
61+
62+
Returns value in wei and timestamp of a feed.
63+
A fee (calculated by the FeeCalculator contract) may need to be paid.
64+
65+
```solidity
66+
function getFeedByIdInWei(
67+
bytes21 _feedId
68+
) external payable returns (
69+
uint256 _value,
70+
uint64 _timestamp
71+
);
72+
```
73+
74+
#### Parameters
75+
76+
- `_feedId`: The id of the feed.
77+
78+
#### Returns
79+
80+
- `_value`: The value for the requested feed in wei (i.e. with 18 decimal places).
81+
- `_timestamp`: The timestamp of the last update.
82+
83+
<details>
84+
<summary>Sample contract usage</summary>
85+
86+
<CodeBlock language="solidity" title="FTSOV2FeedByIdWei.sol">
87+
{FTSOV2FeedByIdWei}
88+
</CodeBlock>
89+
90+
</details>
91+
92+
<Remix fileName="FTSOV2FeedByIdWei.sol">Open sample in Remix</Remix>
93+
<br></br>
94+
5995
### getFeedByIndex
6096

6197
Returns stored data of a feed.

examples/developer-hub-solidity/FTSOV2FeedByIdWei.sol

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,29 @@ import {TestFtsoV2Interface} from "@flarenetwork/flare-periphery-contracts/costo
1111
*/
1212
contract FtsoV2FeedTracker {
1313
TestFtsoV2Interface internal ftsoV2;
14-
15-
// Feed data structure to store multiple feed values with timestamps
16-
struct FeedInfo {
17-
uint256 valueInWei;
18-
uint64 timestamp;
19-
}
20-
21-
// Mapping to store the latest feed data for a given index
22-
mapping(uint256 => FeedInfo) public feedDataByIndex;
23-
uint256 public highestValueIndex;
24-
uint256 public highestValueInWei;
14+
bytes21 public feedId =
15+
bytes21(0x01464c522f55534400000000000000000000000000); // FLR/USD
2516

2617
// Event to track feed retrieval
27-
event FeedFetched(uint256 index, uint256 valueInWei, uint64 timestamp);
28-
29-
/**
30-
* Constructor initializes the FTSOv2 contract.
31-
* The contract registry is used to fetch the FtsoV2 contract address.
32-
*/
33-
constructor() {
34-
/* THIS IS A TEST METHOD, in production use: ftsoV2 = ContractRegistry.getFtsoV2(); */
35-
ftsoV2 = ContractRegistry.getTestFtsoV2();
36-
}
18+
event FeedFetched(bytes21 feedId, uint256 valueInWei, uint64 timestamp);
3719

3820
/**
3921
* Get the current value of a specific feed by its index, in wei.
40-
* @param _index The index of the feed to retrieve.
4122
* @return _feedValue The latest price value of the feed in wei.
4223
* @return _timestamp The timestamp of the last update for the feed.
4324
*/
44-
function getFeedValueByIndexInWei(uint256 _index)
45-
external
46-
payable
47-
returns (uint256 _feedValue, uint64 _timestamp)
25+
function getFeedValueByIdWei()
26+
external
27+
payable
28+
returns (uint256 _feedValue, uint64 _timestamp)
4829
{
30+
/* THIS IS A TEST METHOD, in production use: ftsoV2 = ContractRegistry.getFtsoV2(); */
31+
ftsoV2 = ContractRegistry.getTestFtsoV2();
4932
// Retrieve feed value and timestamp from the FtsoV2 contract
50-
(_feedValue, _timestamp) = ftsoV2.getFeedByInWei{value: msg.value}(_index);
51-
52-
// Store the feed value and timestamp in the contract's storage
53-
feedDataByIndex[_index] = FeedInfo({
54-
valueInWei: _feedValue,
55-
timestamp: _timestamp
56-
});
57-
58-
// Update the highest value feed if applicable
59-
if (_feedValue > highestValueInWei) {
60-
highestValueInWei = _feedValue;
61-
highestValueIndex = _index;
62-
}
33+
(_feedValue, _timestamp) = ftsoV2.getFeedByIdInWei(feedId);
6334

6435
// Emit an event to log the feed retrieval
65-
emit FeedFetched(_index, _feedValue, _timestamp);
36+
emit FeedFetched(feedId, _feedValue, _timestamp);
6637
}
6738

6839
}

0 commit comments

Comments
 (0)