I've been revisiting ethers.js
recently to refresh my understanding of the details and to write a simple tutorial called "WTF Ethers" for beginners.
Twitter: @0xAA_Science
Community: Website wtf.academy | WTF Solidity | discord | WeChat Group Application
All the code and tutorials are open-sourced on GitHub: github.com/WTFAcademy/WTF-Ethers
In this session, we will introduce how to use ethers.js
to collect the assets of multiple wallets (ETH and tokens) into one wallet.
Batch Collect
After interacting and playing around with the chain, we need to consolidate and manage the assets of multiple wallets. You can use HD Wallet or other methods to operate multiple wallets with different private keys, and then use the ethers.js
script to complete the collection. Below, we will demonstrate the collection of ETH
(native token) and WETH
(ERC20 token) separately.
-
Create a
provider
and awallet
, wherewallet
is the receiving wallet for the assets. -
Declare the WETH contract.
-
Create an
HD
wallet to manage multiple wallets. -
Derive
20
wallets from theHD
wallet, and these wallets should have assets. -
Read the ETH and WETH balance of an address.
-
Use the
sendTransaction()
method of the wallet class to send transactions and collectETH
from each wallet. -
Connect the
WETH
contract to the new wallet and call thetransfer()
method to collectWETH
from each wallet.

- Read the post-collection ETH and WETH balance of an address, and you can see the decrease in
ETH
andWETH
balances, collection succeeded!

Summary
In this lecture, we introduced batch collect and used ethers.js
scripts to collect ETH
and WETH
from 20
wallets into one wallet.