Skip to content

Commit 4069466

Browse files
committed
fix: adapt repay rounding
1 parent 568804f commit 4069466

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/PositionsManager.sol

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ contract PositionsManager is IPositionsManager, PositionsManagerInternal {
5959

6060
Types.SupplyRepayVars memory vars = _executeSupply(underlying, amount, from, onBehalf, maxIterations, indexes);
6161

62-
_pool.repayToPool(underlying, market.variableDebtToken, vars.toRepay);
62+
_pool.repayToPool(underlying, market.variableDebtToken, vars.toRepay, indexes.borrow.poolIndex);
6363
_pool.supplyToPool(underlying, vars.toSupply, indexes.supply.poolIndex);
6464

6565
return amount;
@@ -143,7 +143,7 @@ contract PositionsManager is IPositionsManager, PositionsManagerInternal {
143143
Types.SupplyRepayVars memory vars =
144144
_executeRepay(underlying, amount, repayer, onBehalf, _defaultIterations.repay, indexes);
145145

146-
_pool.repayToPool(underlying, market.variableDebtToken, vars.toRepay);
146+
_pool.repayToPool(underlying, market.variableDebtToken, vars.toRepay, indexes.borrow.poolIndex);
147147
_pool.supplyToPool(underlying, vars.toSupply, indexes.supply.poolIndex);
148148

149149
return amount;
@@ -259,7 +259,12 @@ contract PositionsManager is IPositionsManager, PositionsManagerInternal {
259259
underlyingCollateral, vars.seized, borrower, liquidator, collateralIndexes.supply.poolIndex
260260
);
261261

262-
_pool.repayToPool(underlyingBorrowed, _market[underlyingBorrowed].variableDebtToken, repayVars.toRepay);
262+
_pool.repayToPool(
263+
underlyingBorrowed,
264+
_market[underlyingBorrowed].variableDebtToken,
265+
repayVars.toRepay,
266+
borrowIndexes.borrow.poolIndex
267+
);
263268
_pool.supplyToPool(underlyingBorrowed, repayVars.toSupply, borrowIndexes.supply.poolIndex);
264269
_pool.withdrawFromPool(underlyingCollateral, _market[underlyingCollateral].aToken, vars.seized);
265270

src/libraries/PoolLib.sol

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ library PoolLib {
3535

3636
/// @notice Repays `amount` of `underlying` to `pool`.
3737
/// @dev If the debt has been fully repaid already, the function will return early.
38-
function repayToPool(IPool pool, address underlying, address variableDebtToken, uint256 amount) internal {
39-
if (amount == 0 || IVariableDebtToken(variableDebtToken).scaledBalanceOf(address(this)) == 0) return;
38+
function repayToPool(IPool pool, address underlying, address variableDebtToken, uint256 amount, uint256 index)
39+
internal
40+
{
41+
if (amount.rayDivDown(index) == 0 || IVariableDebtToken(variableDebtToken).scaledBalanceOf(address(this)) == 0)
42+
{
43+
return;
44+
}
4045

4146
pool.repay(underlying, amount, Constants.VARIABLE_INTEREST_MODE, address(this)); // Reverts if debt is 0.
4247
}

test/integration/TestIntegrationPoolLib.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ contract TestIntegrationPoolLibRepay is TestIntegrationPoolLib {
8080
uint256 balanceBefore = ERC20(dai).balanceOf(address(this));
8181
uint256 vBalanceBefore = ERC20(vDai).balanceOf(address(this));
8282

83-
pool.repayToPool(dai, vDai, amount / 4);
83+
pool.repayToPool(dai, vDai, amount / 4, pool.getReserveNormalizedVariableDebt(dai));
8484

8585
assertEq(ERC20(dai).balanceOf(address(this)) + amount / 4, balanceBefore, "balance");
8686
assertApproxEqAbs(ERC20(vDai).balanceOf(address(this)) + amount / 4, vBalanceBefore, 2, "vBalance");

0 commit comments

Comments
 (0)