Skip to main content

ERC-20 Approve Facet

@perfect-abstractions/compose/token/ERC20/Approve/ERC20ApproveFacet.sol

Grant a spender an allowance over the caller's tokens

Key Features
  • approve sets the caller's allowance for _spender in ERC20Storage at erc8042:erc20.
  • The new value replaces the old one. It is not added to it.
  • Always acts for msg.sender, so no access control is needed.

Storage

State Variables

PropertyTypeDescriptionSTORAGE_POSITIONbytes32ERC-20 storage position within the diamond (Value: keccak256("erc20"))

ERC20Storage

Definition
/** @custom:storage-location erc8042:erc20 */
struct ERC20Storage {
mapping(address owner => uint256 balance) balanceOf;
uint256 totalSupply;
mapping(address owner => mapping(address spender => uint256 allowance)) allowance;
}

Functions

approve

Sets how many of the caller's tokens _spender may move with transferFrom. The stored allowance becomes exactly _value, overwriting any previous allowance for that spender.

The caller's balance is not checked. You can approve more tokens than you hold, and the limit is enforced later when the spender actually transfers.

function approve(address _spender, uint256 _value) external returns (bool);

Parameters:

PropertyTypeDescription_spenderaddressThe address allowed to spend the caller's tokens. Cannot be address(0)._valueuint256The new allowance. 0 revokes it. type(uint256).max grants an unlimited allowance that is never decremented.

Returns:

PropertyTypeDescription-boolAlways true. Failures revert instead of returning false.

Reverts:

PropertyTypeDescriptionERC20InvalidSpendererror_spender is address(0).

Events

Emitted on every successful approve, including when _value is 0 or unchanged.

Signature:
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
Parameters:
PropertyTypeDescription_owneraddressThe address granting the allowance. Always msg.sender._spenderaddressThe address receiving the allowance._valueuint256The new allowance.

Errors

Thrown by approve when _spender is the zero address.

Signature:
error ERC20InvalidSpender(address _spender);
Parameters:
PropertyTypeDescription_spenderaddressThe rejected spender. Always address(0).

Best Practices

Security Considerations

approve only ever writes the allowance of msg.sender, so a caller cannot grant spending rights over anyone else's tokens.

Changing a non-zero allowance can be front-run. If you lower an allowance from N to M, the spender can see the pending transaction, spend N first, and then spend M as well. This facet has no increaseAllowance or decreaseAllowance. To change a non-zero allowance safely, set it to 0, confirm that transaction, then set the new value.

transferFrom and burnFrom reduce allowances without emitting Approval. Rebuilding allowances from Approval events alone gives stale values, so read allowance() for the current number.

Last updated:

Newsletter

Get notified about releases, feature announcements, and technical deep-dives on building smart contracts with Compose.

No spam. Unsubscribe anytime.