komple_framework_utils/
funds.rs

1use cosmwasm_std::{Coin, DepsMut, MessageInfo, StdError, Uint128};
2use cw20::{Cw20QueryMsg, TokenInfoResponse};
3use komple_framework_types::modules::fee::FundInfo;
4use thiserror::Error;
5
6pub fn check_single_amount(info: &MessageInfo, amount: Uint128) -> Result<(), FundsError> {
7    if info.funds.len() != 1 {
8        return Err(FundsError::MissingFunds {});
9    };
10    let sent_fund = info.funds.get(0).unwrap();
11    if sent_fund.amount != amount {
12        return Err(FundsError::InvalidFunds {
13            got: sent_fund.amount.to_string(),
14            expected: amount.to_string(),
15        });
16    }
17    Ok(())
18}
19
20pub fn check_single_coin(info: &MessageInfo, expected: Coin) -> Result<(), FundsError> {
21    if info.funds.len() != 1 {
22        return Err(FundsError::MissingFunds {});
23    };
24    let sent_fund = info.funds.get(0).unwrap();
25    if sent_fund.denom != expected.denom {
26        return Err(FundsError::InvalidDenom {
27            got: sent_fund.denom.to_string(),
28            expected: expected.denom,
29        });
30    }
31    if sent_fund.amount != expected.amount {
32        return Err(FundsError::InvalidFunds {
33            got: sent_fund.amount.to_string(),
34            expected: expected.amount.to_string(),
35        });
36    }
37    Ok(())
38}
39
40/// Check c20 fund information in `FundInfo`.
41pub fn check_cw20_fund_info(deps: &DepsMut, fund_info: &FundInfo) -> Result<(), FundsError> {
42    if fund_info.cw20_address.is_none() {
43        return Err(FundsError::InvalidCw20Token {});
44    };
45    let res: TokenInfoResponse = deps.querier.query_wasm_smart(
46        fund_info.cw20_address.as_ref().unwrap(),
47        &Cw20QueryMsg::TokenInfo {},
48    )?;
49    if fund_info.denom != res.symbol {
50        return Err(FundsError::InvalidCw20Token {});
51    };
52    Ok(())
53}
54
55#[derive(Error, Debug, PartialEq)]
56pub enum FundsError {
57    #[error("{0}")]
58    Std(#[from] StdError),
59
60    #[error("Invalid denom! Got: {got} - Expected: {expected}")]
61    InvalidDenom { got: String, expected: String },
62
63    #[error("Invalid funds! Got: {got} - Expected: {expected}")]
64    InvalidFunds { got: String, expected: String },
65
66    #[error("No funds found!")]
67    MissingFunds {},
68
69    #[error("Invalid cw20 token")]
70    InvalidCw20Token {},
71}