white_whale_std/pool_network/
router.rs

1use std::fmt;
2
3use cosmwasm_schema::{cw_serde, QueryResponses};
4use cosmwasm_std::{Decimal, Uint128};
5use cw20::Cw20ReceiveMsg;
6
7use crate::pool_network::asset::AssetInfo;
8
9#[cw_serde]
10pub struct InstantiateMsg {
11    pub terraswap_factory: String,
12}
13
14#[cw_serde]
15pub enum SwapOperation {
16    TerraSwap {
17        offer_asset_info: AssetInfo,
18        ask_asset_info: AssetInfo,
19    },
20}
21
22impl SwapOperation {
23    pub fn get_target_asset_info(&self) -> AssetInfo {
24        match self {
25            SwapOperation::TerraSwap { ask_asset_info, .. } => ask_asset_info.clone(),
26        }
27    }
28}
29
30impl fmt::Display for SwapOperation {
31    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32        match self {
33            SwapOperation::TerraSwap {
34                offer_asset_info,
35                ask_asset_info,
36            } => write!(
37                f,
38                "TerraSwap {{ offer_asset_info: {offer_asset_info}, ask_asset_info: {ask_asset_info} }}"
39            ),
40        }
41    }
42}
43
44#[cw_serde]
45pub struct SwapRoute {
46    pub offer_asset_info: AssetInfo,
47    pub ask_asset_info: AssetInfo,
48    pub swap_operations: Vec<SwapOperation>,
49}
50
51// Used for all swap routes
52#[cw_serde]
53pub struct SwapRouteResponse {
54    pub offer_asset: String,
55    pub ask_asset: String,
56    pub swap_route: Vec<SwapOperation>,
57}
58
59impl fmt::Display for SwapRoute {
60    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61        write!(
62            f,
63            "SwapRoute {{ offer_asset_info: {}, ask_asset_info: {}, swap_operations: {:?} }}",
64            self.offer_asset_info, self.ask_asset_info, self.swap_operations
65        )
66    }
67}
68
69#[cw_serde]
70pub enum ExecuteMsg {
71    Receive(Cw20ReceiveMsg),
72    /// Execute multiple [SwapOperation]s, i.e. multi-hop swaps.
73    ExecuteSwapOperations {
74        operations: Vec<SwapOperation>,
75        minimum_receive: Option<Uint128>,
76        to: Option<String>,
77        max_spread: Option<Decimal>,
78    },
79    /// Swap the offer to ask token. This message can only be called internally by the router contract.
80    ExecuteSwapOperation {
81        operation: SwapOperation,
82        to: Option<String>,
83        max_spread: Option<Decimal>,
84    },
85    /// Checks if the swap amount exceeds the minimum_receive. This message can only be called
86    /// internally by the router contract.
87    AssertMinimumReceive {
88        asset_info: AssetInfo,
89        prev_balance: Uint128,
90        minimum_receive: Uint128,
91        receiver: String,
92    },
93    /// Adds swap routes to the router.
94    AddSwapRoutes {
95        swap_routes: Vec<SwapRoute>,
96    },
97    /// Removes swap routes from the router.
98    RemoveSwapRoutes {
99        swap_routes: Vec<SwapRoute>,
100    },
101}
102
103#[cw_serde]
104pub enum Cw20HookMsg {
105    ExecuteSwapOperations {
106        operations: Vec<SwapOperation>,
107        minimum_receive: Option<Uint128>,
108        to: Option<String>,
109        max_spread: Option<Decimal>,
110    },
111}
112
113#[cw_serde]
114#[derive(QueryResponses)]
115pub enum QueryMsg {
116    /// Retrieves the configuration of the router.
117    #[returns(ConfigResponse)]
118    Config {},
119    /// Simulates swap operations.
120    #[returns(SimulateSwapOperationsResponse)]
121    SimulateSwapOperations {
122        offer_amount: Uint128,
123        operations: Vec<SwapOperation>,
124    },
125    /// Simulates a reverse swap operations, i.e. given the ask asset, how much of the offer asset
126    /// is needed to perform the swap.
127    #[returns(SimulateSwapOperationsResponse)]
128    ReverseSimulateSwapOperations {
129        ask_amount: Uint128,
130        operations: Vec<SwapOperation>,
131    },
132    /// Gets the swap route for the given offer and ask assets.
133    #[returns(Vec<SwapOperation>)]
134    SwapRoute {
135        offer_asset_info: AssetInfo,
136        ask_asset_info: AssetInfo,
137    },
138    /// Gets all swap routes registered
139    #[returns(Vec<SwapRouteResponse>)]
140    SwapRoutes {},
141}
142
143// We define a custom struct for each query response
144#[cw_serde]
145pub struct ConfigResponse {
146    pub terraswap_factory: String,
147}
148
149// We define a custom struct for each query response
150#[cw_serde]
151pub struct SimulateSwapOperationsResponse {
152    pub amount: Uint128,
153}
154
155/// We currently take no arguments for migrations
156#[cw_serde]
157pub struct MigrateMsg {}