Router

Module info

  • Name: yuzuswap::router

  • Description: This module contains public functions for other modules/contracts to call and interact with the yuzuswap contract.

Public Functions

Create Pool

Create pool with two fungible assets


Creates a new liquidity pool for two fungible assets. Reverts if the pool already exists.

public fun create_pool(
    creator: &signer,
    token_0: Object<Metadata>,
    token_1: Object<Metadata>,
    fee: u64,
    sqrt_price: u128,
)

Function arguments

Argument
Type
Description

creator

&signer

The creator’s signer

token_0

Object

Fungible asset metadata of token 0

token_1

Object

Fungible asset metadata of token 1

fee

u64

Fee tier of the pool

sqrt_price

u128

Initial sqrt price of the pool

Create pool with one coin and one fungible asset


Creates a new liquidity pool with one coin (Token0) and one fungible asset. Reverts if the pool already exists.

public fun create_pool_one_coin<Token0>(
    creator: &signer,
    token_1: Object<Metadata>,
    fee: u64,
    sqrt_price: u128,
)

Function type arguments

Argument
Description

Token0

The coin’s type.

Function arguments

Argument
Type
Description

creator

&signer

The creator’s signer

token_1

Object

Fungible asset metadata of token 1

fee

u64

Fee tier of the pool

sqrt_price

u128

Initial sqrt price of the pool

Create pool with two coins


Creates a new liquidity pool with two coins Token0 and Token1. Reverts if the pool already exists.

public fun create_pool_both_coins<Coin0, Coin1>(
    creator: &signer,
    fee: u64,
    sqrt_price: u128,
)

Function type arguments

Argument
Description

Coin0

The coin’s type of the token 0

Coin1

The coin’s type of the token 1

Function arguments

Argument
Type
Description

creator

&signer

The creator’s signer.

fee

u64

Fee tier of the pool.

sqrt_price

u128

Initial sqrt price of the pool.

Add liquidity

Add liquidity to a pool with two fungible assets


Adds liquidity to a pool with two fungible assets.

public fun add_liquidity(
    user: &signer,
    pool: Object<LiquidityPool>,
    position_id: u64,
    tick_lower: u32,
    tick_upper: u32,
    token_0: FungibleAsset,
    token_1: FungibleAsset,
    amount_0_min: u64,
    amount_1_min: u64,
): (FungibleAsset, FungibleAsset)

Function arguments

Argument
Type
Description

user

&signer

The user’s signer.

pool

Object

The liquidity pool to add liquidity to.

position_id

u64

The position ID of the liquidity. Leave it as 0 if you want to create a new position or you don’t have any position yet.

tick_lower

u32

The lower tick of the liquidity. Required in the case of creating a new position. Otherwise, leave it as 0.

tick_upper

u32

The upper tick of the liquidity. Required in the case of creating a new position. Otherwise, leave it as 0.

token_0

FungibleAsset

The amount of token 0 to add to the liquidity.

token_1

FungibleAsset

The amount of token 1 to add to the liquidity.

amount_0_min

u64

The minimum amount of token 0 to add to the liquidity.

amount_1_min

u64

The minimum amount of token 1 to add to the liquidity.

Returns

Type
Description

(FungibleAsset, FungibleAsset)

The added fungible assets.

Remove liquidity from a pool


Removes liquidity from a pool.

public fun remove_liquidity(
    user: &signer,
    pool: Object<LiquidityPool>,
    position_id: u64,
    liquidity: u128,
    amount_0_min: u64,
    amount_1_min: u64,
): (FungibleAsset, FungibleAsset)

Function arguments

Argument
Type
Description

user

&signer

The user’s signer.

pool

Object

The liquidity pool to remove liquidity from.

position_id

u64

The position ID of the liquidity.

liquidity

u128

The amount of liquidity to remove.

amount_0_min

u64

The minimum amount of token 0 to get.

amount_1_min

u64

The minimum amount of token 1 to get.

Returns

Type
Description

FungibleAsset

The returned fungible assets of the token 0 after removing liquidity.

FungibleAsset

The returned fungible assets of the token 1 after removing liquidity.

Collect fee


Collects fee from a position.

public fun collect_fee(
    user: &signer,
    pool: Object<LiquidityPool>,
    position_id: u64,
    amount_0_requested: u64,
    amount_1_requested: u64,
): (FungibleAsset, FungibleAsset)

Function arguments

Argument
Type
Description

user

&signer

The user’s signer.

pool

Object

The liquidity pool.

position_id

u64

The position ID.

amount_0_requested

u64

The amount of token 0 requested.

amount_1_requested

u64

The amount of token 1 requested.

Returns

Type
Description

FungibleAsset

The collected fee of the token 0.

FungibleAsset

The collected fee of the token 1.

Collect reward


Collects reward from a position.

public fun collect_reward(
    user: &signer,
    pool: Object<LiquidityPool>,
    position_id: u64,
    reward_index: u64,
    amount_requested: u64,
): FungibleAsset

Function arguments

Argument
Type
Description

user

&signer

The user’s signer.

pool

Object

The liquidity pool.

position_id

u64

The position ID.

reward_index

u64

The reward index.

amount_requested

u64

The amount of reward requested.

Returns

Type
Description

FungibleAsset

The collected reward.

Swap functions

Swap exact fungible asset for fungible asset


Swaps an exact amount of fungible asset for another fungible asset.

public fun swap_exact_fa_for_fa(
    trader: &signer,
    pool: Object<LiquidityPool>,
    token_in: FungibleAsset,
    amount_out_min: u64,
    sqrt_price_limit: u128,
): FungibleAsset

Function arguments

Argument
Type
Description

trader

&signer

The trader’s signer.

pool

Object

The liquidity pool.

token_in

FungibleAsset

The input token.

amount_out_min

u64

The minimum amount of output token.

sqrt_price_limit

u128

The sqrt price limit.

Returns

Type
Description

FungibleAsset

The swapped fungible asset.

Swap exact fungible asset for coin


Swaps an exact amount of fungible asset for a coin.

public fun swap_exact_fa_for_coin<CoinType>(
    trader: &signer,
    pool: Object<LiquidityPool>,
    token_in: FungibleAsset,
    amount_out_min: u64,
    sqrt_price_limit: u128,
): Coin<CoinType>

Function type arguments

Argument
Description

CoinType

The coin’s type

Function arguments

Argument
Type
Description

trader

&signer

The trader’s signer.

pool

Object

The liquidity pool.

token_in

FungibleAsset

The input token.

amount_out_min

u64

The minimum amount of output token.

sqrt_price_limit

u128

The sqrt price limit.

Returns

Type
Description

Coin

The swapped coin.

Swap exact coin for fungible asset


Swaps an exact amount of coin for a fungible asset.

public fun swap_exact_coin_for_fa<CoinType>(
    trader: &signer,
    pool: Object<LiquidityPool>,
    token_in: Coin<CoinType>,
    amount_out_min: u64,
    sqrt_price_limit: u128,
): FungibleAsset

Function type arguments

Argument
Description

CoinType

The coin’s type

Function arguments

Argument
Type
Description

trader

&signer

The trader’s signer.

pool

Object

The liquidity pool.

token_in

Coin

The input coin.

amount_out_min

u64

The minimum amount of output token.

sqrt_price_limit

u128

The sqrt price limit.

Returns

Type
Description

FungibleAsset

The swapped fungible asset.

Swap exact coin for coin


Swaps an exact amount of coin for another coin.

public fun swap_exact_coin_for_coin<CoinIn, CoinOut>(
    trader: &signer,
    pool: Object<LiquidityPool>,
    token_in: Coin<CoinIn>,
    amount_out_min: u64,
    sqrt_price_limit: u128,
): Coin<CoinOut>

Function type arguments

Argument
Description

CoinIn

The input coin’s type

CoinOut

The output coin’s type

Function arguments

Argument
Type
Description

trader

&signer

The trader’s signer.

pool

Object

The liquidity pool.

token_in

Coin

The input coin.

amount_out_min

u64

The minimum amount of output token.

sqrt_price_limit

u128

The sqrt price limit.

Returns

Type
Description

Coin

The swapped coin.

Swap fungible asset for exact fungible asset


Swaps a fungible asset for an exact amount of another fungible asset.

public fun swap_fa_for_exact_fa(
    trader: &signer,
    pool: Object<LiquidityPool>,
    token_in: FungibleAsset,
    amount_out_desired: u64,
    sqrt_price_limit: u128,
): (FungibleAsset, FungibleAsset)

Function arguments

Argument
Type
Description

trader

&signer

The trader’s signer.

pool

Object

The liquidity pool.

token_in

FungibleAsset

The input token.

amount_out_desired

u64

The desired amount of output token.

sqrt_price_limit

u128

The sqrt price limit.

Returns

Type
Description

FungibleAsset

The remaining fungible asset of the input token.

FungibleAsset

The swapped fungible asset of the output token.

Swap fungible asset for exact coin


Swaps a fungible asset for an exact amount of a coin.

public fun swap_fa_for_exact_coin<CoinType>(
    trader: &signer,
    pool: Object<LiquidityPool>,
    token_in: FungibleAsset,
    amount_out_desired: u64,
    sqrt_price_limit: u128,
): (FungibleAsset, Coin<CoinType>)

Function type arguments

Argument
Description

CoinType

The coin’s type

Function arguments

Argument
Type
Description

trader

&signer

The trader’s signer.

pool

Object

The liquidity pool.

token_in

FungibleAsset

The input token.

amount_out_desired

u64

The desired amount of output token.

sqrt_price_limit

u128

The sqrt price limit.

Returns

Type
Description

FungibleAsset

The remaining fungible asset of the input token.

Coin

The swapped coin of the output token.

Swap coin for exact fungible asset


Swaps a coin for an exact amount of a fungible asset.

public fun swap_coin_for_exact_fa<CoinType>(
    trader: &signer,
    pool: Object<LiquidityPool>,
    token_in: Coin<CoinType>,
    amount_out_desired: u64,
    sqrt_price_limit: u128,
): (Coin<CoinType>, FungibleAsset)

Function type arguments

Argument
Description

CoinType

The coin’s type

Function arguments

Argument
Type
Description

trader

&signer

The trader’s signer.

pool

Object

The liquidity pool.

token_in

Coin

The input coin.

amount_out_desired

u64

The desired amount of output token.

sqrt_price_limit

u128

The sqrt price limit.

Returns

Type
Description

Coin

The remaining coin of the input token.

FungibleAsset

The swapped fungible asset of the output token.

Swap coin for exact coin


Swaps a coin for an exact amount of another coin.

public fun swap_coin_for_exact_coin<CoinIn, CoinOut>(
    trader: &signer,
    pool: Object<LiquidityPool>,
    token_in: Coin<CoinIn>,
    amount_out_desired: u64,
    sqrt_price_limit: u128,
): (Coin<CoinIn>, Coin<CoinOut>)

Function type arguments

Argument
Description

CoinIn

The input coin’s type

CoinOut

The output coin’s type

Function arguments

Argument
Type
Description

trader

&signer

The trader’s signer.

pool

Object

The liquidity pool.

token_in

Coin

The input coin.

amount_out_desired

u64

The desired amount of output token.

sqrt_price_limit

u128

The sqrt price limit.

Returns

Type
Description

Coin

The remaining coin of the input token.

Coin

The swapped coin of the output token.

Swap exact fungible asset for fungible asset with multiple hops


Swaps an exact amount of fungible asset for another fungible asset with multiple hops.

public fun swap_exact_fa_for_fa_multi_hops(
    trader: &signer,
    pools: vector<Object<LiquidityPool>>,
    token_in: FungibleAsset,
    amount_out_min: u64,
): FungibleAsset

Function arguments

Argument
Type
Description

trader

&signer

The trader’s signer.

pools

vector<Object>

The liquidity pools.

token_in

FungibleAsset

The input token.

amount_out_min

u64

The minimum amount of output token.

Returns

Type
Description

FungibleAsset

The swapped fungible asset.

Swap exact coin for fungible asset with multiple hops


Swaps an exact amount of coin for a fungible asset with multiple hops.

public fun swap_exact_coin_for_fa_multi_hops<CoinType>(
    trader: &signer,
    pools: vector<Object<LiquidityPool>>,
    token_in: Coin<CoinType>,
    amount_out_min: u64,
): FungibleAsset

Function type arguments

Argument
Description

CoinType

The coin’s type

Function arguments

Argument
Type
Description

trader

&signer

The trader’s signer.

pools

vector<Object>

The liquidity pools.

token_in

Coin

The input coin.

amount_out_min

u64

The minimum amount of output token.

Returns

Type
Description

FungibleAsset

The swapped fungible asset.

Swap fungible asset for exact fungible asset with multiple hops


Swaps a fungible asset for an exact amount of another fungible asset with multiple hops.

public fun swap_fa_for_exact_fa_multi_hops(
    trader: &signer,
    pools: vector<Object<LiquidityPool>>,
    token_in: FungibleAsset,
    amount_out_desired: u64,
): (FungibleAsset, FungibleAsset)

Function arguments

Argument
Type
Description

trader

&signer

The trader’s signer.

pools

vector<Object>

The liquidity pools.

token_in

FungibleAsset

The input token.

amount_out_desired

u64

The desired amount of output token.

sqrt_price_limit

u128

The sqrt price limit.

Returns

Type
Description

FungibleAsset

The remaining fungible asset of the input token.

FungibleAsset

The swapped fungible asset of the output token.

Swap coin for exact fungible asset with multiple hops


Swaps a coin for an exact amount of a fungible asset with multiple hops.

public fun swap_coin_for_exact_fa_multi_hops<CoinType>(
    trader: &signer,
    pools: vector<Object<LiquidityPool>>,
    token_in: Coin<CoinType>,
    amount_out_desired: u64,
): (Coin<CoinType>, FungibleAsset)

Function type arguments

Argument
Description

CoinType

The coin’s type

Function arguments

Argument
Type
Description

trader

&signer

The trader’s signer.

pools

vector<Object>

The liquidity pools.

token_in

Coin

The input coin.

amount_out_desired

u64

The desired amount of output token.

Returns

Type
Description

Coin

The remaining coin of the input token.

FungibleAsset

The swapped fungible asset of the output token.

Get pool functions

Get pool


Gets the liquidity pool for two fungible assets.

#[view]
public fun get_pool(
    token_0: Object<Metadata>,
    token_1: Object<Metadata>,
    fee: u64,
): Object<LiquidityPool>

Function arguments

Argument
Type
Description

token_0

Object

Fungible asset metadata of token 0

token_1

Object

Fungible asset metadata of token 1

fee

u64

Fee tier of the pool

Returns

Type
Description

Object

The liquidity pool.

Get pool with one coin and one fungible asset


Gets the liquidity pool for one coin (CoinType) and one fungible asset.

#[view]
public fun get_pool_one_coin<CoinType>(
    token_1: Object<Metadata>,
    fee: u64,
): Object<LiquidityPool>

Function type arguments

Argument
Description

CoinType

The coin’s type

Function arguments

Argument
Type
Description

token_1

Object

Fungible asset metadata of token 1

fee

u64

Fee tier of the pool

Returns

Type
Description

Object

The liquidity pool.

Get pool with two coins


Gets the liquidity pool for two coins Coin0 and Coin1.

#[view]
public fun get_pool_both_coins<Coin0, Coin1>(
    fee: u64,
): Object<LiquidityPool>

Function type arguments

Argument
Description

Coin0

The coin’s type of the token 0

Coin1

The coin’s type of the token 1

Function arguments

Argument
Type
Description

fee

u64

Fee tier of the pool

Returns

Type
Description

Object

The liquidity pool.

Last updated