Swap and Calculate Swap Result

1. Swap

1.1. Function params

  • account: the signer of the user.

  • pool: the object of the pool.

  • a_to_b: the swap direction, a2b equals true means sold coin a then get coin b.

  • by_amount_in: when it equal true means want to fix the amount of input coin. when it equal false means want to fix the amount of output coin.

  • amount: when by_amount_in equals true, amount means the quantity of input coin, when by_amount_in equals false, amount means the quantity of output coin.

  • amount_limit: the threshold value of coin. when by_amount_in equals true, it means the minimum amount about received coin, when by_amount_in equals false, it means the maximum amount abount sold.

  • sqrt_price_limit: two constant of sqrt price(x64 fixed-point number). When a2b equals true, it equals 4295048016, when a2b equals false, it equals 79226673515401279992447579055.

1.2. Function

// tucana_clmm::router
public entry fun swap(
    account: &signer,
    pool: Object<Pool>,
    a_to_b: bool,
    by_amount_in: bool,
    amount: u64,
    amount_limit: u64,
    sqrt_price_limit: u128,
) {
    ...
}

2. Swap with partner

2.1 Function params

  • account: the signer of the user.

  • pool: the object of the pool.

  • partner: the object of the partner, if you want to create, then connect manager.

  • a_to_b: the swap direction, a2b equals true means sold coin a then get coin b.

  • by_amount_in: when it equal true means want to fix the amount of input coin. when it equal false means want to fix the amount of output coin.

  • amount: when by_amount_in equals true, amount means the quantity of input coin, when by_amount_in equals false, amount means the quantity of output coin.

  • amount_limit: the threshold value of coin. when by_amount_in equals true, it means the minimum amount about received coin, when by_amount_in equals false, it means the maximum amount abount sold.

  • sqrt_price_limit: two constant of sqrt price(x64 fixed-point number). When a2b equals true, it equals 4295048016, when a2b equals false, it equals 79226673515401279992447579055.

2.2 Function

// tucana_clmm::router
public entry fun swap_with_partner(
    account: &signer,
    pool: Object<Pool>,
    partner: Object<Partner>,
    a_to_b: bool,
    by_amount_in: bool,
    amount: u64,
    amount_limit: u64,
    sqrt_price_limit: u128,
) {
    ...
}

3. Calculate swap result

2.1 View function params

  • pool: the object of the pool.

  • a_to_b: the swap direction, a2b equals true means sold coin a then get coin b.

  • by_amount_in: when it equal true means want to fix the amount of input coin. when it equal false means want to fix the amount of output coin.

  • amount: when by_amount_in equals true, amount means the quantity of input coin, when by_amount_in equals false, amount means the quantity of output coin.

2.2 View Function

// tucana_clmm::router
/// The calculated swap result
struct CalculatedSwapResult has copy, drop, store {
    amount_in: u64,
    amount_out: u64,
    fee_amount: u64,
    fee_rate: u64,
    after_sqrt_price: u128,
    current_sqrt_price: u128,
    is_exceed: bool,
    step_results: vector<SwapStepResult>
}


#[view]
public fun calculate_swap_result(
    pool: Object<Pool>,
    a2b: bool,
    by_amount_in: bool,
    amount: u64,
): CalculatedSwapResult acquires Pool {
    ...
}

Last updated