Astroport
Search…
πŸ§ͺ
Router
Contract used to simulate and execute swaps for Astroport pools

1. Overview

The Router contract contains logic to specify a custom (multi-hop) path to swap tokens on Astroport.
For example, someone can specify paths to swap the following tokens:
  • bLUNA -> LUNA -> UST
  • ANC -> UST -> ASTRO
The contract will check whether the resulting token (in these cases, UST and ASTRO respectively) is the correct one as well as check that the amount of tokens received is equal to or higher than the minimum amount asked.
The router contract code can be found here.

2. Variables and Functions

Constants

Name
Description
MAX_SWAP_OPERATIONS
The maximum number of swaps that can happen in a multi-hop swap operation

Structs

Name
Description
Contains
InstantiateMsg
This struct holds data used to instantiate the router contract
  • astroport_factory - The address of the Astroport factory
ConfigResponse
This struct holds data returned when querying for the router contract configuration
  • astroport_factory - The address of the Astroport factory
SimulateSwapOperationsResponse
This struct holds data returned when simulating a swap
  • amount - The amount of ask tokens returned by the simulation
Config
This struct holds the config data for the router
  • astroport_factory - The address of the Astroport factory

Functions

Name
Params
Description
instantiate
(deps: DepsMut, _env: Env, _info: MessageInfo, msg: InstantiateMsg)
This function instantiates a new router contract
execute
(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> Result<Response, ContractError>
Exposes all the execute functions available in the contract
receive_cw20
(deps: DepsMut, env: Env, info: MessageInfo, cw20_msg: Cw20ReceiveMsg) -> Result<Response, ContractError>
Receives a message of type [Cw20ReceiveMsg] and processes it depending on the received template
execute_swap_operations
(deps: DepsMut, env: Env, _info: MessageInfo, sender: Addr, operations: Vec<SwapOperation>, minimum_receive: Option<Uint128>, to: Option<Addr>, max_spread: Option<Decimal>) -> Result<Response, ContractError>
Executes swap operations
assert_minimum_receive
(deps: Deps, asset_info: AssetInfo, prev_balance: Uint128, minimum_receive: Uint128, receiver: Addr)
Internal function that checks if an ask amount is equal to or above a minimum amount. AssetInfo is imported from the asset file​
query
(deps: Deps, _env: Env, msg: QueryMsg) -> Result<Binary, ContractError>
Exposes all the queries available in the contract
query_config
(deps: Deps) -> Result<ConfigResponse, ContractError>
Returns the router contract configuration
migrate
(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response>
Used for the contract migration
simulate_swap_operations
(deps: Deps, offer_amount: Uint128, operations: Vec<SwapOperation>) -> Result<SimulateSwapOperationsResponse, ContractError>
Internal function that simulates a one or multi-hop swap and returns the end result
assert_operations
(api: &dyn Api, operations: &[SwapOperation]) -> Result<(), ContractError>
Internal function that validates swap operations

3. Walkthrough

The following are examples and descriptions of core functions that can be called by anyone to specify a custom (multi-hop) path to swap tokens on Astroport.

execute_swap_operations

Any address can call the execute_swap_operations function to perform multi-hop swap operations. Hopes execute one-by-one and the last swap will return the ask (requested) token.
The execute_swap_operations function can takes in AstroSwap or NativeSwap for the operations parameter. The former performs a swap with non-native tokens via one of the Astroport pools while the latter performs native swaps using Terra's built-in market module. The function also takes in an offer and and ask info for both swaps in the operation, as well as additional parameters:
  • minimum_received - Used to guarantee that the ask amount is above a minimum amount
  • to - This is the recipient of the ask tokens
  • max_spread - The spread is calculated as the difference between the ask amount (using the constant pool price) before and after the swap operation. Once max_spread is set, it will be compared against the actual swap spread. In case the swap spread exceeds the provided max limit, the swap will fail. Note that the spread is calculated before commission deduction in order to properly represent the pool's ratio change

Swap bLUNA -> LUNA -> UST

1
{
2
"execute_swap_operations": {
3
"operations": [
4
{
5
"astro_swap": {
6
"offer_asset_info": {
7
"token": {
8
"contract_addr": "terra1kc87mu460fwkqte29rquh4hc20m54fxwtsx7gp"
9
}
10
},
11
"ask_asset_info": {
12
"native_token": {
13
"denom": "uluna"
14
}
15
}
16
}
17
},
18
{
19
"native_swap":{
20
"offer_denom":"uluna",
21
"ask_denom":"uusd"
22
}
23
}
24
],
25
"minimum_receive": "123",
26
"to": "terra...",
27
"max_spread": "0.05"
28
}
29
}
Copied!

Swap ANC -> UST -> ASTRO

1
{
2
"execute_swap_operations": {
3
"operations": [
4
{
5
"astro_swap": {
6
"offer_asset_info": {
7
"token": {
8
"contract_addr": "terra14z56l0fp2lsf86zy3hty2z47ezkhnthtr9yq76"
9
}
10
},
11
"ask_asset_info": {
12
"native_token": {
13
"denom": "uusd"
14
}
15
}
16
}
17
},
18
{
19
"astro_swap": {
20
"offer_asset_info": {
21
"native_token": {
22
"denom": "uusd"
23
}
24
},
25
"ask_asset_info": {
26
"token": {
27
"contract_addr": "terra1xj49zyqrwpv5k928jwfpfy2ha668nwdgkwlrg3"
28
}
29
}
30
}
31
}
32
],
33
"minimum_receive": "123",
34
"to": "terra...",
35
"max_spread": "0.05"
36
}
37
}
Copied!

assert_minimum_receive

Checks that the swap amount exceeds minimum_receive. This is an internal function.
1
{
2
"assert_minimum_receive": {
3
"asset_info": {
4
"token": {
5
"contract_addr": "terra..."
6
}
7
},
8
"prev_balance": "123",
9
"minimum_receive": "123",
10
"receiver": "terra..."
11
}
12
}
Copied!

config

Returns general settings for the router contract, including the astroport_factory contract address.
Query
Ex. Response
1
{
2
"config": {}
3
}
Copied!
1
{
2
"astroport_factory": "terra1fnywlw4edny3vw44x04xd67uzkdqluymgreu7g"
3
}
Copied!

simulate_swap_operations

An address can call the simulate_swap_operations query to simulate a one or multi-hop swap operation. The query takes in AstroSwap or NativeSwap for the operations parameter. The function also takes in an offer and and ask for both tokens in the operation, as well as additional parameters:
  • offer_amount - This is the number of offer assets being swapped.

Simulate bLUNA -> LUNA -> UST

Query
Ex. Response
1
{
2
"simulate_swap_operations": {
3
"offer_amount": "123",
4
"operations": [
5
{
6
"astro_swap": {
7
"offer_asset_info": {
8
"token": {
9
"contract_addr": "terra1kc87mu460fwkqte29rquh4hc20m54fxwtsx7gp"
10
}
11
},
12
"ask_asset_info": {
13
"native_token": {
14
"denom": "uluna"
15
}
16
}
17
}
18
},
19
{
20
"native_swap": {
21
"offer_denom": "uluna",
22
"ask_denom": "uusd"
23
}
24
}
25
]
26
}
27
}
Copied!
1
{
2
"amount": "8842"
3
}
Copied!

Simulate ANC -> UST -> ASTRO

Query
Ex. Response
1
{
2
"simulate_swap_operations": {
3
"offer_amount": "123",
4
"operations": [
5
{
6
"astro_swap": {
7
"offer_asset_info": {
8
"token": {
9
"contract_addr": "terra14z56l0fp2lsf86zy3hty2z47ezkhnthtr9yq76"
10
}
11
},
12
"ask_asset_info": {
13
"native_token": {
14
"denom": "uusd"
15
}
16
}
17
}
18
},
19
{
20
"astro_swap": {
21
"offer_asset_info": {
22
"native_token": {
23
"denom": "uusd"
24
}
25
},
26
"ask_asset_info": {
27
"token": {
28
"contract_addr": "terra1xj49zyqrwpv5k928jwfpfy2ha668nwdgkwlrg3"
29
}
30
}
31
}
32
}
33
]
34
}
35
}
Copied!
1
{
2
"amount": "464"
3
}
Copied!