diff --git a/README-CN.md b/README-CN.md index cb1786e14..aad2fda89 100644 --- a/README-CN.md +++ b/README-CN.md @@ -224,7 +224,7 @@ DAPP架构请参考文章--[从架构维度看Web2.0与Web3.0应用之别](https 3. [Curve](defi/Curve/README.md) ⌛ 4. [Compound](defi/Compound/readme.md) ✅ 5. [Aave](https://docs.aave.com/portal/) ✅ -6. [Liquity](defi/Liquity/readme.md) ⌛ +6. [Liquity](defi/Liquity/) ⌛ 7. [Euler](defi/Euler/readme.md) ⌛ 8. [SNX](https://github.com/Synthetixio)⌛ 9. [YFI](https://yearn.finance/)⌛ @@ -233,7 +233,7 @@ DAPP架构请参考文章--[从架构维度看Web2.0与Web3.0应用之别](https 12. [OPYN](https://v2.opyn.co/)⌛ 13. [Alchemix](https://github.com/alchemix-finance/alchemix-protocol) ✅ 14. [AMPL](https://roninchow.gitbook.io/ampleforth-cn/technology) [👉 认领这个 Task](https://github.com/rebase-network/Dapp-Learning/issues/new) -15. [RAI](https://www.youtube.com/watch?v=ADK91Zw4sJA&list=PL-GxJch-YeZerVVV8dY8qU2VkJfbnSfdN&index=3) +15. [RAI](defi/RAI/)⌛ 16. [DYDX](https://dydx.exchange/)[👉 认领这个 Task](https://github.com/rebase-network/Dapp-Learning/issues/new) 17. [INDEX](https://mp.weixin.qq.com/s/PXOY3G6AIbhzRUptjYoWIw)[👉 认领这个 Task](https://github.com/rebase-network/Dapp-Learning/issues/new) 18. [DODO](https://dodoex.github.io/docs/zh/docs/tradeApi) [👉 认领这个 Task](https://github.com/rebase-network/Dapp-Learning/issues/new) diff --git a/README.md b/README.md index 941408a6a..353ee9967 100644 --- a/README.md +++ b/README.md @@ -218,7 +218,7 @@ You can refer to our study group's learning roadmap, which is better with the vi 03. [Curve](defi/Curve/README.md) ⌛ 04. [Compound](defi/Compound/readme.md) ✅ 05. [Aave](https://docs.aave.com/portal/) ✅ -06. [Liquity](defi/Liquity/readme.md) ✅ +06. [Liquity](defi/Liquity/) ✅ 07. [Euler](defi/Euler/readme.md) ⌛ 08. [SNX](https://github.com/Synthetixio) [👉 Pick up this Task](https://github.com/Dapp-Learning-DAO/Dapp-Learning/issues/new) 09. [YFI](https://yearn.finance/) ⌛ @@ -227,7 +227,7 @@ You can refer to our study group's learning roadmap, which is better with the vi 12. [OPYN](https://v2.opyn.co/)⌛ 13. [Alchemix](https://github.com/alchemix-finance/alchemix-protocol) ✅ 14. [AMPL](https://roninchow.gitbook.io/ampleforth-cn/technology) [👉 Pick up this Task](https://github.com/Dapp-Learning-DAO/Dapp-Learning/issues/new) -15. [RAI](https://www.youtube.com/watch?v=ADK91Zw4sJA& list=PL-GxJch-YeZerVVV8dY8qU2VkJfbnSfdN&index=3) +15. [RAI](defi/RAI/)⌛ 16. [DYDX](https://dydx.exchange/)[👉 Pick up this Task](https://github.com/Dapp-Learning-DAO/Dapp-Learning/issues/new) 17. [INDEX](https://mp.weixin.qq.com/s/PXOY3G6AIbhzRUptjYoWIw)[👉 Pick up this Task](https://github.com/Dapp-Learning-DAO/Dapp-Learning/issues/new) 18. [DODO Trading API](https://dodoex.github.io/docs/zh/docs/tradeApi) [👉 Pick up this Task](https://github.com/Dapp-Learning-DAO/Dapp-Learning/issues/new) @@ -249,6 +249,7 @@ You can refer to our study group's learning roadmap, which is better with the vi - 04. [Rarible](https://docs.rarible.org/overview/union/) - 05. [Zora](https://docs.zora.co/) - 06. [Alchemy](https://www.alchemy.com/nft-api) +- 07. [Infura](https://www.infura.io/platform/nft-api) ## DAO advanced tasks diff --git a/basic/02-web3js-transaction/index.js b/basic/02-web3js-transaction/index.js index 659c78c99..5ce9a694d 100644 --- a/basic/02-web3js-transaction/index.js +++ b/basic/02-web3js-transaction/index.js @@ -5,6 +5,10 @@ const contractOfIncrementer = require('./compile'); require('dotenv').config(); const privatekey = process.env.PRIVATE_KEY; +function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + /* -- Define Provider -- */ @@ -155,11 +159,8 @@ const Trans = async () => { // goerli don't support http protocol to event listen, need to use websocket // more details , please refer to https://medium.com/blockcentric/listening-for-smart-contract-events-on-public-blockchains-fdb5a8ac8b9a const web3Socket = new Web3( - new Web3.providers.WebsocketProvider( 'wss://goerli.infura.io/ws/v3/' + process.env.INFURA_ID - ) ); - incrementer = new web3Socket.eth.Contract(abi, createReceipt.contractAddress); // listen to Increment event only once incrementer.once('Increment', (error, event) => { @@ -167,9 +168,20 @@ const Trans = async () => { }); // listen to Increment event continuously - incrementer.events.Increment(() => { - console.log('I am a longlive event listener, I get a event now'); - }); + web3Socket.eth.subscribe('logs',{ + address: createReceipt.contractAddress, + topics: [] + },(error,result) => { + if(error){ + console.error(error) + } + } + ).on("data", (event) => { + console.log("New event: ", event); + }) + .on("error", (error) => { + console.error("Error: ", error); + }); for (let step = 0; step < 3; step++) { incrementTransaction = await web3.eth.accounts.signTransaction( @@ -183,6 +195,9 @@ const Trans = async () => { await web3.eth.sendSignedTransaction(incrementTransaction.rawTransaction); + console.log("Waiting for events") + await sleep(3000); + if (step == 2) { // clear all the listeners web3Socket.eth.clearSubscriptions(); diff --git a/basic/14-chainlink-price-feed/README-CN.md b/basic/14-chainlink-price-feed/README-CN.md index 2bf240d62..519589f57 100644 --- a/basic/14-chainlink-price-feed/README-CN.md +++ b/basic/14-chainlink-price-feed/README-CN.md @@ -3,6 +3,8 @@ 区块链是非常安全可靠的价值交换网络,但却无法安全防篡改地获取链下数据或将数据发送至链下系统。使用 Chainlink 预言机喂价, 通过预言机网络在链上直接获取实时金融市场价格数据 +首先,我们可以在[这里](https://docs.chain.link/data-feeds/price-feeds/addresses)找到各个网络的喂价合约地址。然后,以ETH_USD为例,表示一个ETH能兑换多少USD,注意不要理解反了,不是ETH per USD的意思。接下来无论链上还是链下,我们都需要通过读取喂价器合约的方式获取价格信息。 + ## 测试流程 ### 配置私钥 @@ -28,7 +30,7 @@ npx hardhat run scripts/01-PriceConsumerV3Deploy.js --network goerli ``` ### 链下调用喂价机 - +#### 获取最新数据 ```js // ./UsingDataFeedsByEthers.js require('dotenv').config(); @@ -74,6 +76,29 @@ Latest Round Data [ - 完整示例看这里 [:point_right: UsingDataFeedsByEthers.js](./UsingDataFeedsByEthers.js) +#### 链下获取历史数据 + +Chainlink链上Api提供两个接口: +- latestRoundData:获取最新价格数据 +- getRoundData:获取历史价格数据 + +首先,了解什么round。chainlink会将链下聚合器的数据持续更新到链上,每次更新,就会更新roundId。更新的间隔是不定时的,快则几分钟更新一次,慢则几个小时,甚至几天更新一次价格。 + +其次,roundId的构成,由两部分:phaseId和aggregatorRoundId. roundId生成方式如下: +``` +roundId = (phaseId << 64) | aggregatorRoundId +``` +其中: +- phaseId表示历史上聚合器的版本,从1开始,每次聚合器更新,就会让phase+1. +- aggregatorRoundId表示对应聚合器版本下的轮数,每次更新一次喂价器数据,就会让这个编号加1. + +所以,遍历历史数据的思路如下: +- 先根据latestRoundData,读取最新的phaseId. +- phaseId从1开始,然后遍历aggregatorRoundId,也从1开始,如果遇到revert错误,就将phaseId+1. + +可以参考例子:[ethers获取历史数据](./scripts/04-HistoryData.js) + + ### Chainlink VRF @@ -154,3 +179,6 @@ SubscriptionId=ddddd - https://learnblockchain.cn/article/2558 - https://learnblockchain.cn/article/1056 - https://mp.weixin.qq.com/s/h0uTWY7vzd-CMdr1pE7_YQ +- https://docs.chain.link/data-feeds/examples +- https://docs.chain.link/data-feeds/historical-data +- https://docs.chain.link/data-feeds/price-feeds/addresses \ No newline at end of file diff --git a/basic/14-chainlink-price-feed/hardhat.config.js b/basic/14-chainlink-price-feed/hardhat.config.js index 7ed4e3f73..13de0d632 100644 --- a/basic/14-chainlink-price-feed/hardhat.config.js +++ b/basic/14-chainlink-price-feed/hardhat.config.js @@ -39,10 +39,10 @@ module.exports = { // url: mainnetRpcUrl // } }, - goerli: { - url: 'https://goerli.infura.io/v3/' + process.env.INFURA_ID, //<---- YOUR INFURA ID! (or it won't work) - accounts: [mnemonic()], - }, + // goerli: { + // url: 'https://goerli.infura.io/v3/' + process.env.INFURA_ID, //<---- YOUR INFURA ID! (or it won't work) + // accounts: [mnemonic()], + // }, }, namedAccounts: { deployer: { diff --git a/basic/14-chainlink-price-feed/package.json b/basic/14-chainlink-price-feed/package.json index 225f43faa..7ec18f2ad 100644 --- a/basic/14-chainlink-price-feed/package.json +++ b/basic/14-chainlink-price-feed/package.json @@ -24,6 +24,7 @@ "dependencies": { "@chainlink/contracts": "^0.4.0", "@nomiclabs/hardhat-etherscan": "^3.0.1", - "dotenv": "^10.0.0" + "dotenv": "^10.0.0", + "moment": "^2.29.4" } } diff --git a/basic/14-chainlink-price-feed/scripts/04-HistoryData.js b/basic/14-chainlink-price-feed/scripts/04-HistoryData.js new file mode 100644 index 000000000..842fe639e --- /dev/null +++ b/basic/14-chainlink-price-feed/scripts/04-HistoryData.js @@ -0,0 +1,129 @@ +// We require the Hardhat Runtime Environment explicitly here. This is optional +// but useful for running the script in a standalone fashion through `node