What are fee-on-transfer token issues?

Fee-on-transfer token issues happen when a contract assumes that a transfer of amount tokens delivers amount, but the token keeps part of it as a fee, so the contract receives less. The contract credits users for tokens it never received, and the shortfall lands on whoever withdraws last. Credit the balance difference measured around each incoming transfer, guard that measurement against reentrancy, or reject tokens that change amounts in transit.

Last updated . 6 minute read. By the QuillAudits Team.

How do fee-on-transfer tokens break contracts?

ERC-20 doesn't promise that transferFrom(from, to, amount) adds exactly amount to the balance of to. Some tokens deduct a fee and burn it or send it elsewhere, some can switch a fee on later, and rebasing tokens change balances without any transfer at all. Code written for plain tokens breaks on them in predictable places:

  • Deposits. The contract credits amount and holds less, and every deposit widens the gap between what users are owed and what is there.
  • Pool reserves. An AMM or lending pool that adds amount to its internal reserves drifts away from its real balance, and its prices follow the wrong number.
  • Forwarding. A router that receives tokens and forwards amount to the next contract fails, because it holds less than amount.
  • Payouts. Users receive less than the contract sent, and a contract on the receiving side that checks the amount reverts.
  • Other users pay. In the vulnerable vault below, a deposit and withdrawal of 100 brings in 98 and sends out 100, and the 2 missing tokens come out of everyone else's deposits.

The fee doesn't need to be large. A protocol that lets anyone list a token can be handed one whose fee or transfer logic was written to break its accounting.

What does vulnerable code look like?

Vulnerable: deposits credited with the requested amount (illustrative, not audited code) AmountTrustingVault.sol

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

interface IERC20 {
    function balanceOf(address account) external view returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
}

contract AmountTrustingVault {
    IERC20 public immutable token;
    mapping(address => uint256) public balances;
    uint256 public totalDeposits;

    constructor(IERC20 token_) {
        token = token_;
    }

    function deposit(uint256 amount) external {
        require(token.transferFrom(msg.sender, address(this), amount), "transfer failed");
        // With a 2% fee token the vault received 98% of `amount`, but credits all of it.
        balances[msg.sender] += amount;
        totalDeposits += amount;
    }

    function withdraw(uint256 amount) external {
        balances[msg.sender] -= amount;
        totalDeposits -= amount;
        // Pays out of other users' deposits until the last ones can't withdraw.
        require(token.transfer(msg.sender, amount), "transfer failed");
    }
}

With a token that keeps 2% of every transfer, two users who each deposit 100 tokens are credited 200 in total while the vault holds 196. The first withdrawal of 100 succeeds and leaves 96, and the second user can't withdraw their 100. A Foundry test with a mock 2% fee token shows exactly that.

How do you fix it?

Fixed: credit the measured balance difference, under a reentrancy guard (illustrative, not audited code) BalanceDeltaVault.sol

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

interface IERC20 {
    function balanceOf(address account) external view returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
}

contract BalanceDeltaVault {
    IERC20 public immutable token;
    mapping(address => uint256) public balances;
    uint256 public totalDeposits;
    uint256 private locked = 1;

    // Measuring a balance difference is only safe if nothing can run in between
    // the two reads, so a token with transfer hooks can't call back in.
    modifier nonReentrant() {
        require(locked == 1, "reentrant call");
        locked = 2;
        _;
        locked = 1;
    }

    constructor(IERC20 token_) {
        token = token_;
    }

    function deposit(uint256 amount) external nonReentrant returns (uint256 received) {
        uint256 before = token.balanceOf(address(this));
        require(token.transferFrom(msg.sender, address(this), amount), "transfer failed");
        received = token.balanceOf(address(this)) - before; // credit what arrived, not what was asked
        require(received > 0, "nothing received");
        balances[msg.sender] += received;
        totalDeposits += received;
    }

    function withdraw(uint256 amount) external nonReentrant {
        balances[msg.sender] -= amount;
        totalDeposits -= amount;
        // The user may get less than `amount` if the token also charges on the way out.
        require(token.transfer(msg.sender, amount), "transfer failed");
    }
}
  • Credit what arrived. Read the contract's balance before and after transferFrom and credit the difference. In the same test, the fixed vault credits 98 for a deposit of 100.
  • Guard the measurement. A token with transfer hooks can call back between the two balance reads and make one deposit count twice, so both functions sit behind a reentrancy guard.
  • Or refuse such tokens. If the protocol isn't meant to support them, require that the difference equals amount and revert otherwise. That also catches a token that switches a fee on after it was listed.
  • Keep your own reserves. Price from internal accounting and compare it with the real balance, so a fee or a donation can't move prices on its own.
  • Quote net amounts. Tell users, and any contract that receives tokens from you, that they may receive less than the amount sent.

How has a fee-on-transfer token drained a pool?

Protocol Balancer STA pools
Date 28 June 2020
Chain Ethereum
Loss about $452,000 by The Block's count; CertiK put the attacker's profit at 565.5 ETH, about $127,250
What happened Balancer pools kept an internal record of each token balance and priced trades from it. STA burned 1% of every transfer, so on each trade the pool expected to receive more STA than arrived, as Balancer's post-mortem explains. According to SlowMist, the attacker borrowed WETH through a dYdX flash loan and called swapExactAmountIn 24 times to bring the pool's STA balance close to zero. It then kept trading small amounts of STA into the pool and calling gulp(), a function anyone could call that overwrites the pool's record with the balance it actually holds (CertiK). With almost no STA left, STA was priced extremely high against the pool's WETH, WBTC, LINK and SNX, and the attacker sold it for them. Balancer said two pools were drained, one holding STA and one holding STONK.
Sources Balancer, SlowMist, CertiK, The Block

The pool credited what each trade was supposed to deliver, not what arrived, and a public function let the attacker reset its records whenever that helped. Balancer's post-mortem says this is why STA had been left off its BAL mining whitelist, which didn't stop a pool holding STA from being drained. Measure what arrives, as the fixed vault does, and review every function that rewrites recorded balances as part of the attack surface.

Which token behaviors need the same care?

Behavior What breaks Defense
Fee on transfer Deposits and reserves credited with more than arrived Credit the balance difference, or reject the token
A fee that can be switched on later A contract that was safe when the token was listed Check the difference on every transfer, not once at listing
Rebasing balances Balances that change without a transfer, up or down Account in shares of the balance, or accept only a non-rebasing wrapper
Transfer hooks Callbacks in the middle of a deposit A reentrancy guard around balance reads and state changes
No return value, or false instead of a revert Code that assumes transferFrom returns true A wrapper such as OpenZeppelin's SafeERC20, which treats false as a failure and still works with tokens that return nothing

What should a reviewer check?

  • Which tokens can each contract receive: a fixed list, governance-listed tokens, or anything a user passes in?
  • Does every incoming transfer credit the measured balance difference, or revert when it differs from amount?
  • Are the functions that measure a balance difference protected against reentrancy?
  • Are internal reserves or totals ever increased by amount instead of what was received?
  • Does any contract forward amount after receiving it, and does it actually hold that much?
  • Can a listed token add a fee or change behavior through an upgrade, and what happens to existing deposits then?
  • Do tests run the core flows with a mock token that keeps a fee on every transfer?

How does QuillShield check for fee-on-transfer token issues?

Whether this is a bug depends on which tokens your protocol accepts, so say so up front. Before an audit you can add context such as the whitepaper, trust assumptions, privileged roles and previous audit reports, and the list of supported tokens belongs in the trust assumptions.

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. Put the one this class breaks into your own tests too: the sum of user balances never exceeds what the contract holds.

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. It then checks every pull request on GitHub or GitLab.com against that graph. New markets and token listings often reuse deposit code written for plain tokens, so they deserve that check.

Run a full audit on the contracts that accept tokens. New accounts get $50 in credit, good for up to half of an order.

FAQ

How do I know if a token charges a fee on transfer?

Transfer a known amount to a fresh address in a fork test and compare the balance change. Read the token's code too, because a fee can be zero today and switched on later.

Is measuring the balance difference always safe?

Only with a reentrancy guard. A token with transfer hooks can call back between the two balance reads and make one deposit count twice.

Should my protocol support fee-on-transfer tokens?

Only if you need them, because support means balance-difference accounting everywhere tokens move. If you don't, revert when the received amount differs from the amount sent, and say so in your docs.

Can a fee-on-transfer token be used to drain a pool?

It can when the pool trusts amounts instead of balances. Every swap or deposit credited with the full amount moves the pool's records further from what it holds, and a pool that prices from those records can be traded against at the wrong price.

Sources

QuillShield is building the security layer between AI and money, so you can keep shipping between audits.

Sign-up is free, and new accounts get $50 in credit, good for up to half of an order.

  1. 1Sign up and import a repo
  2. 2See the exact price before anything is charged
  3. 3Approve it, and the audit starts