-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1212 from cheng521521/main
fix: upgrade etherjs and hardhat to latest version
- Loading branch information
Showing
8 changed files
with
81 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
PRIVATE_KEY=xxxxxxxxxxxxxxxx | ||
INFURA_ID=yyyyyyyy | ||
SubscriptionId=ddddd | ||
PRIVATE_KEY=xxxx | ||
PUBLIC_ADDRESS=xxxx | ||
MNEMONIC=xxxx | ||
API_KEY=xxxx | ||
SubscriptionId=xxxx |
47 changes: 16 additions & 31 deletions
47
basic/14-chainlink-price-feed/contracts/PriceConsumerV3.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,33 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.7; | ||
|
||
import {AggregatorV3Interface} from '@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol'; | ||
|
||
/** | ||
* THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED | ||
* VALUES FOR CLARITY. | ||
* THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE. | ||
* DO NOT USE THIS CODE IN PRODUCTION. | ||
*/ | ||
|
||
/** | ||
* If you are reading data feeds on L2 networks, you must | ||
* check the latest answer from the L2 Sequencer Uptime | ||
* Feed to ensure that the data is accurate in the event | ||
* of an L2 sequencer outage. See the | ||
* https://docs.chain.link/data-feeds/l2-sequencer-feeds | ||
* page for details. | ||
*/ | ||
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol"; | ||
|
||
contract PriceConsumerV3 { | ||
AggregatorV3Interface internal dataFeed; | ||
|
||
AggregatorV3Interface internal priceFeed; | ||
|
||
/** | ||
* Network: Sepolia | ||
* Aggregator: BTC/USD | ||
* Address: 0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43 | ||
* Aggregator: ETH/USD | ||
* Address: 0x694AA1769357215DE4FAC081bf1f309aDC325306 | ||
*/ | ||
constructor(address _priceFeed) { | ||
dataFeed = AggregatorV3Interface(_priceFeed); | ||
priceFeed = AggregatorV3Interface(_priceFeed); | ||
} | ||
|
||
/** | ||
* Returns the latest answer. | ||
* Returns the latest price | ||
*/ | ||
function getChainlinkDataFeedLatestAnswer() public view returns (int) { | ||
// prettier-ignore | ||
function getLatestPrice() public view returns (int) { | ||
( | ||
/* uint80 roundID */, | ||
int answer, | ||
/*uint startedAt*/, | ||
/*uint timeStamp*/, | ||
/*uint80 answeredInRound*/ | ||
) = dataFeed.latestRoundData(); | ||
return answer; | ||
uint80 roundID, | ||
int price, | ||
uint startedAt, | ||
uint timeStamp, | ||
uint80 answeredInRound | ||
) = priceFeed.latestRoundData(); | ||
return price; | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
basic/14-chainlink-price-feed/contracts/RandomNumberConsumer.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 22 additions & 25 deletions
47
basic/14-chainlink-price-feed/scripts/01-PriceConsumerV3Deploy.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,37 @@ | ||
const hre = require("hardhat"); | ||
require("@nomiclabs/hardhat-web3"); | ||
const ethers = hre.ethers; | ||
|
||
async function main () { | ||
async function main() { | ||
const singer = await ethers.getSigner(process.env.PUBLIC_ADDRESS); | ||
console.log("Deploying contracts with the account:", singer.address); | ||
|
||
const [deployer] = await ethers.getSigners(); | ||
// PRICE_FEED_CONTRACT 在 sepolia 上的合约地址 | ||
const PRICE_FEED_CONTRACT = "0x694AA1769357215DE4FAC081bf1f309aDC325306"; | ||
|
||
console.log( | ||
"Deploying contracts with the account:", | ||
deployer.address | ||
); | ||
// deploy PriceConsumerV3 Contract | ||
const PriceConsumerV3 = await ethers.getContractFactory("PriceConsumerV3"); | ||
const PriceConsumerV3Ins = await PriceConsumerV3.deploy(PRICE_FEED_CONTRACT); | ||
await PriceConsumerV3Ins.waitForDeployment(); | ||
|
||
// PRICE_FEED_CONTRACT 在 sepolia 上的合约地址 | ||
const PRICE_FEED_CONTRACT = "0x694AA1769357215DE4FAC081bf1f309aDC325306"; | ||
console.log("----------------------------------------------------"); | ||
console.log("PriceConsumerV3 address:", await PriceConsumerV3Ins.getAddress()); | ||
|
||
// 部署 PriceConsumerV3 合约 | ||
const priceConsumerV3 = await ethers.getContractFactory("PriceConsumerV3"); | ||
const PriceConsumerV3Ins = await priceConsumerV3.deploy(PRICE_FEED_CONTRACT); | ||
|
||
console.log("----------------------------------------------------") | ||
console.log("PriceConsumerV3 address:", PriceConsumerV3Ins.address); | ||
|
||
//await priceConsumerV3.deployed() | ||
console.log("Read Price from PRICE_FEED") | ||
|
||
await PriceConsumerV3Ins.getChainlinkDataFeedLatestAnswer().then(function (data) { | ||
console.log('data', data) | ||
console.log('Current price of ETH / USD is: ', web3.utils.hexToNumber(data._hex)) | ||
}) | ||
// 读取价格 | ||
console.log("Read Price from PRICE_FEED"); | ||
const latestPrice = await PriceConsumerV3Ins.getLatestPrice(); | ||
console.log('Current price of ETH / USD is: ', latestPrice.toString()); | ||
|
||
// 使用 singer 获取合约 | ||
const contract = await ethers.getContractAt("PriceConsumerV3", await PriceConsumerV3Ins.getAddress(), singer); | ||
const latestPriceFromContract = await contract.getLatestPrice(); | ||
console.log('Current price of ETH / USD from contract is: ', latestPriceFromContract.toString()); | ||
} | ||
|
||
// We recommend this pattern to be able to use async/await everywhere | ||
// and properly handle errors. | ||
main() | ||
.then(() => process.exit(0)) | ||
.catch(error => { | ||
console.error(error); | ||
process.exit(1); | ||
console.error(error); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters