1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{Coin, CosmosMsg, HumanAddr};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
/// TerraMsgWrapper is an override of CosmosMsg::Custom to show this works and can be extended in the contract
pub struct TerraMsgWrapper {
    pub route: String,
    pub msg_data: TerraMsg,
}

// this is a helper to be able to return these as CosmosMsg easier
impl Into<CosmosMsg<TerraMsgWrapper>> for TerraMsgWrapper {
    fn into(self) -> CosmosMsg<TerraMsgWrapper> {
        CosmosMsg::Custom(self)
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum TerraMsg {
    Swap {
        trader: HumanAddr,
        offer_coin: Coin,
        ask_denom: String,
    },
    SwapSend {
        from_address: HumanAddr,
        to_address: HumanAddr,
        offer_coin: Coin,
        ask_denom: String,
    },
}

// create_swap_msg returns wrapped swap msg
pub fn create_swap_msg(
    trader: HumanAddr,
    offer_coin: Coin,
    ask_denom: String,
) -> CosmosMsg<TerraMsgWrapper> {
    TerraMsgWrapper {
        route: "market".to_string(),
        msg_data: TerraMsg::Swap {
            trader,
            offer_coin,
            ask_denom,
        },
    }
    .into()
}

// create_swap_send_msg returns wrapped swap send msg
pub fn create_swap_send_msg(
    from_address: HumanAddr,
    to_address: HumanAddr,
    offer_coin: Coin,
    ask_denom: String,
) -> CosmosMsg<TerraMsgWrapper> {
    TerraMsgWrapper {
        route: "market".to_string(),
        msg_data: TerraMsg::SwapSend {
            from_address,
            to_address,
            offer_coin,
            ask_denom,
        },
    }
    .into()
}