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 learn how to declare a writable Contract
variable and interact with the WETH
contract on the test network.
Creating a Writable Contract
Variable
The rule for declaring a writable Contract
variable is as follows:
Here, address
is the contract address, ABI
is the contract's ABI interface, and signer
is the wallet
object. Note that you need to provide a signer
here, whereas when declaring a readable contract, you only need to provide a provider
.
You can also convert a readable contract into a writable contract using the following method:
Contract Interaction
In Lesson 3, we learned how to read contract information. It does not require any gas. Here, we will learn how to write to a contract, which involves building a transaction and paying for gas. This transaction will be validated by every node and miner on the network, and the blockchain state will be changed.
You can interact with a contract using the following methods:
Here, METHOD_NAME
is the name of the function to be called, args
is the function's parameters, and [, overrides]
is optional data that can be passed, including:
- gasPrice: Gas price
- gasLimit: Gas limit
- value: Ether sent during the call (in wei)
- nonce: Nonce
Note: This method cannot fetch the return value of the contract. If you need it, you have to use Solidity events to record the value and then query using the transaction receipt.
Example: Interacting with the Test Network's WETH
Contract
WETH
(Wrapped ETH) is a wrapped version of ETH
. It wraps native Ethereum tokens using a smart contract to conform to the ERC20
standard. For more detailed content about the WETH
contract, refer to the WTF Solidity Tutorial on WETH.
-
Create the
provider
andwallet
variables. -
Create a writable
WETH
contract variable. We include four functions that we will call in the contract's ABI:balanceOf(address)
: Query theWETH
balance of an address.deposit()
: Convert transferredETH
toWETH
within the contract.transfer(address, uint256)
: TransferWETH
to an address.withdraw(uint256)
: Withdraw funds from the contract.
-
Read the account's
WETH
balance. You can see that the balance is0.002
. -
Call the
deposit()
function of theWETH
contract to convert0.001 ETH
into0.001 WETH
. Print the transaction details and the balance. You can see that the balance becomes0.003
. -
Call the
transfer()
function of theWETH
contract to transfer0.001 WETH
to Vitalik. Print the balance. You can see that the balance becomes0.007
.

Note: Observe the deposit()
function and the balanceOf()
function, why do they return different values? Why does the former return a bunch of data while the latter only returns a specific value? This is because for a wallet balance, it is a read-only operation, it reads what it is. However, for a function call, it does not know when the data will be confirmed on the blockchain, so it only returns information about the transaction. In summary, for non-pure
/view
function calls, it will return the transaction information. Suppose you want to know the changes in contract variables during the execution of a function, you can use emit
to output events in the contract and read the event information from the returned transaction
to obtain the corresponding values.
Summary
In this lesson, we have learned how to declare writable Contract
variables and interact with the WETH
contract on the test network. We called the deposit()
function of WETH
to convert 0.001 ETH
into WETH
, and transferred it to Vitalik.