RebateFi Hook

First Flight #53
Beginner FriendlyDeFi
100 EXP
View results
Submission Details
Impact: medium
Likelihood: high
Invalid

Fee Calculation Precision Loss

Description

  • The fee calculation mechanism uses integer division which causes precision loss for small swap amounts, potentially allowing fee avoidance through micro-transactions and undermining the protocol's revenue model.

if (isReFiBuy) {
fee = buyFee;
emit ReFiBought(sender, swapAmount);
} else {
fee = sellFee;
@> uint256 feeAmount = (swapAmount * sellFee) / 100000;
emit ReFiSold(sender, swapAmount, feeAmount);
}

Risk

Impact:

  • Very small swaps may incur zero fees

  • Potential for fee avoidance through micro-transactions

  • Minor economic impact

Proof of Concept

Add the following to `RebateFiHookTest.t.sol`

function test_FeeCalculationPrecisionLoss() public {
uint256 smallSwapAmount = 100; // 100 wei
uint256 expectedFee = (smallSwapAmount * 3000) / 100000; // = 3 wei
uint256 tinySwapAmount = 1;
uint256 tinyFee = (tinySwapAmount * 3000) / 100000; // = 0 due to integer division
assertEq(tinyFee, 0); // Fee rounds down to 0
}

Recommended Mitigation

if (isReFiBuy) {
fee = buyFee;
- emit ReFiBought(sender, swapAmount);
+ uint256 feeAmount = (swapAmount * buyFee * SCALING_FACTOR) / 100000 / SCALING_FACTOR;
+ if (feeAmount == 0 && swapAmount > 0) feeAmount = MIN_FEE_AMOUNT;
+ emit ReFiBought(sender, swapAmount, feeAmount);
} else {
fee = sellFee;
- uint256 feeAmount = (swapAmount * sellFee) / 100000;
- emit ReFiSold(sender, swapAmount, feeAmount);
+ uint256 feeAmount = (swapAmount * sellFee * SCALING_FACTOR) / 100000 / SCALING_FACTOR;
+ if (feeAmount == 0 && swapAmount > 0) feeAmount = MIN_FEE_AMOUNT;
+ emit ReFiSold(sender, swapAmount, feeAmount);
}

Also:

  • Consider minimum swap amounts

  • Use scaled fee calculations for better precision

  • Accept as known limitation of integer math

Updates

Lead Judging Commences

chaossr Lead Judge
13 days ago
chaossr Lead Judge 12 days ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!