Crypto Training

Audit Checklist: Safe ERC-20 Integration in a Hostile World

A review procedure for code that moves ERC-20 tokens: who picks the token, how calls are wrapped, what gets credited, how allowances and permits are handled, and which paths must keep working when a transfer fails.

Crypto Training•2026-02-06•8 min read

A protocol that accepts tokens inherits their behaviour, and the ERC-20 interface promises little about what that behaviour is. The adversarial token integration post catalogues the behaviours and the incidents behind them. This post is the procedure to run against the integrating code, arranged so that the cheap checks with the highest yield come first.

The steps at a glance#

StepQuestionWhat a failure looks like
1Who chooses the token?a permissionless market accepts a token the attacker wrote
2Is every token call wrapped?transfer reverts on USDT, or a failed transfer counts as success
3What amount is credited?the requested amount is booked for a fee-on-transfer token
4Can an outbound transfer block other users?one blocked recipient stops a batch or a liquidation
5How are allowances set and cleared?approve reverts on USDT, or a router keeps a leftover allowance
6Is permit optional?a front-run permit reverts the whole deposit
7Which amounts and decimals are assumed?18 decimals hard-coded, or type(uint256).max passed through
8What tells one token from another?a rescue function sweeps the underlying through a second address

1. Who chooses the token#

This question sets the threat model for every later step. When only governance can list tokens, the realistic risks are the known behaviours of those tokens and whatever their next upgrade brings. When a user can supply a token address, through pool creation, a swap path or a "reward token" parameter, the token is attacker code. Its balanceOf can return anything, its transfer can call back into the protocol, and its decimals can change between two calls.

To answer it, find every function parameter and stored address that is later used as a token and trace who can set it. The callback surfaces post describes two incidents, Penpie and Orion, that began with a user-supplied market or token.

2. Wrap every token call#

Search for .transfer(, .transferFrom( and .approve( on token types. Each should go through SafeERC20 or an equivalent library, for two reasons. USDT and similar tokens return nothing, so a call through the IERC20 interface reverts when the compiler finds no return data. ZRX and EURS return false on failure instead of reverting, so an unchecked call carries on after a transfer that did not happen.

Then look at which library it is. OpenZeppelin's implementation treats a call as successful if it returned exactly true, or if it returned nothing and the token address has code. The code check is the part that older copies of other libraries lack, since a low-level call to an address without code succeeds with empty return data. Solmate's SafeTransferLib gained the check in October 2024, so a vendored copy from before that date needs the caller to verify the token address itself.

3. Credit what arrived#

SOLIDITY
using SafeERC20 for IERC20;

function deposit(IERC20 token, uint256 amount) external nonReentrant {
    uint256 balanceBefore = token.balanceOf(address(this));
    token.safeTransferFrom(msg.sender, address(this), amount);
    uint256 received = token.balanceOf(address(this)) - balanceBefore;
    require(received > 0, "nothing received");

    _credit(msg.sender, token, received); // never `amount`
}

Every inbound transfer whose amount feeds accounting should look like this, or should require received == amount if the protocol has decided to reject fee-on-transfer tokens. The lock is part of the pattern, because a token that calls out during the transfer could otherwise nest a second deposit inside the measurement.

Three details are easy to miss in this step. Shares must be priced from totals taken before the deposit, or the depositor's own tokens dilute the price they pay. A user-supplied amount of type(uint256).max tells cUSDCv3 to move the sender's whole balance, so crediting amount there credits an absurd number. Outbound transfers have the mirror problem: when the protocol owes a recipient an exact amount and the token takes a fee, someone has to bear the difference, and the code should say who.

Routers show the failure clearly. Uniswap V2's swapExactTokensForTokens computes every hop's output from the nominal input, so with a fee-on-transfer input the pair receives less than the calculation assumed and its invariant check reverts the swap. The SupportingFeeOnTransferTokens variants exist because they read the pair's balance to find out what arrived. The Foundry post shows how to test the delta pattern.

4. Outbound transfers and liveness#

For each function that has to stay callable (withdraw, repay, liquidate, settle), list the token transfers it makes and who chose each recipient. A transfer to an address picked by a counterparty, such as the previous bidder, the borrower being liquidated or a fee recipient, can revert because the token has blocked that address or is paused. If the function cannot complete without that transfer, one address controls it for everyone.

The fix is to credit the amount and let the recipient claim it. Since Contracts 5.3, OpenZeppelin's trySafeTransfer returns false instead of reverting, which fits a branch that pays when it can and records a debt when it cannot. Skip transfers of zero as well, because some tokens revert on them. The gas griefing post covers this pattern together with the ETH version.

5. Allowances#

A bare approve through the IERC20 interface reverts on USDT for the return-data reason in step 2, and USDT also rejects a change from one non-zero allowance to another. forceApprove handles both cases, and safeIncreaseAllowance suits code that adds to an existing allowance. UNI and COMP store amounts in 96 bits and revert on larger values, apart from a special case for the maximum.

After a call to a router or another spender, check what is left. An allowance that the spender did not fully use stays in place, and it becomes the spender's bug budget. Resetting it to zero is the usual answer, with the caveat from weird-erc20 that BNB reverts on a zero approval.

The allowances that users have granted to your contract deserve the same attention. A contract that holds allowances and can be made to call an arbitrary target with arbitrary calldata will be used to call transferFrom on its users. Seneca (February 2024, about $6.4M) is one of several incidents on this site with that root cause.

6. Permit#

Treat a permit as a convenience that may fail. OpenZeppelin's IERC20Permit documentation gives the pattern: call permit inside try/catch, ignore the result, and let the following safeTransferFrom decide, since it reverts when no allowance exists. That one structure tolerates a front-run permit, a token without permit and a token whose fallback accepts the call silently. OpenZeppelin removed its safePermit helper in Contracts 5.0 in favour of this guidance.

Three more checks belong here. The owner passed to permit should be msg.sender, because a function that lets the caller name another owner turns existing allowances into deposits for the caller, as the Multichain disclosure showed. There should be an entry point that does not need a permit at all, since the same documentation points out that smart contract wallets cannot produce these signatures. DAI's permit has a different signature from ERC-2612, so it needs its own code path or no support. The signatures post covers replay and front-running in detail.

7. Amounts and decimals#

decimals() is optional in the standard and varies in practice. USDC has 6, Gemini USD has 2 and YAM-V2 has 24, so code that assumes 18 either loses precision or overflows. Read the value once when the token is listed, store it, and write the scaling so that the rounding direction is deliberate. The rounding post covers scaling between tokens with different decimals.

8. Telling tokens apart#

Rescue functions, "is this a pool token" checks and duplicate filters usually compare addresses. A token reachable through two addresses defeats them, as TUSD did for Compound's sweepToken until February 2022. Where the check protects funds, compare the contract's balance of the protected token before and after the call instead.

Rebasing belongs in this step too, because it changes what a stored amount means. If stETH, AMPL or a similar token can be listed, the accounting should be in shares of the contract's balance, or the protocol should accept only the non-rebasing wrapper.

Tests that cover the list#

The weird-erc20 repository ships minimal tokens for most of these behaviours, including TransferFee.sol, MissingReturns.sol, ReturnsFalse.sol, Reentrant.sol, BlockList.sol, Pausable.sol and Approval.sol. Running the existing test suite once per mock token is cheap and finds the unwrapped calls and nominal credits quickly. A fork test against the deployed USDT and USDC then covers what mocks cannot, such as proxy behaviour and real blocklist logic. For tokens the protocol issues itself, Trail of Bits' checklist recommends slither-check-erc and slither-prop.

The short version#

  1. Can a user choose the token, and if so, what can a hostile token do at each call site?
  2. Does every transfer and approval go through a wrapper that checks return data and code existence?
  3. Is accounting based on the measured balance change, under a reentrancy lock?
  4. Can a reverting or blocked transfer stop a function that other users depend on?
  5. Are allowances exact, cleared after use, and unreachable through arbitrary calls?
  6. Does the flow still work when permit fails or does nothing?
  7. Are decimals read from the token and is rounding chosen on purpose?
  8. Do rescue and identity checks hold for a token with two addresses or a rebasing balance?

Sources#