forked from flare-foundation/developer-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_and_verify_anchor_onchain.js
40 lines (33 loc) · 1.16 KB
/
fetch_and_verify_anchor_onchain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// THIS IS EXAMPLE CODE. DO NOT USE THIS CODE IN PRODUCTION.
import { artifacts } from "hardhat";
import { fetchAnchorFeeds } from "./fetch_anchor_feeds";
const FtsoV2AnchorFeedConsumer = artifacts.require(
"FtsoV2AnchorFeedConsumer.sol",
);
// Feed IDs, see https://dev.flare.network/ftso/scaling/anchor-feeds for full list
const BTC_USD_FEED_ID = "0x014254432f55534400000000000000000000000000";
const TARGET_VOTING_ROUND = 823402;
async function main() {
// Deploy FtsoV2AnchorFeedConsumer contract
const feedConsumer = await FtsoV2AnchorFeedConsumer.new();
// Fetch price from DA Layer
const feedData = await fetchAnchorFeeds(
[BTC_USD_FEED_ID],
TARGET_VOTING_ROUND,
);
// Save fetched price to contract
await feedConsumer.savePrice({
proof: feedData[0].proof,
body: feedData[0].data,
});
// Query saved price from contract
const savedPrice = await feedConsumer.provenFeeds.call(
TARGET_VOTING_ROUND,
BTC_USD_FEED_ID,
);
const formattedPrice = savedPrice.value * Math.pow(10, -savedPrice.decimals);
console.log(
`Saved price: ${formattedPrice} USD at voting round: ${savedPrice.votingRoundId.toString()}`,
);
}
main();