Skip to content

Commit

Permalink
refactor: add repay buffer (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
cucupac committed Jan 16, 2024
1 parent d8d8ee5 commit df885a9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/services/DebtService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions test/PositionAdmin.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -153,25 +153,22 @@ 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
vm.prank(_sender);
(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);
}

Expand Down
5 changes: 1 addition & 4 deletions test/PositionFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -274,25 +274,22 @@ 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
vm.prank(_sender);
(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);
}

Expand Down
1 change: 1 addition & 0 deletions test/common/Constants.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
13 changes: 11 additions & 2 deletions test/services/DebtService.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit df885a9

Please sign in to comment.