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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
pub mod context;
pub mod expiration;
pub mod queries;
pub mod rates;
pub mod response;
pub mod withdraw;

use crate::error::ContractError;
use cosmwasm_std::{
    ensure, from_binary, to_binary, BankMsg, Binary, Coin, CosmosMsg, QuerierWrapper, SubMsg,
};
use cw20::Cw20Coin;

use serde::{de::DeserializeOwned, Serialize};
use std::collections::BTreeMap;

use cosmwasm_schema::cw_serde;
#[cw_serde]
pub enum OrderBy {
    Asc,
    Desc,
}

pub fn parse_struct<T>(val: &Binary) -> Result<T, ContractError>
where
    T: DeserializeOwned,
{
    let data_res = from_binary(val);
    match data_res {
        Ok(data) => Ok(data),
        Err(err) => Err(ContractError::ParsingError {
            err: err.to_string(),
        }),
    }
}

pub fn parse_message<T: DeserializeOwned>(data: &Option<Binary>) -> Result<T, ContractError> {
    let data = unwrap_or_err(data, ContractError::MissingRequiredMessageData {})?;
    parse_struct::<T>(data)
}

pub fn encode_binary<T>(val: &T) -> Result<Binary, ContractError>
where
    T: Serialize,
{
    match to_binary(val) {
        Ok(encoded_val) => Ok(encoded_val),
        Err(err) => Err(err.into()),
    }
}

pub fn unwrap_or_err<T>(val_opt: &Option<T>, err: ContractError) -> Result<&T, ContractError> {
    match val_opt {
        Some(val) => Ok(val),
        None => Err(err),
    }
}

pub fn query_primitive<T>(
    _querier: QuerierWrapper,
    _contract_address: String,
    _key: Option<String>,
) -> Result<T, ContractError>
where
    T: DeserializeOwned,
{
    todo!()
}

#[cw_serde]
pub enum Funds {
    Native(Coin),
    Cw20(Cw20Coin),
}

impl Funds {
    // There is probably a more idiomatic way of doing this with From and Into...
    pub fn try_get_coin(&self) -> Result<Coin, ContractError> {
        match self {
            Funds::Native(coin) => Ok(coin.clone()),
            Funds::Cw20(_) => Err(ContractError::ParsingError {
                err: "Funds is not of type Native".to_string(),
            }),
        }
    }
}

/// Merges bank messages to the same recipient to a single bank message. Any sub messages
/// that do not contain bank messages are left as is. Note: Original order is not necessarily maintained.
///
/// ## Arguments
/// * `msgs`  - The sub messages to merge.
///
/// Returns a Vec<SubMsg> containing the merged bank messages.
pub fn merge_sub_msgs(msgs: Vec<SubMsg>) -> Vec<SubMsg> {
    // BTreeMap used instead of HashMap for determinant ordering in tests. Both should work
    // on-chain as hashmap randomness is fixed in cosmwasm. We get O(logn) instead of O(1)
    // performance this way which is not a huge difference.
    let mut map: BTreeMap<String, Vec<Coin>> = BTreeMap::new();

    let mut merged_msgs: Vec<SubMsg> = vec![];
    for msg in msgs.into_iter() {
        match msg.msg {
            CosmosMsg::Bank(BankMsg::Send { to_address, amount }) => {
                let current_coins = map.get_mut(&to_address);
                match current_coins {
                    Some(current_coins) => merge_coins(current_coins, amount),
                    None => {
                        map.insert(to_address.to_owned(), amount);
                    }
                }
            }
            _ => merged_msgs.push(msg),
        }
    }

    for (to_address, amount) in map.into_iter() {
        merged_msgs.push(SubMsg::new(CosmosMsg::Bank(BankMsg::Send {
            to_address,
            amount,
        })));
    }

    merged_msgs
}

/// Adds coins in `coins_to_add` to `coins` by merging those of the same denom and
/// otherwise appending.
///
/// ## Arguments
/// * `coins`        - Mutable reference to a vec of coins which will be modified in-place.
/// * `coins_to_add` - The `Vec<Coin>` to add, it is assumed that it contains no coins of the
///                    same denom
///
/// Returns nothing as it is done in place.
pub fn merge_coins(coins: &mut Vec<Coin>, coins_to_add: Vec<Coin>) {
    // Not the most efficient algorithm (O(n * m)) but we don't expect to deal with very large arrays of Coin,
    // typically at most 2 denoms. Even in the future there are not that many Terra native coins
    // where this will be a problem.
    for coin in coins.iter_mut() {
        let same_denom_coin = coins_to_add.iter().find(|&c| c.denom == coin.denom);
        if let Some(same_denom_coin) = same_denom_coin {
            coin.amount += same_denom_coin.amount;
        }
    }
    for coin_to_add in coins_to_add.iter() {
        if !coins.iter().any(|c| c.denom == coin_to_add.denom) {
            coins.push(coin_to_add.clone());
        }
    }
}

/// Deducts a given amount from a vector of `Coin` structs. Alters the given vector, does not return a new vector.
///
/// ## Arguments
/// * `coins` - The vector of `Coin` structs from which to deduct the given funds
/// * `funds` - The amount to deduct
pub fn deduct_funds(coins: &mut [Coin], funds: &Coin) -> Result<bool, ContractError> {
    let coin_amount = coins.iter_mut().find(|c| c.denom.eq(&funds.denom));

    match coin_amount {
        Some(c) => {
            ensure!(
                c.amount >= funds.amount,
                ContractError::InsufficientFunds {}
            );
            c.amount -= funds.amount;
            Ok(true)
        }
        None => Err(ContractError::InsufficientFunds {}),
    }
}

#[cfg(test)]
mod test {
    use cosmwasm_std::{coin, to_binary, Uint128, WasmMsg};
    use cw20::Expiration;

    use super::*;

    #[cw_serde]
    struct TestStruct {
        name: String,
        expiration: Expiration,
    }

    #[test]
    fn test_parse_struct() {
        let valid_json = to_binary(&TestStruct {
            name: "John Doe".to_string(),
            expiration: Expiration::AtHeight(123),
        })
        .unwrap();

        let test_struct: TestStruct = parse_struct(&valid_json).unwrap();
        assert_eq!(test_struct.name, "John Doe");
        assert_eq!(test_struct.expiration, Expiration::AtHeight(123));

        let invalid_json = to_binary("notavalidteststruct").unwrap();

        assert!(parse_struct::<TestStruct>(&invalid_json).is_err())
    }

    #[test]
    fn test_merge_coins() {
        let mut coins = vec![coin(100, "uusd"), coin(100, "uluna")];
        let funds_to_add = vec![coin(25, "uluna"), coin(50, "uusd"), coin(100, "ucad")];

        merge_coins(&mut coins, funds_to_add);
        assert_eq!(
            vec![coin(150, "uusd"), coin(125, "uluna"), coin(100, "ucad")],
            coins
        );
    }

    #[test]
    fn test_merge_sub_messages() {
        let sub_msgs: Vec<SubMsg> = vec![
            SubMsg::new(CosmosMsg::Bank(BankMsg::Send {
                to_address: "A".to_string(),
                amount: vec![coin(100, "uusd"), coin(50, "uluna")],
            })),
            SubMsg::new(CosmosMsg::Bank(BankMsg::Send {
                to_address: "A".to_string(),
                amount: vec![coin(100, "uusd"), coin(50, "ukrw")],
            })),
            SubMsg::new(CosmosMsg::Bank(BankMsg::Send {
                to_address: "B".to_string(),
                amount: vec![coin(100, "uluna")],
            })),
            SubMsg::new(CosmosMsg::Bank(BankMsg::Send {
                to_address: "B".to_string(),
                amount: vec![coin(50, "uluna")],
            })),
            SubMsg::new(CosmosMsg::Wasm(WasmMsg::Execute {
                contract_addr: "C".to_string(),
                funds: vec![],
                msg: encode_binary(&"").unwrap(),
            })),
        ];

        let merged_msgs = merge_sub_msgs(sub_msgs);
        assert_eq!(
            vec![
                SubMsg::new(CosmosMsg::Wasm(WasmMsg::Execute {
                    contract_addr: "C".to_string(),
                    funds: vec![],
                    msg: encode_binary(&"").unwrap(),
                })),
                SubMsg::new(CosmosMsg::Bank(BankMsg::Send {
                    to_address: "A".to_string(),
                    amount: vec![coin(200, "uusd"), coin(50, "uluna"), coin(50, "ukrw")],
                })),
                SubMsg::new(CosmosMsg::Bank(BankMsg::Send {
                    to_address: "B".to_string(),
                    amount: vec![coin(150, "uluna")],
                })),
            ],
            merged_msgs
        );

        assert_eq!(3, merged_msgs.len());
    }

    #[test]
    fn test_deduct_funds() {
        let mut funds: Vec<Coin> = vec![coin(100, "uluna")];

        deduct_funds(&mut funds, &coin(10, "uluna")).unwrap();

        assert_eq!(Uint128::from(90_u64), funds[0].amount);
        assert_eq!(String::from("uluna"), funds[0].denom);

        let mut funds: Vec<Coin> = vec![Coin {
            denom: String::from("uluna"),
            amount: Uint128::from(5_u64),
        }];

        let e = deduct_funds(&mut funds, &coin(10, "uluna")).unwrap_err();

        assert_eq!(ContractError::InsufficientFunds {}, e);
    }
}