Does a contract that works with Wrapped Ether(WETH) require a fallback or receive functions? Let say I send WETH to a smart contract with/without fallback functions, what will happen?
No it doesn't need a fallback, and receive has no notion of erc20 transfer.
In general you should have a deposit function or a function that moves the tokens on behalf of a user and updates a balance.
While you can for example send weth to a uniswap pool in one block and then call swap on the contract in another, in practice, on a mainnet the token will likely be skimmed by a bot before you are able to take an action that allows the contract to know its balance of the token has changed and so can fulfill your swap.
A token is not like a native coin, there isn't a way for the contract to know its balance has updated without some function being called, there's no way to auto reject it through not having payable functions or fallback.
Weths entire use case is as an erc20 conforming representation of eth, it exists to be treated as any other erc20, so there are no special opcodes or considerations by the evm.
When you "send" an ERC20 to an address, it actually only updates the balances of the accounts.
It doesn't notify the receiving address in any way.
The only proof of this transfer is the transaction itself and the
But smart contracts are not able to listen for transactions or events.
This is actually one of the main shortcomings of
ERC20 and the reason why
ERC1363 were created.
ERC721 (the NFT standard) introduced the idea of callback:
safeTransferFrom()will verify that the address is a contract
- if it is, that the contract implements the
- and if it does it will finally call the function
onERC721Receivedon the called contract, enabling the called contract to be notified of the transfer and to add logic to react.
ERC777 and ERC1363 were built on this logic.
It works a bit like a
fallback() function to enable handling specific cases on being called. The "receive" function will be notified and called when ether is transferred while the "fallback" will be called when the contract is called with any calldata that isn't recognized.
To wrap up:
Does a contract that works with Wrapped Ether(WETH) require a fallback or receive functions?
YES. But not because of the WETH itself but because those two functions are generally required to handle cases of wrong calls or direct ether transfers !