Diving into the WETH smart contract
Wrapped ETH (WETH) is an example of a wrapped native token on Ethereum. The smart contract of WETH doesn’t only implement the required interfaces of an ERC20 token but also the functions to wrap and unwrap the native token, ETH. Figure 8.1 shows how a user interacts with a WETH smart contract to wrap ETH and unwrap WETH.
Figure 8.1 – The process of wrapping ETH and unwrapping WETH
As shown in Figure 8.1, a user can deposit ETH to a WETH smart contract, and the user will get the same amount of WETH with the given amount of ETH. The user can withdraw the original ETH by redeeming the same amount of WETH. Based on this, we will need to implement the deposit
function to wrap ETH and the withdraw
function to unwrap WETH in the WETH smart contract.
Demystifying the WETH smart contract
Now, let’s create a new solidity source file at src/backend/contracts/WETH.sol
for the WETH smart contract, and copy...