Skip to content

Commit

Permalink
feat: add repay method (#14)
Browse files Browse the repository at this point in the history
* feat: add repay method

* tests: add more tests

* fix: update tests to use msg.sender
  • Loading branch information
sakulstra authored Sep 21, 2022
1 parent 6ce8276 commit e00fd13
Showing 1 changed file with 50 additions and 8 deletions.
58 changes: 50 additions & 8 deletions src/ProtocolV3TestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ contract ProtocolV3TestBase is Test {

function e2eTest(IPool pool) public {
ReserveConfig[] memory configs = _getReservesConfigs(pool);
deal(address(this), 1000 ether);
deal(msg.sender, 1000 ether);
uint256 snapshot = vm.snapshot();
_supplyWithdrawFlow(configs, pool);
vm.revertTo(snapshot);
Expand Down Expand Up @@ -146,7 +146,7 @@ contract ProtocolV3TestBase is Test {
for (uint256 i = 0; i < configs.length; i++) {
uint256 amount = 10**configs[i].decimals;
if (configs[i].borrowingEnabled) {
_borrow(configs[i], pool, amount, false);
this._borrow(configs[i], pool, amount, false);
} else {
console.log('SKIP: BORROWING_DISABLED %s', configs[i].symbol);
}
Expand All @@ -163,7 +163,7 @@ contract ProtocolV3TestBase is Test {
for (uint256 i = 0; i < configs.length; i++) {
uint256 amount = 10**configs[i].decimals;
if (configs[i].borrowingEnabled && configs[i].stableBorrowRateEnabled) {
_borrow(configs[i], pool, amount, true);
this._borrow(configs[i], pool, amount, true);
} else {
console.log('SKIP: STABLE_BORROWING_DISABLED %s', configs[i].symbol);
}
Expand All @@ -175,10 +175,13 @@ contract ProtocolV3TestBase is Test {
IPool pool,
uint256 amount
) internal {
deal(config.underlying, address(this), amount);
uint256 aTokenBefore = IERC20(config.aToken).balanceOf(msg.sender);
deal(config.underlying, msg.sender, amount);
IERC20(config.underlying).approve(address(pool), amount);
console.log('SUPPLY: %s, Amount: %s', config.symbol, amount);
pool.deposit(config.underlying, amount, address(this), 0);
pool.deposit(config.underlying, amount, msg.sender, 0);
uint256 aTokenAfter = IERC20(config.aToken).balanceOf(msg.sender);
require(_almostEqual(aTokenAfter, aTokenBefore + amount), '_deposit() : ERROR');
}

function _withdraw(
Expand All @@ -187,12 +190,22 @@ contract ProtocolV3TestBase is Test {
uint256 amount,
bool max
) internal returns (uint256) {
uint256 aTokenBefore = IERC20(config.aToken).balanceOf(msg.sender);
uint256 amountOut = pool.withdraw(
config.underlying,
max ? type(uint256).max : amount,
address(this)
msg.sender
);
console.log('WITHDRAW: %s, Amount: %s', config.symbol, amountOut);
uint256 aTokenAfter = IERC20(config.aToken).balanceOf(msg.sender);
if (aTokenBefore < amount || max) {
require(aTokenAfter == 0, '_widthdraw(): DUST_AFTER_WITHDRAW_ALL');
} else {
require(
_almostEqual(aTokenAfter, aTokenBefore - amount),
'_withdraw() : INCONSISTENT_ATOKEN_BALANCE_AFTER'
);
}
return amountOut;
}

Expand All @@ -201,9 +214,29 @@ contract ProtocolV3TestBase is Test {
IPool pool,
uint256 amount,
bool stable
) internal {
) external {
address debtToken = stable ? config.stableDebtToken : config.variableDebtToken;
uint256 debtBefore = IERC20(debtToken).balanceOf(msg.sender);
console.log('BORROW: %s, Amount %s, Stable: %s', config.symbol, amount, stable);
pool.borrow(config.underlying, amount, stable ? 1 : 2, 0, address(this));
pool.borrow(config.underlying, amount, stable ? 1 : 2, 0, msg.sender);
uint256 debtAfter = IERC20(debtToken).balanceOf(msg.sender);
require(_almostEqual(debtAfter, debtBefore + amount), '_borrow() : ERROR');
}

function _repay(
ReserveConfig memory config,
IPool pool,
uint256 amount,
bool stable
) internal {
address debtToken = stable ? config.stableDebtToken : config.variableDebtToken;
uint256 debtBefore = IERC20(debtToken).balanceOf(msg.sender);
deal(config.underlying, msg.sender, amount);
IERC20(config.underlying).approve(address(pool), amount);
console.log('REPAY: %s, Amount: %s', config.symbol, amount);
pool.repay(config.underlying, amount, stable ? 1 : 2, msg.sender);
uint256 debtAfter = IERC20(debtToken).balanceOf(msg.sender);
require(debtAfter == ((debtBefore > amount) ? debtBefore - amount : 0), '_repay() : ERROR');
}

function _isInUint256Array(uint256[] memory haystack, uint256 needle)
Expand Down Expand Up @@ -856,4 +889,13 @@ contract ProtocolV3TestBase is Test {
revert('_getAssetOnEmodeCategory(): LESS_ASSETS_IN_CATEGORY_THAN_EXPECTED');
}
}

/// @dev To contemplate +1/-1 precision issues when rounding, mainly on aTokens
function _almostEqual(uint256 a, uint256 b) internal pure returns (bool) {
if (b == 0) {
return (a == b) || (a == (b + 1));
} else {
return (a == b) || (a == (b + 1)) || (a == (b - 1));
}
}
}

0 comments on commit e00fd13

Please sign in to comment.