Skip to content
Smart contracts Audit finding Intermediate

ERC20 tokens with different decimals than 18 leads to loss of funds

The contract calculates rewards using a hardcoded 18-decimal value for all ERC20 tokens, so pools using tokens with different decimals (such as USDC with 6 decimals) produce incorrect rewards and cause loss of funds.

reassor Follow

Originally disclosed on Code4rena

Submitted by reassor, also found by hyh, IllIllI, kenzo, leastwood, rajatbeladiya, VAD37, and ych18

https://github.com/code-423n4/2022-05-factorydao/blob/db415804c06143d8af6880bc4cda7222e5463c0e/contracts/PermissionlessBasicPoolFactory.sol#L169

https://github.com/code-423n4/2022-05-factorydao/blob/db415804c06143d8af6880bc4cda7222e5463c0e/contracts/PermissionlessBasicPoolFactory.sol#L282

Impact

Contract PermissionlessBasicPoolFactory calculates rewards by using hardcoded value of decimals 18 (1e18) for ERC20 tokens. This leads to wrong rewards calculations and effectively loss of funds for all pools that will be using ERC20 tokens with different decimals than 18. Example of such a token is USDC that has 6 decimals only.

Proof of Concept

Tools Used

Manual Review / VSCode

Recommended Mitigation Steps

It is recommended to add support for different number of decimals than 18 by dynamically checking decimals() for the tokens that are part of the rewards calculations. Alternatively if such a support is not needed, new require statements should be added to addPool that will be checking that the number of decimals for all ERC20 tokens is 18.

illuzen (FactoryDAO) confirmed


The original report as published on Code4rena, by reassor. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 4 min read.

Context

FactoryDAO ran a Code4rena audit contest in May 2022 covering its contracts, and this finding concerns PermissionlessBasicPoolFactory, the contract that creates and manages permissionless reward pools. A pool holds ERC20 tokens and pays out rewards to participants; the factory is responsible for computing those rewards.

The reward calculation in PermissionlessBasicPoolFactory assumes every ERC20 token involved has 18 decimals. It uses a hardcoded value of 1e18 rather than reading the token's own decimals(). Any pool built on a token with a different precision, such as USDC with 6 decimals, will therefore compute rewards incorrectly.

The finding was submitted by reassor and independently reported by hyh, IllIllI, kenzo, leastwood, rajatbeladiya, VAD37 and ych18. It was rated medium severity and disclosed on 2 August 2022.

Setup

The review was a manual code review carried out in VSCode. No accounts, deployed environment or special tooling were needed: the issue is visible by reading the contract source. The relevant code is in PermissionlessBasicPoolFactory.sol, at two locations in the repository revision under audit:

Finding it

The researcher noticed that the reward maths in PermissionlessBasicPoolFactory uses the literal 1e18 as the unit for token amounts. That literal encodes the assumption that one whole token is 10^18 base units, which is only true for 18-decimal tokens. The contract never queries decimals() on the tokens it handles, so nothing corrects the assumption at runtime.

Because the same hardcoded scale is used at both line 169 and line 282, the error affects the reward calculation rather than a single isolated branch. The researcher cites USDC, with 6 decimals, as a concrete example of a token that would trigger the problem.

Exploitation

The report does not give a transaction-level walkthrough; it identifies the faulty computation and its consequence. The steps below follow the reasoning as presented.

Step 1: Create a pool with a non-18-decimal token

A pool can therefore be created around a token such as USDC, which has 6 decimals.

Step 2: Rewards are computed against a hardcoded 1e18 scale

The reward calculation at line 169 and line 282 uses 1e18 as the assumed size of one token.

Step 3: Participants receive wrong rewards

The result is incorrect reward amounts for every pool using a token with decimals other than 18. The researcher characterises the outcome as an effective loss of funds for those pools.

Root cause

The contract hardcodes the ERC20 precision as 18 instead of deriving it from the token. ERC20 does not mandate 18 decimals; decimals() is part of the standard precisely because tokens differ, and USDC's 6 decimals is a common example. The report does not describe any other defence that would have caught the mismatch.

Impact

Every pool that uses an ERC20 token with decimals other than 18 will calculate rewards wrongly, and the researcher states this leads to loss of funds for those pools. The report does not quantify the loss or describe a specific drain scenario beyond the miscalculation itself.

Fix

The recommended mitigation is to support tokens with a number of decimals other than 18 by dynamically checking decimals() for the tokens that take part in the reward calculations. If that support is not wanted, the alternative is to add new require statements in addPool that check the number of decimals for all ERC20 tokens is 18, rejecting any pool that would break the arithmetic.

FactoryDAO's illuzen confirmed the finding in the contest issue thread.

Variants to test

  • Any contract that scales token amounts with a literal 1e18. Grep the codebase for 1e18, 10**18 and similar constants in arithmetic that touches token balances. A positive result is a code path where the constant stands in for one whole token and the token's decimals() is never read.
  • Pool, vault and staking factories that accept arbitrary ERC20 tokens. Check whether the creation function validates precision. A positive result is an addPool-style function with no decimals check and downstream maths that assumes 18.
  • Tokens with more than 18 decimals as well as fewer. The same mismatch applies in both directions; a positive result is any token whose decimals() differs from the hardcoded value and reaches the calculation.
  • Reward and share calculations that mix two tokens. If one leg is 18-decimal and the other is not, check whether a single hardcoded scale is applied to both. A positive result is a ratio or payout computed with one constant for tokens of different precision.

Hunting notes

This class of bug hides in arithmetic that converts between human-readable token amounts and base units. During review, look for hardcoded 1e18 constants in contracts that are explicitly permissionless or generic, since those are designed to accept tokens the author did not enumerate. The signal is a factory or pool contract whose creation function takes a token address and performs no decimals() lookup or precision validation before storing it.

The project confirmed the finding, and the report was rated medium severity.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 8 min read.

What happened, in plain words

FactoryDAO is a project that ran a public code review contest in May 2022 on Code4rena, a platform where independent reviewers look through a project's code and report problems. This article is about one of those reports.

The piece of code involved is a smart contract called PermissionlessBasicPoolFactory. A smart contract is a program that lives on a blockchain and runs automatically when people send it instructions. This particular contract creates and manages "pools": a pool is a place where digital tokens are collected and later paid out as rewards to the people who take part. The word "permissionless" means anyone can create a pool, without asking the project for approval first.

The tokens in question are ERC20 tokens. ERC20 is a common standard for tokens on Ethereum-style blockchains: it is a fixed list of rules that a token contract must follow so that wallets and other programs know how to handle it. One of those rules is a function called decimals(), which tells you how a token's smallest unit relates to one whole token.

That last point is the heart of the bug. Tokens do not all split into the same number of tiny pieces. Many tokens use 18 decimals, meaning one whole token is made of 10 to the power of 18 base units. Others use fewer, for example USDC, which uses 6 decimals, so one whole USDC is 10 to the power of 6 base units. The factory contract ignores this and always assumes 18. It writes the number 1e18 directly into its reward maths instead of asking each token for its real decimals() value.

The consequence: any pool built on a token that does not use 18 decimals will calculate rewards wrongly, and the researcher describes this as an effective loss of funds for those pools. Pools using 18-decimal tokens are not affected.

The finding was submitted by reassor, and the same issue was independently found by hyh, IllIllI, kenzo, leastwood, rajatbeladiya, VAD37 and ych18. It was rated medium severity and disclosed on 2 August 2022. A member of the FactoryDAO team, illuzen, confirmed it.

Setup

No special environment was needed to find this. The review was a manual code review, done by reading the source code in VSCode, a free code editor. There were no accounts to create, no deployed test network, and no automated tools involved: the problem is visible simply by reading the contract.

The relevant file is PermissionlessBasicPoolFactory.sol, and the problem appears in two places in the version of the code that was under review:

Those links point at a specific snapshot of the project's code on GitHub, identified by a long commit hash. A commit is a saved version of a codebase; pinning the link to one commit means the reader sees exactly the lines the researcher saw, even if the project changes the file later.

Finding it

The researcher noticed that the reward maths in PermissionlessBasicPoolFactory uses the literal 1e18 as the unit for token amounts. In programming, 1e18 is shorthand for 1 followed by 18 zeros, that is, 10 to the power of 18. Writing it into the code encodes an assumption: that one whole token equals 10 to the power of 18 base units. That is only true for tokens with 18 decimals.

The contract never asks the tokens it handles for their decimals(), so nothing corrects the assumption while the contract runs. A useful everyday comparison: imagine a shop that always assumes a customer is paying in pounds, even when the price list is in a different currency. The arithmetic still runs, but the totals are meaningless for anyone paying in something else.

Because the same hardcoded scale is used at both line 169 and line 282, the error affects the reward calculation as a whole rather than one isolated branch of the code. The researcher cites USDC, with 6 decimals, as a concrete example of a token that would trigger the problem.

Exploitation

The report does not give a transaction-level walkthrough. It identifies the faulty computation and its consequence. The steps below follow the reasoning as presented.

Step 1: Create a pool with a non-18-decimal token

A pool can therefore be created around a token such as USDC, which has 6 decimals.

Step 2: Rewards are computed against a hardcoded 1e18 scale

The reward calculation at line 169 and line 282 uses 1e18 as the assumed size of one token.

Step 3: Participants receive wrong rewards

The result is incorrect reward amounts for every pool using a token with decimals other than 18. The researcher characterises the outcome as an effective loss of funds for those pools.

Root cause

The contract hardcodes the ERC20 precision as 18 instead of deriving it from the token. ERC20 does not mandate 18 decimals; decimals() is part of the standard precisely because tokens differ, and USDC's 6 decimals is a common example. The report does not describe any other defence that would have caught the mismatch.

Impact

Every pool that uses an ERC20 token with decimals other than 18 will calculate rewards wrongly, and the researcher states this leads to loss of funds for those pools. The report does not quantify the loss or describe a specific drain scenario beyond the miscalculation itself.

Fix

The recommended mitigation is to support tokens with a number of decimals other than 18 by dynamically checking decimals() for the tokens that take part in the reward calculations. "Dynamically" here means asking the token contract at the moment it is needed, rather than writing a fixed number into the code.

If that support is not wanted, the alternative is to add new require statements in addPool that check the number of decimals for all ERC20 tokens is 18, rejecting any pool that would break the arithmetic. A require statement is a check that makes the contract stop and refuse the action if the condition is not met, like a ticket gate that will not open without a valid ticket.

FactoryDAO's illuzen confirmed the finding in the contest issue thread.

Variants to test

These are the wider patterns to look for when reviewing similar code.

  • Any contract that scales token amounts with a literal 1e18. Search the codebase for 1e18, 10**18 and similar constants in arithmetic that touches token balances. A positive result is a code path where the constant stands in for one whole token and the token's decimals() is never read.
  • Pool, vault and staking factories that accept arbitrary ERC20 tokens. Check whether the creation function validates precision. A positive result is an addPool-style function with no decimals check and downstream maths that assumes 18.
  • Tokens with more than 18 decimals as well as fewer. The same mismatch applies in both directions; a positive result is any token whose decimals() differs from the hardcoded value and reaches the calculation.
  • Reward and share calculations that mix two tokens. If one leg is 18-decimal and the other is not, check whether a single hardcoded scale is applied to both. A positive result is a ratio or payout computed with one constant for tokens of different precision.

Hunting notes

This class of bug hides in arithmetic that converts between human-readable token amounts and base units. During review, look for hardcoded 1e18 constants in contracts that are explicitly permissionless or generic, since those are designed to accept tokens the author did not enumerate. The signal is a factory or pool contract whose creation function takes a token address and performs no decimals() lookup or precision validation before storing it.

The project confirmed the finding, and the report was rated medium severity.

Glossary

  • Smart contract: a program stored on a blockchain that runs automatically when someone sends it a transaction.
  • ERC20: a widely used standard that defines the functions and behaviour a token contract must implement so wallets and other programs can handle it.
  • Token: a digital asset issued by a smart contract on a blockchain, following a standard such as ERC20.
  • Decimals: a number reported by a token contract that says how many base units make up one whole token; 18 decimals means one token equals 10 to the power of 18 base units.
  • Base unit: the smallest indivisible amount of a token; whole tokens are counted in these units inside contracts.
  • 1e18: programming shorthand for 10 to the power of 18, that is, 1 followed by 18 zeros.
  • Hardcoded: a value written directly into the code instead of being read from somewhere at runtime.
  • decimals(): the ERC20 function that returns a token's number of decimals.
  • Pool: in this context, a contract that holds tokens and pays out rewards to participants.
  • Factory: a contract whose job is to create other contracts or pools on demand.
  • Permissionless: open to anyone, with no approval step or allowlist required.
  • addPool: the function on PermissionlessBasicPoolFactory that creates a new pool.
  • require: a check in a smart contract that stops the action and rejects it if a condition is not satisfied.
  • Reward calculation: the arithmetic that decides how many tokens each participant receives.
  • Loss of funds: a situation where tokens are paid out in the wrong amount, so value is lost or misallocated.
  • Severity: the rating given to a finding to express how serious its impact is; this one was rated medium.
  • Code4rena: a platform that runs public contests in which independent reviewers audit a project's code for payment.
  • Commit: a saved version of a codebase; links can be pinned to one commit so the code does not change under the reader.
  • VSCode: a free code editor used to open and read source files.
  • Manual code review: reading code by hand to look for problems, as opposed to running automated scanning tools.
  • Mitigation: a change to the code that removes or reduces the problem.
  • USDC: a real token that uses 6 decimals, cited in the report as an example of a token that would trigger the bug.

More by reassor

View all

5 more disclosed reports, best first.

More from FactoryDAO contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.