Token Approval in Ethereum: How ERC-20 Spending Rights Work
A token approval in Ethereum lets a named contract move up to a specified amount of one ERC-20 token from your address using transferFrom, until the allowance is spent or revoked. The token contract tracks that limit sep
A token approval in Ethereum lets a named contract move up to a specified amount of one ERC-20 token from your address using transferFrom, until the allowance is spent or revoked. The token contract tracks that limit separately for each owner and spender, so an approval applies to the token and contract you specify.
How Does an ERC-20 Token Approval Work?
An ERC-20 approval sets a spending limit in the token contract before another contract pulls your tokens. Four parts determine what it permits:
- Owner: the address holding the tokens.
- Token: the ERC-20 contract, such as USDC.
- Spender: the address allowed to pull them.
- Allowance: the amount the spender may still pull.
Your wallet sends approve(spender, amount) to the token contract, which stores allowance[owner][spender]. The spender can then call transferFrom to move tokens from your address, up to the remaining allowance and your available balance. The EIP-20 token standard defines these three functions: approve authorizes a spender up to an amount, allowance reports what remains, and transferFrom moves tokens from an authorized address. These rules govern the ERC-20 token contract involved in the swap.
Say you approve 100 USDC for a swap. The approval is its own on-chain transaction and costs gas; signing it does not execute the swap. After it confirms, you sign the swap transaction separately. If the spender pulls all 100 USDC, the remaining allowance is zero. If it pulls less, or the swap never happens, the unused allowance remains available until you change it.
How Much Should You Approve for a Swap?
For one swap, approving the quoted input amount limits what the spender can pull. Compare two approvals for the same 100 USDC trade and the same spender: an allowance of 100 USDC can cover that trade, while an unlimited allowance can also cover later USDC sent to your address. If the spender contract is exploited, the amount left available to it changes with that one choice.
An unlimited approval sets the allowance to the maximum uint256 value. It can save the gas of another approval on repeat trades with the same spender, but the approval can stay active after todayβs swap finishes. Even a smaller approval may leave a remainder when the swap uses less than expected. Review the remaining allowance after the trade, rather than assuming the swap cleared it.
A wallet might show a signing prompt instead of a separate approve transaction. EIP-2612 permit lets a signature set an ERC-20 allowance when someone submits it on-chain. Permit2 can use signed permissions after you have approved that token for the Permit2 contract; its permissions may cover a limited amount and duration or a single transfer. Read the token, spender, amount and deadline in a permit prompt: a signature can carry spending rights even though signing itself costs no gas.
Related Questions
How do I revoke a token approval?
Revoke an ERC-20 approval by setting that spenderβs allowance to zero. Look up your address in the Etherscan Token Approval Checker, select the ERC-20 token and approved spender, then submit its revoke transaction from your wallet. Revocation costs gas and takes effect when the transaction confirms. If you used Permit2, review its spender permissions as well as the tokenβs allowance to Permit2.
Is an unlimited token approval safe?
An unlimited token approval gives the named spender ongoing access to that token, subject to your balance. Its convenience is most useful when you expect repeat trades with the same contract and accept that continuing exposure. For a one-off trade, an amount sized to the input gives the spender less room to act if something goes wrong.
On a first USDC swap that uses a 0x settlement contract, your wallet asks you to approve that contract before the separate swap transaction. When using Matcha swap, a decentralized exchange aggregator that finds routes across liquidity sources, compare the spender shown in your wallet with the swap quote and approve the USDC amount you intend to trade.
After the swap, verify the remaining allowance and revoke any access you no longer need.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.