Executing a Rollover

Similar to the borrow flow, there are various approvals that must be done before executing the rollover function on the contracts. Additionally, some values must be calculated first before executing the calls and the checks.

Calculations

Calculating the maxPrincipalPerCollateral

To avoid any issues with decimals and to ensure that we are using the correct values in the case that there are multiple hops required for a swap, we have to compute the amount using uniswap data and use the minimum between the computed amount below and the maxPrincipalPerCollateralAmount.

  1. First query the contract to get the required routes

functionName: "getAllCommitmentUniswapPoolRoutes",
arguments: [commitment?.id]
contract: LenderCommitmentForwarderAlpha
  1. Then query the contract to get the price ratio

functionName: "getUniswapPriceRatioForPoolRoutes",
arguments: [routes] # comes from previous query
contract: LenderCommitmentForwarderAlpha
  1. Then query the contract to get ltv ratio

functionName: "getCommitmentPoolOracleLtvRatio",
arguments: [commitment?.id]
contract: LenderCommitmentForwarderAlpha
  1. Finally compute the maxCollateralPerPrincipal

    1. priceRatio * ltvRatio / 10000

    2. use min between computed amount and maxPrincipalPerCollateralAmount

Calculating the borrow amount for the loan

In case the new loan requires a payment, the borrowerAmount must be queried.

If the borrowerAmount is positive - then the new loan requires a payment, if it's negative the new loan will reimburse the user.

Arguments:

  • rolloverCommitment?.forwarderAddress: forwarder address that comes from the subgraph query

  • bidId: id of the previous bid that comes from subgraph

  • acceptCommitmentArgs : a tuple that is defined below

  • "9": the flash loan premium percentage, hard coded as 9 for aave

  • future15Mins: the current time plus 15 minutes.

This function returns 2 values, the first one is the flashLoanAmount and the second one is the borrowAmount

acceptCommitmentArgs

This tuple is also used for executing the rollover.

The parameters are below, and these are all of the future commitment that will be used to rollover the previous loan

Approvals

Once these 2 values have been calculated, you can proceed with the approvals

Allowance for the borrowAmount on the principal token

Check the amount

If the amount is less than the collateral amount, then approve the amount

Allowance for the collateralAmount on the collateral token

Check the amount

If the amount is less than the collateral amount, then approve the amount

Approve market forwarder

Check if already approved

If not, then approve

Approve extension

Check if already approved

If not, then approve

Execute the rollover

Once all the checks have been completed and there is enough allowance, the rolloverLoanWithFlash function can be called from the contract.

Arguments:

  • forwarderAddress: queried from the subgraph

  • loanId: id of the current loan

  • flashLoanAmount: first item from the response of calculateRolloverAmount

  • borrowerAmount: second item from the response of calculateRolloverAmount

  • acceptCommitmentArgs: same as the argument for calculateRolloverAmount

Last updated