Skip to content

Commit

Permalink
feat: upgrade ethers.js v6 06-ethersjs-waffle
Browse files Browse the repository at this point in the history
  • Loading branch information
gongdaxia committed May 8, 2024
1 parent 8de015f commit 6b9d23a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 22 deletions.
13 changes: 9 additions & 4 deletions basic/06-ethersjs-waffle/README-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ $ yarn --versionyarn --version

- waffle 官方文档: <https://ethereum-waffle.readthedocs.io/en/latest/getting-started.html>

- ehterjs 官方文档: <https://docs.ethers.io/v4/api-providers.html>
<https://docs.ethers.io/v5/getting-started/#getting-started--contracts>

- 中文文档: <https://learnblockchain.cn/docs/ethers.js/api-providers.html>
- ehterjs 官方文档:
- v4 <https://docs.ethers.io/v4/api-providers.html>
- v5 <https://docs.ethers.io/v5/getting-started/#getting-started--contracts>
- v6 <https://docs.ethers.org/v6/getting-started>

- 中文文档:
- v4 <https://learnblockchain.cn/docs/ethers.js/api-providers.html>
- v5 <https://learnblockchain.cn/ethers_v5/getting-started>
- v6 <https://www.wtf.academy/docs/ethers-101/HelloVitalik/>
13 changes: 9 additions & 4 deletions basic/06-ethersjs-waffle/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[中文](./README-CN.md) / English
[中文](./README-cn.md) / English

## Preface

Expand Down Expand Up @@ -93,7 +93,12 @@ afer changed:

- waffle offical document: <https://ethereum-waffle.readthedocs.io/en/latest/getting-started.html>

- etherjs offical document: <https://docs.ethers.io/v4/api-providers.html>
<https://docs.ethers.io/v5/getting-started/#getting-started--contracts>
- etherjs offical document:
- v4 <https://docs.ethers.io/v4/api-providers.html>
- v5 <https://docs.ethers.io/v5/getting-started/#getting-started--contracts>
- v6 <https://docs.ethers.org/v6/getting-started>

- Chinese document: <https://learnblockchain.cn/docs/ethers.js/api-providers.html>
- Chinese document:
- v4 <https://learnblockchain.cn/docs/ethers.js/api-providers.html>
- v5 <https://learnblockchain.cn/ethers_v5/getting-started>
- v6 <https://www.wtf.academy/docs/ethers-101/HelloVitalik/>
74 changes: 74 additions & 0 deletions basic/06-ethersjs-waffle/index-v5.js
Original file line number Diff line number Diff line change
@@ -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();
34 changes: 20 additions & 14 deletions basic/06-ethersjs-waffle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -47,26 +52,27 @@ async function checkBalance() {
checkBalance();

let token;
async function deploy() {
async function deploy () {
let option = await getGasPrice();
// 常见合约工厂实例
const simpletoken = new ethers.ContractFactory(
SimpleToken.abi,
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());
}
Expand Down

0 comments on commit 6b9d23a

Please sign in to comment.