# Exchange

This is a smart contract to expand Jetton that is created with every liquidity pair. The Router deploys this as new pool creation is requested. Although the Router actually stores the two tokens of a pair and swapping takes place through it, the Exchange Contract must be called for liquidity to be provided or taken.

## Code

Github Link: (to be updated)

## 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
// TL-B schema
// transfer query_id:uint64 amount:VarUInteger 16 destination:MsgAddress response_destination:MsgAddress custom_payload:Maybe ^Cell forward_ton_amount:VarUInteger 16 forward_payload:Either Cell ^Cell = InternalMsgBody

// 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
// TL-B schema
// burn query_id:uint64 amount:VarUInteger 16 response_destination:MsgAddress custom_payload:Maybe ^Cell = InternalMsgBody

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
// TL-B schema
// claim query_id:uint64 = InternalMsgBody

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/developers/contract/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.
