Skip to content
Business logic Audit finding Intermediate

Incorrect protocol fee is taken when changing NFTs

The protocol fee for changing NFTs was calculated as a percentage of the pool fee rather than the input amount, making it extremely low. This resulted in a loss of yield for the Caviar protocol.

Voyvoda Follow with saian, GT_Blockchain and 7 others

Originally disclosed on Code4rena

Submitted by Voyvoda, also found by saian, GT_Blockchain, Josiah, CodingNameKiki, JGcarv, DishWasher, neumo, RaymondFam, and J4de

Incorrect protocol fee is taken when changing NFTs which results in profit loss for the Caviar protocol.

Proof of Concept

The protocol fee in changeFeeQuote is calculated as a percentage of the feeAmount which is based on the input amount:

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

function changeFeeQuote(uint256 inputAmount) public view returns (uint256 feeAmount, uint256 protocolFeeAmount) {
    ...
    protocolFeeAmount = feeAmount * Factory(factory).protocolFeeRate() / 10_000;

This seems wrong as in buyQuote and sellQuote the protocol fee is calculated as a percentage of the input amount, not the pool fee amount:

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

function buyQuote(uint256 outputAmount)
    ...
    protocolFeeAmount = inputAmount * Factory(factory).protocolFeeRate() / 10_000;

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

function sellQuote(uint256 inputAmount)
    ...
    protocolFeeAmount = outputAmount * Factory(factory).protocolFeeRate() / 10_000;

This makes the protocol fee extremely low meaning a profit loss for the protocol.

Recommended Mitigation Steps

protocolFeeAmount in changeFeeQuote should be a percentage of the input amount instead of the pool fee.

outdoteth (Caviar) confirmed, but disagreed with severity and commented:

There is no risk of fund loss here. But agree that this is an issue.

outdoteth (Caviar) mitigated:

Fix is here: https://github.com/outdoteth/caviar-private-pools/pull/13.

Proposed fix is to add a separate fee called protocolChangeFeeRate which can be much higher than protocolFeeRate. For example, protocolChangeFeeRate could be on the order of ~20-30%. For example, if the fixed changeFee is 0.1 ETH, the NFT is worth 1.5 ETH, and the protocolChangeFeeRate is 30%, then the protocol fee would be 0.03 ETH on a change() or flashLoan().

function changeFeeQuote(uint256 inputAmount) public view returns (uint256 feeAmount, uint256 protocolFeeAmount) {
    // multiply the changeFee to get the fee per NFT (4 decimals of accuracy)
    uint256 exponent = baseToken == address(0) ? 18 - 4 : ERC20(baseToken).decimals() - 4;
    uint256 feePerNft = changeFee * 10 ** exponent;

    feeAmount = inputAmount * feePerNft / 1e18;
    protocolFeeAmount = feeAmount * Factory(factory).protocolChangeFeeRate() / 10_000;
}

Alex the Entreprenerd (judge) decreased severity to Medium and commented: The Warden has shown an inconsistency in how protocolFees are computed, because this is limited to a loss of Yield, I believe Medium Severity to be more appropriate.

Status: Mitigation confirmed. Full details in reports from rbserver, KrisApostolov, and rvierdiiev.


The original report as published on Code4rena, by Voyvoda. 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

Likely, the changeFeeQuote function calculated the protocol fee based on the pool fee amount instead of the input amount, unlike buyQuote and sellQuote.

Impact

The protocol fee was extremely low, causing a profit loss for the protocol; the judge rated this as Medium severity because it is limited to a loss of yield.

What the attacker needed

None

The fix

The fix was to add a separate protocolChangeFeeRate and calculate the protocol fee as a percentage of the fee amount using that rate.

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.

In a smart contract, a fee is taken when users change NFTs. The fee was calculated incorrectly, so the protocol received much less money than it should have. This meant the protocol lost out on potential earnings.

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 Voyvoda

View all

2 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.

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.