Skip to content

Commit 12d891e

Browse files
authored
Revert "Revert "[LILA-7980] Do not throw on zero fee returned""
1 parent 2588ab2 commit 12d891e

11 files changed

+74
-73
lines changed

src/FeeCalculator.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ contract FeeCalculator is IFeeCalculator, Ownable {
2020
/// @dev Version-related parameters. VERSION keeps track of production
2121
/// releases. VERSION_RELEASE_CANDIDATE keeps track of iterations
2222
/// of a VERSION in our staging environment.
23-
string public constant VERSION = "1.1.0";
23+
string public constant VERSION = "1.2.0";
2424
uint256 public constant VERSION_RELEASE_CANDIDATE = 1;
2525

2626
SD59x18 private _zero = sd(0);
@@ -406,7 +406,9 @@ contract FeeCalculator is IFeeCalculator, Ownable {
406406
uint256 feeAmount = calculator(requestedAmount, projectSupply, totalPoolSupply);
407407

408408
require(feeAmount <= requestedAmount, "Fee must be lower or equal to requested amount");
409-
require(feeAmount != 0, "Fee must be greater than 0");
409+
if (feeAmount == 0) {
410+
return FeeDistribution(new address[](0), new uint256[](0));
411+
}
410412

411413
return calculateFeeShares(feeAmount);
412414
}

src/FlatFeeCalculator.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ contract FlatFeeCalculator is IFeeCalculator, Ownable {
1919
/// @dev Version-related parameters. VERSION keeps track of production
2020
/// releases. VERSION_RELEASE_CANDIDATE keeps track of iterations
2121
/// of a VERSION in our staging environment.
22-
string public constant VERSION = "1.0.0";
22+
string public constant VERSION = "1.1.0";
2323
uint256 public constant VERSION_RELEASE_CANDIDATE = 2;
2424

2525
uint256 public feeBasisPoints = 300;
@@ -158,7 +158,9 @@ contract FlatFeeCalculator is IFeeCalculator, Ownable {
158158
require(requestedAmount != 0, "requested amount must be > 0");
159159

160160
uint256 feeAmount = requestedAmount * feeBasisPoints / 10000;
161-
require(feeAmount != 0, "Fee must be greater than 0");
161+
if (feeAmount == 0) {
162+
return FeeDistribution(new address[](0), new uint256[](0));
163+
}
162164

163165
return calculateFeeShares(feeAmount);
164166
}

test/FeeCalculator/AbstractFeeCalculator.t.sol

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ abstract contract AbstractFeeCalculatorTest is Test {
254254
assertEq(fees[0], 46413457506542766270);
255255
}
256256

257-
function testCalculateDepositFees_DepositOfOneWei_ShouldThrowException() public {
257+
function testCalculateDepositFees_DepositOfOneWei_ShouldNotThrowException() public {
258258
// Arrange
259259
// Set up your test data
260260
uint256 depositAmount = 1;
@@ -264,11 +264,13 @@ abstract contract AbstractFeeCalculatorTest is Test {
264264
setProjectSupply(address(mockToken), 1e4 * 1e18);
265265

266266
// Act
267-
vm.expectRevert("Fee must be greater than 0");
268-
calculateDepositFees(address(mockPool), address(mockToken), depositAmount);
267+
FeeDistribution memory feeDistribution =
268+
calculateDepositFees(address(mockPool), address(mockToken), depositAmount);
269+
assertEq(feeDistribution.recipients.length, 0);
270+
assertEq(feeDistribution.shares.length, 0);
269271
}
270272

271-
function testCalculateDepositFees_DepositOfHundredWei_ShouldThrowError() public {
273+
function testCalculateDepositFees_DepositOfHundredWei_ShouldNotThrowError() public {
272274
//Note! This is a bug, where a very small deposit to a very large pool
273275
//causes a == b because of precision limited by ratioDenominator in FeeCalculator
274276

@@ -281,8 +283,10 @@ abstract contract AbstractFeeCalculatorTest is Test {
281283
setProjectSupply(address(mockToken), 1e4 * 1e18);
282284

283285
// Act
284-
vm.expectRevert("Fee must be greater than 0");
285-
calculateDepositFees(address(mockPool), address(mockToken), depositAmount);
286+
FeeDistribution memory feeDistribution =
287+
calculateDepositFees(address(mockPool), address(mockToken), depositAmount);
288+
assertEq(feeDistribution.recipients.length, 0);
289+
assertEq(feeDistribution.shares.length, 0);
286290
}
287291

288292
function testCalculateDepositFees_DepositOfHundredThousandsPartOfOne_NonzeroFee() public {

test/FeeCalculatorFuzzy/AbstractFeeCalculator.fuzzy.t.sol

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ abstract contract AbstractFeeCalculatorTestFuzzy is Test {
5050
uint256 total
5151
) public virtual;
5252

53-
function testCalculateDepositFees_FuzzyExtremelySmallDepositsToLargePool_ShouldThrowError(uint256 depositAmount)
53+
function testCalculateDepositFees_FuzzyExtremelySmallDepositsToLargePool_ShouldNotThrowError(uint256 depositAmount)
5454
public
5555
{
5656
vm.assume(depositAmount <= 1e-14 * 1e18);
@@ -66,8 +66,10 @@ abstract contract AbstractFeeCalculatorTestFuzzy is Test {
6666
mockPool.setTotalSupply(1e12 * 1e18);
6767
setProjectSupply(address(mockToken), 1e9 * 1e18);
6868

69-
vm.expectRevert("Fee must be greater than 0");
70-
calculateDepositFees(address(mockPool), address(mockToken), depositAmount);
69+
FeeDistribution memory feeDistribution =
70+
calculateDepositFees(address(mockPool), address(mockToken), depositAmount);
71+
assertEq(feeDistribution.recipients.length, 0);
72+
assertEq(feeDistribution.shares.length, 0);
7173
}
7274

7375
function testCalculateRedemptionFeesFuzzy_RedemptionDividedIntoOneChunkFeesGreaterOrEqualToOneRedemption(

test/FeeCalculatorFuzzy/FeeCalculatorERC1155.fuzzy.t.sol

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ contract FeeCalculatorERC1155TestFuzzy is AbstractFeeCalculatorTestFuzzy {
4343
try feeCalculator.calculateDepositFees(address(mockPool), address(mockToken), 1, depositAmount) {}
4444
catch Error(string memory reason) {
4545
assertTrue(
46-
keccak256(bytes("Fee must be greater than 0")) == keccak256(bytes(reason))
47-
|| keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
48-
"error should be 'Fee must be greater than 0' or 'Fee must be lower or equal to requested amount'"
46+
keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
47+
"error should be 'Fee must be lower or equal to requested amount'"
4948
);
5049
}
5150
}
@@ -95,9 +94,8 @@ contract FeeCalculatorERC1155TestFuzzy is AbstractFeeCalculatorTestFuzzy {
9594
} catch Error(string memory reason) {
9695
oneTimeRedemptionFailed = true;
9796
assertTrue(
98-
keccak256(bytes("Fee must be greater than 0")) == keccak256(bytes(reason))
99-
|| keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
100-
"error should be 'Fee must be greater than 0' or 'Fee must be lower or equal to requested amount'"
97+
keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
98+
"error should be 'Fee must be lower or equal to requested amount'"
10199
);
102100
}
103101

@@ -125,9 +123,8 @@ contract FeeCalculatorERC1155TestFuzzy is AbstractFeeCalculatorTestFuzzy {
125123
} catch Error(string memory reason) {
126124
multipleTimesRedemptionFailedCount++;
127125
assertTrue(
128-
keccak256(bytes("Fee must be greater than 0")) == keccak256(bytes(reason))
129-
|| keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
130-
"error should be 'Fee must be greater than 0' or 'Fee must be lower or equal to requested amount'"
126+
keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
127+
"error should be 'Fee must be lower or equal to requested amount'"
131128
);
132129
}
133130
}
@@ -168,9 +165,8 @@ contract FeeCalculatorERC1155TestFuzzy is AbstractFeeCalculatorTestFuzzy {
168165
} catch Error(string memory reason) {
169166
oneTimeDepositFailed = true;
170167
assertTrue(
171-
keccak256(bytes("Fee must be greater than 0")) == keccak256(bytes(reason))
172-
|| keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
173-
"error should be 'Fee must be greater than 0' or 'Fee must be lower or equal to requested amount'"
168+
keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
169+
"error should be 'Fee must be lower or equal to requested amount'"
174170
);
175171
}
176172

@@ -192,9 +188,8 @@ contract FeeCalculatorERC1155TestFuzzy is AbstractFeeCalculatorTestFuzzy {
192188
} catch Error(string memory reason) {
193189
multipleTimesDepositFailedCount++;
194190
assertTrue(
195-
keccak256(bytes("Fee must be greater than 0")) == keccak256(bytes(reason))
196-
|| keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
197-
"error should be 'Fee must be greater than 0' or 'Fee must be lower or equal to requested amount'"
191+
keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
192+
"error should be 'Fee must be lower or equal to requested amount'"
198193
);
199194
}
200195
}

test/FeeCalculatorFuzzy/FeeCalculatorTCO2.fuzzy.t.sol

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ contract FeeCalculatorTCO2TestFuzzy is AbstractFeeCalculatorTestFuzzy {
4040
try feeCalculator.calculateDepositFees(address(mockPool), address(mockToken), depositAmount) {}
4141
catch Error(string memory reason) {
4242
assertTrue(
43-
keccak256(bytes("Fee must be greater than 0")) == keccak256(bytes(reason))
44-
|| keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
45-
"error should be 'Fee must be greater than 0' or 'Fee must be lower or equal to requested amount'"
43+
keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
44+
"error should be 'Fee must be lower or equal to requested amount'"
4645
);
4746
}
4847
}
@@ -90,9 +89,8 @@ contract FeeCalculatorTCO2TestFuzzy is AbstractFeeCalculatorTestFuzzy {
9089
} catch Error(string memory reason) {
9190
oneTimeRedemptionFailed = true;
9291
assertTrue(
93-
keccak256(bytes("Fee must be greater than 0")) == keccak256(bytes(reason))
94-
|| keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
95-
"error should be 'Fee must be greater than 0' or 'Fee must be lower or equal to requested amount'"
92+
keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
93+
"error should be 'Fee must be lower or equal to requested amount'"
9694
);
9795
}
9896

@@ -120,9 +118,8 @@ contract FeeCalculatorTCO2TestFuzzy is AbstractFeeCalculatorTestFuzzy {
120118
} catch Error(string memory reason) {
121119
multipleTimesRedemptionFailedCount++;
122120
assertTrue(
123-
keccak256(bytes("Fee must be greater than 0")) == keccak256(bytes(reason))
124-
|| keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
125-
"error should be 'Fee must be greater than 0' or 'Fee must be lower or equal to requested amount'"
121+
keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
122+
"error should be 'Fee must be lower or equal to requested amount'"
126123
);
127124
}
128125
}
@@ -163,9 +160,8 @@ contract FeeCalculatorTCO2TestFuzzy is AbstractFeeCalculatorTestFuzzy {
163160
} catch Error(string memory reason) {
164161
oneTimeDepositFailed = true;
165162
assertTrue(
166-
keccak256(bytes("Fee must be greater than 0")) == keccak256(bytes(reason))
167-
|| keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
168-
"error should be 'Fee must be greater than 0' or 'Fee must be lower or equal to requested amount'"
163+
keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
164+
"error should be 'Fee must be lower or equal to requested amount'"
169165
);
170166
}
171167

@@ -187,9 +183,8 @@ contract FeeCalculatorTCO2TestFuzzy is AbstractFeeCalculatorTestFuzzy {
187183
} catch Error(string memory reason) {
188184
multipleTimesDepositFailedCount++;
189185
assertTrue(
190-
keccak256(bytes("Fee must be greater than 0")) == keccak256(bytes(reason))
191-
|| keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
192-
"error should be 'Fee must be greater than 0' or 'Fee must be lower or equal to requested amount'"
186+
keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason)),
187+
"error should be 'Fee must be lower or equal to requested amount'"
193188
);
194189
}
195190
}

test/FeeCalculatorLaunchParams/AbstractFeeCalculatorLaunchParams.t.sol

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ abstract contract AbstractFeeCalculatorLaunchParamsTest is Test {
147147
assertEq(fees[0], 53013879215838797358);
148148
}
149149

150-
function testCalculateDepositFees_DepositOfOneWei_ShouldThrowException() public {
150+
function testCalculateDepositFees_DepositOfOneWei_ShouldNotThrowException() public {
151151
// Arrange
152152
// Set up your test data
153153
uint256 depositAmount = 1;
@@ -157,11 +157,13 @@ abstract contract AbstractFeeCalculatorLaunchParamsTest is Test {
157157
setProjectSupply(address(mockToken), 1e4 * 1e18);
158158

159159
// Act
160-
vm.expectRevert("Fee must be greater than 0");
161-
calculateDepositFees(address(mockPool), address(mockToken), depositAmount);
160+
FeeDistribution memory feeDistribution =
161+
calculateDepositFees(address(mockPool), address(mockToken), depositAmount);
162+
assertEq(feeDistribution.recipients.length, 0);
163+
assertEq(feeDistribution.shares.length, 0);
162164
}
163165

164-
function testCalculateDepositFees_DepositOfHundredWei_ShouldThrowError() public {
166+
function testCalculateDepositFees_DepositOfHundredWei_ShouldNotThrowError() public {
165167
//Note! This is a bug, where a very small deposit to a very large pool
166168
//causes a == b because of precision limited by ratioDenominator in FeeCalculator
167169

@@ -174,8 +176,10 @@ abstract contract AbstractFeeCalculatorLaunchParamsTest is Test {
174176
setProjectSupply(address(mockToken), 1e4 * 1e18);
175177

176178
// Act
177-
vm.expectRevert("Fee must be greater than 0");
178-
calculateDepositFees(address(mockPool), address(mockToken), depositAmount);
179+
FeeDistribution memory feeDistribution =
180+
calculateDepositFees(address(mockPool), address(mockToken), depositAmount);
181+
assertEq(feeDistribution.recipients.length, 0);
182+
assertEq(feeDistribution.shares.length, 0);
179183
}
180184

181185
function testCalculateDepositFees_DepositOfHundredThousandsPartOfOne_NonzeroFee() public {

test/FeeCalculatorLaunchParamsFuzzy/AbstractFeeCalculatorLaunchParams.fuzzy.t.sol

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ abstract contract AbstractFeeCalculatorLaunchParamsTestFuzzy is Test {
4747
uint256 total
4848
) public virtual;
4949

50-
function testCalculateDepositFees_FuzzyExtremelySmallDepositsToLargePool_ShouldThrowError(uint256 depositAmount)
50+
function testCalculateDepositFees_FuzzyExtremelySmallDepositsToLargePool_ShouldNotThrowError(uint256 depositAmount)
5151
public
5252
{
5353
vm.assume(depositAmount <= 1e-14 * 1e18);
@@ -63,8 +63,10 @@ abstract contract AbstractFeeCalculatorLaunchParamsTestFuzzy is Test {
6363
mockPool.setTotalSupply(1e12 * 1e18);
6464
setProjectSupply(address(mockToken), 1e9 * 1e18);
6565

66-
vm.expectRevert("Fee must be greater than 0");
67-
calculateDepositFees(address(mockPool), address(mockToken), depositAmount);
66+
FeeDistribution memory feeDistribution =
67+
calculateDepositFees(address(mockPool), address(mockToken), depositAmount);
68+
assertEq(feeDistribution.recipients.length, 0);
69+
assertEq(feeDistribution.shares.length, 0);
6870
}
6971

7072
function testCalculateDepositFeesFuzzy_DepositDividedIntoOneChunkFeesGreaterOrEqualToOneDeposit(

test/FeeCalculatorLaunchParamsFuzzy/FeeCalculatorLaunchParamsERC1155.fuzzy.t.sol

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ contract FeeCalculatorLaunchParamsERC1155TestFuzzy is AbstractFeeCalculatorLaunc
4040
try feeCalculator.calculateDepositFees(address(mockPool), address(mockToken), 1, depositAmount) {}
4141
catch Error(string memory reason) {
4242
assertTrue(
43-
keccak256(bytes("Fee must be greater than 0")) == keccak256(bytes(reason))
44-
|| keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason))
43+
keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason))
4544
|| keccak256(bytes("Deposit outside range")) == keccak256(bytes(reason)),
46-
"error should be 'Fee must be greater than 0' or 'Fee must be lower or equal to requested amount' or 'Deposit outside range'"
45+
"error should be 'Fee must be lower or equal to requested amount' or 'Deposit outside range'"
4746
);
4847
}
4948
}
@@ -80,10 +79,9 @@ contract FeeCalculatorLaunchParamsERC1155TestFuzzy is AbstractFeeCalculatorLaunc
8079
} catch Error(string memory reason) {
8180
oneTimeDepositFailed = true;
8281
assertTrue(
83-
keccak256(bytes("Fee must be greater than 0")) == keccak256(bytes(reason))
84-
|| keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason))
82+
keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason))
8583
|| keccak256(bytes("Deposit outside range")) == keccak256(bytes(reason)),
86-
"error should be 'Fee must be greater than 0' or 'Fee must be lower or equal to requested amount' or 'Deposit outside range'"
84+
"error should be 'Fee must be lower or equal to requested amount' or 'Deposit outside range'"
8785
);
8886
}
8987

@@ -105,10 +103,9 @@ contract FeeCalculatorLaunchParamsERC1155TestFuzzy is AbstractFeeCalculatorLaunc
105103
} catch Error(string memory reason) {
106104
multipleTimesDepositFailedCount++;
107105
assertTrue(
108-
keccak256(bytes("Fee must be greater than 0")) == keccak256(bytes(reason))
109-
|| keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason))
106+
keccak256(bytes("Fee must be lower or equal to requested amount")) == keccak256(bytes(reason))
110107
|| keccak256(bytes("Deposit outside range")) == keccak256(bytes(reason)),
111-
"error should be 'Fee must be greater than 0' or 'Fee must be lower or equal to requested amount' or 'Deposit outside range'"
108+
"error should be 'Fee must be lower or equal to requested amount' or 'Deposit outside range'"
112109
);
113110
}
114111
}

0 commit comments

Comments
 (0)