Liquidity Pool

Module Info

  • Name: yuzuswap::liquidity_pool

  • Description: This module contains the core logic for the liquidity pool of the yuzuswap contract. This module only works with fungible assets. If you want to use coins, please check the router module, which contains functions to work with coins.

Public Functions

Swap releated functions

The liquidity_pool uses the "Hot potato" pattern for swapping feature.

Swap Functions


Swaps tokens in the liquidity pool. This function returns a SwapReciept object and requires to pay back in the same transaction to complete ("Hot potato" pattern).

public fun swap(
    trader: &signer,
    pool: Object<LiquidityPool>,
    zero_for_one: bool,
    is_exact_in: bool,
    specified_amount: u64,
    sqrt_price_limit: u128,
): (FungibleAsset, SwapReciept)

Function arguments

Argument
Type
Description

trade

&signer

The trader’s signer.

pool

Object

The liquidity pool.

zero_for_one

bool

Direction of the swap. true for token 0 to token 1, false for token 1 to token 0.

is_exact_in

bool

Whether the specified amount is the exact input amount.

specified_amount

u64

The specified amount for the swap.

sqrt_price_limit

u128

The sqrt price limit for the swap.

Returns

Type
Description

FungibleAsset

The swapped fungible asset

SwapReciept

The swap receipt.

SwapReciept

struct SwapReciept {
    pool: Object<LiquidityPool>,
    token_metadata: Object<Metadata>,
    amount_in: u64,
}
Field
Type
Description

pool

Object

The liquidity pool of the swap.

token_metadata

Object

The metadata of the token in of the swap.

amount_in

u64

The amount of token in needs to be paid for the swap.

Get swap receipt amount


Gets the amount from a SwapReciept.

public fun get_swap_receipt_amount(swap_receipt: &SwapReciept): u64

Function arguments

Argument
Type
Description

swap_receipt

&SwapReciept

The swap receipt.

Returns

Type
Description

u64

The amount in the receipt.

Get swap receipt token metadata


Gets the token metadata from a SwapReciept.

public fun get_swap_receipt_token_metadata(swap_receipt: &SwapReciept): Object<Metadata>

Function arguments

Argument
Type
Description

swap_receipt

&SwapReciept

&SwapReciept

Returns

Type
Description

Object

The token metadata in the receipt.

Pay swap


Pays the swap using the provided token and receipt.

public fun pay_swap(
    token_in: FungibleAsset,
    reciept: SwapReciept,
)

Function arguments

Argument
Type
Description

token_in

FungibleAsset

The input fungible asset.

receipt

SwapReciept

The swap receipt.

Quote swap


Quotes a swap in the liquidity pool without executing it.

public fun quote_swap(
    trader: address,
    pool: Object<LiquidityPool>,
    zero_for_one: bool,
    is_exact_in: bool,
    specified_amount: u64,
    sqrt_price_limit: u128,
): (u64, u64, u64)

Function arguments

Argument
Type
Description

trader

address

The trader’s address.

pool

Object

The liquidity pool.

zero_for_one

bool

Direction of the swap. true for token 0 to token 1, false for token 1 to token 0.

is_exact_in

bool

Whether the specified amount is the exact input amount.

specified_amount

u64

The specified amount for the swap.

sqrt_price_limit

u128

The sqrt price limit for the swap.

Returns

Type
Description

u64

The amount of token in.

u64

The amount of token out.

u64

The fee amount for the swap.

Last updated