Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(01_HelloVitalik): 修复en中的ethers引入方式以及增加注释 #143

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions 01_HelloVitalik/HelloVitalik.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { ethers } from "ethers";

// 利用ethers默认的Provider连接以太坊网络
// const provider = new ethers.getDefaultProvider();
const ALCHEMY_MAINNET_URL = 'https://eth-mainnet.g.alchemy.com/v2/oKmOQKbneVkxgHZfibs-iFhIlIAl6HDN';
const provider = new ethers.JsonRpcProvider(ALCHEMY_MAINNET_URL)
// 注意:如果当前的URL无法使用,需要主动去NFURA或者ALCHEMY官网注册并获得URl
const ALCHEMY_MAINNET_URL = "https://eth-mainnet.g.alchemy.com/v2/oKmOQKbneVkxgHZfibs-iFhIlIAl6HDN";
const provider = new ethers.JsonRpcProvider(ALCHEMY_MAINNET_URL);

const main = async () => {
// 查询vitalik的ETH余额
const balance = await provider.getBalance(`vitalik.eth`);
// 将余额输出在console
console.log(`ETH Balance of vitalik: ${ethers.formatEther(balance)} ETH`);}
main()
// 查询vitalik的ETH余额
const balance = await provider.getBalance(`vitalik.eth`); //0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045
// 将余额输出在console
console.log(`ETH Balance of vitalik: ${ethers.formatEther(balance)} ETH`);
};
main();
2 changes: 1 addition & 1 deletion 03_ReadContract/ReadContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ethers } from "ethers";

// 利用Alchemy的rpc节点连接以太坊网络
// 准备 alchemy API 可以参考https://github.com/AmazingAng/WTFSolidity/blob/main/Topics/Tools/TOOL04_Alchemy/readme.md
const ALCHEMY_MAINNET_URL = 'https://eth-mainnet.g.alchemy.com/v2/oKmOQKbneVkxgHZfibs-iFhIlIAl6HDN';
const ALCHEMY_MAINNET_URL = "https://eth-mainnet.g.alchemy.com/v2/xvx-3ZrK3AdmeL2dzMVqzJcCiejbiSbs";
const provider = new ethers.JsonRpcProvider(ALCHEMY_MAINNET_URL);

// 第1种输入abi的方式: 复制abi全文
Expand Down
4 changes: 2 additions & 2 deletions 04_SendETH/SendETH.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ethers } from "ethers";

// 利用Alchemy的rpc节点连接以太坊测试网络
// 准备 alchemy API 可以参考https://github.com/AmazingAng/WTFSolidity/blob/main/Topics/Tools/TOOL04_Alchemy/readme.md
const ALCHEMY_GOERLI_URL = 'https://eth-goerli.alchemyapi.io/v2/GlaeWuylnNM3uuOo-SAwJxuwTdqHaY5l';
const ALCHEMY_GOERLI_URL = "https://eth-mainnet.g.alchemy.com/v2/xvx-3ZrK3AdmeL2dzMVqzJcCiejbiSbs";
const provider = new ethers.JsonRpcProvider(ALCHEMY_GOERLI_URL);

// 创建随机的wallet对象
Expand All @@ -13,7 +13,7 @@ const wallet1WithProvider = wallet1.connect(provider)
const mnemonic = wallet1.mnemonic // 获取助记词

// 利用私钥和provider创建wallet对象
const privateKey = '0x227dbb8586117d55284e26620bc76534dfbd2394be34cf4a09cb775d593b6f2b'
const privateKey = "b3e0cc0591ef7d6018e69984045cf8d54933ad207c21a55caec240fa2ed5791b";
const wallet2 = new ethers.Wallet(privateKey, provider)

// 从助记词创建wallet对象
Expand Down
23 changes: 13 additions & 10 deletions en/01_HelloVitalik/HelloVitalik.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// Import the ethers package
const { ethers } = require("ethers");
import { ethers } from "ethers";
// playcode's free version cannot install ethers, use this command to import the package from the internet (comment out the line above)
// import { ethers } from "https://cdnjs.cloudflare.com/ajax/libs/ethers/6.2.3/ethers.js";

// Connect to the Ethereum network using any desired provider (e.g INFURA or ALCHEMY)
// Connect to the Ethereum network using any desired provider (e.g INFURA or ALCHEMY)
// const provider = new ethers.JsonRpcProvider();
const provider = new ethers.JsonRpcProvider(`https://mainnet.infura.io/v3/8b9750710d56460d940aeff47967c4ba`)
// Note: if the current URL cannot be used, you need to register at INFURA or ALCHEMY's official website and get URL by yourself.
const provider = new ethers.JsonRpcProvider(
`https://mainnet.infura.io/v3/8b9750710d56460d940aeff47967c4ba`,
);
// Specify the address you want to query
const address = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045' //or 'vitalik.eth'
const address = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"; //or 'vitalik.eth'

const main = async () => {
// Query the ETH balance of vitalik
const balance = await provider.getBalance(address) //or 'vitalik.eth'
// Output the balance to the console
console.log(`\nETH Balance of ${address} --> ${ethers.formatEther(balance)} ETH\n`)
}
main()
// Query the ETH balance of vitalik
const balance = await provider.getBalance(address); //or 'vitalik.eth'
// Output the balance to the console
console.log(`\nETH Balance of ${address} --> ${ethers.formatEther(balance)} ETH\n`);
};
main();
4 changes: 2 additions & 2 deletions en/01_HelloVitalik/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Now, let's write our first program using `ethers` called `HelloVitalik`: it will
![Hello Vitalik](./img/1-4.png)

```javascript
const { ethers } = require("ethers");
import { ethers } from "ethers";
const provider = new ethers.JsonRpcProvider(`NODE_URL`) //e.g ALCHEMY, INFURA
const address = 'vitalik.eth'
const main = async() => {
Expand All @@ -81,7 +81,7 @@ Let's analyze the program line by line:

The first line fetches the installed `ethers` library:
```javascript
const { ethers } require "ethers";
import { ethers } from "ethers";
```
If you are using the `playcode` platform where free accounts cannot install external libraries, you can directly import from the `ethers` CDN (for educational purposes only due to security considerations):
```javascript
Expand Down
20 changes: 10 additions & 10 deletions en/02_Provider/Provider.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Import the ethers package
const { ethers } require "ethers";
import { ethers } from "ethers";
// For the free version of playcode that cannot install ethers, use this command to import the package from the web (comment out the line above)
// import { ethers } from "https://cdn-cors.ethers.io/lib/ethers-5.6.9.esm.min.js";

// Connect to the Ethereum network using Infura's RPC node
// Prepare the Infura API by signing up on app.infura.io
const INFURA_MAINNET_URL = new ethers.JsonRpcProvider(`https://mainnet.infura.io/v3/8b9750710d56460d940aeff47967c4ba`)
const INFURA_MAINNET_URL = new ethers.JsonRpcProvider(`https://eth-mainnet.g.alchemy.com/v2/xvx-3ZrK3AdmeL2dzMVqzJcCiejbiSbs`)
const SEPOLIA_TESTNET_URL = new ethers.JsonRpcProvider('https://sepolia.infura.io/v3/8b9750710d56460d940aeff47967c4ba')
// Connect to the Ethereum mainnet
const providerETH = new ethers.JsonRpcProvider(INFURA_MAINNET_URL)
// Connect to the Sepolia testing network
const providerSepolia = new ethers.JsonRpcProvider(INFURA_SEPOLIA_URL)
const providerSepolia = new ethers.JsonRpcProvider(SEPOLIA_TESTNET_URL)

const main = async () => {
// Retrieve on-chain information using the provider
Expand All @@ -24,35 +24,35 @@ const main = async () => {
console.log(`\nSepoliaETH Balance of ${address} --> ${ethers.formatEther(balanceSepolia)} ETH\n`)

// 2. Query the network that the provider is connected to
const network1 = await provider1.getNetwork();
const network2 = await provider2.getNetwork();
const network1 = await providerETH.getNetwork();
const network2 = await providerSepolia.getNetwork();

console.log("Network 1:", network1.name, "ID:", network1.chainId);
console.log("Network 2:", network2.name, "ID:", network2.chainId);

// 3. Query the block number
const main = async () => {
const blockNumber = await provider1.getBlockNumber();
const blockNumber = await providerETH.getBlockNumber();
console.log(blockNumber);

// 4. Query the transaction count of vitalik's wallet
const main = async () => {
const txcount = await provider1.getTransactionCount('vitalik.eth');
const txcount = await providerETH.getTransactionCount('vitalik.eth');
console.log(txcount);

// 5. Query the current suggested gas settings
const main = async () => {
const feeData = await provider1.getFeeData();
const feeData = await providerETH.getFeeData();
console.log(feeData);

// 6. Query block information
const main = async () => {
const block = await provider1.getBlock(0);
const block = await providerETH.getBlock(0);
console.log(block);

// 7. Query the bytecode of a given contract address, using WETH address as an example
const main = async () => {
const bytecode = await provider1.getCode('0xc778417e063141139fce010982780140aa0cd5ab');
const bytecode = await providerETH.getCode('0xc778417e063141139fce010982780140aa0cd5ab');
console.log(bytecode);
};

Expand Down
2 changes: 1 addition & 1 deletion en/02_Provider/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The most commonly used provider in `ethers` is `jsonRpcProvider`, which allows u

### Creating an API Key for the Node Service Provider

First, you need to register and create an API Key on the website of the node service provider (e.g INFURA, ALCHEMY). In the "Tools" section of the "WTF Solidity" tutorial, we introduced the methods to create API Keys for two projects, Infura and Alchemy. You can refer to the tutorial for more information.
First, you need to register and create an API Key on the website of the node service provider (e.g [Infura](https://github.com/AmazingAng/WTFSolidity/blob/main/Topics/Tools/TOOL02_Infura/readme.md), [Alchemy](https://github.com/AmazingAng/WTFSolidity/blob/main/Topics/Tools/TOOL04_Alchemy/readme.md). In the "Tools" section of the "WTF Solidity" tutorial, we introduced the methods to create API Keys for two projects, Infura and Alchemy. You can refer to the tutorial for more information.

![Infura API Key](img/2-1.png)

Expand Down
22 changes: 11 additions & 11 deletions en/03_ReadContract/ReadContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Parameters are the contract address `address`, contract `ABI`, Provider variable `provider`
// const contract = new ethers.Contract(`address`, `ABI`, `provider`);

const { ethers } = require "ethers";
import { ethers } from "ethers";

// Connect to the Ethereum network using the Infura RPC node
const INFURA_MAINNET_URL = 'https://mainnet.infura.io/v3/8b9750710d56460d940aeff47967c4ba';
Expand Down Expand Up @@ -38,22 +38,22 @@ const main = async () => {
console.log(`Total Supply: ${ethers.formatEther(totalSupply)}`);

const balanceAddress = 'vitalik.eth';
const balance = await WETH_contract.balanceOf('vitalik.eth');
const balance = await WETH_contract.balanceOf(balanceAddress);
console.log(`Vitalik's Balance: ${ethers.formatEther(balance)}\n`);

// 2. Reading on-chain information of the DAI contract (ERC20 interface contract)
const name = await DAI_contract.name();
const symbol = await DAI_contract.symbol();
const totalSupply = await DAI_contract.totalSupply();
const nameDAI = await DAI_contract.name();
const symbolDAI = await DAI_contract.symbol();
const totalSupplyDAI = await DAI_contract.totalSupply();

console.log(`\nReading from ${DAI_address}\n`);
console.log(`Name: ${name}`);
console.log(`Symbol: ${symbol}`);
console.log(`Total Supply: ${ethers.formatEther(totalSupply)}`);
console.log(`Name: ${nameDAI}`);
console.log(`Symbol: ${symbolDAI}`);
console.log(`Total Supply: ${ethers.formatEther(totalSupplyDAI)}`);

const balanceAddress = 'vitalik.eth';
const balance = await DAI_contract.balanceOf('vitalik.eth');
console.log(`Vitalik's Balance: ${ethers.formatEther(balance)}\n`);
const balanceAddressDAI = "vitalik.eth";
const balanceDAI = await DAI_contract.balanceOf(balanceAddressDAI);
console.log(`Vitalik's Balance: ${ethers.formatEther(balanceDAI)}\n`);
};

main()
4 changes: 2 additions & 2 deletions en/04_SendETH/SendETH.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Send ETH using the Wallet class
const { ethers } = require("ethers");
import { ethers } from "ethers";
// Connect to the Ethereum test network using the Infura or Alchemy RPC node
const provider = new ethers.JsonRpcProvider(`https://sepolia.infura.io/v3/8b9750710d56460d940aeff47967c4ba`)

const INFURA_SEPOLIA_URL = 'https://sepolia.infura.io/v3/8b9750710d56460d940aeff47967c4ba';
const provider = new ethers.JsonRpcProvider(INFURA_SEPOLIA_URL);

// Send ETH using the Wallet class
const { ethers } = require("ethers");
import { ethers } from "ethers";
// Connect to the Ethereum test network using the Infura or Alchemy RPC node
const provider = new ethers.JsonRpcProvider(`https://sepolia.infura.io/v3/8b9750710d56460d940aeff47967c4ba`)

Expand Down
2 changes: 1 addition & 1 deletion en/04_SendETH/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ NB: For exercise purposes, connect a testnet address on your metamask with fauce

```javascript
// Send ETH using the Wallet class
const { ethers } = require("ethers");
import { ethers } from "ethers";
// Connect to the Ethereum test network using the Infura or Alchemy RPC node
const provider = new ethers.JsonRpcProvider(`https://sepolia.infura.io/v3/8b9750710d56460d940aeff47967c4ba`)
```
Expand Down
2 changes: 1 addition & 1 deletion en/05_WriteContract/WriteContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// const contract = new ethers.Contract(address, ABI, signer)
// The parameters are the contract address `address`, contract ABI `abi`, and the signer variable `signer`

const { ethers } = require("ethers");
import { ethers } from "ethers";
// The free version of playcode does not allow the installation of ethers.
// Use this command to import the package from the web (comment out the line above)
// import { ethers } from "https://cdn-cors.ethers.io/lib/ethers-5.6.9.esm.min.js";
Expand Down
2 changes: 1 addition & 1 deletion en/05_WriteContract/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Here, `METHOD_NAME` is the name of the function to be called, `args` is the func
1. Create the `provider` and `wallet` variables.

```js
const { ethers } = require("ethers");
import { ethers } from "ethers";

// Connect to the Ethereum network using Infura's RPC node
const provider = new ethers.JsonRpcProvider(`https://sepolia.infura.io/v3/8b9750710d56460d940aeff47967c4ba`);
Expand Down
2 changes: 1 addition & 1 deletion en/06_DeployContract/DeployContract.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion en/06_DeployContract/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ For an introduction to the `ERC20` standard token contract, refer to the WTF Sol

1. Create `provider` and `wallet` variables.
```js
const { ethers } = require("ethers");
import { ethers } from "ethers";

// Connect to the Ethereum network using Infura's RPC node
// Connect to the Sepolia test network
Expand Down
2 changes: 1 addition & 1 deletion en/07_Event/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// const transferEvents = await contract.queryFilter("Event Name", [Start Block Height, End Block Height])
// Start Block Height and End Block Height are optional parameters.

const { ethers } = require("ethers");
import { ethers } from "ethers";
// Connect to the Ethereum network using Infura's RPC node
// If you use a testnet, it may throw error "undefined", so mainnet is advisable.
const INFURA_MAINNET_URL = 'https://mainnet.infura.io/v3/8b9750710d56460d940aeff47967c4ba'
Expand Down
2 changes: 1 addition & 1 deletion en/07_Event/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const transferEvents = await contract.queryFilter('event name', starting block,

1. Create a `provider`.
```js
const { ethers } = require("ethers");
import { ethers } from "ethers";
// Connect to the Ethereum network using Infura's RPC node
const INFURA_MAINNET_URL = (`https://mainnet.infura.io/v3/8b9750710d56460d940aeff47967c4ba`);
const provider = new ethers.JsonRpcProvider(INFURA_MAINNET_URL);
Expand Down
2 changes: 1 addition & 1 deletion en/08_ContractListener/ContractListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// 2. Listen only once
// contractUSDT.once("event name", Listener)

const { ethers } = require("ethers");
import { ethers } from "ethers";
// Connect to the mainnet provider
const provider = new ethers.JsonRpcProvider(`https://mainnet.infura.io/v3/8b9750710d56460d940aeff47967c4ba`);

Expand Down
2 changes: 1 addition & 1 deletion en/08_ContractListener/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ contract.once("eventName", function)
1. Declare the `provider`: Infura is a free ETH node provider. You need to apply for one before proceeding as we've established from Tutorial 1. [Apply here](app.infura.io).

```js
const { ethers } = require("ethers");
import { ethers } from "ethers";
// Connect to the mainnet provider
const provider = new ethers.JsonRpcProvider(`https://mainnet.infura.io/v3/8b9750710d56460d940aeff47967c4ba`);
```
Expand Down
2 changes: 1 addition & 1 deletion en/10_Units/Units.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ethers } = require("ethers");
import { ethers } from "ethers";

// 1. BigNumber class
console.group('\n1. BigNumber class');
Expand Down
2 changes: 1 addition & 1 deletion en/11_StaticCall/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ console.log(`Will the transaction succeed?`, tx)

1. Create `provider` and `wallet` objects.
```js
const { ethers } = require("ethers");
import { ethers } from "ethers";

// Prepare Alchemy or Infura API, can refer to
const provider = new ethers.JsonRpcProvider( "https://mainnet.infura.io/v3/8b9750710d56460d940aeff47967c4ba"
Expand Down
2 changes: 1 addition & 1 deletion en/11_StaticCall/staticCall.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ethers } = require("ethers");
import { ethers } from "ethers";
const provider = new ethers.JsonRpcProvider(
"https://mainnet.infura.io/v3/8b9750710d56460d940aeff47967c4ba");

Expand Down
2 changes: 1 addition & 1 deletion en/12_ERC721Check/ERC721Check.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ethers } = require("ethers");
import { ethers } from "ethers";

const INFURA_MAINNET_URL = 'https://mainnet.infura.io/v3/8b9750710d56460d940aeff47967c4ba';
const provider = new ethers.JsonRpcProvider(INFURA_MAINNET_URL);
Expand Down
2 changes: 1 addition & 1 deletion en/13_EncodeCalldata/EncodeCalldata.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// const interface = ethers.Interface(abi)
// Get directly from the contract
// const interface2 = contract.interface
const { ethers } = require("ethers");
import { ethers } from "ethers";

const provider = new ethers.JsonRpcProvider(
"https://sepolia.infura.io/v3/8b9750710d56460d940aeff47967c4ba"
Expand Down
2 changes: 1 addition & 1 deletion en/14_HDwallet/HDwallet.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ethers } = require("ethers");
import { ethers } from "ethers";

// 1. Create HD wallet
console.log("\n1. Create HD wallet")
Expand Down
2 changes: 1 addition & 1 deletion en/15_MultiTransfer/MultiTransfer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ethers } = require("ethers");
import { ethers } from "ethers";
console.log("\n1. Create HD wallet")
// Generate random mnemonic
console.log("\n1. Create an HD wallet");
Expand Down
2 changes: 1 addition & 1 deletion en/19_Mempool/Mempool.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// provider.on("pending", listener)
const { ethers } = require("ethers");
import { ethers } from "ethers";

// 1. Create provider and wallet, recommend using wss connection instead of http when listening for events
console.log("\n1. Connecting to wss RPC")
Expand Down
2 changes: 1 addition & 1 deletion en/21_VanityAddress/VanityAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Example: two 0 at the beginning and two 1 at the end
// const regex = /^0x00.*11$/

const { ethers } = require("ethers");
import { ethers } from "ethers";
var wallet // Wallet
const regex = /^0x000.*$/ // Expression
var isValid = false
Expand Down
2 changes: 1 addition & 1 deletion en/21_VanityAddress/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ isValid = regex.test(wallet.address) // Check the regular expression
The logic of the vanity address generator is very simple. It continuously generates random wallets until it matches the desired vanity address. In our test, generating a vanity address starting with `0x000` takes only a few seconds, and each additional `0` increases the time by 16 times.

```js
const { ethers } = require("ethers");
import { ethers } from "ethers";
var wallet // Wallet
const regex = /^0x000.*$/ // Expression
var isValid = false
Expand Down
2 changes: 1 addition & 1 deletion en/26_EIP712/eip712.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ethers } = require("ethers");
import { ethers } from "ethers";

// Connect to the Ethereum network using Infura or Alchemy's RPC endpoint
const SEPOLIA_TESTNET_URL = 'https://sepolia.infura.io/v3/8b9750710d56460d940aeff47967c4ba';
Expand Down