Block: 60

Timestamp: 02:15:21

AuditProfile

Security blog

Possible problems with totalSupply

While reading bug reports on Code4rena, I noticed an interesting one closely related to the unusual implementation of totalSupply. How do you think the next one code formula can cause you a Med severity problem in your protocol?

return (curves.curvesTokenSupply(token) - curves.curvesTokenBalance(token, address(curves))) * PRECISION;

First of all, let's take a look on the totalSupply() implementaion:

    function totalSupply(address token) public view returns (uint256) {
        //@dev: this is the amount of tokens that are not locked in the contract. The locked tokens are in the ERC20 contract
        return (curves.curvesTokenSupply(token) - curves.curvesTokenBalance(token, address(curves))) * PRECISION;
    }

It is used during token transfers in an addFee() call.

    function _transferFees(
        address curvesTokenSubject,
        bool isBuy,
        uint256 price,
        uint256 amount,
        uint256 supply
    ) internal {
        (uint256 protocolFee, uint256 subjectFee, uint256 referralFee, uint256 holderFee, ) = getFees(price);
        {
            ...

            if (feesEconomics.holdersFeePercent > 0 && address(feeRedistributor) != address(0)) {
                feeRedistributor.onBalanceChange(curvesTokenSubject, msg.sender);
                feeRedistributor.addFees{value: holderFee}(curvesTokenSubject);
            }
        }
        ...
    }
    function addFees(address token) public payable onlyManager {
        uint256 totalSupply_ = totalSupply(token);
        if (totalSupply_ == 0) revert NoTokenHolders();
        TokenData storage data = tokensData[token];
        data.cumulativeFeePerToken += (msg.value * PRECISION) / totalSupply_;
    }

So =addFees() reverts if the totalSupply of the token is 0. Now consider the following scenario:

1. Alice buys 1 Curve token to initiate herself as a token subject and withdraws her token to ERC20.

2. Bob buys 1 Curve token.

3. Charlie buys 10 Curve tokens and immediately withdraws all of them to the ERC20 contract.

4. Bob tries to sell 1 Curve token but is not able to because all other tokens are ERC20.

Bob’s transaction will revert because FeeSplitter.totalSupply() will return 0.

As a recommendation, auditor offers to check if the contract balance matches the total supply, and if so, send the holder fee to the protocol.

Use caution when adding unusual functionality to your protocols, and always check for potential vulnerabilities.

Read the full report here:

Link: https://code4rena.com/reports/2024-01-curves#m-07-selling-will-be-bricked-if-all-other-tokens-are-withdrawn-to-erc20-token

#totalsupply

#token

Connent with me:

Регистрация прошла успешно! Спасибо за внимание!

loader