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
use cosmwasm_std::{Coin, StdError, StdResult};
pub fn assert_sent_sufficient_coin(sent: &[Coin], required: Option<Coin>) -> StdResult<()> {
if let Some(required_coin) = required {
let required_amount = required_coin.amount.u128();
if required_amount > 0 {
let sent_sufficient_funds = sent.iter().any(|coin| {
coin.denom == required_coin.denom && coin.amount.u128() >= required_amount
});
return if sent_sufficient_funds {
Ok(())
} else {
Err(StdError::generic_err("Insufficient funds sent"))
};
}
}
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
use cosmwasm_std::{coin, coins, StdError};
#[test]
fn assert_sent_sufficient_coin_works() {
match assert_sent_sufficient_coin(&vec![], Some(coin(0, "token"))) {
Ok(()) => {}
Err(e) => panic!("Unexpected error: {:?}", e),
};
match assert_sent_sufficient_coin(&vec![], Some(coin(5, "token"))) {
Ok(()) => panic!("Should have raised insufficient funds error"),
Err(StdError::GenericErr { msg, .. }) => assert_eq!(msg, "Insufficient funds sent"),
Err(e) => panic!("Unexpected error: {:?}", e),
};
match assert_sent_sufficient_coin(&coins(10, "smokin"), Some(coin(5, "token"))) {
Ok(()) => panic!("Should have raised insufficient funds error"),
Err(StdError::GenericErr { msg, .. }) => assert_eq!(msg, "Insufficient funds sent"),
Err(e) => panic!("Unexpected error: {:?}", e),
};
match assert_sent_sufficient_coin(&coins(10, "token"), Some(coin(5, "token"))) {
Ok(()) => {}
Err(e) => panic!("Unexpected error: {:?}", e),
};
let sent_coins = vec![coin(2, "smokin"), coin(5, "token"), coin(1, "earth")];
match assert_sent_sufficient_coin(&sent_coins, Some(coin(5, "token"))) {
Ok(()) => {}
Err(e) => panic!("Unexpected error: {:?}", e),
};
}
}