# Router

### router

* [Description](#description)
* [Data Storage](#data-storage)
* [Interface](#interface)
* [Messages](#messages)

## Description

This contract implements the router and does the management of the pools. Due to the distributed nature of the TON Blockchain router, it can't do many checks, so it's mostly the proxy for the calls. The router contract so far is used as an owner of all the wallets holding the funds invested by liquidity providers.

The main idea is that if malformed or corrupted data is sent to the router it would create a malformed address of the pool and the message sent to it would fail. So if the message reaches the pool it means some criteria are satisfied.

## Data Storage

<table data-full-width="true"><thead><tr><th width="70">Index</th><th width="100">Type</th><th width="100">Size (b/r)</th><th width="58">Cell</th><th width="280">Name</th><th>Description</th></tr></thead><tbody><tr><td>1</td><td>addr</td><td>267 / 0</td><td>1</td><td>router::admin_address</td><td>Admin address.</td></tr><tr><td>2</td><td>addr</td><td>267 / 0</td><td>1</td><td>router::pool_admin_address</td><td>Admin address.</td></tr><tr><td>3</td><td>addr</td><td>267 / 0</td><td>1</td><td>router::pool_factory_address</td><td>PoolFactory address. Only this address and admin can create new pools</td></tr><tr><td>4</td><td>uint64</td><td>64 / 0</td><td>1</td><td>router::flags</td><td>Flags that control some router features</td></tr><tr><td>5</td><td>uint64</td><td>64 / 0</td><td>1</td><td>router::pool_seqno</td><td>Number of pools created. Used by indexer to ensure that none of pools are skipped</td></tr><tr><td>6</td><td>code</td><td>0 / 1</td><td>11</td><td>router::poolv3_code</td><td>The cell with the code of the pool, that is needed to create a pool contract</td></tr><tr><td>7</td><td>code</td><td>0 / 1</td><td>11</td><td>router::accountv3_code</td><td>The cell with the code of the account, that is needed to create initial data for pool contract</td></tr><tr><td>8</td><td>code</td><td>0 / 1</td><td>11</td><td>router::position_nftv3_code</td><td>The cell with the code of the user NFT position, that is needed to create initial data for pool contract</td></tr><tr><td>9</td><td>cell</td><td>0 / 1</td><td>1</td><td>router::timelocked_updates</td><td>This cell holds 3 maybe_refs to cells with timelock data for changeable structures</td></tr></tbody></table>

## Cells

| Name | Size | Free |
| ---- | ---- | ---- |
| 1    | 929  | 94   |
| 11   | 0    | 1023 |

## Interface

### getRouterState

(slice, slice, slice, int, int) getRouterState ()

returns router admin address

#### Return Values

| # | Type  | Description                                                 |
| - | ----- | ----------------------------------------------------------- |
| 0 | slice | router admin address (router::admin\_address)               |
| 1 | slice | default value for pool admin (router::pool\_admin\_address) |
| 2 | slice | pool factory address (router::pool\_factory\_address)       |
| 3 | int   | currently active flags ( 0 for working and active router)   |
| 4 | int   | router internal number of pool creation or reinit messages  |

### getPoolAddress

(slice) getPoolAddress (slice jetton\_wallet0, slice jetton\_wallet1)

returns pool address for two given jetton\_wallets belonging to the router

#### Parameters

| # | Type  | Name            | Description                                        |
| - | ----- | --------------- | -------------------------------------------------- |
| 0 | slice | jetton\_wallet0 | Address of the jetton 0 wallet belonging to router |
| 1 | slice | jetton\_wallet1 | Address of the jetton 1 wallet belonging to router |

#### Return Values

| # | Type  | Description  |
| - | ----- | ------------ |
| 0 | slice | pool address |

### getChildContracts

(cell, cell, cell) getChildContracts ()

returns code of the child contracts

#### Return Values

| # | Type | Description                       |
| - | ---- | --------------------------------- |
| 0 | cell | code of the pool contract         |
| 1 | cell | code of the account contract      |
| 2 | cell | code of the nft position contract |

## Messages

### JETTON\_TRANSFER\_NOTIFICATION

Opcode : **0x7362d09c**

Process router funding, payload determines if it is mint or swap\ <br>

<table data-full-width="true"><thead><tr><th width="50">Path</th><th width="200">Mnemonic</th><th width="180">Type</th><th>Description</th></tr></thead><tbody><tr><td></td><td>op</td><td>Uint(32),op</td><td></td></tr><tr><td></td><td>query_id</td><td>Uint(64)</td><td>queryid as of the TON documentation</td></tr><tr><td></td><td>jetton_amount</td><td>Coins(124)</td><td>Amount of coins sent to the router</td></tr><tr><td></td><td>from_user</td><td>Address(267)</td><td>User that originated the transfer</td></tr><tr><td></td><td>forward_payload</td><td>Cell(0),Either, Payload</td><td>Payload for processing</td></tr></tbody></table>

<details>

<summary>TL-B Description (TBD)</summary>

This is a preliminary tl-b - subject to change

{% code title="Tlb for JETTON\_TRANSFER\_NOTIFICATION" overflow="wrap" lineNumbers="true" %}

```javascript
JETTON_TRANSFER_NOTIFICATION#7362d09c 
    query_id:uint64
    jetton_amount:(VarUInteger 16)
    from_user:MsgAddress
    forward_payload:(Either ^Cell Cell)
= ContractMessages;
```

{% endcode %}

</details>

### ROUTERV3\_CREATE\_POOL

Opcode : **0x2e3034ef**

Operation that deploys and inits new [Pool](https://docs.tonco.io/dokumentaciya-i-gaidy/technical-reference/contracts/pool) contract for two given jettons identified by their wallets. New pool would reorder the jettons to match the invariant `slice_hash(jetton0_address) > slice_hash(jetton1_address).`\ <br>

<table data-full-width="true"><thead><tr><th width="50">Path</th><th width="200">Mnemonic</th><th width="180">Type</th><th>Description</th></tr></thead><tbody><tr><td></td><td>op</td><td>Uint(32),op</td><td></td></tr><tr><td></td><td>query_id</td><td>Uint(64)</td><td>queryid as of the TON documentation</td></tr><tr><td></td><td>jetton_wallet0</td><td>Address(267)</td><td>Address of the jetton0 wallet. Used to compute pool address</td></tr><tr><td></td><td>jetton_wallet1</td><td>Address(267)</td><td>Address of the jetton1 wallet. Used to compute pool address</td></tr><tr><td></td><td>tick_spacing</td><td>Int(24)</td><td>Tick spacing to be used in the pool</td></tr><tr><td></td><td>initial_priceX96</td><td>Uint(160),PriceX96</td><td>Initial price for the pool</td></tr><tr><td></td><td>protocol_fee</td><td>Uint(16),Fee</td><td>Liquidity provider fee. base in FEE_DENOMINATOR parts. If value is more than 10000 value would be default</td></tr><tr><td></td><td>lp_fee_base</td><td>Uint(16),Fee</td><td>Protocol fee in FEE_DENOMINATOR. If value is more than 10000 value would be default</td></tr><tr><td></td><td>lp_fee_current</td><td>Uint(16),Fee</td><td>Current value of the pool fee, in case of dynamic adjustment. If value is more than 10000 value would be default</td></tr><tr><td></td><td>nftv3_content</td><td>Cell(0),Metadata</td><td>Metadata for the NFT Collection</td></tr><tr><td></td><td>nftv3item_content</td><td>Cell(0),Metadata</td><td>Metadata for the NFT Item</td></tr><tr><td></td><td>0</td><td>Cell(0) minter_cell</td><td>Cell With Minters</td></tr><tr><td>0</td><td>jetton0_minter</td><td>Address(267)</td><td>Address of the jetton0 minter, used by indexer and frontend</td></tr><tr><td>0</td><td>jetton1_minter</td><td>Address(267)</td><td>Address of the jetton1 minter, used by indexer and frontend</td></tr><tr><td>0</td><td>controller_addr</td><td>Address(267)</td><td>Address that is allowed to change the fee. Can always be updated by admin. If has_controller is false could be 00b</td></tr></tbody></table>

<details>

<summary>TL-B Description (TBD)</summary>

This is a preliminary tl-b - subject to change

{% code title="Tlb for ROUTERV3\_CREATE\_POOL" overflow="wrap" lineNumbers="true" %}

```javascript
ROUTERV3_CREATE_POOL#2e3034ef 
    query_id:uint64
    jetton_wallet0:MsgAddress
    jetton_wallet1:MsgAddress
    tick_spacing:int24
    initial_priceX96:uint160
    protocol_fee:uint16
    lp_fee_base:uint16
    lp_fee_current:uint16
    nftv3_content:Cell
    nftv3item_content:Cell
    minter_cell:^[
        jetton0_minter:MsgAddress
        jetton1_minter:MsgAddress
        controller_addr:MsgAddress
    ]  
= ContractMessages;
```

{% endcode %}

</details>

### ROUTERV3\_PAY\_TO

Opcode : **0xa1daa96d**

This is not a message Op this is a payload format for JETTON\_TRANSFER\_NOTIFICATION\ <br>

<table data-full-width="true"><thead><tr><th width="50">Path</th><th width="200">Mnemonic</th><th width="180">Type</th><th>Description</th></tr></thead><tbody><tr><td></td><td>op</td><td>Uint(32),op</td><td></td></tr><tr><td></td><td>query_id</td><td>Uint(64)</td><td>queryid as of the TON documentation</td></tr><tr><td></td><td>reciever0</td><td>Address(267)</td><td>Address of the first reciever of the funds</td></tr><tr><td></td><td>reciever1</td><td>Address(267)</td><td>Address of the second reciever of the funds</td></tr><tr><td></td><td>exit_code</td><td>Uint(32)</td><td>queryid as of the TON documentation</td></tr><tr><td></td><td>seqno</td><td>Uint(64),Indexer</td><td>queryid as of the TON documentation</td></tr><tr><td></td><td>0</td><td>Maybe Cell(1) coinsinfo_cell</td><td>Cell with info about the coins</td></tr><tr><td>0</td><td>amount0</td><td>Coins(124)</td><td>Amount of coins to be payed to reciever0</td></tr><tr><td>0</td><td>jetton0_address</td><td>Address(267)</td><td>Jetton to be sent to reciever0 identified by the wallet that belongs to router</td></tr><tr><td>0</td><td>amount1</td><td>Coins(124)</td><td>Amount of coins to be payed to reciever1</td></tr><tr><td>0</td><td>jetton1_address</td><td>Address(267)</td><td>Jetton to be sent to reciever1 identified by the wallet that belongs to router</td></tr><tr><td>Predicate</td><td></td><td>exit_code = 200</td><td></td></tr><tr><td></td><td>1</td><td>Maybe Cell(1) indexer_swap_info_cell</td><td>Information for indexer to process after the swap</td></tr><tr><td>1</td><td>liquidity</td><td>Uint(128),Indexer</td><td>Post-swap concentrated liquidity at current tick</td></tr><tr><td>1</td><td>price_sqrt</td><td>Uint(160),Indexer,PriceX96</td><td>Post-swap square root of the price stored as fixed point 64.96</td></tr><tr><td>1</td><td>tick</td><td>Int(24),Indexer</td><td>Post-swap current tick</td></tr><tr><td>1</td><td>feeGrowthGlobal0X128</td><td>Int(256),Indexer</td><td>Current range fee per unit of the liquidity for jetton0</td></tr><tr><td>1</td><td>feeGrowthGlobal1X128</td><td>Int(256),Indexer</td><td>Current range fee per unit of the liquidity for jetton1</td></tr><tr><td>Predicate</td><td></td><td>exit_code = 201</td><td></td></tr><tr><td></td><td>2</td><td>Maybe Cell(1) indexer_burn_info_cell</td><td>Information for indexer to process after the burn</td></tr><tr><td>2</td><td>nftIndex</td><td>Uint(64),Indexer</td><td>Nft index that is burned</td></tr><tr><td>2</td><td>liquidityBurned</td><td>Uint(128),Indexer</td><td>Amount of liquidity burned</td></tr><tr><td>2</td><td>tickLower</td><td>Int(24),Indexer</td><td>Lower tick of the range in which liquidity was burned</td></tr><tr><td>2</td><td>tickUpper</td><td>Int(24),Indexer</td><td>Upper tick of the range in which liquidity was burned</td></tr><tr><td>2</td><td>tick</td><td>Int(24),Indexer</td><td>Post-burn current tick</td></tr></tbody></table>

<details>

<summary>TL-B Description (TBD)</summary>

This is a preliminary tl-b - subject to change

{% code title="Tlb for ROUTERV3\_PAY\_TO" overflow="wrap" lineNumbers="true" %}

```javascript
ROUTERV3_PAY_TO#a1daa96d 
    query_id:uint64
    reciever0:MsgAddress
    reciever1:MsgAddress
    exit_code:uint32
    seqno:uint64
    coinsinfo_cell:(Maybe ^[
        amount0:(VarUInteger 16)
        jetton0_address:MsgAddress
        amount1:(VarUInteger 16)
        jetton1_address:MsgAddress
    ] ) 
    (exit_code = 200)?(
        indexer_swap_info_cell:(Maybe ^[
            liquidity:uint128
            price_sqrt:uint160
            tick:int24
            feeGrowthGlobal0X128:int256
            feeGrowthGlobal1X128:int256
        ] ) 
    )
    (exit_code = 201)?(
        indexer_burn_info_cell:(Maybe ^[
            nftIndex:uint64
            liquidityBurned:uint128
            tickLower:int24
            tickUpper:int24
            tick:int24
        ] ) 
    )
= ContractMessages;
```

{% endcode %}

</details>

### ROUTERV3\_RESET\_GAS

Opcode : **0x42a0fb43**

This operation allows router owners the gas if too much accumulated on the contract\
Access Rights: This operation is allowed for router::admin\_address<br>

<table data-full-width="true"><thead><tr><th width="50">Path</th><th width="200">Mnemonic</th><th width="180">Type</th><th>Description</th></tr></thead><tbody><tr><td></td><td>op</td><td>Uint(32),op</td><td></td></tr><tr><td></td><td>query_id</td><td>Uint(64)</td><td>queryid as of the TON documentation</td></tr></tbody></table>

<details>

<summary>TL-B Description (TBD)</summary>

This is a preliminary tl-b - subject to change

{% code title="Tlb for ROUTERV3\_RESET\_GAS" overflow="wrap" lineNumbers="true" %}

```javascript
ROUTERV3_RESET_GAS#42a0fb43 
    query_id:uint64
= ContractMessages;
```

{% endcode %}

</details>
