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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
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, has_coins, to_binary, BankMsg, Binary, Coin, CosmosMsg, QuerierWrapper,
    SubMsg, Uint128,
};
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(&to_address);
                match current_coins {
                    Some(current_coins) => {
                        map.insert(
                            to_address.to_owned(),
                            merge_coins(current_coins.to_vec(), 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: Vec<Coin>, coins_to_add: Vec<Coin>) -> Vec<Coin> {
    let mut new_coins: Vec<Coin> = if !coins.is_empty() {
        merge_coins(vec![], coins.to_vec())
    } else {
        vec![]
    };
    // 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_to_add {
        let mut same_denom_coins = new_coins.iter_mut().filter(|c| c.denom == coin.denom);
        if let Some(same_denom_coin) = same_denom_coins.next() {
            same_denom_coin.amount += coin.amount
        } else {
            new_coins.push(coin);
        }
    }

    new_coins
}

/// Checks if the required funds can be covered by merging the provided coins.
///
/// ## Arguments
/// * `coins` - The vector of `Coin` structs representing the available coins
/// * `required` - The vector of `Coin` structs representing the required funds
///
/// Returns true if the required funds can be covered by merging the available coins, false otherwise.
pub fn has_coins_merged(coins: &[Coin], required: &[Coin]) -> bool {
    let merged_coins = merge_coins(vec![], coins.to_vec());
    let merged_required = merge_coins(vec![], required.to_vec());

    for required_funds in merged_required {
        if !has_coins(&merged_coins, &required_funds) {
            return false;
        };
    }

    true
}

/// 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<(), ContractError> {
    let coin_amount: Vec<&mut Coin> = coins
        .iter_mut()
        .filter(|c| c.denom.eq(&funds.denom))
        .collect();

    let mut remainder = funds.amount;
    for same_coin in coin_amount {
        if same_coin.amount > remainder {
            same_coin.amount = same_coin.amount.checked_sub(remainder)?;
            return Ok(());
        } else {
            remainder = remainder.checked_sub(same_coin.amount)?;
            same_coin.amount = Uint128::zero();
        }
    }

    ensure!(
        remainder == Uint128::zero(),
        ContractError::InsufficientFunds {}
    );

    Ok(())
}

#[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 coins = vec![coin(100, "uusd"), coin(100, "uluna")];
        let funds_to_add = vec![
            coin(25, "uluna"),
            coin(50, "uusd"),
            coin(100, "ucad"),
            coin(50, "uluna"),
            coin(100, "uluna"),
            coin(100, "ucad"),
        ];

        let res = merge_coins(coins, funds_to_add);
        assert_eq!(
            vec![coin(150, "uusd"), coin(275, "uluna"), coin(200, "ucad")],
            res
        );
    }

    #[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(5, "uluna"), coin(100, "uusd"), coin(100, "uluna")];

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

        assert_eq!(Uint128::zero(), funds[0].amount);
        assert_eq!(String::from("uluna"), funds[0].denom);
        assert_eq!(Uint128::from(95u128), funds[2].amount);
        assert_eq!(String::from("uluna"), funds[2].denom);

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

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

        assert_eq!(ContractError::InsufficientFunds {}, e);
    }
    #[test]
    fn test_has_coins_merged() {
        let available_coins: Vec<Coin> = vec![
            coin(50, "uluna"),
            coin(200, "uusd"),
            coin(50, "ukrw"),
            coin(25, "uluna"),
            coin(25, "uluna"),
        ];
        let required_funds: Vec<Coin> = vec![
            coin(50, "uluna"),
            coin(100, "uusd"),
            coin(50, "ukrw"),
            coin(50, "uluna"),
        ];

        assert!(has_coins_merged(&available_coins, &required_funds));

        let insufficient_funds: Vec<Coin> =
            vec![coin(10, "uluna"), coin(100, "uusd"), coin(50, "ukrw")];

        assert!(!has_coins_merged(&insufficient_funds, &required_funds));
    }
}