> For the complete documentation index, see [llms.txt](https://docs.teller.org/teller-lite/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.teller.org/teller-lite/dev-guide/rollover/executing-a-rollover.md).

# 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.&#x20;

## 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
```

2. Then query the contract to get the price ratio

```
functionName: "getUniswapPriceRatioForPoolRoutes",
arguments: [routes] # comes from previous query
contract: LenderCommitmentForwarderAlpha
```

3. Then query the contract to get ltv ratio

```
functionName: "getCommitmentPoolOracleLtvRatio",
arguments: [commitment?.id]
contract: LenderCommitmentForwarderAlpha
```

4. Finally compute the `maxCollateralPerPrincipal`
   1. priceRatio \* ltvRatio / 10000
   2. use min between computed amount and maxPrincipalPerCollateralAmount

### Calculating the borrow amount for the loan <a href="#collateral-allowance" id="collateral-allowance"></a>

In case the new loan requires a payment, the borrowerAmount must be queried.&#x20;

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

Arguments:&#x20;

* `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.

```
functionName: "calculateRolloverAmount",
arguments: [rolloverCommitment?.forwarderAddress,bidId, acceptCommitmentArgs, "9",future15Mins]
contract: LenderCommitmentForwarderAlpha
```

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

#### `acceptCommitmentArgs` <a href="#collateral-allowance" id="collateral-allowance"></a>

This tuple is also used for executing the rollover.&#x20;

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

Check the amount

```
functionName: "allowance",
arguments: [userAddress, flashRolloverLoanAddress]
contract: principalTokenName
```

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

```
functionName: "approve",
arguments: [flashRolloverLoanAddress, borrowAmount]
contract: collateralTokenAddress
```

### Allowance for the `collateralAmount` on the collateral token

Check the amount

```
functionName: "allowance",
arguments: [userAddress, collateralManagerAddress]
contract: collateralTokenAddress
```

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

```
functionName: "approve",
arguments: [flashRolloverLoanAddress, collateralAmount ]
contract: collateralTokenAddress
```

### Approve market forwarder

Check if already approved

```
functionName: "hasApprovedMarketForwarder",
arguments: [marketplaceId, forwarderAddress, userAddress] # marketplaceId and forwarderAddress both come from subgraph
contract: LenderCommitmentForwarderAlpha
```

If not, then approve

```
functionName: "approveMarketForwarder",
arguments: [marketplaceId, forwarderAddress]
contract: LenderCommitmentForwarderAlpha
```

### Approve extension&#x20;

Check if already approved

```
functionName: "hasExtension",
arguments: [userAddress, flashRolloverLoanAddress]
contract: flashRolloverLoanAddress
```

If not, then approve

```
functionName: "addExtension",
arguments: [flashRolloverLoanAddress]
contract: LenderCommitmentForwarderAlphaAddress
```

## Execute the rollover

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

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`

```
function: "rolloverLoanWithFlash"
args: [forwarderAddress,loanId,flashLoanAmount,borrowAmount, acceptCommitmentArgs]
contract: flashRolloverLoan
```
