Skip to content
Smart contracts Audit finding Intermediate

changeFeeQuote will fail for low decimal ERC20 tokens

The changeFeeQuote function in PrivatePool calculates a fee exponent by subtracting 4 from the token's decimals. For ERC20 tokens with fewer than 4 decimals, this subtraction underflows and reverts, causing any call to change or changeFeeQuote to fail.

adriro Follow with ToonVH, saian and 23 others

Originally disclosed on Code4rena

Submitted by adriro, also found by ToonVH, saian, 0x5rings, joestakey, anodaram, giovannidisiena, chaduke, Koolex, cryptonue, 0x5rings, aviggiano, T1MOH, shaka, 0xLanterns, ayden, ElKu, 0xbepresent, 0x4non, Brenzee, ck, 0xWeiss, indijanc, Naubit, yixxas, and Kek

Private pools have a “change” fee setting that is used to charge fees when a change is executed in the pool (user swaps tokens for some tokens in the pool). This setting is controlled by the changeFee variable, which is intended to be defined using 4 decimals of precision:

https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L87-L88

87:     /// @notice The change/flash fee to 4 decimals of precision. For example, 0.0025 ETH = 25. 500 USDC = 5_000_000.
88:     uint56 public changeFee;

As the comment says, in the case of ETH a value of 25 should represent 0.0025 ETH. In the case of an ERC20 this should be scaled accordingly based on the number of decimals of the token. The implementation is defined in the changeFeeQuote function.

https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L731-L738

731:     function changeFeeQuote(uint256 inputAmount) public view returns (uint256 feeAmount, uint256 protocolFeeAmount) {
732:         // multiply the changeFee to get the fee per NFT (4 decimals of accuracy)
733:         uint256 exponent = baseToken == address(0) ? 18 - 4 : ERC20(baseToken).decimals() - 4;
734:         uint256 feePerNft = changeFee * 10 ** exponent;
735:
736:         feeAmount = inputAmount * feePerNft / 1e18;
737:         protocolFeeAmount = feeAmount * Factory(factory).protocolFeeRate() / 10_000;
738:     }

As we can see in the previous snippet, in case the baseToken is an ERC20, then the exponent is calculated as ERC20(baseToken).decimals() - 4. The main issue here is that if the token decimals are less than 4, then the subtraction will cause an underflow due to Solidity’s default checked math, causing the whole transaction to be reverted.

Such tokens with low decimals exist, one major example is GUSD, Gemini dollar, which has only two decimals. If any of these tokens is used as the base token of a pool, then any call to the change will be reverted, as the scaling of the charge fee will result in an underflow.

Proof of Concept

In the following test we recreate the “Gemini dollar” token (GUSD) which has 2 decimals and create a Private Pool using it as the base token. Any call to change or changeFeeQuote will be reverted due to an underflow error.

Note: the snippet shows only the relevant code for the test. Full test file can be found here.

function test_PrivatePool_changeFeeQuote_LowDecimalToken() public {
    // Create a pool with GUSD which has 2 decimals
    ERC20 gusd = new GUSD();

    PrivatePool privatePool = new PrivatePool(
        address(factory),
        address(royaltyRegistry),
        address(stolenNftOracle)
    );
    privatePool.initialize(
        address(gusd), // address _baseToken,
        address(milady), // address _nft,
        100e18, // uint128 _virtualBaseTokenReserves,
        10e18, // uint128 _virtualNftReserves,
        500, // uint56 _changeFee,
        100, // uint16 _feeRate,
        bytes32(0), // bytes32 _merkleRoot,
        false, // bool _useStolenNftOracle,
        false // bool _payRoyalties
    );

    // The following will fail due an overflow. Calls to `change` function will always revert.
    vm.expectRevert();
    privatePool.changeFeeQuote(1e18);
}

Recommended Mitigation Steps

The implementation of changeFeeQuote should check if the token decimals are less than 4 and handle this case by dividing by the exponent difference to correctly scale it (i.e. chargeFee / (10 ** (4 - decimals))). For example, in the case of GUSD with 2 decimals, a chargeFee value of 5000 should be treated as 0.50.

outdoteth (Caviar) acknowledged

Alex the Entreprenerd (judge) commented:

I have considered downgrading as I don’t believe most tokens meet this requirement.

That said, I believe the finding is valid per our rules, with some tokens, taking the changeFeeQuote will revert due to an assumption that decimals - 4 wouldn’t revert.

The contracts cannot be used for those tokens, but since this is contingent on using such a low decimal token, I agree with Medium Severity and believe a nofix to be fine since most Stablecoins have more than 4 decimals.


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

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The code assumes token decimals are always at least 4, so subtracting 4 underflows for tokens with fewer decimals.

Impact

Pools using tokens with fewer than 4 decimals cannot process changes, as all calls to change or changeFeeQuote revert.

What the attacker needed

Pool uses a token with <4 decimals

The fix

The changeFeeQuote function should check if token decimals are less than 4 and scale the fee by dividing instead of multiplying.

How it was found

  1. Create a private pool using a token with 2 decimals (e.g., GUSD) as the base token.
  2. Call changeFeeQuote or change on the pool.
  3. The transaction reverts due to underflow.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

Some tokens, like Gemini dollar, only have 2 decimal places instead of the usual 18. The pool's fee calculation assumes at least 4 decimals, so when it subtracts 4 from 2, the number goes negative and the transaction fails. This means users cannot swap tokens in pools that use such low-decimal tokens.

What is Smart contracts?

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

More by adriro

View all

57 more disclosed reports, best first.

More from Caviar Private Pools

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.