# What is the ERC-20 approval race condition?

URL: https://shield.quillaudits.com/learn/vulnerabilities/approval-race-condition/

The approval race condition is a front-running problem in ERC-20's `approve`: when an owner changes a spender's allowance from one non-zero value to another, the spender can spend the old allowance before the change lands and then spend the new one too. An owner who meant to lower an allowance from 100 to 50 can lose 150. Set the allowance to zero and check what was spent before you set a new value, or use a token function that changes the allowance only if it still holds the value you expect.

Last updated 3 December 2026. 5 minute read. By the QuillAudits Team.

## How does the approval race work?

1. Alice approves Bob to spend 100 of her tokens.
2. Alice decides 50 is enough and sends `approve(Bob, 50)`.
3. Bob sees that transaction pending and sends `transferFrom(Alice, Bob, 100)` with a higher fee, so it runs first.
4. Alice's approve lands and sets the allowance to 50, as if nothing had happened.
5. Bob calls `transferFrom` again for 50. He has taken 150, more than either allowance Alice chose.

The cause is that `approve` replaces the allowance instead of changing it from a known value. It matters wherever the spender can't be trusted to honor a reduction: an externally owned account, a contract whose owner can change it, or a protocol that was compromised after the first approval.

Contracts run into the same function from the other side. Some tokens refuse to change a non-zero allowance to another non-zero value, so a protocol that re-approves a router without resetting to zero first reverts on those tokens.

## What does vulnerable code look like?

**Vulnerable: approve replaces the allowance whatever was already spent (illustrative, not audited code)** `OverwriteApproveToken.sol`

```
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

contract OverwriteApproveToken {
    mapping(address => uint256) public balanceOf;
    mapping(address => mapping(address => uint256)) public allowance;

    constructor(uint256 supply) {
        balanceOf[msg.sender] = supply;
    }

    // Replaces the allowance whatever happened to the old one. A spender who sees
    // this in the mempool can spend the old allowance first, then the new one.
    function approve(address spender, uint256 amount) external returns (bool) {
        allowance[msg.sender][spender] = amount;
        return true;
    }

    function transferFrom(address from, address to, uint256 amount) external returns (bool) {
        allowance[from][msg.sender] -= amount;
        balanceOf[from] -= amount;
        balanceOf[to] += amount;
        return true;
    }
}
```

This is how a minimal ERC-20 is usually written. In a Foundry test of the five steps above, the spender ended with 150 tokens.

## How do you fix it?

**Fixed: approve only from or to zero, plus a compare-and-set change and a relative decrease (illustrative, not audited code)** `ExpectedAllowanceToken.sol`

```
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

contract ExpectedAllowanceToken {
    mapping(address => uint256) public balanceOf;
    mapping(address => mapping(address => uint256)) public allowance;

    constructor(uint256 supply) {
        balanceOf[msg.sender] = supply;
    }

    // Plain approve only sets an allowance from zero or back to zero, so a
    // non-zero allowance can't be swapped for another one in a single step.
    function approve(address spender, uint256 amount) external returns (bool) {
        require(amount == 0 || allowance[msg.sender][spender] == 0, "use changeAllowance");
        allowance[msg.sender][spender] = amount;
        return true;
    }

    // Changes the allowance only if it still holds the value the owner saw. If the
    // spender used it in the meantime, this reverts instead of granting more.
    function changeAllowance(address spender, uint256 expected, uint256 amount) external returns (bool) {
        require(allowance[msg.sender][spender] == expected, "allowance changed");
        allowance[msg.sender][spender] = amount;
        return true;
    }

    // Lowering by a relative amount fails if the spender already used the allowance.
    function decreaseAllowance(address spender, uint256 subtracted) external returns (bool) {
        uint256 current = allowance[msg.sender][spender];
        require(current >= subtracted, "below zero");
        allowance[msg.sender][spender] = current - subtracted;
        return true;
    }

    function transferFrom(address from, address to, uint256 amount) external returns (bool) {
        allowance[from][msg.sender] -= amount;
        balanceOf[from] -= amount;
        balanceOf[to] += amount;
        return true;
    }
}
```

- Change from a known value. `changeAllowance(spender, expected, amount)` only works if the allowance still holds what the owner saw. If the spender used it first, the call reverts, and the spender keeps at most the old allowance, never both. SWC-114 suggests the same fix: an approve that takes the expected current allowance.
- Decrease relatively. `decreaseAllowance` subtracts from the current value and reverts if the spender already used more than would remain.
- Force two steps in approve, with care. The fixed `approve` only sets an allowance from zero or to zero, which protects owners who check what was spent between the steps. It also breaks integrations that re-approve without resetting first, and EIP-20 itself says the token contract shouldn't enforce it, so a token that has to stay compatible can leave `approve` as it is and add the safer functions next to it.
- On the owner's side, set zero first. Wallets and protocols that change an allowance should set zero, wait for it, check what the spender used, and only then set the new value. Approving the exact amount each operation needs, and resetting to zero afterwards, leaves nothing to race for.

In the same test, `changeAllowance` reverted with `allowance changed` once the spender had used the old allowance, and `decreaseAllowance` reverted instead of granting more.

## Has the approval race been exploited?

We know of no exploit with a published loss. It is a documented weakness, recorded in the token standard, the SWC registry and OpenZeppelin's docs:

| Protocol | ERC-20 approve race (a documented weakness, not an exploit) |
| --- | --- |
| Date | 19 November 2015 |
| Chain | Any EVM chain |
| Loss | None reported |
| What happened | EIP-20, the ERC-20 standard, created on 19 November 2015 and now Final, tells user interfaces to set an allowance to 0 before setting it to another value for the same spender, to prevent the attack its `approve` section links to, and says the token contract itself shouldn't enforce that ([EIP-20](https://eips.ethereum.org/EIPS/eip-20)). The SWC registry uses the same race as one of its two examples of transaction order dependence and suggests an approve that takes the expected current value ([SWC-114](https://swcregistry.io/docs/SWC-114/)). OpenZeppelin's docs for `approve` warn that someone may use both the old and the new allowance through unfortunate transaction ordering ([OpenZeppelin](https://docs.openzeppelin.com/contracts/api/token/erc20)). |
| Sources | [Ethereum Improvement Proposals](https://eips.ethereum.org/EIPS/eip-20), [SWC Registry](https://swcregistry.io/docs/SWC-114/), [OpenZeppelin docs](https://docs.openzeppelin.com/contracts/api/token/erc20) |

## Is the approval race condition still a real risk?

It needs three things: a non-zero allowance being changed to another non-zero value, a spender willing to front-run, and a mempool where the change is visible before it lands. An unlimited approval to a trusted contract never gets changed, so it never races, but it carries the larger risk of that contract being compromised. Rate it by who the spender is: low for a router you trust, higher when a protocol changes allowances it granted to parties it doesn't control.

## What should a reviewer check?

- In the token: does `approve` replace the allowance, and is there a way to change it from a known value or by a relative amount?
- In contracts that hold tokens: where do they call `approve`, and do they ever change a non-zero allowance to another non-zero value?
- Do those calls handle tokens that require resetting to zero first?
- Are approvals to routers and adapters exact and reset to zero after use, or unlimited and permanent?
- If a spender can be replaced or upgraded, what could the new one do with allowances already granted?
- Is there a test that front-runs an allowance change and asserts the spender can't take more than the larger of the two values?

## How does QuillShield check for approval race conditions?

Allowance bugs show up where tokens and the contracts that hold them meet, so put both in scope when you can. QuillShield runs a full audit of a Solidity codebase in a Foundry project. It builds a security graph of the code and lists the invariants the code must keep.

A separate reviewer agent tries to disprove every finding before you see it, and each finding that holds up comes with the exact lines, the impact, a recommended fix and, on full audits, a proof of concept. For an ordering bug like this one, the proof of concept is a test that runs the transactions in the attacker's order.

It then checks every pull request on GitHub or GitLab.com against that graph. QuillShield does not replace a manual audit. It reviews every change between audits, and you can [request a manual audit from QuillAudits](https://www.quillaudits.com/smart-contract-audit). [Start free](https://shield.quillaudits.com/register). New accounts get $50 in credit, good for up to half of an order.

## FAQ

### Does increaseAllowance fix the approval race?

Raising an allowance is safe, because the spender gets at most the old amount plus the increase, which the owner intended anyway. Lowering one safely needs a relative decrease that reverts if the spender already used too much, or a change from an expected value.

### Should a token refuse to change a non-zero allowance?

It protects careful owners, but it breaks contracts that re-approve without resetting first. If you add that rule, document it, and give integrators a function that changes the allowance from an expected value.

### Does permit avoid the race?

No. A permit sets the allowance to a value, the same way approve does, and it can be front-run as well.

### How do I test for the approval race in Foundry?

Approve 100, call `transferFrom` for 100 from the spender's address before the owner's change to 50 lands, then call it again for 50. Assert that the spender's total is at most 100; the vulnerable token above fails with 150.

## Sources

- [ERC-20: Token Standard, Ethereum Improvement Proposals](https://eips.ethereum.org/EIPS/eip-20) · checked 3 December 2026
- [SWC-114: Transaction Order Dependence, SWC Registry](https://swcregistry.io/docs/SWC-114/) · checked 3 December 2026
- [ERC 20 API, OpenZeppelin docs](https://docs.openzeppelin.com/contracts/api/token/erc20) · checked 3 December 2026
- [Smart Contract Weakness Classification Registry, SWC Registry](https://swcregistry.io/) · checked 3 December 2026
