Liquidswap Docs
GithubDappPontem Network
Liquidswap V0 Docs
Liquidswap V0 Docs
  • Introduction
    • Security Audits
    • Bounty Program
    • Developer Links
  • Protocol Overview
  • Smart Contracts
    • Generics
    • LP Coins
    • Routers
    • Scripts
    • Advanced Topics
    • 💥V0.5 Update
  • Integration
    • Test Coins
    • Let's Swap
    • Create Pool
    • Add Liquidity
    • Burn Liquidity
    • Basic Oracle
    • Flashloans
    • Unit Testing
  • Staking / Harvest
    • Overview
    • Smart Contracts
    • Integration Examples
    • DApp
    • Create your own farming pool
  • SDKs
  • Liquidswap Widget
  • Liquidswap DApp
    • Bridge
    • Testnet
  • Coins Registry
Powered by GitBook
On this page
  • Smart Contracts
  • Example
  1. Integration

Flashloans

PreviousBasic OracleNextUnit Testing

Last updated 2 years ago

The Liquidswap allows to loan liquidity from pools, use that liquidity in any 3rd party DeFi protocol or even in another Liquidswap pool itself, and return liquidity to the pool by paying the pool's standard fee.

The loan must happen in one transaction (and it's impossible to loan and then return the loan in a separate transaction!) and be returned in one transaction, which means Liquidswap liquidity never drains.

Smart Contracts

Source code - .

The idea of Flashloan is the "Hot Potato" concept; read more in ; in a nutshell: because Move language structs have abilities, we can create a such structure that can't be copied, dropped, stored, but only can be destructed.

In same time Move language allows to objects only in modules where initial structure defined.

So by creating and returning the Flashloan object to the loaner, we force the loaner to return the loan together with the Flashloan object by calling the function used to pay the loan.

struct Flashloan<phantom X, phantom Y, phantom Curve> {
    x_loan: u64,
    y_loan: u64
}

Functions to implement flashloan in your code:

Most of the functions of the specific LiquidityPool<X, Y, Curve> wouldn't work during flashloan on that pool.

The list of flashloan functions

  • flashloan<X, Y, Curve>(x_loan, y_loan) - to loan X and Y coins from the pool.

    • Generics X and Y must be sorted.

    • x_loan - amount of X coins to loan.

    • y_loan - amount of Y coins to loan.

    • Returns Coin<X>, Coin<Y>and Flashloan<X,Y, Curve> object.

  • pay_flashloan<X, Y, Curve>(x_in, y_in, loan):

    • Generics X and Y must be sorted.

    • x_in - Coin<X> to return to pool.

    • y_in - Coin<Y> to return to the pool.

Example

/sources/swap/liquidity_pool.move
the medium article
Flashloan Example