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 lesson, we will introduce the Contract class and use it to read information from contracts on Ethereum.
The Contract Class
In ethers, the Contract class is an abstraction of contracts (EVM bytecode) deployed on the Ethereum network. Through it, developers can easily read (call) and transact with contracts, as well as obtain transaction results and events. The power of Ethereum lies in its contracts, so it is essential to have a good understanding of contract operations.
Creating a Contract Variable
Read-only and Read-write Contracts
Contract objects can be divided into two types: read-only and read-write. Read-only Contract objects can only read contract information on the chain by executing call operations, which involves calling the view and pure functions in the contract. They cannot perform transaction operations. The methods of creating these two types of Contract variables are different:
- Read-only
Contractvariable: The parameters are the contract address, contractABI, andprovidervariable (read-only).
- Read-write
Contractvariable: The parameters are the contract address, contractABI, andsignervariable. TheSigneris another class inethersthat is used for signing transactions, which we will cover later.
Note: In ethers, the call operation refers to a read-only operation, which is different from the call in Solidity.
Reading Contract Information
1. Creating a Provider
We will use the Infura node's API Key to create a Provider:
2. Creating a Read-only Contract Instance
To create a read-only contract instance, we need to provide three parameters: the contract address, contract ABI, and provider variable. The contract address can be found online, and we have already created the provider variable in the previous step. But how do we fill in the ABI?
ABI (Application Binary Interface) is the standard for interacting with Ethereum smart contracts. For more information, refer to WTF Solidity Tutorials Lesson 27: ABI Encoding. ethers supports two ways of providing ABI:
- Method 1: Directly input the contract
ABI. You can copy it from the compilation page of Remix, generate it locally when compiling contracts (located in theartifactfolder), or obtain it from the code page of an open-source contract on Etherscan. We will use this latter approach to create an instance for theWETHcontract:

- Method 2: Since the readability of
ABIis poor,ethersintroduced an innovative approach calledHuman-Readable ABI. Developers can writeABIusingfunction signatureandevent signature. We will use this method to create an instance for the stablecoinDAI:
3. Reading on-chain information of WETH and DAI
We can use the read-only Contract instance to call the view and pure functions of the contract to obtain on-chain information:


As you can see, instances of contracts created using both methods can successfully interact with the blockchain. Vitalik's wallet contains 16 WETH and 1818 DAI, as shown in the figures above.
Explanation:
We can verify the balance of WETH and DAI in Vitalik's wallet whether it matches the one read from the Contract using the Ethereum browser. By using ENS, we found that Vitalik's wallet address is 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045. Using the balanceOf contract method, we obtained a balance of exactly 16 WETH and 1818 DAI. The conclusion is consistent!

Summary
In this lesson, we introduced the Contract class in ethers and created read-only Contract instances for WETH and DAI. We successfully read Vitalik's balances for these two tokens.
Post-class assignment: Use Etherscan to verify that the balance of DAI in Vitalik's wallet is consistent.