# 交換（Exchange）

這是一個擴展Jetton的智能合約，它是用每個流動性對創建的。路由器在請求創建新池時部署它。儘管路由器實際上存儲了一對中的兩個代幣並通過它進行交換，但必須調用交換合約才能提供或獲取流動性。

## Code

Github鏈接：（待更新）

## Read-Only Functions

### Exchange Minter

{% tabs %}
{% tab title="Read-Only Functions" %}
**get\_jetton\_data**

```
(int, int, slice, cell, cell) get_jetton_data() method_id
```

* int totalSupply
* int mintable
* slice admin\_address
* cell exchange\_content
* cell exchange\_wallet\_code

**get\_lp\_swap\_data**

```
(int, int, slice, slice, slice, int, int, slice, slice, int, int) get_lp_swap_data() method_id
```

* int swap\_fee
* int min\_amount
* slice router\_address
* slice jettonA\_address
* slice jettonA\_wallet\_address
* int jettonA\_balance
* int jettonA\_pending\_balance
* slice jettonB\_address
* slice jettonB\_wallet\_address
* int jettonB\_balance
* int jettonB\_pending\_balance

**get\_lp\_mining\_data**

```
(int, int, int, int, int, int, cell) get_lp_mining_data() method_id
```

* int mining\_amount
* int datetime\_amount
* int minable\_time
* int half\_life
* int last\_index
* int last\_mined
* cell mining\_rate\_cell

**get\_lp\_user\_info\_dict**

```
(cell) get_lp_user_info_dict() method_id
```

* cell ( dict ) total\_user\_info\_dict

**get\_lp\_user\_info**

```
(int, int, int, int) get_lp_user_info(slice user_address) method_id
```

* int find
* int balance
* int user\_reward\_sum
* int last\_user\_index

**get\_wallet\_address**

```
slice get_wallet_address(slice owner_address) method_id
```

* slice user\_wallet\_address
  {% endtab %}

{% tab title="State-Changing Functions" %}

#### How to build transaction

* Add Liquidity

```typescript
// 1. add liquidity tokenA
await sendInternalMessageWithWallet({
    walletContract,
    secretKey,
    to: userJettonAWalletAddress,
    value: toNano(0.7),
    body: beginCell()
        .storeUint(0xf8a7ea5, 32)
        .storeUint(query_id, 64)
        .storeCoins(jettonAAmount)
        .storeAddress(lpAddress)
        .storeAddress(responseAddress)
        .storeDict(beginCell().endCell())
        .storeCoins(toNano(0.5))
        .storeDict(beginCell()
            .storeCoins(minAmountForJettonA)
            .storeCoins(minAmountForJettonB)
        .endCell())
    .endCell()
});

// 2. add liquidity tokenB
await sendInternalMessageWithWallet({
    walletContract,
    secretKey,
    to: userJettonBWalletAddress,
    value: toNano(0.7),
    body: beginCell()
        .storeUint(0xf8a7ea5, 32)
        .storeUint(query_id, 64)
        .storeCoins(jettonBAmount)
        .storeAddress(lpAddress)
        .storeAddress(responseAddress)
        .storeDict(beginCell().endCell())
        .storeCoins(toNano(0.5))
        .storeDict(beginCell()
            .storeCoins(minAmountForJettonA)
            .storeCoins(minAmountForJettonB)
        .endCell())
    .endCell()
});
```

* Remove Liquidity

```typescript
await sendInternalMessageWithWallet({
    walletContract,
    secretKey,
    to: lpAddress,
    value: toNano(0.35),
    body: beginCell()
        .storeUint(0x595f07bc, 32)
        .storeUint(query_id, 64)
        .storeCoins(lpAmount)
        .storeDict(beginCell()
            .storeCoins(minAmountForJettonA)
            .storeCoins(minAmountForJettonB)
        .endCell())
    .endCell()
});
```

* Claim

```typescript
await sendInternalMessageWithWallet({
    walletContract,
    secretKey,
    to: lpAddress,
    value: toNano(0.15),
    body: beginCell()
        .storeUint(0x6b14cfe1, 32)
        .storeUint(query_id, 64)
    .endCell()
});
```

{% endtab %}
{% endtabs %}

### Exchange Wallet

{% tabs %}
{% tab title="Read-Only Functions" %}
**get\_wallet\_data**

```
(int, slice, slice, cell) get_wallet_data() method_id
```

* int balance
* slice owner\_address
* slice lp\_minter\_address
* cell lp\_wallet\_code

**get\_wallet\_extra\_data**

```
(int, int, slice, slice) get_wallet_extra_data() method_id
```

* int jettonA\_pending\_balance
* int jettonB\_pending\_balance
* slice jettonA\_address
* slice jettonB\_address
  {% endtab %}
  {% endtabs %}


---

# 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.megaton.fi/zh-tw/developers/he-yue/jiao-huan-exchange.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.
