> For the complete documentation index, see [llms.txt](https://tucana-1.gitbook.io/tucana-developer-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tucana-1.gitbook.io/tucana-developer-docs/spot-dex/spot-contract/feature-available/swap-and-calculate-swap-result.md).

# 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

```rust
// 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&#x20;

### 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

```rust
// 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

```rust
// 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 {
    ...
}

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tucana-1.gitbook.io/tucana-developer-docs/spot-dex/spot-contract/feature-available/swap-and-calculate-swap-result.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
