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
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{
    to_binary, CosmosMsg, HumanAddr, Querier, QuerierWrapper, StdResult, Uint128, WasmMsg,
    WasmQuery,
};

use crate::{
    AllowanceResponse, BalanceResponse, Cw20HandleMsg, Cw20QueryMsg, MinterResponse,
    TokenInfoResponse,
};

/// Cw20Contract is a wrapper around HumanAddr that provides a lot of helpers
/// for working with this.
///
/// If you wish to persist this, convert to Cw20CanonicalContract via .canonical()
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Cw20Contract(pub HumanAddr);

impl Cw20Contract {
    pub fn addr(&self) -> HumanAddr {
        self.0.clone()
    }

    pub fn call<T: Into<Cw20HandleMsg>>(&self, msg: T) -> StdResult<CosmosMsg> {
        let msg = to_binary(&msg.into())?;
        Ok(WasmMsg::Execute {
            contract_addr: self.addr(),
            msg,
            send: vec![],
        }
        .into())
    }

    /// Get token balance for the given address
    pub fn balance<Q: Querier>(&self, querier: &Q, address: HumanAddr) -> StdResult<Uint128> {
        let msg = Cw20QueryMsg::Balance { address };
        let query = WasmQuery::Smart {
            contract_addr: self.addr(),
            msg: to_binary(&msg)?,
        }
        .into();
        let res: BalanceResponse = QuerierWrapper::new(querier).query(&query)?;
        Ok(res.balance)
    }

    /// Get metadata from the contract. This is a good check that the address
    /// is a valid Cw20 contract.
    pub fn meta<Q: Querier>(&self, querier: &Q) -> StdResult<TokenInfoResponse> {
        let msg = Cw20QueryMsg::TokenInfo {};
        let query = WasmQuery::Smart {
            contract_addr: self.addr(),
            msg: to_binary(&msg)?,
        }
        .into();
        QuerierWrapper::new(querier).query(&query)
    }

    /// Get allowance of spender to use owner's account
    pub fn allowance<Q: Querier>(
        &self,
        querier: &Q,
        owner: HumanAddr,
        spender: HumanAddr,
    ) -> StdResult<AllowanceResponse> {
        let msg = Cw20QueryMsg::Allowance { owner, spender };
        let query = WasmQuery::Smart {
            contract_addr: self.addr(),
            msg: to_binary(&msg)?,
        }
        .into();
        QuerierWrapper::new(querier).query(&query)
    }

    /// Find info on who can mint, and how much
    pub fn minter<Q: Querier>(&self, querier: &Q) -> StdResult<Option<MinterResponse>> {
        let msg = Cw20QueryMsg::Minter {};
        let query = WasmQuery::Smart {
            contract_addr: self.addr(),
            msg: to_binary(&msg)?,
        }
        .into();
        QuerierWrapper::new(querier).query(&query)
    }

    /// returns true if the contract supports the allowance extension
    pub fn has_allowance<Q: Querier>(&self, querier: &Q) -> bool {
        self.allowance(querier, self.addr(), self.addr()).is_ok()
    }

    /// returns true if the contract supports the mintable extension
    pub fn is_mintable<Q: Querier>(&self, querier: &Q) -> bool {
        self.minter(querier).is_ok()
    }
}