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
providerand awallet, wherewalletis the receiving wallet for the assets. -
Declare the WETH contract.
-
Create an
HDwallet to manage multiple wallets.
-
Derive
20wallets from theHDwallet, 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 collectETHfrom each wallet.
-
Connect the
WETHcontract to the new wallet and call thetransfer()method to collectWETHfrom each wallet.

- Read the post-collection ETH and WETH balance of an address, and you can see the decrease in
ETHandWETHbalances, 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.