EIPs Explained

ERC-7410 · Draft · ERC · 2023-07-26

ERC-7410: ERC-20 Update Allowance By Spender

A draft ERC-20 extension that lets the spender, not just the owner, reduce or revoke an allowance it was given, useful when the owner is a slow multisig or treasury.

What problem does it solve?

In ERC-20, only the token owner can change an allowance. That is a problem when the owner is a treasury or multisig that granted a spender far more than needed: shrinking the approval requires collecting signatures and coordinating, which takes time. Meanwhile the spender, perhaps a protocol contract or service account, may know it no longer needs the allowance or may fear its key is at risk.

ERC-7410 lets the spender give the permission back on its own, adding a quick safety valve without changing anything for owners.

How it works

The token adds a single function:

function decreaseAllowanceBySpender(address owner, uint256 subtractedValue) external;
  • The caller is the spender; it lowers allowance(owner, msg.sender) by subtractedValue.
  • If subtractedValue is greater than or equal to the current allowance, the allowance becomes 0.
  • If the current allowance is type(uint256).max (an "infinite" approval), any call sets it to 0. Partial decreases of infinite approvals are not possible.
  • The standard ERC-20 Approval event must be emitted with the new value.
  • The token must implement ERC-165 and return true for interface ID 0x12860fba.

Sample code

Adding the extension to an OpenZeppelin ERC-20:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";

contract SpenderRevocableToken is ERC20, ERC165 {
    constructor() ERC20("Demo", "DEMO") { _mint(msg.sender, 1e24); }

    function decreaseAllowanceBySpender(address owner, uint256 subtractedValue) external {
        uint256 current = allowance(owner, msg.sender);
        uint256 next = (current == type(uint256).max || subtractedValue >= current)
            ? 0
            : current - subtractedValue;
        _approve(owner, msg.sender, next, true); // emits Approval
    }

    function supportsInterface(bytes4 id) public view override returns (bool) {
        return id == 0x12860fba || super.supportsInterface(id);
    }
}

Calling it as the spender (ethers v6):

import { Contract, MaxUint256, parseUnits } from "ethers";

const token = new Contract(tokenAddr, ["function decreaseAllowanceBySpender(address,uint256)"], spenderWallet);
await token.decreaseAllowanceBySpender(treasuryAddr, parseUnits("500", 18)); // shrink by 500
await token.decreaseAllowanceBySpender(treasuryAddr, MaxUint256);           // revoke fully

Things to know

  • Status is Draft.
  • It only works on tokens that implement it; existing ERC-20 tokens cannot be retrofitted.
  • A spender can cancel an allowance the owner wanted to keep (for example for recurring payments). The spec accepts this, since giving up a right is less harmful than using it, but integrators should not assume allowances stay stable.
  • Related approaches to safer approvals include ERC-2612 permit, which avoids long-lived allowances.

#erc20 #allowance #approval #security

Details

Authors
Mohammad Zakeri Rad (@zakrad), Adam Boudjemaa (@aboudjem), Mohamad Hammoud (@mohamadhammoud)
Status
Draft
Type
Standards Track / ERC
Created
2023-07-26
Requires

Machine-readable: Markdown · JSON