Skip to content
Business logic Audit finding Intermediate

There is a logical error in the _distributeFees() function, resulting in an unfair distribution of fees

ZeroTrust Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-08-flayer-judging/issues/328

Found by

ZeroTrust

Summary

There is a logical error in the _distributeFees() function, resulting in an unfair distribution of fees.

Vulnerability Detail

JavaScript
        function _distributeFees(PoolKey memory _poolKey) internal {
        // If the pool is not initialized, we prevent this from raising an exception and bricking hooks
@>>        PoolId poolId = _poolKey.toId();
        PoolParams memory poolParams = _poolParams[poolId];

        if (!poolParams.initialized) {
            return;
        }

        // Get the amount of the native token available to donate
        uint donateAmount = _poolFees[poolId].amount0;

        // Ensure that the collection has sufficient fees available
        if (donateAmount < donateThresholdMin) {
            return;
        }

        // Reduce our available fees
        _poolFees[poolId].amount0 = 0;

        // Split the donation amount between beneficiary and LP
@>>        (uint poolFee, uint beneficiaryFee) = feeSplit(donateAmount);

        // Make our donation to the pool, with the beneficiary amount remaining in the
        // contract ready to be claimed.
        if (poolFee > 0) {
            // Determine whether the currency is flipped to determine which is the donation side
            (uint amount0, uint amount1) = poolParams.currencyFlipped ? (uint(0), poolFee) : (poolFee, uint(0));
            BalanceDelta delta = poolManager.donate(_poolKey, amount0, amount1, '');

            // Check the native delta amounts that we need to transfer from the contract
            if (delta.amount0() < 0) {
                _pushTokens(_poolKey.currency0, uint128(-delta.amount0()));
            }

            if (delta.amount1() < 0) {
                _pushTokens(_poolKey.currency1, uint128(-delta.amount1()));
            }

            emit PoolFeesDistributed(poolParams.collection, poolFee, 0);
        }

        // Check if we have beneficiary fees to distribute
        if (beneficiaryFee != 0) {
            // If our beneficiary is a Flayer pool, then we make a direct call
@>>            if (beneficiaryIsPool) {
                // As we don't want to make a transfer call, we just extrapolate
                // the required logic from the `depositFees` function.
@>>                _poolFees[_poolKeys[beneficiary].toId()].amount0 += beneficiaryFee;
                emit PoolFeesReceived(beneficiary, beneficiaryFee, 0);
            }
            // Otherwise, we can just update the escrow allocation
            else {
                beneficiaryFees[beneficiary] += beneficiaryFee;
                emit BeneficiaryFeesReceived(beneficiary, beneficiaryFee);
            }
        }
    }

If beneficiaryIsPool = true, then BaseImplementation::beneficiary is the Flayer protocol’s NFT Collection. We can also confirm this from the setBeneficiary() function.

JavaScript
    function setBeneficiary(address _beneficiary, bool _isPool) public onlyOwner {
        beneficiary = _beneficiary;
        beneficiaryIsPool = _isPool;

        // If we are setting the beneficiary to be a Flayer pool, then we want to
        // run some additional logic to confirm that this is a valid pool by checking
        // if we can match it to a corresponding {CollectionToken}.
@>>        if (_isPool && address(locker.collectionToken(_beneficiary)) == address(0)) {
            revert BeneficiaryIsNotPool();
        }

        emit BeneficiaryUpdated(_beneficiary, _isPool);
    }

The function checks that the NFT collection must have the corresponding collectionToken.

JavaScript
     function feeSplit(uint _amount) public view returns (uint poolFee_, uint beneficiaryFee_) {
        // If our beneficiary royalty is zero, then we can exit early and avoid reverts
        if (beneficiary == address(0) || beneficiaryRoyalty == 0) {
            return (_amount, 0);
        }

        // Calculate the split of fees, prioritising benefit to the pool
        beneficiaryFee_ = _amount * beneficiaryRoyalty / ONE_HUNDRED_PERCENT;
        poolFee_ = _amount - beneficiaryFee_;
    }

In the feeSplit() function, the fees are divided into two parts: poolFee (95%) and beneficiaryFee (5%). The poolFee goes to the LP Holders of the collectionToken for some NFT, while the beneficiaryFee goes to the LP Holders of the collectionToken for the Flayer protocol’s NFT.

The root cause of the issue is that when _distributeFees() is called for the collectionToken of the Flayer protocol’s NFT Collection (which we will refer to as the collectionToken of Flayer), _poolKeys[beneficiary].toId() and PoolId poolId = _poolKey.toId(); result in the same poolId. This leaves 5% of the fees undistributed, which is clearly wrong. It should distribute 100% of the fees to the current LP Holders.

According to the current logic in the code, when _distributeFees() is called for the collectionToken of the Flayer protocol’s NFT, it always leaves 5% of the fees unallocated. If a user provides liquidity to the pool, 95% of the fees will be distributed to the original LP Holders. However, the new LP Holder, upon joining, will immediately gain a share of the remaining 5% of the fees. That's wrong.

Impact

The newly joined LP Holders receive an unfair portion of the fees, leading to a loss for the original LP Holders.

Code Snippet

https://github.com/sherlock-audit/2024-08-flayer/blob/main/flayer/src/contracts/implementation/UniswapImplementation.sol#L308

Tool used

Manual Review

Recommendation

Diff
     // Split the donation amount between beneficiary and LP
     (uint poolFee, uint beneficiaryFee) = feeSplit(donateAmount);

+    if(poolId==_poolKeys[beneficiary].toId()){
+        poolFee = donateAmount;
+        beneficiaryFee = 0;
+    }

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

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

What is known

A medium-severity business logic bug in Flayer. Sherlock made it public on 10 October 2024. Found in the 468 audit contest (finding M-11).

About Business logic

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

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

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by ZeroTrust

View all

14 more disclosed reports, best first.

More from Flayer

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.