Skip to main content

Lending and Flash Loans

In your life, you have probably had to borrow money, e.g. for a student loan, a car loan, or a mortgage. Lending and borrowing are essential services in any financial system. Traditionally, lenders such as banks, provide you with a loan in exchange for a fee in the form of an interest rate. Repaying the said loan is enforced by legal contracts with the bank.

Similar situations can be found on the blockchain as well. A borrower needs a sum of money immediately available for an operation, and a lender may agree to provide such a loan in exchange for a fee.

DeFi loans

Definition

DeFi lending allows users to become lenders or borrowers in a completely decentralized and permissionless way while maintaining full custody over their coins.

Users, who want to become lenders, supply their tokens to a particular money market and start receiving interest on their tokens according to the current supply APY (Annual Percentage Yield).

But how do you enforce the repayment of a loan on the blockchain?

Each loan on the blockchain needs to be collateralized with other tokens. Even worse, as tokens are very volatile, blockchain loans are over-collateralized (often at 133%). For instance, if you want to borrow $1m in USDS, you need to provide $1.33m in MVRK as collateral.

You may then wonder, why would anyone take a loan if they need to provide even more as collateral?

There are multiple reasons:

  • They may not want to sell their original tokens.
  • They may want to avoid or delay paying capital gain taxes on their tokens.
  • They may want to use the borrowed funds to increase their leverage in a certain position.

Example

For instance, Alice has 133 USDS and believes the price of MVRK will go down. To benefit from this, Alice takes a loan of 20 MVRK using here 133 USDS (considering 5 USDS per MVRK, plus 33% for over-collateralization). She immediately sells the 20 MVRK for 100 USDS. She now has 233 USDS (133 initially plus 100 from the sale) and owes a debt of 20 MVRK. Consider that MVRK goes down to $2. Alice buys 20 MVRK for 40 USDS, repays the 20 MVRK to the lender, and retrieves her 133 USDS collateral back. Alice now has 23340=193233 - 40 = 193 USDS. She has made a $60 benefit. This whole operation is called a short position.

Liquidation

As long as the value of the collateral is superior or equal to 133% of the borrowed funds, there are no limits to how long a user can keep the borrowed funds for.

However, if the value of the collateral falls below 133%, the user then has his collateral liquidated for the protocol to repay the borrowed amount.

Interests

Borrowers pay lenders interests on their loans, plus a platform fee. The interest rate is determined by supply and demand. For instance, if many lenders are offering loans for MVRK and demand is low, interest rates will be lowered to attract more demand. However, if there are few lenders but demand is high, interests are increased (lenders obviously want to earn as much money as possible). For instance, if traders believe the price of MVRK will go down, a lot of them might want to open short positions as seen in the example above, and therefore increase the demand for loans.

Interests paid on loans are based on their APY. Lenders usually offer two kinds of APY:

  • A variable APY, which is updated at every block depending on offer and demand.
  • A much higher but stable APY that will not change during the whole period of your loan.

Variable rates may sometimes be low and attractive, but keep in mind that they can sharply move at any time. As seen above, if the exchange rate of MVRK goes down, demand for loans increases, and variable APYs can skyrocket.

For instance, if you borrow $100 for a year at a stable APY of 10%, you will pay $10 interest. Now consider borrowing at a 5% variable APY, but after 6 months, the APY jumps to 30% for the rest of the year. You would then have to pay $2.5 interest for the first 6 months and $15 for the last 6 months.

Note that interests are computed at each block, increasing your debt a little every time. If APY jumps sharply, your debt might surpass the 133% collateral limits. You must close your loan or add more collateral, or your loan will be liquidated. This is how you can get liquidated based only on accrued interests even if the tokens borrowed and used as collateral hasn't changed in price (typically borrowing stablecoins with stablecoins).

Lending on Mavryk

Maven Finance: the first open-source and non-custodial lending protocol on Mavryk

Flash loans

Flash loans have been popularized in 2020 and are very useful, they allow one to borrow tokens without any collateral. What's the catch?

A flash loan has to be borrowed and repaid within the same blockchain transaction.

To understand this concept, let's recall how transactions work on Mavryk.

Transactions

A transaction represents a set of steps that must be executed in an atomic way, i.e., either all the steps are executed, or the transaction is rolled back, and none of the steps take place.

Sending MVRK, sending FA2 tokens, and interacting with smart contracts can be steps executed within the scope of one transaction.

Transactions are grouped and included in a Mavryk block. Each transaction can be observed on a block explorer such as the Mavryk Explorer.

On Mavryk, transactions can consist of multiple consecutive steps, e.g. you could supply MVRK and borrow USDT on Maven Finance, swap half of your borrowed USDT for wBTC through the Mavryk Wallet and provide liquidity to the MVRK/USDT pool on Maven Finance - all-in-one single Mavryk transaction. If any of these steps result in an error, the whole transaction would be rolled back, and none of the steps would occur. Note that you would still have to pay the gas fees. Even failed contract executions consume gas.

The gas cost bounds the number of steps in a single transaction. So although you could, in theory, create a valid transaction with thousands of steps, it'd be rejected because of the maximum limit on gas cost per block.

Remember that a flash loan has to be borrowed and repaid within the same transaction, so there is no risk that borrowers will not repay their borrowed amount.

flash-loan

FIGURE 1: Operation flow of a flash loan in the case of a successful repayment (green arrows) or failed repayment (red arrows)

Use cases

Here are three of the most common use cases for flash loans:

Arbitrage

Arbitrage is the simultaneous purchase and sale of the same asset in different exchanges to profit from tiny differences in the asset's listed price. It exploits short-lived variations in the price of identical or similar financial instruments in other markets.

Flash loans are particularly interesting for arbitrageurs to increase profits. FIGURE 2 presents an arbitrage opportunity. Let's suppose that the price of USDS is currently 1 kUSD on Dexter but 0.99 kUSD on Quipuswap. An arbitrageur would exploit the difference as follows:

  • Step 1: Borrow 100,000 USDT from Maven Finance
  • Step 2: Exchange 100,000 USDT against 101,010 EURT through the Mavryk Wallet
  • Step 3: Exchange 101,010 EURT against 101,010 USDT on Maven Finance
  • Step 4: Repay the 100,000 USDT on Maven Finance (plus usually a 0.09% fee making the total repayment at 100,900 USDT)
  • Step 5: Keep the remaining 110 USDT in profits!

All these steps are included in one transaction. They either all succeed, or they will all fail.

arbitrage

FIGURE 2: A typical arbitrage operation involving a flash loan.

This process looks pretty easy, but keep in mind that arbitrageurs will still need to:

  • pay network fees, which can be pretty high in complex transactions.
  • take price slippage into account. (As seen in the DEX chapter, depending on the size of your order, marginal price and effective swap price can be quite different, leading to possible unprofitable outcomes).
  • face fierce competition. Arbitrage is a well-known practice, and many traders are doing it. On top of that, bots that monitor the mempool [4] can pick up your profitable arbitrage opportunity and send the same transaction with a higher gas fee, which would be inserted in a block sooner, basically stealing your arbitrage opportunity.

Collateral Swaps

Another application of flash loans is for collateral swaps. Let's say you have borrowed USDT from Maven Finance with MVRK as collateral. You can swap your collateral from MVRK to, for example, wBTC in the following way:

  • Step 1: Take a flash loan in kUSD to cover the amount of kUSD that was borrowed
  • Step 2: Repay your Maven Finance loan with borrowed kUSD
  • Step 3: Withdraw your MVRK
  • Step 4: Swap your MVRK for wBTC on Uniswap
  • Step 5: Supply wBTC as collateral on Maven Finance
  • Step 6: Borrow kUSD against your wBTC collateral
  • Step 7: Repay flash loan with the borrowed kUSD + fee
  • Step 8: Congrats, you just swapped your collateral from MVRK to wBTC and only paid 0.09% of the borrowed amount for this.

Self-liquidation

Finally, flash loans can also be used for self-liquidation. Let's say that you have a loan in USDT on Maven Finance with MVRK as collateral. The MVRK price keeps going down, and you're approaching the liquidation level. You also don't have or don't want to deposit more MVRK to decrease your liquidation level and you also don't have the kUSD required to repay the loan. Now, instead of allowing the contract to liquidate your collateral and charge you the liquidation fee, you can take the following steps:

  • Step 1: Take a flash loan for the amount of USDT that you owe
  • Step 2: Repay your USDT loan and withdraw your MVRK
  • Step 3: Swap enough MVRK to USDT to repay the flash loan + fees
  • Step 4: Keep the rest of your MVRK

Conclusion

Flash loans are helpful building blocks in DeFi as they can be used for things like arbitrage, swapping collateral, and self-liquidation. Of course, the concept of flash loans is relatively new, and there are use cases still to be discovered in the future.

Lending and Flash loans on Mavryk

Flash loans are not yet available on Mavryk, but works are in progress.

References

[3] https://finematics.com/lending-and-borrowing-in-defi-explained/

[4] A pool (set) of operations maintained by a node and not yet included in a block.