From df885a928190bf450a916bfd694b4cf89f8f3f4f Mon Sep 17 00:00:00 2001 From: Adam Cuculich <46691282+cucupac@users.noreply.github.com> Date: Tue, 16 Jan 2024 16:24:56 -0600 Subject: [PATCH] refactor: add repay buffer (#6) --- src/services/DebtService.sol | 5 +++-- test/PositionAdmin.t.sol | 5 +---- test/PositionFactory.t.sol | 5 +---- test/common/Constants.t.sol | 1 + test/services/DebtService.t.sol | 13 +++++++++++-- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/services/DebtService.sol b/src/services/DebtService.sol index 806ef0d2..c6102d0c 100644 --- a/src/services/DebtService.sol +++ b/src/services/DebtService.sol @@ -102,11 +102,12 @@ contract DebtService is PositionAdmin { /** * @notice Returns this contract's total debt (principle + interest). - * @return outstandingDebt This contract's total debt (units: D_DECIMALS). + * @return outstandingDebt This contract's total debt + small buffer (units: D_DECIMALS). */ function _getDebtAmt() internal view returns (uint256) { address variableDebtTokenAddress = IPool(AAVE_POOL).getReserveData(D_TOKEN).variableDebtTokenAddress; - return IERC20(variableDebtTokenAddress).balanceOf(address(this)); + /// @dev adds repay buffer of 2 units to ensure a full repay (units: D_DECIMALS) + return IERC20(variableDebtTokenAddress).balanceOf(address(this)) + 2; } /** diff --git a/test/PositionAdmin.t.sol b/test/PositionAdmin.t.sol index 86979cd1..465e07c8 100644 --- a/test/PositionAdmin.t.sol +++ b/test/PositionAdmin.t.sol @@ -153,12 +153,11 @@ contract PositionAdminTest is Test, TokenUtils { // - The contract's native balance should increase by the amount transferred. function testFuzz_Receive(uint256 _amount, address _sender) public { // Assumptions - vm.assume(_amount != 0 && _amount <= 1000 ether); + _amount = bound(_amount, 1, 1_000 ether); uint256 gasMoney = 1 ether; vm.deal(_sender, _amount + gasMoney); // Pre-Act Data - uint256 preSenderBalance = _sender.balance; uint256 preContractBalance = positionAddr.balance; // Act @@ -166,12 +165,10 @@ contract PositionAdminTest is Test, TokenUtils { (bool success,) = payable(positionAddr).call{ value: _amount }(""); // Post-Act Data - uint256 postSenderBalance = _sender.balance; uint256 postContractBalance = positionAddr.balance; // Assertions assertTrue(success); - assertEq(postSenderBalance, preSenderBalance - _amount); assertEq(postContractBalance, preContractBalance + _amount); } diff --git a/test/PositionFactory.t.sol b/test/PositionFactory.t.sol index 92f9390f..762392f3 100644 --- a/test/PositionFactory.t.sol +++ b/test/PositionFactory.t.sol @@ -274,12 +274,11 @@ contract PositionFactoryTest is Test, TokenUtils { // - The contract's native balance should increase by the amount transferred. function testFuzz_Receive(uint256 _amount, address _sender) public { // Assumptions - vm.assume(_amount != 0 && _amount <= 1000 ether); + _amount = bound(_amount, 1, 1_000 ether); uint256 gasMoney = 1 ether; vm.deal(_sender, _amount + gasMoney); // Pre-Act Data - uint256 preSenderBalance = _sender.balance; uint256 preContractBalance = address(positionFactory).balance; // Act @@ -287,12 +286,10 @@ contract PositionFactoryTest is Test, TokenUtils { (bool success,) = payable(address(positionFactory)).call{ value: _amount }(""); // Post-Act Data - uint256 postSenderBalance = _sender.balance; uint256 postContractBalance = address(positionFactory).balance; // Assertions assertTrue(success); - assertEq(postSenderBalance, preSenderBalance - _amount); assertEq(postContractBalance, preContractBalance + _amount); } diff --git a/test/common/Constants.t.sol b/test/common/Constants.t.sol index d913262c..883a6bba 100644 --- a/test/common/Constants.t.sol +++ b/test/common/Constants.t.sol @@ -19,6 +19,7 @@ address constant USDC_HOLDER = 0x47c031236e19d024b42f8AE6780E44A573170703; uint256 constant PROFIT_PERCENT = 25; uint256 constant REPAY_PERCENT = 75; uint256 constant WITHDRAW_BUFFER = 100_000; +uint256 constant REPAY_BUFFER = 2; contract Assets { address[4] public supported = [USDC, DAI, WETH, WBTC]; diff --git a/test/services/DebtService.t.sol b/test/services/DebtService.t.sol index 03108a88..81af5f66 100644 --- a/test/services/DebtService.t.sol +++ b/test/services/DebtService.t.sol @@ -9,7 +9,16 @@ import { PositionAdmin } from "src/PositionAdmin.sol"; import { DebtServiceHarness } from "test/harness/DebtServiceHarness.t.sol"; import { DebtUtils } from "test/services/utils/DebtUtils.t.sol"; import { TokenUtils } from "test/common/utils/TokenUtils.t.sol"; -import { Assets, AAVE_ORACLE, AAVE_POOL, DAI, USDC, USDC_HOLDER, WITHDRAW_BUFFER } from "test/common/Constants.t.sol"; +import { + Assets, + AAVE_ORACLE, + AAVE_POOL, + DAI, + REPAY_BUFFER, + USDC, + USDC_HOLDER, + WITHDRAW_BUFFER +} from "test/common/Constants.t.sol"; import { IAaveOracle } from "src/interfaces/aave/IAaveOracle.sol"; import { IPool } from "src/interfaces/aave/IPool.sol"; import { IERC20 } from "src/interfaces/token/IERC20.sol"; @@ -295,7 +304,7 @@ contract DebtServiceTest is Test, DebtUtils, TokenUtils { uint256 debtAmt = debtServices[i].exposed_getDebtAmt(); // Assertions - assertApproxEqAbs(debtAmt, dAmt, 1); + assertApproxEqAbs(debtAmt, dAmt + REPAY_BUFFER, 1); } }