# Liquidity Pool

## Module Info

* **Name**: <mark style="color:red;">`yuzuswap::liquidity_pool`</mark>
* **Description**: This module contains the core logic for the liquidity pool of the <mark style="color:red;">`yuzuswap`</mark> contract. This module only works with fungible assets. If you want to use coins, please check the <mark style="color:red;">`router`</mark> module, which contains functions to work with coins.

## Public Functions

### Swap releated functions

The <mark style="color:red;">`liquidity_pool`</mark> uses the <mark style="color:red;">`"Hot potato"`</mark> pattern for swapping feature.

### Swap Functions

***

Swaps tokens in the liquidity pool. This function returns a <mark style="color:red;">`SwapReciept`</mark> object and requires to pay back in the same transaction to complete (<mark style="color:red;">`"Hot potato"`</mark> 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. <mark style="color:red;">`true`</mark> for token 0 to token 1, <mark style="color:red;">`false`</mark> 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 <mark style="color:red;">`SwapReciept`</mark>.

```
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 <mark style="color:red;">`SwapReciept`</mark>.

```
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. <mark style="color:red;">`true`</mark> for token 0 to token 1, <mark style="color:red;">`false`</mark> 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. |


---

# Agent Instructions: 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://docs.yuzu.finance/technical/smart-contracts/yuzu-clmm/liquidity-pool.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.
