From 1d15617fb8e6a1a1797dd4b31adcc1e6289c5b01 Mon Sep 17 00:00:00 2001 From: Xiangyu Xu Date: Mon, 20 Nov 2023 22:00:58 -0800 Subject: [PATCH 1/2] Fixed a mistake in the fee calculation with round up --- patterns/flash-loans/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/patterns/flash-loans/README.md b/patterns/flash-loans/README.md index c02c45c..2bd3e3b 100644 --- a/patterns/flash-loans/README.md +++ b/patterns/flash-loans/README.md @@ -76,7 +76,7 @@ function flashLoan( uint256 balanceBefore = token.balanceOf(address(this)); require(balanceBefore >= borrowAmount, 'too much'); // Compute the fee, rounded up. - uint256 fee = FEE_BPS * (borrowAmount + 1e4-1) / 1e4; + uint256 fee = (FEE_BPS * borrowAmount + 1e4-1) / 1e4; // Transfer tokens to the borrower contract. token.transfer(address(borrower), borrowAmount); // Let the borrower do its thing. From 178fa9f160453e679dd3c2a75ec2224982f94f24 Mon Sep 17 00:00:00 2001 From: Xiangyu Xu Date: Tue, 21 Nov 2023 09:50:30 -0800 Subject: [PATCH 2/2] Applied the fix of fee round up to the contract code & test script --- patterns/flash-loans/FlashLoanPool.sol | 2 +- test/FlashLoanPool.t.sol | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/patterns/flash-loans/FlashLoanPool.sol b/patterns/flash-loans/FlashLoanPool.sol index 1c39ae6..8d24175 100644 --- a/patterns/flash-loans/FlashLoanPool.sol +++ b/patterns/flash-loans/FlashLoanPool.sol @@ -48,7 +48,7 @@ contract FlashLoanPool { uint256 balanceBefore = token.balanceOf(address(this)); require(balanceBefore >= borrowAmount, 'too much'); // Compute the fee, rounded up. - uint256 fee = FEE_BPS * (borrowAmount + 1e4-1) / 1e4; + uint256 fee = (FEE_BPS * borrowAmount + 1e4-1) / 1e4; // Transfer tokens to the borrower contract. token.transfer(address(borrower), borrowAmount); // Let the borrower do its thing. diff --git a/test/FlashLoanPool.t.sol b/test/FlashLoanPool.t.sol index 14e4165..f6a29b6 100644 --- a/test/FlashLoanPool.t.sol +++ b/test/FlashLoanPool.t.sol @@ -161,7 +161,7 @@ contract FlashLoanPoolTest is TestUtils, FlashLoanValidator { } function _getFee(uint256 amount) private view returns (uint256) { - return pool.FEE_BPS() * (amount + 1e4-1) / 1e4; + return (pool.FEE_BPS() * amount + 1e4-1) / 1e4; } function test_canWithdraw() external {