From 6b9d23a3fa6036adc43a5a7d2d503f6cad2700cb Mon Sep 17 00:00:00 2001 From: gongdaxia Date: Wed, 8 May 2024 22:27:12 +0800 Subject: [PATCH] feat: upgrade ethers.js v6 06-ethersjs-waffle --- basic/06-ethersjs-waffle/README-cn.md | 13 +++-- basic/06-ethersjs-waffle/README.md | 13 +++-- basic/06-ethersjs-waffle/index-v5.js | 74 +++++++++++++++++++++++++++ basic/06-ethersjs-waffle/index.js | 34 +++++++----- 4 files changed, 112 insertions(+), 22 deletions(-) create mode 100644 basic/06-ethersjs-waffle/index-v5.js diff --git a/basic/06-ethersjs-waffle/README-cn.md b/basic/06-ethersjs-waffle/README-cn.md index 9ffc736f2..8fc510ec4 100644 --- a/basic/06-ethersjs-waffle/README-cn.md +++ b/basic/06-ethersjs-waffle/README-cn.md @@ -92,7 +92,12 @@ $ yarn --versionyarn --version - waffle 官方文档: -- ehterjs 官方文档: - - -- 中文文档: +- ehterjs 官方文档: + - v4 + - v5 + - v6 + +- 中文文档: + - v4 + - v5 + - v6 diff --git a/basic/06-ethersjs-waffle/README.md b/basic/06-ethersjs-waffle/README.md index 3a22791da..d926a3e7b 100644 --- a/basic/06-ethersjs-waffle/README.md +++ b/basic/06-ethersjs-waffle/README.md @@ -1,4 +1,4 @@ -[中文](./README-CN.md) / English +[中文](./README-cn.md) / English ## Preface @@ -93,7 +93,12 @@ afer changed: - waffle offical document: -- etherjs offical document: - +- etherjs offical document: + - v4 + - v5 + - v6 -- Chinese document: +- Chinese document: + - v4 + - v5 + - v6 diff --git a/basic/06-ethersjs-waffle/index-v5.js b/basic/06-ethersjs-waffle/index-v5.js new file mode 100644 index 000000000..c446f4a8e --- /dev/null +++ b/basic/06-ethersjs-waffle/index-v5.js @@ -0,0 +1,74 @@ +const fs = require("fs"); +const SimpleToken = require("./build/SimpleToken.json"); + +const { ethers } = require("ethers"); + +require("dotenv").config(); +const privateKey = process.env.PRIVATE_KEY; + +// const web3 = new Web3.providers.HttpProvider('https://sepolia.infura.io/v3/0aae8358bfe04803b8e75bb4755eaf07'); +// let web3Provider = new ethers.providers.Web3Provider(web3) + +const web3Provider = new ethers.providers.InfuraProvider( + "sepolia", + process.env.INFURA_ID +); + +const wallet = new ethers.Wallet(privateKey, web3Provider); + +let address = "0x54A65DB20D7653CE509d3ee42656a8F138037d51"; + +let bal; + +// support eip1559 +async function getGasPrice () { + return await web3Provider.getFeeData().then(async function (res) { + let maxFeePerGas = res.maxFeePerGas; + let maxPriorityFeePerGas = res.maxPriorityFeePerGas; + console.log("maxFeePerGas: ", maxFeePerGas.toString()); + console.log("maxPriorityFeePerGas:", maxPriorityFeePerGas.toString()); + + return { + maxFeePerGas: maxFeePerGas, + maxPriorityFeePerGas: maxPriorityFeePerGas, + }; + }); +} + +async function checkBalance () { + bal = await web3Provider.getBalance(address).then((balance) => { + // balance is a BigNumber (in wei); format is as a sting (in ether) + let etherString = ethers.utils.formatEther(balance); + return etherString; + }); + console.log("balance: ", bal); +} + +checkBalance(); + +let token; +async function deploy () { + let option = await getGasPrice(); + // 常见合约工厂实例 + const simpletoken = new ethers.ContractFactory( + SimpleToken.abi, + SimpleToken.bytecode, + wallet + ); + token = await simpletoken.deploy("HEHE", "HH", 1, 100000000); + tx = await token.transfer( + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ethers.utils.parseEther("0.00000000001"), + option + ); + console.log(token.address); + + console.log(token.deployTransaction.hash); + + await token.deployed(); + + let bal = await token.balanceOf(wallet.address); + console.log(bal.toString()); +} + +deploy(); \ No newline at end of file diff --git a/basic/06-ethersjs-waffle/index.js b/basic/06-ethersjs-waffle/index.js index 8f4ce8516..0894b94a1 100644 --- a/basic/06-ethersjs-waffle/index.js +++ b/basic/06-ethersjs-waffle/index.js @@ -6,22 +6,27 @@ const { ethers } = require("ethers"); require("dotenv").config(); const privateKey = process.env.PRIVATE_KEY; -// const web3 = new Web3.providers.HttpProvider('https://sepolia.infura.io/v3/0aae8358bfe04803b8e75bb4755eaf07'); +// const web3 = new Web3.providers.HttpProvider('https://sepolia.infura.io/v3' + process.env.INFURA_ID); // let web3Provider = new ethers.providers.Web3Provider(web3) -const web3Provider = new ethers.providers.InfuraProvider( +const web3Provider = new ethers.InfuraProvider( "sepolia", process.env.INFURA_ID ); +// or +// const web3Provider = new ethers.JsonRpcProvider('https://sepolia.infura.io/v3/' + process.env.INFURA_ID, +// 'sepolia' +// ); + const wallet = new ethers.Wallet(privateKey, web3Provider); -let address = "0x54A65DB20D7653CE509d3ee42656a8F138037d51"; +let address = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"; let bal; // support eip1559 -async function getGasPrice() { +async function getGasPrice () { return await web3Provider.getFeeData().then(async function (res) { let maxFeePerGas = res.maxFeePerGas; let maxPriorityFeePerGas = res.maxPriorityFeePerGas; @@ -35,10 +40,10 @@ async function getGasPrice() { }); } -async function checkBalance() { +async function checkBalance () { bal = await web3Provider.getBalance(address).then((balance) => { // balance is a BigNumber (in wei); format is as a sting (in ether) - let etherString = ethers.utils.formatEther(balance); + let etherString = ethers.formatEther(balance); return etherString; }); console.log("balance: ", bal); @@ -47,7 +52,7 @@ async function checkBalance() { checkBalance(); let token; -async function deploy() { +async function deploy () { let option = await getGasPrice(); // 常见合约工厂实例 const simpletoken = new ethers.ContractFactory( @@ -55,18 +60,19 @@ async function deploy() { SimpleToken.bytecode, wallet ); + console.log('start deploy') token = await simpletoken.deploy("HEHE", "HH", 1, 100000000); + + console.log(token.target); + + await token.waitForDeployment(); + console.log('deployed') tx = await token.transfer( "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", - ethers.utils.parseEther("0.00000000001"), + ethers.parseEther("0.00000000001"), option ); - console.log(token.address); - - console.log(token.deployTransaction.hash); - - await token.deployed(); - + console.log('hash', tx.hash); let bal = await token.balanceOf(wallet.address); console.log(bal.toString()); }