Leverage
Flash Swap is a Uniswap feature that allows Uniswap LP Pairs to send assets to the recipient before enforcing that enough Input tokens have been received. This is possible as all transactions on the blockchain are atomic in nature. This means the whole transaction can be reverted to the previous state if enough Input tokens were not received by the pair contract.
INFO
Read more about Flash Swaps here.
How do we allow Leverage?
MethLab use Flash Swaps to allow borrowers to create loans using leverage.
Step | Description |
---|---|
1. Derive Implied LTV of a vault | Implied LTV is calculated using the weighted average of Strike Price and amount borrowed per Lender according to the current Spot Price. |
2. Using the I-LTV, calculate the maximum leverage possible | Max Leverage = 1 divided by (1 - LTV). Eg. at 80% LTV, 1 / (1-0.8), gives 5x leverage to the borrower. |
3. Borrower selects leverage parameter(optional) | Borrower may choose to not leverage by keep it at 1x. Or Borrower can leverage which reduces Collateral required to open the position. |
4. The transaction is structured with calculated value for Flash Swap | If borrower chooses to leverage, swaps take place that involve a DEX for swapping the Borrowed Asset for Collateral Asset. |
5. The Collateral is escrowed and the Loan contract is created | Loan contract is created per Lender and Borrower's collateral is locked in each Loan contract which can reclaimed when the Borrower repays. |
Let's consider an example.
Strike Price = 1500 USDT (with mETH as collateral)
Spot Price of mETH= 2000 USDT
Implied LTV =
This means that the borrower can create a loan with 4x leverage by borrowing the collateral using the Flash Swap.
How to create a Loan using Leverage?
Borrowers can create leveraged loans using one of two ways.
Using the
createLoan
function available in LenderVault.solTo create a loan directly from the Vault, the borrower can call the
createLoan
function with an extra bytes parameter.soliditybytes memory data = abi.encode( vault, collSwapAmount, swapper, swapperData );
Using the
executeLoans
function available in LoanExecutor.solLoanExecutor allows the borrowers to create multiple loans at the same time. To create loans using LoanExecutor, the borrower writes the LoanExecParams and calls the
executeLoans
with the respective params.
The borrower approves the LoanExecutor to use the collateral token. The borrower then inputs the loan executor params followed by calling the executeLoans on the LoanExecutor.
The LoanExecutor acts as a borrower and takes a Flash Swap of the collateral amount from the Vault itself. This works by using the UniswapSwapper which allows to make a swap internally. Fundamentally, all swaps are flash-swaps because transactions are atomic in nature.
This internally creates a loan on the borrower's behalf by calling the createLoan function.
IMPORTANT
When a borrower creates a loan using LoanExecutor, LoanExecutor creates a loan on borrower's behalf. For the duration of this transaction, the LoanExecutor acts like a borrower. At the end, LoanExecutor transfers the loan to the borrower.
Similarly, the borrowers can use leverage to repay the loan. Borrower can call the repayLoan from the LoanExecutor.