Blog

Mastering Solidity: Understanding receive() & fallback() Functions and WETH Withdrawal Limitations

By now, everyone who follows this channel should know about Solidity's receive and fallback function which are intended to receive native ETH. From time to time, these functions are used in interaction with the WETH contract, specifically when unwrapping WETH to ETH.

The WETH9 contract allows users to wrap and unwrap ETH through deposit and withdraw functions.

Deposit Function: Users send ETH to the contract, and in return, they receive an equivalent amount of WETH.

Withdraw Function: Users can burn their WETH tokens to receive the equivalent amount of ETH back. However, there is a catch: the withdrawal function uses transfer() to send ETH back to the user.

Here's how the withdraw function looks in the WETH9 contract:

The transfer() function, while simple and secure, only forwards 2300 gas to the recipient's contract. This limited gas allowance can lead to problems if the receiving contract’s receive() or fallback() function tries to perform actions that require more than 2300 gas.

If you now develop a contract which intends to handle the receipt of ETH from the WETH contract, it becomes mandatory to ensure that you are not accidentally including logic in the receive/fallback function which consumes more than 2300 gas, as this would effectively DoS the receipt of ETH.