Trading
This document describes various swap and liquidity management functions available on the Veax decentralized exchange platform. Each function has its specific arguments, prerequisites, and possible errors. Examples are provided to illustrate how to use these functions with the NEAR CLI.
swap_exact_in
(transaction)
Execute a swap, or a chain of swaps, given the exact amount of tokens to be paid, with a limit on the minimal amount of tokens to be received.
Swaps are performed in the sequence defined by tokens
if the first token is swapped into the second, then the resulting amount of second token is swapped into the third and so on.
Thus, tokens
must contain at least two items, where the first one is the token paid by the trader, and the last—is the one trader receives.
If the resulting amount of tokens is less than min_amount_out
, the transaction is aborted.
A detailed description of swap logic is given in section 4.
Arguments
tokens
:[AccountId]
: array of two or more tokens defining the swap or swap chain.amount_in
:U128
: amount of tokens to be paid.min_amount_out
:U128
: minimal required amount of tokens to receive.
Attached NEAR
None.
Returns:
AmountInOut {
amount_in: U128,
amount_out: U128
}
Where:
AmountInOut.amount_in
: the amount of tokens paid by the trader (equal to theamount_in
argument).AmountInOut.amount_out
: the amount of tokens received by the trader.
Prerequisites
- The caller is registered on Veax.
- The caller has at least
amount_in
of tokens to be paid. - Pools exist and contain sufficient liquidity.
- Pool states are such that at least
min_amout_out
of output tokens can be obtained as a result of the specified swaps. - At least two tokens passed to swap method.
Errors
AccountNotRegistered
: user is not registered on Veax.TokenNotRegistered
: a token is not registered for the current user.Slippage
min amount out
is higher that resulting amount.InsufficientLiquidity
insufficient liquidity in the pool to swapamount_in
tokens.AtLeastOneSwap
: there is less than two tokens passed.TokenDuplicates
: tokens to swap duplicate.NotEnoughTokens
: not enough tokens to swap in.SwapAmountTooLarge
: resulting swap amount is too large to fit intoAmount
type.SwapAmountTooSmall
: resulting swap amount is too small.DepositWouldOverflow
: resulting deposit amount is too bing to fit intoAmount
type.
Example
near call --account_id trader1.test.near veax.test.near swap_exact_in --args '
{
"tokens": ["wnear_demo.test.near", "usdt_demo.test.near"],
"amount_in": "4500",
"min_amount_out": "2800"
}'
Scheduling a call: veax.test.near.swap_exact_in(
{
"tokens": ["wnear_demo.test.near", "usdt_demo.test.near"],
"amount_in": "4500",
"min_amount_out": "2800"
}) with attached 0.000000000000000000000001 NEAR
Loaded master account test.near key from /home/user/.neartosis/validator-key.json with public key = ed25519:3Kuyi2DUXdoHgoaNEvCxa1m6G8xqc6Xs7WGajaqLhNmW
Doing account.functionCall()
Receipt: 6YFEx99xP3UJGZHuREZRTfqGaTtbjHsvVQ6y9AHMiSAe
Log [veax.test.near]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"storage_balance","data":{"user":"trader1.test.near","available":"4994660000000000000000000","total":"5000000000000000000000000"}}
Log [veax.test.near]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"swap","data":{"user":"trader1.test.near","tokens":["wnear_demo.test.near","usdt_demo.test.near"],"amounts":["4500","2870"],"fees":[]}}
Transaction Id 879cTyo1T1oKVC6zQQsKNV5CABJWs52ouwZ4k1v1tswY
To see the transaction in the transaction explorer, please open this url in your browser
http://127.0.0.1:8331/transactions/879cTyo1T1oKVC6zQQsKNV5CABJWs52ouwZ4k1v1tswY
{ amount_in: '4500', amount_out: '2870' }
swap_to_price
(transaction)
Execute a swap, given the exact number of tokens to be exchanged, but not for a price greater than price_limit
.
Thus tokens
must contain exactly two items, where the first one is the token paid by the trader, and the last is the one trader receives.
If the current pool price is greater than price_limit
, the transaction is aborted.
Arguments
token_ids
:[AccountId]
: tokens to swap.amount_in
:U128
: maximum number of tokens to be exchangedprice_limit
:f64
: maximum price to exchange the tokens at
Returns:
AmountInOut {
amount_in: U128,
amount_out: U128
}
Where:
AmountInOut.amount_in
: the number of tokens paid by the trader. Equals toamount_in
if price limit hasn't been reachedAmountInOut.amount_out
: the number of tokens received by the trader.
Prerequisites
- Pools exist and contain sufficient liquidity.
- A trader has enough tokens to swap in.
- Exactly two tokens are passed to the swap method.
price_limit
is greater than the current pool price
Errors
InsufficientLiquidity
: insufficient liquidity in the pool to swapamount_out
tokens.ExactlytOneSwap
: tokens len is less of more than two.TokenDuplicates
: tokens to swap duplicates.NotEnoughTokens
: not enough tokens to swap in.SwapAmountTooLarge
: resulting swap amount is too large to fit into theAmount
type.SwapAmountTooSmall
: resulting swap amount is too small.DepositWouldOverflow
: resulting deposit amount is too big to fit into theAmount
type.
Example
near call --account_id trader1.test.near veax.test.near swap_to_price --args '
{
"tokens": ["wnear_demo.test.near", "usdt_demo.test.near"],
"amount_in": "4500",
"price_limit": 5.0
}'
Scheduling a call: veax.test.near.swap_to_price(
{
"tokens": ["wnear_demo.test.near", "usdt_demo.test.near"],
"amount_in": "4500",
"price_limit": 5.0
}) with attached 0.000000000000000000000001 NEAR
Loaded master account test.near key from /home/user/.neartosis/validator-key.json with public key = ed25519:3Kuyi2DUXdoHgoaNEvCxa1m6G8xqc6Xs7WGajaqLhNmW
Doing account.functionCall()
Receipt: 6YFEx99xP3UJGZHuREZRTfqGaTtbjHsvVQ6y9AHMiSAe
Log [veax.test.near]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"storage_balance","data":{"user":"trader1.test.near","available":"4994660000000000000000000","total":"5000000000000000000000000"}}
Log [veax.test.near]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"swap","data":{"user":"trader1.test.near","tokens":["wnear_demo.test.near","usdt_demo.test.near"],"amounts":["4500","2870"],"fees":[]}}
Transaction Id 879cTyo1T1oKVC6zQQsKNV5CABJWs52ouwZ4k1v1tswY
To see the transaction in the transaction explorer, please open this url in your browser
http://127.0.0.1:8331/transactions/879cTyo1T1oKVC6zQQsKNV5CABJWs52ouwZ4k1v1tswY
{ amount_in: '4500', amount_out: '2870' }
swap_exact_out
(transaction)
Execute a swap, or a chain of swaps, given the exact amount of tokens to be received, with a limit on the maximal amount of tokens to be paid.
Swaps are performed in the sequence defined by tokens
. The first token is swapped into the second, and then the resulting amount of second token is swapped into the third, and so on.
Thus tokens
must contain at least two items, where the first one is the token paid by the trader, and the last is the one trader receives.
If the resulting amount of tokens is less than min_amount_out
, the transaction is aborted.
A detailed description of swap logic is given in section 4.
Arguments
tokens
:[AccountId]
: the array of two or more tokens defining the swap or swap chain.amount_out
:U128
: the amount of tokens to be received.max_amount_in
:U128
: maximal allowed amount of tokens to be paid.
Attached NEAR
None.
Returns
AmountInOut {
amount_in: U128,
amount_out: U128
}
Where:
AmountInOut.amount_in
: the amount of tokens paid by the trader.AmountInOut.amount_out
: the amount of tokens received by the trader (equal toamount_out
argument).
Prerequisites
- The caller is registered on Veax.
- Pools exist and contain sufficient liquidity.
- Trader has enough tokens to swap in.
- Pool states are such that at most
max_amout_in
need to be paid in order to perform the specified swap (or swap chain). - At least two tokens passed to swap method.
Errors
AccountNotRegistered
: user is not registered on Veax.TokenNotRegistered
: a token is not registered for the current user.Slippage
max_amount_in
: is less that resulting amount.InsufficientLiquidity
: insufficient liquidity in the pool to swapamount_out
tokens.AtLeastOneSwap
: there is less than two tokens passed.TokenDuplicates
: tokens to swap duplicate.NotEnoughTokens
: not enough tokens to swap in.SwapAmountTooLarge
: resulting swap amount is too large to fit intoAmount
type.SwapAmountTooSmall
: resulting swap amount is too small.DepositWouldOverflow
: resulting deposit amount is too bing to fit intoAmount
type.
Example
$ near call --account_id trader1.test.near veax.test.near swap_exact_out --depositYocto 1 --args '
{
"tokens": ["usdt_demo.test.near", "wnear_demo.test.near"],
"amount_out": "4500",
"max_amount_in": "3000"
}'
Scheduling a call: veax.test.near.swap_exact_out(
{
"tokens": ["usdt_demo.test.near", "wnear_demo.test.near"],
"amount_out": "4500",
"max_amount_in": "3000"
}) with attached 0.000000000000000000000001 NEAR
Loaded master account test.near key from /home/user/.neartosis/validator-key.json with public key = ed25519:3Kuyi2DUXdoHgoaNEvCxa1m6G8xqc6Xs7WGajaqLhNmW
Doing account.functionCall()
Receipt: DBXJMoc3SqGfeDD2BqySmgcxZ4ESHmezbRtA24fVnoYR
Log [veax.test.near]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"storage_balance","data":{"user":"trader1.test.near","available":"4994660000000000000000000","total":"5000000000000000000000000"}}
Log [veax.test.near]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"swap","data":{"user":"trader1.test.near","tokens":["usdt_demo.test.near","wnear_demo.test.near"],"amounts":["2935","4500"],"fees":[]}}
Transaction Id AAD6nFeADwfdsgk844zFz8WojRjkzprReHWJ8raTb6G
To see the transaction in the transaction explorer, please open this url in your browser
http://127.0.0.1:8331/transactions/AAD6nFeADwfdsgk844zFz8WojRjkzprReHWJ8raTb6G
{ amount_in: '2935', amount_out: '4500' }
open_position
(transaction)
Deposit liquidity into the pool.
A detailed description of liquidity provider's role on the DEX is given in section 4.
Arguments
token_a
:AccountId
: the first token to be deposited.token_b
:AccountId
: the second token to be deposited.fee_rate
:u16
: the selected fee rate in Basis Points.position
:PositionInit
: the position kind and ranges of deposited amounts in the following structure:PositionInit {
amount_ranges: [
{
min: U128,
max: U128
},
{
min: U128,
max: U128
}
]
ticks_range: [i32, i32]
}
Where:
PositionInit.amount_ranges[0].min
: lower limit on the amount oftoken_a
to deposit.PositionInit.amount_ranges[0].max
: upper limit on the amount oftoken_a
to deposit.PositionInit.amount_ranges[1].min
: lower limit on the amount oftoken_b
to deposit.PositionInit.amount_ranges[1].max
: upper limit on the amount oftoken_b
to deposit.
Attached NEAR
Exactly one YoctoNEAR.
Returns
(position_id: U64, accepted_amount_a: U128, accepted_amount_b: U128, accounted_liquidity: f64)
Where:
position_id
: assigned position ID.accepted_amount_a
: amount oftoken_a
accepted to create the position.accepted_amount_b
: amount oftoken_b
accepted to create the position.accounted_liquidity
: accounted amount of liquidity.
Prerequisites
- The caller is registered on Veax.
- The caller has at least
PositionInit.amount_ranges[0].max
oftoken_a
. - The caller has at least
PositionInit.amount_ranges[1].max
oftoken_b
.
Errors
AccountNotRegistered
: a caller is not registered on Veax.NotEnoughTokens
: Account doesn't have enough tokens to create the position.InvalidParams
: invalid fee rate, or position range.TokenDuplicates
:token_a
andtoken_b
are the same tokens.WrongRatio
: token ranges are inconsistent with the current spot price.LiquidityTooSmall
: Resulting liquidity is too small.LiquidityTooBig
: resulting liquidity is too big.PriceTickOutOfBounds
: tick value is either too large or too small.PositionAlreadyExists
: position with given tokens already exists.Requires attached deposit of exactly 1 yoctoNEAR
: attached NEAR amount differs from one YoctoNEAR.
Example
$ near call --account_id liquidity_provider1.test.near veax.test.near open_position --deposit 1 --args '
{
"token_a": "usdt_demo.test.near",
"token_b": "wnear_demo.test.near",
"fee_rate": 16,
"position": {
"amount_ranges": {
"left": {
"min": "0",
"max": "500000"
},
"right": {
"min": "800000",
"max": "800000"
}
},
"ticks_range": [-100, 100]
}
}'
Scheduling a call: veax.test.near.open_position(
{
"token_a": "usdt_demo.test.near",
"token_b": "wnear_demo.test.near",
"fee_rate": 16,
"position": {
"amount_ranges": {
"left": {
"min": "0",
"max": "500000"
},
"right": {
"min": "800000",
"max": "800000"
}
},
"ticks_range": [-100, 100]
}
}) with attached 1 NEAR
Loaded master account test.near key from /home/user/.neartosis/validator-key.json with public key = ed25519:3Kuyi2DUXdoHgoaNEvCxa1m6G8xqc6Xs7WGajaqLhNmW
Doing account.functionCall()
Receipts: BzKaautmGFn4Rc4WsdgHNRYuqb1FoH9a4mGfTivLMf1s, KXnD2Asf1aP23ywNyJnNvNCFaHXixabJpHHsNyXRBBn
Log [veax.test.near]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"storage_balance","data":{"user":"liquidity_provider1.test.near","available":"4994660000000000000000000","total":"5000000000000000000000000"}}
Log [veax.test.near]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"open_position","data":{"user":"liquidity_provider1.test.near","pool":["wnear_demo.test.near","usdt_demo.test.near"],"amounts":["800000","500000"],"fee_rate":"16","position_id":"0"}}
Transaction Id 9PR7DqzmGjXR8Fn4AN9ipKxikKxXa8uqNuL371TJmXrG
To see the transaction in the transaction explorer, please open this url in your browser
http://127.0.0.1:8331/transactions/9PR7DqzmGjXR8Fn4AN9ipKxikKxXa8uqNuL371TJmXrG
[ '0', '500000', '800000', 632455.5320336758]
close_position
(transaction)
Withdraw liquidity from the pool along with the fee earned by the position onto the caller's account on Veax.
A detailed description of liquidity provider's role on dex is given in section 4.
Arguments
position_id
: U64
: position to close.
Attached NEAR
Exactly one YoctoNEAR.
Returns
Nothing.
Prerequisites
A position exists and belongs to the caller.
Errors
AccountNotRegistered
: a caller is not registered on Veax.NotYourPosition
: a position with the given ID doesn't belong to the user.PositionDoesNotExist
: no position with the given ID.Requires attached deposit of exactly 1 yoctoNEAR
: attached NEAR amount differs from one YoctoNEAR.
Example
$ near call --account_id liquidity_provider1.test.near veax.test.near close_position --depositYocto 1 --args '
{
"position_id": "0"
}'
Scheduling a call: veax.test.near.close_position(
{
"position_id": "0"
}) with attached 0.000000000000000000000001 NEAR
Loaded master account test.near key from /home/user/.neartosis/validator-key.json with public key = ed25519:3Kuyi2DUXdoHgoaNEvCxa1m6G8xqc6Xs7WGajaqLhNmW
Doing account.functionCall()
Receipt: FS5Vs71x66fubbdiKYEAYNSscNbPr25ZcW6x3zxTCyZk
Log [veax.test.near]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"storage_balance","data":{"user":"liquidity_provider1.test.near","available":"4994660000000000000000000","total":"5000000000000000000000000"}}
Log [veax.test.near]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"close_position","data":{"position_id":"0"}}
Log [veax.test.near]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"storage_balance","data":{"user":"liquidity_provider1.test.near","available":"4997440000000000000000000","total":"5002780000000000000000000"}}
Transaction Id D1WMS4vJtZwTNwaRStPvkCsa3pi3AmFy9mwGvPTy6gbv
To see the transaction in the transaction explorer, please open this url in your browser
http://127.0.0.1:8331/transactions/D1WMS4vJtZwTNwaRStPvkCsa3pi3AmFy9mwGvPTy6gbv
''
withdraw_fee
(transaction)
Withdraw the fee earned by the position onto the caller's account on Veax.
A detailed description of liquidity provider's role on dex is given in section 4.
Arguments
position_id
: U64
: position to withdraw the fee from.
Attached NEAR
Exactly one YoctoNEAR.
Returns
Pair<U128> {
left: U128,
right: U128
}
Where:
Pair<U128>.left
: fee amount of the first token in the position.Pair<U128>.right
: fee amount of the second token in the position.
Prerequisites
A position exists and belongs to the caller.
Errors
AccountNotRegistered
:: a caller is not registered on Veax.NotYourPosition
: a position with the given ID doesn't belong to the user.PositionDoesNotExist
: no position with the given ID.Requires attached deposit of exactly 1 yoctoNEAR
: attached NEAR amount differs from one YoctoNEAR.
Example
near call --account_id liquidity_provider1.test.near veax.test.near withdraw_fee --depositYocto 1 --args '
{
"position_id": "0"
}'
Scheduling a call: veax.test.near.withdraw_fee(
{
"position_id": "0"
}) with attached 0.000000000000000000000001 NEAR
Loaded master account test.near key from /home/user/.neartosis/validator-key.json with public key = ed25519:3Kuyi2DUXdoHgoaNEvCxa1m6G8xqc6Xs7WGajaqLhNmW
Doing account.functionCall()
Receipt: Cm1KJsm3zZ3r68BKmcQn2dLXRXqjj1cceCXvD8mb6uai
Log [veax.test.near]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"storage_balance","data":{"user":"liquidity_provider1.test.near","available":"4994660000000000000000000","total":"5000000000000000000000000"}}
Transaction Id DSC9UrkmeBJaGuooPTBVvanauSNmcssYFe7vXpmPjXvF
To see the transaction in the transaction explorer, please open this url in your browser
http://127.0.0.1:8331/transactions/DSC9UrkmeBJaGuooPTBVvanauSNmcssYFe7vXpmPjXvF
{ left: '0', right: '16' }
get_position_info
(query)
Get position status.
Arguments
position_id
: U64
: position to get info for
Returns
PositionInfo {
tokens_ids: (AccountId, AccountId),
balance: (U128, U128),
range_ticks: (Option<i32>, Option<i32>),
reward_since_last_withdraw: (U128, U128),
reward_since_creation: (U128, U128),
init_sqrt_price: f64,
}
Where:
PositionInfo.token_ids
: tokens pair of the position.PositionInfo.balance
: token amounts in the position (rounded).PositionInfo.range_ticks
: ticks range the position was create for.PositionInfo.reward_since_last_withdraw
: accumulated fees since the last withdraw call.PositionInfo.reward_since_creation
: accumulated fees since position creation.PositionInfo.init_sqrt_price
: squared price of the pair when created.
Prerequisites
Position exists.
Errors
PositionDoesNotExist
: no position with the given ID.
Example
$ near view veax.test.near get_position_info --args '{"position_id": "0"}'
View call: veax.test.near.get_position_info({"position_id": "0"})
Loaded master account test.near key from /home/user/.neartosis/validator-key.json with public key = ed25519:3Kuyi2DUXdoHgoaNEvCxa1m6G8xqc6Xs7WGajaqLhNmW
{
tokens_ids: [ 'wnear_demo.test.near', 'usdt_demo.test.near'],
balance: [ '780009', '512814'],
liquidity_share: '1',
range_ticks: [-30000, 30000],
reward_since_last_withdraw: [ '16', '12'],
reward_since_creation: [ '178', '122'],
init_sqrt_price: '1.2345'
}
Estimations
DX25 allows users to estimate results of certain transactions: swaps and pool liquidity addition. The methods has the same arguments as the corresponding transactions, except they have slippage tolerance parameters.
Slippage tolerance allows users to set a fraction by which current spot price may deviate in unfavorable for the trader direction. The tolerance can have zero value, which means current spot price is the only acceptable price for a client.
Let's review some of the examples for swap estimations. We'll review swap scenarios for a pool with 1000
of each tokens, and try to swap 100
of a single token (result values are properly encoded JSON's for convenience). Note, in all three cases result
and swap_price
are the same. Those are values, which are amount and price a user gets with the current spot price.
Also, in the examples we use full range single position pool for clarity. With multiple positions and concentrated liquidity results would depend on the current pool state, and worst value won't change in a linear matter unlike we see in the examples.
0
basis points (0%)
result
and result_bound
; swap_price
and swap_price_worst
are the same because of the zero slippage tolerance.
{
"result": "90",
"result_bound": "90",
"price_impact": {
"nominator": "7199274220329340",
"denominator": "72057594037927936"
},
"swap_price": {
"nominator": "5003999585967218",
"denominator": "4503599627370496"
},
"swap_price_worst": {
"nominator": "5003999585967218",
"denominator": "4503599627370496"
},
"fee_in_spent_tok": "0",
"num_tick_crossings": 0
}
100
basis points (1%)
Both worst resulting amounts and prices differ for about 1%.
{
"result": "90",
"result_bound": "89",
"price_impact": {
"nominator": "7199274220329340",
"denominator": "72057594037927936"
},
"swap_price": {
"nominator": "5003999585967218",
"denominator": "4503599627370496"
},
"swap_price_worst": {
"nominator": "5054545036330523",
"denominator": "4503599627370496"
},
"fee_in_spent_tok": "0",
"num_tick_crossings": 0
}
5000
basis points (50%)
As you'd expect, both worst resulting amounts and prices differ for about 50%.
{
"result": "90",
"result_bound": "45",
"price_impact": {
"nominator": "7199274220329340",
"denominator": "72057594037927936"
},
"swap_price": {
"nominator": "5003999585967218",
"denominator": "4503599627370496"
},
"swap_price_worst": {
"nominator": "5003999585967218",
"denominator": "2251799813685248"
},
"fee_in_spent_tok": "0",
"num_tick_crossings": 0
}
estimate_swap_exact
(query)
Estimate a swap, or a chain of swaps, in the given moment of time, given the exact number of tokens to be paid (in case of swap in) or received (in case of swap out), with a limit given by a slippage basis point value.
Swaps are performed in the sequence defined by tokens
if the first token is swapped into the second, then the resulting amount of the second token is swapped into the third, and so on.
Arguments
is_exact_in
:bool
: if swap in (true
) or out.token_in
:TokenId
: token to swap in.token_out
:TokenId
: token to swap out.amount
:U128
: paid amount for exact-in swap, receved amount for exact-out swap.slippage_tolerance_bp
:u16
slippage tolerance in basis point units (1 basis point = 0.01%).
Attached NEAR
Exactly one YoctoNEAR.
Returns
struct EstimateSwapExactResult {
result: U128,
result_bound: U128,
price_impact: Fraction,
swap_price: Option<Fraction>,
swap_price_worst: Option<Fraction>,
fee_in_spent_tok: U128,
num_tick_crossings: u32,
}
Where:
EstimateSwapExactResult.result
: resulting amount, i.e. amount_out for exact-in swap or amount_in for exact-out swap, assuming no price slippage.EstimateSwapExactResult.result_bound
: resulting amount, i.e. amount_out for exact-in swap, or amount_in for exact-out swap, assmuing maximal allowed price slippage in the unfavorable for the trader direction.EstimateSwapExactResult.price_impact
: price impact assuming no price slippage (as a common fraction).EstimateSwapExactResult.swap_price
: swap price, i.e. amount_in / amount_out, assuming no price slippage (as a common fraction).EstimateSwapExactResult.swap_price_worst
: swap price, i.e. amount_in / amount_out, assuming maximal allowed price slippage in the unfavorable for the trader direction (as a common fraction).EstimateSwapExactResult.fee_in_spent_tok
: total fee amount, i.e. the sum of LP reward and dex fee on all levels, assuming no price slippage.EstimateSwapExactResult.num_tick_crossings
: number of crossed active ticks, assuming no price slippage (correlates with the gas consumption).
Prerequisites
Pool exists.
Errors
PoolNotRegistered
: pool is not registered.
Example
Run
$ near call --account_id trader1.test.near veax.test.near estimate_swap_exact --depositYocto 1 --args '
{
"is_exact_in": true,
"max_amount_in": "100",
"token_in": {
"contract": {
"index": 4517,
"subindex": 0
},
"id": ""
},
"token_out": {
"contract": {
"index": 4514,
"subindex": 0
},
"id": ""
},
"amount": "1000",
"slippage_tolerance_bp": 1000
}
View call: veax.test.near.get_position_info({
"is_exact_in": true,
"max_amount_in": "100",
"token_in": {
"contract": {
"index": 4517,
"subindex": 0
},
"id": ""
},
"token_out": {
"contract": {
"index": 4514,
"subindex": 0
},
"id": ""
},
"amount": "1000",
"slippage_tolerance_bp": 1000
})
Loaded master account test.near key from /home/user/.neartosis/validator-key.json with public key = ed25519:3Kuyi2DUXdoHgoaNEvCxa1m6G8xqc6Xs7WGajaqLhNmW
{
result: '543',
result_bound: '620',
price_impact: {
nominator: '7',
denominator: '3',
},
swap_price: {
nominator: '234',
denominator: '10',
},
swap_price_worst: {
nominator: '234',
denominator: '11',
},
fee_in_spent_tok: '31',
num_tick_crossings: 32
}
estimate_liquidity_add
(query)
Estimate adding liquidity to a pool.
Arguments
tokens
:(TokenId, TokenId)
: pool tokens pair.fee_rate
:u16
: fee rate in units of1/FEE_DIVISOR
.ticks_range
:(Option<i32>, Option<i32>)
: position price range, in ticks.amount_a
:U128
: amount of token A to be deposited.amount_b
:U128
: amount of token B to be deposited.user_price
:Option<Fraction>
: spot price to set, if pool did not exist (as a common fraction).slippage_tolerance_bp
:u16
slippage tolerance in basis point units (1 basis point = 0.01%).
Attached NEAR
Exactly one YoctoNEAR.
Returns
struct EstimateAddLiquidityResult {
min_a: U128,
max_a: U128,
min_b: U128,
max_b: U128,
pool_exists: bool,
spot_price: Option<Fraction>,
position_price: Fraction,
position_net_liquidity: Fraction,
tx_cost: TxCostEstimate,
}
struct TxCostEstimate {
gas_cost_max: U128,
storage_fee_max: U128,
}
Where:
EstimateAddLiquidityResult.min_a
,EstimateAddLiquidityResult.max_a
: minimum and maximum amount of the first token.EstimateAddLiquidityResult.min_b
,EstimateAddLiquidityResult.max_b
: minimum and maximum amount of the second token.EstimateAddLiquidityResult.pool_exists
: if the given pool exists.EstimateAddLiquidityResult.spot_price
: spot price if the pool exists (as a common fraction).EstimateAddLiquidityResult.position_price
: created position price (as a common fraction).EstimateAddLiquidityResult.position_net_liquidity
: position net liquidity (as a common fraction).EstimateAddLiquidityResult.tx_cost
: TX gas cost if pool exists.
Prerequisites
Given fee exists, at least one token amount is scpecified, and if user_price
is set, pool doen't exist.
Errors
IllegalFee
: Illegal fee specified.
SwapAmountTooSmall
: Both prices are None
PoolNotRegistered
: user_price
is set, but token pair pool already exists
Example
Run
$ near call --account_id trader1.test.near veax.test.near estimate_swap_exact --depositYocto 1 --args '
{
"tokens": [
{
"contract": {
"index": 4517,
"subindex": 0
},
"id": ""
},
{
"contract": {
"index": 4514,
"subindex": 0
},
"id": ""
}
],
"fee_rate": 1,
"ticks_range": [
"",
""
],
"amount_a": "1000",
"amount_a": "1000",
"user_price": "",
"slippage_tolerance_bp": 5000
}
View call: veax.test.near.get_position_info({
"tokens": [
{
"contract": {
"index": 4517,
"subindex": 0
},
"id": ""
},
{
"contract": {
"index": 4514,
"subindex": 0
},
"id": ""
}
],
"fee_rate": 1,
"ticks_range": [
"",
""
],
"amount_a": "1000",
"amount_a": "1000",
"user_price": "",
"slippage_tolerance_bp": 5000
})
Loaded master account test.near key from /home/user/.neartosis/validator-key.json with public key = ed25519:3Kuyi2DUXdoHgoaNEvCxa1m6G8xqc6Xs7WGajaqLhNmW
{
min_a: '132072',
max_a: '131573',
min_b: '131572',
max_b: '132072',
pool_exists: false,
spot_price: '',
position_price:
{
nominator: '508906757892866050',
denominator: '508906757892866048'
},
position_net_liquidity: {
nominator: '513192217578824028',
denominator: '434597364041252864'
},
tx_cost: {
gas_cost_max: '0',
storage_fee_max: '0',
}
}