DexPair
This smart contract defines the logic for managing exchange, cross pair exchange and deposit/withdraw liquidity inside the pair’s pool.
Last updated
This smart contract defines the logic for managing exchange, cross pair exchange and deposit/withdraw liquidity inside the pair’s pool.
Last updated
Smart contract of the dex pair responsible for managing exchange, cross pair exchange, deposit/withdraw liquidity inside the pair’s pool.
Derives following classes and interfaces: DexContractBase, IDexPair, IAcceptTokensTransferCallback, IUpgradableByRequest, IResetGas, ITokenOperationStructure.
getRoot
Returns address of the current Dex root.
Return values:
Name | Type | Description |
---|
getTokenRoots
Returns left, right root and liquidity pool root address.
Return values:
Name | Type | Description |
---|
getTokenWallets
Returns left, right and liquidity pool wallet address.
Return values:
getVersion
Returns current version of the dex pair.
Return values:
getVault
Returns the vault of the desired dex pair.
Return values:
getVaultWallets
Returns left and right vault wallet addresses.
Return values:
setFeeParams
Sets fees based on the numerator and denominator and emits FeesParamsUpdated to notify that new fees were set.
Parameters:
getFeeParams
Returns fee numerator and denominator
Return values:
isActive
Checks whether the DexPair account is active, returns true if yes, false if not.
Return values:
getBalances
Returns liquidity pool supply and left and right balance.
Return values:
buildExchangePayload
Builds the payload data for the exchange operation and converts it to cell.
Parameters:
Return values:
buildDepositLiquidityPayload
Builds the payload data for the deposit liquidity operation and converts it to cell.
Parameters:
Return values:
buildWithdrawLiquidityPayload
Builds the payload data for the withdraw liquidity operation and converts it to cell.
Parameters:
Return values:
buildCrossPairExchangePayload
Builds the payload data for the cross-pair exchange operation and converts it to cell.
Parameters:
Return values:
onAcceptTokenTransfer
Changes values of contract’s data such as left balance, right balance, liquidity pool supply etc. according to the operation that is related to the token transfer such as: Deposit liquidity, Exchange, Cross pair exchange and according to the operations flows (right to left/left to right). Depending on the operation success callbacks are called and proper events are emitted. If any of the operations are canceled, tokens used in transfer are burned and cancel callbacks and events are called.
Parameters:
expectedDepositLiquidity
expectedDepositLiquidity
Called before depositing liquidity in order to calculate the expected deposit liquidity.
If liquidity supply is 0 create DepositLiquidityResult with left amount, right amount, max value of left and right amountl.
Else calculate it by calling _expectedDepositLiquidity
.
Parameters:
Return values:
_expectedDepositLiquidity
Does the actual calculation for expected deposit liquidity and returns deposit liquidity result. If the left and right amount for depositing are greater than 0, calculate the first deposit for the left and right token and liquidity pool reward and after that calculate current left, right balance and lp supply in order to either continue with the exchange if they are greater than 0, or create deposit results right away. When all checks are done and all values are calculated, DepositLiquidityResult is built and returned as expected.
Parameters:
Return values:
depositLiquidity
Calculates and stores data such as liquidity pool supply, left balance, right balance and if it is first deposit to the current pool calls dexPairDepositLiquiditySuccess callback. If not, according to the data retrieved calling _expectedDepositLiquidity
changes left and right balance values, calls DexAccount’s InternalPairTransfer if there is need for exchange.
When everything is successfully done, mint is started in the dex pair’s root where recipient is account owner and successCallback is called to notify about successful deposit action.
Parameters:
_withdrawLiquidityBase
Reduces right and left balance for the amount that is being withdrawn and reduces the liquidity pool supply for the total value of withdrawal, emits WithdrawLiquidity with lp amount withdrawn and left and right amount taken from the pool.
Parameters:
Return values:
expectedWithdrawLiquidity
Calculates the left and right amount to be withdrawn based on the total value that is requested to be withdrawn.
Parameters:
Return values:
withdrawLiquidity
Calculating and applying new liquidity pool balances and supply after withdrawal, transferring withdrawn amount to DexAccount. If initial required conditions are fulfilled, get left and right amount of tokens to withdraw by calling _withdrawLiquidityBase
, call dexPairWithdrawSuccess callback for account owner to notify successful withdrawal.
If left and right amount to withdraw are greater than 0 transfer them to the account by calling DexAccount’s internalPairTransfer, burn withdrawn tokens in the specified pool by calling burnTokens method from the IBurnableByRootTokenRoot
.
Parameters:
expectedExchange
Returns the expected exchange amount and exchange fee based on the amount to exchange and token used for exchange by calling the private method _expectedExchange
.
Parameters:
Return values:
expectedSpendAmount
Returns the expected spend amount and expected fee by calling the private method _expectedSpendAmount
based on receive amount and specified token root address
Parameters:
Return values:
exchange
Swap function between tokens of one liquidity pool, checks in which direction the exchange is initiated and if all the conditions are fulfilled (enough tokens, fees greater than 0, send amount ge expected amount…) balance on sender token side will decrease and balance on the receiver token side will increase.
Parameters:
_expectedExchange
Does the actual calculation for the desired exchange, calculates exchange fee, returns amount of tokens that are exchanged calculated by a special equation and fee for the given exchange token.
Parameters:
_expectedSpendAmount
Calculates the expected spending amount based on the tokens amount that will be taken from exchange, total amount of tokens in the pool of the token that will be given for exchange and total amount of tokens in the pool from which desired tokens will be taken, returns expected amount of tokens that will be given for an exchange and fee for exchange.
Parameters:
Return values:
crossPairExchange
Checks whether the exchange is happening between pairs of different liquidity pools or between tokens in the same liquidity pool, if it is between lps crossPairExchange is called and if it’s inside of one lp the DexVault’s transfer function is called. If the conditions such as sufficient balance, fees > 0 etc. are not fulfilled, operation is canceled.
Parameters:
checkPair
Calls DexAccount’s checkPairCallbeck to check weather the pair exists if not, deploys wallets for each side of the token pair and lp root if it doesn’t exist.
Parameters:
upgrade
Upgrades pair to a newer version and builds tokens, liquidity tokens, balances, fees, wallets, vault wallets with new upgraded data, calls onCodeUpgrade from new code
Parameters:
onCodeUpgrade
Private method used in Upgrade, resets storage, creates new TvmSlice instance and fills it by calling upgrade_data
, loads and decodes from slice data such as left, right root, vault address, root address, old version number, current version, send gas to address, after that checks if old version is 0
If it is, initializes fee params, adds liquidity to a new dex pair by calling DexVault’s addLiquidityToken.
If not sets pool to active, load and decodes lp supply, left, right balance, fee params, lp wallet, left and right wallet and vault’s left and right wallet
Parameters:
_configureTokenRootWallets
Using address deploys new wallet and if token root is not equal to the lp root return wallet whose owner is vault.
Parameters:
onTokenWallet
Configure wallet based on the sender’s role (left root/right root/lp root) and if non of the wallets included in the lp pool are empty set wallet to active.
Parameters:
onVaultTokenWallet
If sender is either right or left root and based on which root it is if one of those vault wallets are not configured, the wallet address from the function params will be passed as vault wallet address.
Parameters:
liquidityTokenRootDeployed
Configures token root wallets for lp root, left token root and right token root, calls DexRoot’s onPairCreated callback that notifies that the pair is created.
Parameters:
liquidityTokenRootNotDeployed
Returns gas to the deployer address, it is used by DexVault’s onLiquidityTokenNotDeployed
.
Parameters:
PairCodeUpgraded
Emitted when pair code upgrades.
FeesParamsUpdated
Emitted when numerator and denominator value gets updated.
DepositLiquidity
Emitted when liquidity is deposited.
WithdrawLiquidity
Emitted when withdrawing liquidity.
ExchangeLeftToRight
Emitted whenever left token to right token exchange occurs.
ExchangeRightToLeft
Emitted whenever right token to left token exchange occurs.
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Type | Description |
---|
Type | Description |
---|
Name | Type | Description |
---|
Type | Description |
---|
Name | Type | Description |
---|
Type | Description |
---|
Name | Type | Description |
---|
Type | Description |
---|
Name | Type | Description |
---|
Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Type | Description |
---|
Name | Type | Description |
---|
Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Type | Description |
---|
Name | Type | Description |
---|
Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
Name | Type | Description |
---|
left | address | Address of the wallet’s left token |
right | address | Address of the wallet’s right token |
lp | address | Address of the liquidity pool wallet |
version | uint32 | Current version |
dex_vault | address | Vault address of dex pair |
left | address | Vault address of the left pair’s token |
right | address | Vault address of the right pair’s token |
numerator | uint16 | Numerator value for setting fees |
denominator | uint16 | Denominator value for setting fees |
numerator | uint16 | Numerator value |
denominator | uint16 | Denominator value |
bool | DexPair account active or not |
IDexPairBalances | Pair’s balance information |
id | uint64 | Id of the operation call |
deploy_wallet_grams | uint128 | Fee for deploying wallet |
expected_amount | uint128 | Expected amount of receive token after exchange |
TvmCell | Payload data needed for exchange |
id | uint64 | Id of the operation call |
deploy_wallet_grams | uint128 | Fee for deploying wallet |
TvmCell | Payload data needed for deposit to liquidity pool |
id | uint64 | Id of the operation call |
deploy_wallet_grams | uint128 | Fee for deploying wallet |
TvmCell | Payload data needed for withdrawal from liquidity pool |
id | uint64 | Id of the operation call |
deploy_wallet_grams | uint128 | Fee for deploying wallet |
expected_amount | uint128 | Expected amount of receive token after exchange |
steps | TokenOperation[] | Next pairs for exchange |
TvmCell | Payload data needed for cross-pair exchange |
token_root | address | Token root address of the token that is transferred |
tokens_amount | uint128 | Amount of the token to be transferred |
sender_address | address | Address of the sender account |
sender_wallet | address | Sender’s wallet address |
original_gas_to | address | Address where to send gas when calling contract’s methods |
payload | TvmCell | Additional data about transaction flow |
left_amount | uint128 | Left token amount in liquidity pool |
right_amount | uint128 | Right token amount in liquidity pool |
auto_change | bool | If true automatically deposit to liquidity |
DepositLiquidityResult |
left_amount | uint128 | Left token amount in liquidity pool |
right_amount | uint128 | Right token amount in liquidity pool |
auto_change | bool | If true automatically deposit to liquidity |
DepositLiquidityResult | Deposit to liquidity pool result |
call_id | uint64 | ID of the operation call |
left_amount | uint128 | Left token amount to be deposited in the liquidity pool |
right_amount | uint128 | Right token amount to be deposited in the liquidity pool |
expected_lp_root | address | Root address of the liquidity pool used for depositing |
auto_change | bool | If true automatically deposit right and left token |
account_owner | address | Address of the account’s owner |
account_version | uint32 | Current version of the account |
send_gas_to | address | Address where to send gas when calling contract’s methods |
lp_amount | uint128 | Amount to be withdrawn from the liquidity pool |
uint128 | Amount of the left token to be withdrawn |
uint128 | Amount of the right token to be withdrawn |
lp_amount | uint128 | Amount to be withdrawn from the liquidity pool |
uint128 | Amount of the right token to be withdrawn |
expected_left_amount | uint128 | Calculated left token amount to be withdrawn from the liquidity pool |
expected_right_amount | uint128 | Calculated right token amount to be withdrawn from the liquidity pool |
call_id | uint64 | ID of the operation call |
lp_amount | uint128 | Amount to be withdrawn from the liquidity pool |
expected_lp_root | address | Root address of the liquidity pool used for withdrawal |
account_owner | address | Address of the account’s owner |
account_version | uint32 | Current version of the account |
send_gas_to | address | Address where to send gas when calling contract’s methods |
amount | uint128 | Token amount to be exchanged |
spent_token_root | address | Address of the token root which will be exchanged |
expected_amount | uint128 | Calculated exchange amount |
expected_fee | uint128 | Calculated exchange fee |
receive_amount | uint128 | Token amount to receive after exchange |
receive_token_root | address | Token root address of the receiving exchange token |
expected_amount | uint128 | Calculated amount to be spent |
expected_fee | uint128 | Calculated fee |
call_id | uint64 | ID of the operation call |
spent_amount | uint128 | Amount of the spend token to be exchanged for receive token |
spent_token_root | address | Address of the token which will be exchanged for receive token |
receive_token_root | address | Address of the token for which exchange is happening |
expected_amount | uint128 | Expected amount of the receive token |
account_owner | address | Address of the account which is the exchange initiator |
account_version | uint32 | Current version of the account |
send_gas_to | address | Address where to send gas when calling contract’s methods |
a_amount | uint128 | Token amount to be exchanged |
a_pool | uint128 | Total amount of left/right tokens |
b_pool | uint128 | Total amount of right/left tokens |
uint128 | Token amount to get after exchange |
uint128 | Exchange fee |
b_amount | uint128 | Token amount to get after exchange |
a_pool | uint128 | Total amount of left/right tokens |
b_pool | uint128 | Total amount of right/left tokens |
uint128 | Token amount to exchange |
uint128 | Exchange fee |
id | uint64 | ID of the operation call |
prev_pair_version | uint32 | Current version of the first pair of the cross exchange |
prev_pair_left_root | address | Left token root address of the first pair of the cross exchange |
prev_pair_right_root | address | Right token root address of the first pair of the cross exchange |
spent_token_root | address | Token root address which will be exchanged |
spent_amount | uint128 | Amount of the token to be exchanged |
sender_address | address | Address of the account which is the cross exchange initiator |
original_gas_to | address | Address where to send gas when calling contract’s methods |
deploy_wallet_grams | uint128 | Fee for deploying wallet |
payload | TvmCell | Additional data about next exchange in row |
account_owner | address | Address of the account which is the exchange initiator |
account_version | uint32 | Current version of the account |
code | TvmCell | Code to be set for upgraded version |
new_version | uint32 | New version value |
send_gas_to | address | Address where to send gas when calling contract’s methods |
upgrade_data | TvmCell | Cell containing all the data about certain DexAccount that should be replaced after upgrading |
token_root | address | Address of the token root |
wallet | address | Address of the token wallet |
wallet | address | Address of the vault’s wallet |
lp_root_ | address | Address of the liquidity pool |
send_gas_to | address | Address where to send gas when calling contract’s methods |
lp_root_ | address | Address of the liquidity pool |
send_gas_to | address | Address where to send gas when calling contract’s methods |
dex_root | address | Address of the pair’s root address |
left | address | Address of the pair’s left token root |
right | address | Address of the pair’s right token root |
lp | address | Address of the liquidity pool root |