Skip to content
This repository was archived by the owner on Dec 18, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/PositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ contract PositionsManager is IPositionsManager, PositionsManagerInternal {

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

_pool.repayToPool(underlying, market.variableDebtToken, vars.toRepay);
_pool.repayToPool(underlying, market.variableDebtToken, vars.toRepay, indexes.borrow.poolIndex);
_pool.supplyToPool(underlying, vars.toSupply, indexes.supply.poolIndex);

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

_pool.repayToPool(underlying, market.variableDebtToken, vars.toRepay);
_pool.repayToPool(underlying, market.variableDebtToken, vars.toRepay, indexes.borrow.poolIndex);
_pool.supplyToPool(underlying, vars.toSupply, indexes.supply.poolIndex);

return amount;
Expand Down Expand Up @@ -259,7 +259,12 @@ contract PositionsManager is IPositionsManager, PositionsManagerInternal {
underlyingCollateral, vars.seized, borrower, liquidator, collateralIndexes.supply.poolIndex
);

_pool.repayToPool(underlyingBorrowed, _market[underlyingBorrowed].variableDebtToken, repayVars.toRepay);
_pool.repayToPool(
underlyingBorrowed,
_market[underlyingBorrowed].variableDebtToken,
repayVars.toRepay,
borrowIndexes.borrow.poolIndex
);
_pool.supplyToPool(underlyingBorrowed, repayVars.toSupply, borrowIndexes.supply.poolIndex);
_pool.withdrawFromPool(underlyingCollateral, _market[underlyingCollateral].aToken, vars.seized);

Expand Down
11 changes: 8 additions & 3 deletions src/libraries/PoolLib.sol
Comment thread
QGarchery marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ library PoolLib {
/// @dev The pool supply `index` must be passed as a parameter to skip the supply on pool
/// if it were to revert due to the amount being too small.
function supplyToPool(IPool pool, address underlying, uint256 amount, uint256 index) internal {
if (amount.rayDiv(index) == 0) return;
if (amount.rayDivDown(index) == 0) return;

pool.supply(underlying, amount, address(this), Constants.NO_REFERRAL_CODE);
}
Expand All @@ -35,8 +35,13 @@ library PoolLib {

/// @notice Repays `amount` of `underlying` to `pool`.
/// @dev If the debt has been fully repaid already, the function will return early.
function repayToPool(IPool pool, address underlying, address variableDebtToken, uint256 amount) internal {
if (amount == 0 || IVariableDebtToken(variableDebtToken).scaledBalanceOf(address(this)) == 0) return;
function repayToPool(IPool pool, address underlying, address variableDebtToken, uint256 amount, uint256 index)
internal
{
uint256 scaledBalance = IVariableDebtToken(variableDebtToken).scaledBalanceOf(address(this));
uint256 paybackAmount = amount < scaledBalance ? amount : scaledBalance;

if (paybackAmount.rayDivDown(index) == 0) return;

pool.repay(underlying, amount, Constants.VARIABLE_INTEREST_MODE, address(this)); // Reverts if debt is 0.
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/TestIntegrationPoolLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ contract TestIntegrationPoolLibRepay is TestIntegrationPoolLib {
uint256 balanceBefore = ERC20(dai).balanceOf(address(this));
uint256 vBalanceBefore = ERC20(vDai).balanceOf(address(this));

pool.repayToPool(dai, vDai, amount / 4);
pool.repayToPool(dai, vDai, amount / 4, 1e27);

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