Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pool

pool

Description

This contract implements V3 like functionality of the pool with concentrated liquidity.

Due to the new storage organization and availability of the dict data type we don't need the tickBitmap data structures. However, gas consumption while using the dict is also quite significant

Data Storage

IndexTypeSize (b/r)CellNameDescription
1addr267 / 01poolv3::router_addressAddress of the router contract that created this pool (totally immutable set at creation )
2uint1616 / 01poolv3::lp_fee_baseLiquidity provider fee. base in FEE_DENOMINATOR parts
3uint1616 / 01poolv3::protocol_feeProtocol fee in FEE_DENOMINATOR
4uint1616 / 01poolv3::lp_fee_currentCurrent value of the pool fee, in case of dynamic adjustment
5addr267 / 01poolv3::jetton0_walletAddress of the 0 token in the pool. This is an address of the jetton0 wallet attached to router
6addr267 / 01poolv3::jetton1_walletAddress of the 1 token in the pool This is an address of the jetton0 wallet attached to router
7int2424 / 01poolv3::tick_spacingSpacing of the ticks, 24 bits of signed int would be used
8uint6464 / 01poolv3::seqnoUsed by indexer to ensure that none of pool interactions are skipped
9uint256256 / 011poolv3::feeGrowthGlobal0X128This variable stores current collected fee per the unit of liquidity in jetton0
10uint256256 / 011poolv3::feeGrowthGlobal1X128This variable stores current collected fee per the unit of liquidity in jetton0
11uint128128 / 011poolv3::collectedProtocolFee0Collected protocol fee of the jetton0
12uint128128 / 011poolv3::collectedProtocolFee1Collected protocol fee of the jetton1
13coins124 / 011poolv3::reserve0These are additional separate protection system - it calculates the reserves of the pool
In case main math has a bug it protects the funds of other pools. Reserve of the jetton0
14coins124 / 011poolv3::reserve1Reserve of the jetton1
15uint11 / 012poolv3::pool_activePool acitve flag 0 is inactive, 1 is active
16int2424 / 012poolv3::tickCurrent tick, signed, 24 bits would be used. Pool maintains it in correspondence to poolv3::price_sqrt
17uint160160 / 012poolv3::price_sqrtCurrent square root of the price in Q64.96 format using 160 bits
18uint128128 / 012poolv3::liquidityCurrent active concentrated liquidity in 128 bits
19uint2424 / 012poolv3::occupied_ticksnumber of occupied ticks in storage
20uint6464 / 012poolv3::nftv3item_counterPool is also an NFT collection. So this is the counter of currently minted positions
21uint6464 / 012poolv3::nftv3items_activePool is also an NFT collection. Number of unburnt items
22addr267 / 012poolv3::admin_addressAdmin address. Can init pool
23addr267 / 012poolv3::controller_addressController address. Can change fee and lock and unlock pool
24addr267 / 0121poolv3::jetton0_minterAddress of the 0 token minter. Beta only.
25addr267 / 0121poolv3::jetton1_minterAddress of the 1 token minter. Beta only.
26dict0 / 113poolv3::ticks_dictionaryStorage of the ticks
27code0 / 114poolv3::accountv3_codePool knows how to create user accounts to store fund pairs for a particular user
28code0 / 114poolv3::position_nftv3_codePool knows how to create user position. So it stores it's code
29cell0 / 114poolv3::nftv3_contentpacked metadata that would be given to nft that corresponds to nft collection
30cell0 / 114poolv3::nftv3item_contentpacked metadata that would be given to nft that corresponds to the position

Cells

NameSizeFree
193786
1110167
1299924
1301023
1401023
121534489

Interface

getIsActive

(int) getIsActive ()

Returns is pool is active

Return Values

#TypeDescription
0intint containing the poolv3::pool_active

getPoolStateAndConfiguration

(slice, slice, slice, slice, slice, slice, slice, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int ) getPoolStateAndConfiguration ()

Returns pool state and configuration

Return Values

#TypeDescription
0sliceRouter address
1sliceAdmin address
2sliceController address
3sliceJetton 0 Wallet address. The wallet is owned by the Router
4sliceJetton 1 Wallet address. The wallet is owned by the Router
5sliceJetton 0 Minter address.
6sliceJetton 1 Minter address.
7intFlag that denotes if the pool is active
8intPool tick spacing
9intFee that is used as a base. Stored in x 1/10000
10intFee that protocol takes. Stored in x 1/10000
11intFee that is currently active. Stored in x 1/10000
12intCurrent tick
13intCurrent price
14intCurrent liquidity
15intpoolv3::feeGrowthGlobal0X128
16intpoolv3::feeGrowthGlobal1X128
17intAmount of jetton0 fee collected for protocol owners
18intAmount of jetton0 fee collected for protocol owners
19intNumber of total minted NFT positions
20intReserves of the jetton0
21intReserves of the jetton1
22intNumber of active NFT positions
23intNumber of currently occupied ticks
24intNumber of operations with pool since the deploy

getChildContracts

(cell, cell, cell, cell) getChildContracts ()

Return Values

#TypeDescription
0cellcode of the account contract
1cellcode of the nft position contract
2cellmetadata for NFT Collection
3cellmetadata for NFT Item

getAllTickInfos

(cell) getAllTickInfos ()

returns the cell with all the ticks.

Return Values

#TypeDescription
0cellcell that contains the dict with all the ticks.

getTickInfosFrom

(tuple) getTickInfosFrom (int key, int amount, int dir, int full)

Returns ticks starting form key (non-inclusive)

  • 0 forward
  • 1 backward

Parameters

#TypeNameDescription
0intkeykey (tick number) from which to get data
1intamountamount of ticks to get (large values would fail with out of gas)
2intdir0 - for forward search, 1 - for backward
3intfullwith true (non zero) would fill fee counter values

getCollectedFees

(int, int) getCollectedFees (int tickLower, int tickUpper, int posLiquidityDelta, int posFeeGrowthInside0X128, int posFeeGrowthInside1X128)

Predicts how much fees a position can collect

Parameters

#TypeNameDescription
0inttickLower
1inttickUpper
2intposLiquidityDelta
3intposFeeGrowthInside0X128
4intposFeeGrowthInside1X128

Return Values

#TypeDescription
0intamount of jetton0 that is collected
1intamount of jetton1 that is collected

getUserAccountAddress

(slice) getUserAccountAddress (slice user_address)

computes user account address for a given user

Parameters

#TypeNameDescription
0sliceuser_addressaddress for witch account address is requested

Return Values

#TypeDescription
0sliceaccount address

getMintEstimate

(int, int, int) getMintEstimate (int tickLower, int tickUpper, int liquidity)

Computes estimates for the mint

Parameters

#TypeNameDescription
0inttickLowerlower tick for the desired position
1inttickUpperupper tick for the desired position
2intliquidityliquidity that is planned to be minted

Return Values

#TypeDescription
0intamount of jetton0 needed to mint the position
1intamount of jetton1 needed to mint the position
2interror code that shows if the basic checks for the mint would succeed

getSwapEstimate

(int, int) getSwapEstimate (int zeroForOne, int amount, int sqrtPriceLimitX96)

Deprecated. Use getSwapEstimateGas instead

Computes estimates fot the swap

Parameters

#TypeNameDescription
0intzeroForOnedirection of the swap zeroForOne !=0 - swap jetton0 for jetton1, otherwise jetton1 for jetton0
1intamountamount of input token for the swap
2intsqrtPriceLimitX96limit price (formatted as fixed point 64.96 square root of the price).If reached - swap will stop

Return Values

#TypeDescription
0intamount of jetton0 that would be put to/get from the pool
1intamount of jetton1 that would be put to/get from the pool

getSwapEstimateGas

(int, int) getSwapEstimateGas (int zeroForOne, int amount, int sqrtPriceLimitX96, int minOutAmount, int gasLimit)

Computes estimates for the swap

Parameters

#TypeNameDescription
0intzeroForOnedirection of the swap zeroForOne !=0 - swap jetton0 for jetton1, otherwise jetton1 for jetton0
1intamountamount of input token for the swap
2intsqrtPriceLimitX96limit price (formatted as fixed point 64.96 square root of the price).If reached - swap will stop (swap would succeed remaining input amount would return as change )
3intminOutAmountminimum amount. If return value would be less than this, swap will be reverted
4intgasLimitamount of gas (in gas units). If gasLimit is 0 - default value is used - that equals to contract gas limit from the config

Return Values

#TypeDescription
0intamount of jetton0 that would be put to/get from the pool
1intamount of jetton1 that would be put to/get from the pool

get_collection_data

(int, cell, slice) get_collection_data ()

In accordance with TEP-62

Return Values

#TypeDescription
0intpoolv3::nftv3item_counter
1cellpoolv3::nftv3_content
2slicepoolv3::router_address

get_nft_address_by_index

(slice) get_nft_address_by_index (int index)

In accordance with TEP-62

Parameters

#TypeNameDescription
0intindexindex of the requested NFT (Position NFT)

Return Values

#TypeDescription
0sliceaddress of the Position NFT

get_nft_content

(cell) get_nft_content (int index, cell nftv3item_content)

In accordance with TEP-62 we will form the output NFT onchain metadata

If NFTItemContent "description" field that is passed to the pool by the pool administrator (poolv3::nftv3item_content) begins with marker "%N%", data from the NFT is added to the beginning of the "description" field. If there is no data attached to the dictionary by the NFT, NFTItemContent description is unchanged

Parameters

#TypeNameDescription
0intindexindex of the requested NFT
1cellnftv3item_contentNFT content returned from the get_nft_data() of the NFT

Return Values

#TypeDescription
0cellcell with metadata dict()

Messages

POOLV3_INIT

Opcode : 0x441c39ed

The first mandatory operation that fills crucial parameters of the pool
\

PathMnemonicTypeDescription
opUint(32),op
query_idUint(64)queryid as of the TON documentation
from_adminUint(1),BoolFlag that shows if this message goes from router admin or pool factory
has_adminUint(1),BoolFlag that shows if this message have a new admin address
admin_addrAddress(267)New address of the admin. If has_admin is false could be 00b
has_controllerUint(1),BoolFlag that shows if this message have a new controller address
controller_addrAddress(267)Address that is allowed to change the fee. Can always be updated by admin. If has_controller is false could be 00b
set_spacingUint(1),BoolFlag that shows if tick_spacing should be set to the pool or ignored
tick_spacingInt(24)Tick spacing to be used in the pool
set_priceUint(1),BoolFlag that shows if initial_priceX96 should be set to the pool or ignored
initial_priceX96Uint(160),PriceX96Initial price for the pool
set_activeUint(1),BoolFlag that shows if pool_active should be set to the pool or ignored
pool_activeUint(1),BoolFlag is we should start the pool as unlocked
protocol_feeUint(16),FeeLiquidity provider fee. base in FEE_DENOMINATOR parts. If value is more than 10000 value would be default
lp_fee_baseUint(16),FeeProtocol fee in FEE_DENOMINATOR. If value is more than 10000 value would be default
lp_fee_currentUint(16),FeeCurrent value of the pool fee, in case of dynamic adjustment. If value is more than 10000 value would be default
nftv3_contentCell(0),MetadataMetadata for the NFT Collection
nftv3item_contentCell(0),MetadataMetadata for the NFT Item
0Maybe Cell(1) minter_cellCell With Minters
0jetton0_minterAddress(267)Address of the jetton0 minter, used by indexer and frontend
0jetton1_minterAddress(267)Address of the jetton1 minter, used by indexer and frontend
TL-B Description (TBD)

This is a preliminary tl-b - subject to change

Tlb for POOLV3_INIT

POOLV3_INIT#441c39ed 
    query_id:uint64
    from_admin:uint1
    has_admin:uint1
    admin_addr:MsgAddress
    has_controller:uint1
    controller_addr:MsgAddress
    set_spacing:uint1
    tick_spacing:int24
    set_price:uint1
    initial_priceX96:uint160
    set_active:uint1
    pool_active:uint1
    protocol_fee:uint16
    lp_fee_base:uint16
    lp_fee_current:uint16
    nftv3_content:Cell
    nftv3item_content:Cell
    minter_cell:(Maybe ^[
        jetton0_minter:MsgAddress
        jetton1_minter:MsgAddress
    ] ) 
= ContractMessages;

POOLV3_LOCK

Opcode : 0x5e74697

This operation locks the pool. This is allowed to do by the operator and the admin
\

PathMnemonicTypeDescription
opUint(32),op
query_idUint(64)queryid as of the TON documentation
TL-B Description (TBD)

This is a preliminary tl-b - subject to change

Tlb for POOLV3_LOCK

POOLV3_LOCK#5e74697 
    query_id:uint64
= ContractMessages;

POOLV3_UNLOCK

Opcode : 0x3205adbd

This operation locks the pool. This is allowed to do by the operator and the admin
\

PathMnemonicTypeDescription
opUint(32),op
query_idUint(64)queryid as of the TON documentation
TL-B Description (TBD)

This is a preliminary tl-b - subject to change

Tlb for POOLV3_UNLOCK

POOLV3_UNLOCK#3205adbd 
    query_id:uint64
= ContractMessages;

POOLV3_MINT

Opcode : 0x81702ef8


\

PathMnemonicTypeDescription
opUint(32),op
query_idUint(64)queryid as of the TON documentation
amount0FundedCoins(124)Amount of jetton 0 received by router for the mint
amount1FundedCoins(124)Amount of jetton 1 recived by router for the mint
recipientAddress(267)Address that would receive the minted NFT, excesses and refunds
liquidityUint(128)Amount of liquidity to mint
tickLowerInt(24)lower bound of the range in which to mint
tickUpperInt(24)upper bound of the range in which to mint
TL-B Description (TBD)

This is a preliminary tl-b - subject to change

Tlb for POOLV3_MINT

POOLV3_MINT#81702ef8 
    query_id:uint64
    amount0Funded:(VarUInteger 16)
    amount1Funded:(VarUInteger 16)
    recipient:MsgAddress
    liquidity:uint128
    tickLower:int24
    tickUpper:int24
= ContractMessages;

POOLV3_BURN

Opcode : 0xd73ac09d

Burn whole or part of nft. Is sent by Position NFT itself, would only be accepted from the correct NFT itself
\

PathMnemonicTypeDescription
opUint(32),op
query_idUint(64)queryid as of the TON documentation
recipientAddress(267)NFT owner to receive funds
burned_indexUint(64)Index if the NFT to burn. Should match the sender address
liquidityUint(128)NFT liquidity amount prior to burn
tickLowerInt(24)Lower tick of the NFT. Sanitized by NFTPosition contract
tickUpperInt(24)Upper tick of the NFT. Sanitized by NFTPosition contract
liquidity2BurnUint(128)Amount of the liquidity to burn, 0 is a valid amount, in this case only collected fees would be returned
0Cell(0) old_fee_cellFee counters to collect from
0feeGrowthInside0LastX128Uint(256),x128Fee counter inside position range for jetton0, per unit of liquidity, in 128.128 fixed point
0feeGrowthInside1LastX128Uint(256),x128Fee counter inside position range for jetton1, per unit of liquidity, in 128.128 fixed point
1Cell(0) new_fee_cellFee counters to collect to (Used by indexer)
1feeGrowthInside0CurrentX128Uint(256),x128,IndexerFee counter inside position range for jetton0, per unit of liquidity, in 128.128 fixed point
1feeGrowthInside1CurrentX128Uint(256),x128,IndexerFee counter inside position range for jetton1, per unit of liquidity, in 128.128 fixed point
TL-B Description (TBD)

This is a preliminary tl-b - subject to change

Tlb for POOLV3_BURN

POOLV3_BURN#d73ac09d 
    query_id:uint64
    recipient:MsgAddress
    burned_index:uint64
    liquidity:uint128
    tickLower:int24
    tickUpper:int24
    liquidity2Burn:uint128
    old_fee_cell:^[
        feeGrowthInside0LastX128:uint256
        feeGrowthInside1LastX128:uint256
    ]  
    new_fee_cell:^[
        feeGrowthInside0CurrentX128:uint256
        feeGrowthInside1CurrentX128:uint256
    ]  
= ContractMessages;

POOLV3_SET_FEE

Opcode : 0x6bdcbeb8

This operation sets the fee values for the pool. This is allowed to do by the operator and the admin
\

PathMnemonicTypeDescription
opUint(32),op
query_idUint(64)queryid as of the TON documentation
protocol_feeUint(16)Liquidity provider fee. base in FEE_DENOMINATOR parts
lp_fee_baseUint(16)Protocol fee in FEE_DENOMINATOR
lp_fee_currentUint(16)Current value of the pool fee, in case of dynamic adjustment
TL-B Description (TBD)

This is a preliminary tl-b - subject to change

Tlb for POOLV3_SET_FEE

POOLV3_SET_FEE#6bdcbeb8 
    query_id:uint64
    protocol_fee:uint16
    lp_fee_base:uint16
    lp_fee_current:uint16
= ContractMessages;

POOLV3_FUND_ACCOUNT

Opcode : 0x4468de77

Proxy proof of the jettons funding and mint request to the AccountV3. For more information refer to AccountV3
\

PathMnemonicTypeDescription
opUint(32),op
query_idUint(64)queryid as of the TON documentation
owner_addrAddress(267)Address that would receive the minted NFT, excesses and refunds
amount0Coins(124)Amount of jetton0 that is funded for the mint
amount1Coins(124)Amount of jetton1 that is funded for the mint
enough0Coins(124)Minimum amount of jetton0 totally collected on the account that is required to start the mint
enough1Coins(124)Minimum amount of jetton1 totally collected on the account that is required to start the mint
liquidityUint(128)Amount of liquidity to mint
tickLowerInt(24)lower bound of the range in which to mint
tickUpperInt(24)upper bound of the range in which to mint
TL-B Description (TBD)

This is a preliminary tl-b - subject to change

Tlb for POOLV3_FUND_ACCOUNT

POOLV3_FUND_ACCOUNT#4468de77 
    query_id:uint64
    owner_addr:MsgAddress
    amount0:(VarUInteger 16)
    amount1:(VarUInteger 16)
    enough0:(VarUInteger 16)
    enough1:(VarUInteger 16)
    liquidity:uint128
    tickLower:int24
    tickUpper:int24
= ContractMessages;

POOLV3_START_BURN

Opcode : 0x530b5f2c

Burn whole or part of nft. Can be called by anyone, but if not called be the owner - would fail later. This operation would compute the amount of the fees that the position is eligible to get and then forwards a message to the Position NFT contract
\

PathMnemonicTypeDescription
opUint(32),op
query_idUint(64)queryid as of the TON documentation
burned_indexUint(64)Index if the NFT to burn
liquidity2BurnUint(128)Amount of the liquidity to burn, 0 is a valid amount, in this case only collected fees would be returned
tickLowerInt(24)Lower tick of the NFT. Should match the real one
tickUpperInt(24)Upper tick of the NFT. Should match the real one
TL-B Description (TBD)

This is a preliminary tl-b - subject to change

Tlb for POOLV3_START_BURN

POOLV3_START_BURN#530b5f2c 
    query_id:uint64
    burned_index:uint64
    liquidity2Burn:uint128
    tickLower:int24
    tickUpper:int24
= ContractMessages;

POOLV3_SWAP

Opcode : 0xa7fb58f8

Computes the swap math, and issues a command to the router to send funds. Only would be accepted from the router This operation we have several input parameters that would affect the result of the swap

ConditionSwap resultReturned ChangeError Code
Swap finished sqrtPriceLimitX96 not reached. minOutAmount surpassedtotal output number of coins0RESULT_SWAP_OK
Swap finished minOutAmount not surpassed0amountRESULT_SWAP_OUTPUT_TOO_SMALL
Swap reached sqrtPriceLimitX96 after changing part1 coins. minOutAmount surpassedoutput number of coinsamount - part1RESULT_SWAP_OK


Access Rights: This operation is allowed for poolv3::router_address\

PathMnemonicTypeDescription
opUint(32),op
query_idUint(64)queryid as of the TON documentation
owner_addressAddress(267)Owner of the liquidity in swap
source_walletAddress(267)jetton wallet attached to the router. used to identify swap direction
0Cell(0) params_cellFee counters To (Used by indexer)
0amountCoins(124)Input amount of the jettons to be swapped
0sqrtPriceLimitX96Uint(160),PriceX96Limit marginal price. Swap won't go beyond it.
0minOutAmountCoins(124)Minimum amount of the output jettons to get back. If not reached, your input would be returned to you
1Cell(0) payloads_cellCell with payloads for swap result and change
1target_addressAddress(267)Target will receive the result of the swap. Could be addr_none() (*00b*) then owner_address is used
1ok_forward_amountCoins(124)Amount of GRAM to use for forward payload that would be sent with the result of the swap
1ok_forward_payloadCell(0),Maybe,PayloadPayload that would be sent with the jettons of the result of the swap
1ret_forward_amountCoins(124)Amount of GRAM to use for forward payload that would be sent with the change for the swap (if any)
1ret_forward_payloadCell(0),Maybe,PayloadPayload that would be sent with the jettons of the change of the swap (if any)
TL-B Description (TBD)

This is a preliminary tl-b - subject to change

Tlb for POOLV3_SWAP

POOLV3_SWAP#a7fb58f8 
    query_id:uint64
    owner_address:MsgAddress
    source_wallet:MsgAddress
    params_cell:^[
        amount:(VarUInteger 16)
        sqrtPriceLimitX96:uint160
        minOutAmount:(VarUInteger 16)
    ]  
    payloads_cell:^[
        target_address:MsgAddress
        ok_forward_amount:(VarUInteger 16)
        ok_forward_payload:(Maybe ^Cell)
        ret_forward_amount:(VarUInteger 16)
        ret_forward_payload:(Maybe ^Cell)
    ]  
= ContractMessages;