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
maxPrincipalPerCollateralTo 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.
First query the contract to get the required routes
functionName: "getAllCommitmentUniswapPoolRoutes",
arguments: [commitment?.id]
contract: LenderCommitmentForwarderAlphaThen query the contract to get the price ratio
functionName: "getUniswapPriceRatioForPoolRoutes",
arguments: [routes] # comes from previous query
contract: LenderCommitmentForwarderAlphaThen query the contract to get ltv ratio
functionName: "getCommitmentPoolOracleLtvRatio",
arguments: [commitment?.id]
contract: LenderCommitmentForwarderAlphaFinally compute the
maxCollateralPerPrincipalpriceRatio * ltvRatio / 10000
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 querybidId: id of the previous bid that comes from subgraphacceptCommitmentArgs: a tuple that is defined below"9": the flash loan premium percentage, hard coded as 9 for aavefuture15Mins: the current time plus 15 minutes.
functionName: "calculateRolloverAmount",
arguments: [rolloverCommitment?.forwarderAddress,bidId, acceptCommitmentArgs, "9",future15Mins]
contract: LenderCommitmentForwarderAlphaThis function returns 2 values, the first one is the flashLoanAmount and the second one is the borrowAmount
acceptCommitmentArgs
acceptCommitmentArgsThis 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
{
commitmentId: id of the future loan
principalAmount: result of calculating collateralAmount x maxPrincipalPerCollateral / 18 (expansion factor)
collateralAmount: amount inputted by the user
collateralTokenId: hard coded to "0" since this is for a token
collateralTokenAddress: address of the collateral token
interestRate: interest rate of the commitment
loanDuration: duration of the commitment
}Approvals
Once these 2 values have been calculated, you can proceed with the approvals
Allowance for the borrowAmount on the principal token
borrowAmount on the principal tokenCheck the amount
functionName: "allowance",
arguments: [userAddress, flashRolloverLoanAddress]
contract: principalTokenNameIf the amount is less than the collateral amount, then approve the amount
functionName: "approve",
arguments: [flashRolloverLoanAddress, borrowAmount]
contract: collateralTokenAddressAllowance for the collateralAmount on the collateral token
collateralAmount on the collateral tokenCheck the amount
functionName: "allowance",
arguments: [userAddress, collateralManagerAddress]
contract: collateralTokenAddressIf the amount is less than the collateral amount, then approve the amount
functionName: "approve",
arguments: [flashRolloverLoanAddress, collateralAmount ]
contract: collateralTokenAddressApprove market forwarder
Check if already approved
functionName: "hasApprovedMarketForwarder",
arguments: [marketplaceId, forwarderAddress, userAddress] # marketplaceId and forwarderAddress both come from subgraph
contract: LenderCommitmentForwarderAlphaIf not, then approve
functionName: "approveMarketForwarder",
arguments: [marketplaceId, forwarderAddress]
contract: LenderCommitmentForwarderAlphaApprove extension
Check if already approved
functionName: "hasExtension",
arguments: [userAddress, flashRolloverLoanAddress]
contract: flashRolloverLoanAddressIf not, then approve
functionName: "addExtension",
arguments: [flashRolloverLoanAddress]
contract: LenderCommitmentForwarderAlphaAddressExecute 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 subgraphloanId: id of the current loanflashLoanAmount: first item from the response ofcalculateRolloverAmountborrowerAmount: second item from the response ofcalculateRolloverAmountacceptCommitmentArgs: same as the argument forcalculateRolloverAmount
function: "rolloverLoanWithFlash"
args: [forwarderAddress,loanId,flashLoanAmount,borrowAmount, acceptCommitmentArgs]
contract: flashRolloverLoanLast updated