forked from QingyangKong/ChainlinkLearningPath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataFeedTask.sol
98 lines (86 loc) · 2.95 KB
/
DataFeedTask.sol
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
/*
* 任务 1:
* 通过 Chainlink Data Feed 获得 link,eth 和 btc 的 usd 价格
* 参考视频教程:https://www.bilibili.com/video/BV1ed4y1N7Uv?p=3
*
* 任务 1 完成标志:
* 1. 通过命令 "yarn hardhat test" 使得单元测试 1-7 通过
* 2. 通过 Remix 在 Ethereum Sepolia 测试网部署,并且测试执行是否如预期
*/
contract DataFeedTask {
AggregatorV3Interface internal linkPriceFeed;
AggregatorV3Interface internal btcPriceFeed;
AggregatorV3Interface internal ethPriceFeed;
address public owner;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
/**
* 步骤 1 - 在构造这里初始化 3 个 Aggregator
*
* 注意:
* 通过 Remix 部署在非本地环境中时
* 通过 https://docs.chain.link/data-feeds/price-feeds/addresses,获得 Aggregator Sepolia 测试网合约地址
* 本地环境中相关参数已经在测试脚本中配置
* */
constructor(
address _linkPriceFeed,
address _btcPriceFeed,
address _ethPriceFeed) {
owner = msg.sender;
//修改以下 solidity 代码
linkPriceFeed = AggregatorV3Interface(_linkPriceFeed);
btcPriceFeed = AggregatorV3Interface(_btcPriceFeed);
ethPriceFeed = AggregatorV3Interface(_ethPriceFeed);
}
/**
* 步骤 2 - 完成 getLinkLatestPrice 函数
* 获得 link/usd 的价格数据
*/
function getLinkLatestPrice() public view returns (int256) {
//在此添加并且修改 solidity 代码
int answer;
( , answer, , , ) = linkPriceFeed.latestRoundData();
return answer;
}
/**
* 步骤 3 - 完成 getBtcLatestPrice 函数
* 获得 btc/usd 的价格数据
*/
function getBtcLatestPrice() public view returns (int256) {
//在此添加并且修改 solidity 代码
int answer;
( , answer, , , ) = btcPriceFeed.latestRoundData();
return answer;
}
/**
* 步骤 4 - 完成 getEthLatestPrice 函数
* 获得 eth/usd 的价格数据
*/
function getEthLatestPrice() public view returns (int256) {
//在此添加并且修改 solidity 代码
int answer;
( , answer, , , ) = ethPriceFeed.latestRoundData();
return answer;
}
/**
* 步骤 5 - 通过 Remix 将合约部署合约
*
* 任务成功标志:
* 合约部署成功
* 获取 link/usd, btc/usd, eth/usd 价格
*/
function getLinkPriceFeed() public view returns (AggregatorV3Interface) {
return linkPriceFeed;
}
function getBtcPriceFeed() public view returns (AggregatorV3Interface) {
return btcPriceFeed;
}
function getEthPriceFeed() public view returns (AggregatorV3Interface) {
return ethPriceFeed;
}
}