Skip to main content

Batch actions

Veax provides a convenient interface to execute a set of sequential actions. This allows a client to call several methods in one transaction, which reduces total costs and makes swaps easier. For instance you can deposit tokens, swap them and withdraw resulting tokens.

Actions parameters, prerequisites and errors are the same as for distinct calls. See corresponding documentation sections:

execute_actions (transaction)

Executes a list of actions.

Arguments

actions: [Action]: a set of actions to execute

Where:

enum Action {
RegisterAccount,
RegisterTokens([TokenId]),
SwapExactIn(SwapAction),
SwapExactOut(SwapAction),
SwapToPrice(SwapToPriceAction),
Deposit,
Withdraw(TokenId, Amount, Callback),
OpenPosition {
tokens: (TokenId, TokenId),
fee_rate: u16,
position: PositionInit,
},
ClosePosition(U64),
WithdrawFee(U64),
}

Where:

  • Action::RegisterAccount: Request account registration; can occur at most once, as the first action in a batch
  • Action::RegisterTokens([TokenId]): Register specified tokens for the account
  • Action::SwapExactIn(SwapAction): Perform swap-in exchange of tokens
  • Action::SwapExactOut(SwapAction): Perform swap-out exchange of tokens
  • Action::SwapToPrice(SwapAction): perform swap-to-price exchange of tokens.
  • Action::Deposit: Deposit token to account; account, token and amount are passed as part of call context; can appear exactly once in batch
  • Action::Withdraw(TokenId, Amount, Callback): Withdraw specified token from account. Callback is irrelevant for NEAR. Should be null
  • Action::OpenPosition {tokens: (TokenId, TokenId), fee_rate: BasisPoints, position: PositionInit}: Opens position with specified tokens and their specified amounts
  • Action::ClosePosition(U64): Closes specified position
  • Action::WithdrawFee(U64): Withdraw fees collected on specific position. User must own the position

For swaps actions we have a structure similar to the method parameters:

struct SwapAction {
token_in: TokenId,
token_out: TokenId,
amount: Option<Amount>,
amount_limit: Amount,
}

For swaps to price actiona, we have a structure similar to the method parameters:

pub struct SwapToPriceAction {
pub token_in: TokenId,
pub token_out: TokenId,
pub amount: Option<BigUint>,
pub effective_price_limit: f64,
}

Attached NEAR

Exactly one YoctoNEAR.

Returns

The method return a result of the last withdraw action. See corresponding documentation

Prerequisites

Each action has it's own prerequisite. See methods documentation.

Errors

Each action has it's own errors. See methods documentation.

Example

$ near call --account_id=user.testnet eth.fakes.testnet ft_transfer_call --depositYocto 1 --args '{  "receiver_id": "veax-dex13.testnet",  "amount": "10000000000000000000",  "msg": "[\"Deposit\",{\"SwapExactIn\":{\"token_in\":\"eth.fakes.testnet\",\"token_out\":\"wbtc.fakes.testnet\",\"amount\":\"10000000000000000000\",\"amount_limit\":\"123949672\"}},{\"Withdraw\":[\"eth.fakes.testnet\",\"0\",null]},{\"Withdraw\":[\"wbtc.fakes.testnet\",\"0\",null]}]"}' --gas=300000000000000
Scheduling a call: eth.fakes.testnet.ft_transfer_call({  "receiver_id": "veax-dex13.testnet",  "amount": "10000000000000000000",  "msg": "[\"Deposit\",{\"SwapExactIn\":{\"token_in\":\"eth.fakes.testnet\",\"token_out\":\"wbtc.fakes.testnet\",\"amount\":\"10000000000000000000\",\"amount_limit\":\"123949672\"}},{\"Withdraw\":[\"eth.fakes.testnet\",\"0\",null]},{\"Withdraw\":[\"wbtc.fakes.testnet\",\"0\",null]}]"}) with attached 0.000000000000000000000001 NEAR
Doing account.functionCall()
Receipts: BdvGFMGLMU7xVAqa3Xjgu4aWex39g25ck723NnZRPSm5, BFgxxDJ5e3p8a2WqVrVXLBhU9tyG6X6zSsjim3fcsbmG, HJP24aYLEn3yT39sfmhVZJctDquowaPYpzSV5crjRry5
Log [eth.fakes.testnet]: Transfer 10000000000000000000 from user.testnet to veax-dex13.testnet
Receipts: 9h1z6G1jMNz9T8tTbtKpE6AJafmbAF2YaSzs8zLfLCnd, 3djYnGuG5KiYG2E54ce9ETWVFbmaLiFZEV5sWELVv2ud, 59UvWPoSGZCTrhuQPz7G2uJqdjjiPMpdpzVorpWqmQVb, wih5GAiEVrqnz6uThkh2Ph75K1huBxLRMjAFUu2VNko
Log [eth.fakes.testnet]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"deposit","data":{"user":"user.testnet","token_id":"eth.fakes.testnet","amount":"10000000000000000000","balance":"10000000000000000000"}}
Log [eth.fakes.testnet]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"swap","data":{"user":"user.testnet","tokens":["eth.fakes.testnet","wbtc.fakes.testnet"],"amounts":["10000000000000000000","145822900"],"fees":[]}}
Log [eth.fakes.testnet]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"update_pool_state","data":{"pool":["wbtc.fakes.testnet","eth.fakes.testnet"],"type":"swap","amounts_a":["0","67342590760118","19518348374","260524091","59521061766","33052449085","0","4352893500"],"amounts_b":["2414192643195739194","2276495385961329745895174","19927618210068156263738","305497859522115219548","26011781388670681413629","1219316212689614048660","0","512369952126533018258"],"sqrt_prices":[261857.8527880454,261844.7608772911,261839.11926323193,261891.48970547577,261996.26201589504,262205.93239913473,262543.0885012543,261704.33527587345],"liquidities":[0.0,4.554994660120442e19,5110667147933936.0,151113866356633.47,1.5293512308207524e16,2.4228140259257316e16,0.0,3889235505850894.5]}}
Log [eth.fakes.testnet]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"withdraw","data":{"user":"user.testnet","token_id":"wbtc.fakes.testnet","amount":"145822900","balance":"0"}}
Log [eth.fakes.testnet]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"storage_balance","data":{"user":"user.testnet","available":"10000000000000000000000","total":"19340000000000000000000"}}
Log [eth.fakes.testnet]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"storage_balance","data":{"user":"user.testnet","available":"10000000000000000000000","total":"19340000000000000000000"}}
Receipt: 9EA8cRRtXqh3z4oznyMX949UAAYHngd8jyRiaq8X5jJd
Log [eth.fakes.testnet]: Transfer 145822900 from veax-dex13.testnet to user.testnet
Receipt: 6CuFTpoQoCfXWGHWmore1TgwMgYv5TjmrtGvvpj1jyRz
Log [eth.fakes.testnet]: EVENT_JSON:{"standard":"veax","version":"1.0.0","event":"storage_balance","data":{"user":"user.testnet","available":"10000000000000000000000","total":"19340000000000000000000"}}
Transaction Id 24qsRRmQYiHSioL4QNbQBeu9PEU5m54GvbqpVWxFbuEV
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.testnet.near.org/transactions/24qsRRmQYiHSioL4QNbQBeu9PEU5m54GvbqpVWxFbuEV
'10000000000000000000'

Actions examples

The following is a list of examples of each of the action.

note

If you make a deposit in a set of bunch actions you need to make different calls:

  • If you have a deposit you should call the token contract with ft_transfer_call as a method. Also, pass DEX contract and arguments as the call parameters. See example above.
  • If you just want to execute a set of actions, you need to call execute_actions directly.

All examples below use latter form if possible to keep it simple. Also, we don't use shell escape character to escape newlines for the same reason.

RegisterAccount
near call --account_id=user.testnet veax.test.near execute_actions --args '{"actions": ["RegisterAccount"]}' --depositYocto 1
RegisterTokens
near call --account_id=user.testnet veax.test.near execute_actions --args '"actions": [
{
"RegisterTokens": [
"wrap.testnet",
"usdc.fakes.testnet"
]
}
]' --depositYocto 1
SwapExactIn
note

For all numbers we use string representation to fit big numbers in

near call --account_id=user.testnet veax.test.near execute_actions --args '"actions": [
{
"SwapExactIn": {
"token_in": "wrap.testnet",
"token_out": "usdc.fakes.testnet",
"amount": "100",
"amount_limit": "1000",
}
}
]' --depositYocto 1
SwapExactOut
note

For all numbers we use string representation to fit big numbers in

near call --account_id=user.testnet veax.test.near execute_actions --args '"actions": [
{
"SwapExactOut": {
"token_in": "wrap.testnet",
"token_out": "usdc.fakes.testnet",
"amount": "100",
"amount_limit": "1000",
}
}
]' --depositYocto 1
SwapToPrice
note

For all numbers we use string representation to fit big numbers in. Also, effective_price_limit uses Float type, which deserializes from JSON numbers.

near call --account_id=user.testnet veax.test.near execute_actions --args '"actions": [
{
"SwapExactOut": {
"token_in": "wrap.testnet",
"token_out": "usdc.fakes.testnet",
"amount": "100",
"effective_price_limit": 0.5,
}
}
]' --depositYocto 1
Deposit

This is the only action which requires attached tokens. This means we need to call token contract in order to deposit tokens.

near call --account_id=user.testnet usdc.fakes.testnet ft_transfer_call --depositYocto 1 --args '{
"receiver_id": "veax.test.near",
"amount": "10000000000000000000",
"msg": "[\"Deposit\"]"
}' --depositYocto 1
Withdraw
note

Using 0 as the amount means to withdraw all the tokens

near call --account_id=user.testnet veax.test.near execute_actions --args '"actions": [
{
"Withdraw": ["usdc.fakes.testnet", "0", null]
}
]' --depositYocto 1
OpenPosition
near call --account_id=user.testnet veax.test.near execute_actions --args '"actions": [
{
"OpenPosition": {
"tokens":["wrap.testnet","usdc.fakes.testnet"],
"fee_rate":32,
"position":{
"amount_ranges":[{
"min":"569052",
"max":"1000000"
},{
"min":"3414323607849426651",
"max":"6000024620328345984"
}],
"ticks_range":[-301174,-287311]
}
}
}
]' --depositYocto 1
ClosePosition
near call --account_id=user.testnet veax.test.near execute_actions --args '"actions": [
{
"ClosePosition": "0"
}]' --depositYocto 1
WithdrawFee
near call --account_id=user.testnet veax.test.near execute_actions --args '"actions": [
{
"WithdrawFee": "0"
}]' --depositYocto 1