Why USDT Approve Reverts: The Zero-First Fix
Ethereum USDT reverts when you call approve(spender, value) with a nonzero value while that spender already has a nonzero allowance. Set the allowance to zero first, then set the new value; this works around TetherToken’s guard, which was added to mitigate the ERC-20 approval race condition. Say the allowance is 50 USDT and your dapp requests 100. The call to approve(spender, 100) reverts because…
USDT reverts when called with a nonzero approval while the spender already has a nonzero allowance. To workaround TetherToken's guard, first set the allowance to zero, then set the new value. For example, if the allowance is 50 USDT and the dapp requests 100, the approve call will revert because the current allowance is still nonzero.
Calling approve(spender, 0) followed by approve(spender, 100) will succeed. The inner workings of USDT’s approve call involve checking if the new value is nonzero and the current allowance is zero. If the check fails, the call reverts; if successful, the allowance is updated without returning a value. This can cause issues as Solidity checks return data, leading to reverts when decoding missing boolean values.
To handle these cases, use OpenZeppelin Contracts' SafeERC20.forceApprove, which first sets the allowance to zero if the approval fails and retries the requested value. Always verify the specific USDT deployment's approved behavior on the intended network, as TetherToken's rules may vary.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to