Incorrect fee calculation on LBPair (fees collected on swaps are less than what they “should” be)
The LBPair contracts collected less swap fees than specified by their FeeParameters due to incorrect fee calculation logic in SwapHelper.getAmounts and related functions. An attacker could exploit this to pay lower fees, causing a loss of yield for liquidity providers.
Submitted by sha256yan, also found by Jeiwan
LBPair contracts consistently collect less fees than their FeeParameters.
Github and source code
https://github.com/sha256yan/incorrect-fee
Motivation and Severity
Note: please see warden’s original submission for full details.
LBpair contracts’ fees fall short by 0.1% on single bin with the deficit growing exponentially with multi-bin swaps.
This report will refer to this difference in fees, that is, the difference between the expected fees and the actual collected fees as the “Fee Deficit”.
The exponential growth of the Fee Deficit percentage is concerning, considering that the vast majority of the fees collected by LPs and DEXs are during high volatility periods.
Note that the peak Fee Deficit percentage of 1.6% means that 1.6% of expected fees would not be collected.
With an assumed average total fee of 1% (higher than usual due to variableFee component) and average Fee Deficit percentage of 0.4%;
The total Fee Deficit from a period similar to May 7th 2022 - May 14th 2022, with approximately $1.979B in trading volume, would be $79,160 over one week.
SwapHelper.getAmounts carries most of the blame for this error.
3 main causes have been identified and will be discussed in this report.
- Incorrect use of getFeeAmountFrom
- Incorrect conditional for amountIn overflow
- Need for an additional FeeHelper function
Affected contracts and libraries
-
LBPair.sol
-
LBRouter.sol
-
SwapHelper.sol
Proposed changes
-
FeeHelper.sol
- getAmountInWithFees ( New )
-
SwapHelper.sol
- getAmountsV2 ( New )
-
LBRouter.sol
- getSwapIn ( Modified )
- getSwapOut ( Modified )
-
LBPair.sol
- swap ( Modified )
Details
- As mentioned earlier, most issues arise from
SwapHelper.getAmounts. The SwapHelper library is often used for the Bin type. (Example in LBPair). The proposed solution includes the new functions SwapHelper.getAmountsV2 and FeeHelper.getAmountInWithFees. - LBPair.swap uses
_bin.getAmounts(...)on the active bin to calculate fees. (See here) - Inside of
SwapHelper.getAmounts, for a given swap, if a bin has enough liqudity, the fee is calculated using (FeeHelper.getFeeAmountFrom). This results in smaller than expected fees. LBRouter.getSwapOutrelies onSwapHelper.getAmountsto simulate swaps. Its simulations adjust to the correct fee upon usingSwapHelper.getAmountsV2(LBRouter.getSwapOut, SwapHelper.getAmounts, SwapHelper.getAmountsV2)LBRouter.getSwapInhas a fee calculation error which is independent ofSwapHelper.getAmounts. (See here)- As of right now the
LBPair.swapusing getAmountsV2 uses3.8%more gas.
Incorrect use of getFeeAmountFrom
- When there is enough liquidity in a bin for a swap, we should use
FeeHelper.getFeeAmount(amountIn)instead ofFeeHelper.getFeeAmountFrom(amountIn).
Evidence
- amountIn, the parameter passed to calculate fees, is the amount of tokens in the LBPair contract in excess of the reserves and fees of the pair for that token. Inside LBPair.sol - Inside TokenHelper
Will now use example numbers:
- Let amountIn = 1e10 (meaning the user has transferred/minted 1e10 tokens to the LBPair)
- Let PRECISION = 1e18
- Let totalFee = 0.00125 x precision (fee of 0.0125%)
- Let price = 1 (parity)
- If the current bin has enough liqudity, feeAmount must be: (amountIn * totalFee ) / (PRECISION) = 12500000
- FeeHelper.getFeeAmountFrom(amountIn) uses the formula: feeAmount = (amountIn * totalFee) / (PRECISION + totalFee) = 12484394
- FeeHelper.getFeeAmount(amountIn) uses exactly the formula ourlined in the correct feeAmount calculation and is the correct method in this case.
- Visit the tests section to run a test.
Incorrect condition for amountIn overflow
- The condition for when an amountIn overflows the maximum amount available in a bin is flawed.
- The Fee Deficit here could potentially trigger an unnecessary bin de-activation.
Evidence
Snippet 1 (SwapHelper.getAmounts)
fees = fp.getFeeAmountDistribution(fp.getFeeAmount(_maxAmountInToBin));
if (_maxAmountInToBin + fees.total <= amountIn) {
//do things
}
- Collecting the fees on
_maxAmountInToBinbefore doing so onamountInmeans we are not checking to see whetheramountInafter
Consider the following:
Snippet 2 (SwapHelper.getAmountsV2)
fees = fp.getFeeAmountDistribution(fp.getFeeAmount(amountIn));
if (_maxAmountInToBin < amountIn - fees.total) {
//do things
}
- Now, the fees are collected on
amountIn. - Assuming both conditions are true, the fees from Snippet2 will be necessarily larger than those in Snippet1 since in both cases
_maxAmountInToBin < amountIn. - Snippet 1 produces false positives. Meaning, SwapHelper.getAmounts changes its active bin id more than needed. (See Tests section at the bottom for the relevant test)
Need for an additional FeeHelper function
- There are currently functions to answer the following question: How many tokens must a user send, to end up with a given amountInToBin after fees, before the swap itself takes place?
Evidence
-
LBRouter.getSwapIn(, amountOut, )needs this question answered. At a given price, how many tokens must a user send, to receiveamountOut?- We use the
amountOutand price to work backwards to theamountInToBin. - Current approach calculates fees on
amountInToBin. (See here) - This is incorrect as fees should be calculated on
amountIn. (As we discussed in Incorrect use of getFeeAmountFrom)
- We use the
-
SwapHelper.getAmounts needs to know what hypothetical
amountInwould end up asmaxAmountInToBinafter fees. This is needed to be able to avoid Incorrect amountIn overflow
Install dependencies
To install dependencies, run the following to install dependencies:
forge install
Tests
To run tests, run the following command:
forge test --match-contract Report -vv
testSingleBinSwapFeeDifference:
- Simple test to show the Fee Defecit in it’s most basic form.
testFalsePositiveBinDeactivation
- Test that shows false positive resulting from the Incorrect condition
testCorrectFeeBinDeactivation
- Test that shows with getAmountsV2 the false positive issue is resolved.
testMultiBinGrowth
- Generates datapoints used in opening graph.
0x0Louis (Trader Joe) confirmed
Alex the Entreprenerd (judge) decreased severity to Medium and commented:
The warden has shown an incorrect application of the fee formula, which results in an exponentially growing reduction in fees taken.
While the report is thorough, the maximum impact is a loss of up to
2%of the total fees (98%of fees are collected).Because of the reduced order of magnitude for the impact, I think the appropriate severity to be Medium as the finding will cause a loss of yield as shown by the Warden.
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 fee calculation used the wrong formula (getFeeAmountFrom instead of getFeeAmount) and had an incorrect overflow condition, leading to under-collection of fees.
Impact
The fee deficit could reduce collected fees by up to about 2%, causing a loss of yield for liquidity providers; the judge rated it Medium severity.
What the attacker needed
None
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 decentralised exchange, traders pay a small fee on each swap, which goes to liquidity providers. This report found that a coding mistake caused the exchange to charge slightly less than intended, so providers earned less. The error was in how the fee was calculated, especially when a trade used multiple price ranges.
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.