Skip to Content

Repay-with-collateral contract

What each function of the repay-with-collateral contract does, who may call it, when, and what it checks.

RepayWithCollateral. Repays an Aave V3 position's debt out of its own collateral in one transaction: a flash loan of the debt asset, the repay, the caller's collateral pulled and withdrawn, exactly the loan and its premium bought back on Uniswap V3, what the swap did not spend supplied back, and the loan returned, all or nothing. Every amount it moves is the call's own. It withdraws exactly the collateral it pulled from its caller, counts only the flash loan towards the loan, and leaves any balance someone sent to the contract where it is, so such a balance can neither reach the caller nor make the call fail. It acts only on the position of the address that calls it and has no owner; one deployment serves one Aave pool, one swap router and, where a debt in USDS is converted, one PSM wrapper. A debt in USDS takes the same steps around a conversion: the loan is USDC, sold to Sky's PSM wrapper for USDS at par less the caller's bound before the repay, so the collateral is sold on its USDC pool. The strategy's AAVE V3 leg calls it.

The other contracts, and every caller on one table, are on the overview.

The position’s own wallet

Any address, acting on its own Aave position only: the debt, the collateral and the permit are the caller’s.

repayWithCollateral

function repayWithCollateral(
    address collateralAsset,
    address debtAsset,
    uint256 amount,
    uint256 maxCollateralIn,
    uint24 poolFee
) external returns (uint256 debtRepaid, uint256 collateralSpent)

Repays the caller's variable debt in debtAsset with the caller's own collateralAsset collateral, atomically, and returns what it repaid and how much collateral the swap spent.

Who may call: Any address, for its own Aave position only: the debt repaid, the aTokens pulled and the collateral supplied back are all the caller's.

When: Whenever the pool lends: a paused reserve, or one whose flash loans are disabled, makes the call revert.

Parameters

NameTypeDescription
collateralAssetaddressThe collateral to sell: the asset whose aToken the caller holds.
debtAssetaddressThe asset of the variable debt to repay.
amountuint256How much debt to repay, and how much of debtAsset to flash-loan: the pool repays no more than the live debt, and the loan it does not take lowers the buy-back.
maxCollateralInuint256The most collateral (aTokens) the contract may pull, which is also the most the swap may spend.
poolFeeuint24The fee tier of the Uniswap V3 pool between the collateral and the debt asset (500 is 0.05%).

Returns

NameTypeDescription
debtRepaiduint256The debt repaid, in debtAsset.
collateralSpentuint256The collateral withdrawn and sold, in collateralAsset.

What it checks

  • Not re-entered, and amount above zero.
  • Flash-loans amount and repays it, the pool repaying no more than the live variable debt.
  • Pulls exactly maxCollateralIn of the caller's aTokens on the caller's allowance to this contract, and withdraws exactly what that pull added to the contract's aToken balance, never aTokens it held before. The pool's own health check on that pull must pass after the repay.
  • Buys back exactly the loan plus its premium, less the part of the loan the repay did not use (a balance of the debt asset the contract held before the call pays nothing), with an exact-output swap whose input is bounded by the collateral withdrawn: the whole transaction reverts if the pool cannot fill within it. The contract checks no price of its own; the caller bounds it with maxCollateralIn.
  • Supplies the collateral the swap did not spend back to the caller's position, or transfers it to the caller when the pool refuses the supply, and any of the call's own debt asset left over goes to the caller. A balance sent to the contract directly is never moved: it neither reaches the caller nor blocks the call.

repayConvertedWithCollateral

function repayConvertedWithCollateral(
    address collateralAsset,
    uint256 flashAmount,
    uint256 maxCollateralIn,
    uint24 poolFee,
    uint16 maxLossBp
) external returns (uint256 debtRepaid, uint256 collateralSpent)

Repays the caller's variable debt in the PSM wrapper's USDS with the caller's own collateralAsset collateral, atomically: flash-loans flashAmount of the wrapper's gem (USDC), sells all of it to the wrapper for USDS, repays the live debt up to what that bought, buys the loan back with the collateral, and returns what it repaid (in USDS) and how much collateral the swap spent.

Who may call: Any address, for its own Aave position only, as repayWithCollateral.

When: Whenever the pool lends and the wrapper converts: a halted wrapper makes the call revert.

Parameters

NameTypeDescription
collateralAssetaddressThe collateral to sell: the asset whose aToken the caller holds.
flashAmountuint256How much of the wrapper's gem (USDC) to flash-loan and convert, in its units.
maxCollateralInuint256The most collateral (aTokens) the contract may pull, which is also the most the swap may spend.
poolFeeuint24The fee tier of the Uniswap V3 pool between the collateral and the gem (500 is 0.05%).
maxLossBpuint16The most the conversion may lose against par, bp.

Returns

NameTypeDescription
debtRepaiduint256The debt repaid, in USDS.
collateralSpentuint256The collateral withdrawn and sold, in collateralAsset.

What it checks

  • Not re-entered, and flashAmount above zero.
  • This contract pins a wrapper (PSM not zero), and the bound is at most 10,000 bp (NoRoute).
  • The USDS the sale bought, measured by balances, at least the gem sold scaled by the wrapper's conversion factor less maxLossBp (ConversionShort); USDS the contract held before the call is not the call's.
  • Repays at most the caller's live USDS debt, which must be above zero; the USDS the debt did not take goes to the caller.
  • Pulls, withdraws, buys back and settles the collateral exactly as repayWithCollateral does, the loan being the gem: the contract checks no price of its own; the caller bounds it with maxCollateralIn.

Callbacks

Called back by a protocol in the middle of the contract’s own operation, and refused from anyone else.

executeOperation

function executeOperation(
    address asset,
    uint256 amount,
    uint256 premium,
    address initiator,
    bytes calldata params
) external returns (bool)

The flash-loan callback, inside repayWithCollateral: repays, pulls and withdraws the collateral, buys the loan back, settles what is left and lets the pool take its loan back.

Who may call: The pool only, for a flash loan this contract itself started.

Parameters

NameTypeDescription
assetaddressThe asset flash-loaned: the debt asset.
amountuint256The amount flash-loaned.
premiumuint256The flash loan's fee, owed back with the amount.
initiatoraddressWho started the flash loan: only this contract is accepted.
paramsbytesThe data the contract passed when starting the loan.

What it checks

  • The sender is POOL and the loan's initiator is this contract.
  • The position acted on is the address that called repayWithCollateral, carried from that call: never anything a third party supplies.

Read-only

Views and constants: they change nothing, and anyone may read them.

Constants

Values compiled in, or set once when the contract is deployed (immutable).

IPool public immutable POOL;
ISwapRouter02 public immutable ROUTER;
IUsdsPsmWrapper public immutable PSM;
uint256 public constant VARIABLE_RATE_MODE = 2;
NameDescription
POOLThe Aave V3 (or SparkLend) pool it flash-loans from, repays, withdraws from and supplies to.
ROUTERThe Uniswap V3 SwapRouter02 it buys the loan back through.
PSMThe one converter a debt in USDS may go through: Sky's USDS PSM wrapper (zero where the deployment converts no debt, as on Arbitrum).
VARIABLE_RATE_MODEAave's variable interest-rate mode, the only debt it repays.

Events

RepaidWithCollateral event

event RepaidWithCollateral(
    address indexed user,
    address indexed collateralAsset,
    address indexed debtAsset,
    uint256 flashAmount,
    uint256 premium,
    uint256 debtRepaid,
    uint256 collateralPulled,
    uint256 collateralSpent,
    uint256 collateralResupplied,
    uint256 collateralReturned
);

A position's debt was repaid out of its collateral, with every amount the operation moved.

Emitted by: executeOperation

Fields

NameTypeDescription
useraddress · indexedThe position's wallet.
collateralAssetaddress · indexedThe collateral sold.
debtAssetaddress · indexedThe debt repaid.
flashAmountuint256The amount flash-loaned.
premiumuint256The flash loan's fee.
debtRepaiduint256The debt repaid.
collateralPulleduint256The maxCollateralIn the caller named: the most the call may take from the position, not a measured amount. The collateral it withdrew is collateralSpent plus collateralResupplied plus collateralReturned, and Aave's rounding can put it a wei or two off the cap.
collateralSpentuint256The collateral the swap spent.
collateralResupplieduint256The unspent collateral supplied back to the position.
collateralReturneduint256The unspent collateral sent to the wallet instead, when the pool refused to take it back.

DebtConverted event

event DebtConverted(
    address indexed user,
    uint256 gemSold,
    uint256 debtBought,
    uint256 debtReturned
);

A debt in USDS was converted for: the gem sold to the wrapper, the USDS it bought, and the part of it the debt did not take, sent to the user.

Emitted by: executeOperation

Fields

NameTypeDescription
useraddress · indexedThe position's wallet.
gemSolduint256The gem (USDC) sold to the wrapper.
debtBoughtuint256The USDS it bought.
debtReturneduint256The USDS sent to the user: what the debt did not take.

Errors

A call that fails reverts with one of these, so a block explorer or a simulation shows which rule refused it.

ErrorWhenRaised by
Reentrancy()A call re-entered the contract during an operation.repayWithCollateral, repayConvertedWithCollateral
NotPool()The flash-loan callback was called by someone other than the Aave pool.executeOperation
NotInitiator()The flash loan was not started by this contract.executeOperation
NothingToRepay()The flash amount or the repay amount is zero, or the position has no debt.repayWithCollateral, repayConvertedWithCollateral, executeOperation
NoRoute()A converted repay at a deployment that pins no wrapper, or a bound above 10,000 bp.repayConvertedWithCollateral
ConversionShort()The wrapper's USDS for the gem fell short of par less the caller's bound.executeOperation
ShortRepay()The call’s own balance of the loan’s asset does not cover the loan and its premium: a balance already on the contract never pays it.executeOperation

On this page