Solidity – Does the Approve Function on an ERC20 Token Need to Be Run Once or Before Every Relevant Transaction?

solidity

I understand that an ERC20's approve() function has to be run before the token can be sent to another contract. But is this a one-time approval for msg.sender to be granted an approval status or does it need to be executed before each relevant transaction?

Best Answer

It's common for dApps that interact with tokens to specify the maximum uint256 value as allowance (2^256 - 1 or 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff), in which case you only need to run it once basically. The advantage is that the user won't have to constantly send two transactions, but the disadvantage is that there could be some kind of vulnerability in your contract, allowing someone to transfer all user's tokens. It's a trade-off between user exprience and security.

Related Topic