-
Notifications
You must be signed in to change notification settings - Fork 348
/
helpers.rs
105 lines (94 loc) · 3.08 KB
/
helpers.rs
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
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
to_binary, Addr, CosmosMsg, CustomQuery, QuerierWrapper, QueryRequest, StdResult, Uint128,
WasmMsg, WasmQuery,
};
use crate::{
AllowanceResponse, BalanceResponse, Cw20ExecuteMsg, Cw20QueryMsg, MinterResponse,
TokenInfoResponse,
};
/// Cw20Contract is a wrapper around Addr that provides a lot of helpers
/// for working with this.
///
/// If you wish to persist this, convert to Cw20CanonicalContract via .canonical()
#[cw_serde]
pub struct Cw20Contract(pub Addr);
impl Cw20Contract {
pub fn addr(&self) -> Addr {
self.0.clone()
}
pub fn call<T: Into<Cw20ExecuteMsg>>(&self, msg: T) -> StdResult<CosmosMsg> {
let msg = to_binary(&msg.into())?;
Ok(WasmMsg::Execute {
contract_addr: self.addr().into(),
msg,
funds: vec![],
}
.into())
}
fn encode_smart_query<CQ: CustomQuery>(
&self,
msg: Cw20QueryMsg,
) -> StdResult<QueryRequest<CQ>> {
Ok(WasmQuery::Smart {
contract_addr: self.addr().into(),
msg: to_binary(&msg)?,
}
.into())
}
/// Get token balance for the given address
pub fn balance<T, CQ>(&self, querier: &QuerierWrapper<CQ>, address: T) -> StdResult<Uint128>
where
T: Into<String>,
CQ: CustomQuery,
{
let query = self.encode_smart_query(Cw20QueryMsg::Balance {
address: address.into(),
})?;
let res: BalanceResponse = 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<CQ: CustomQuery>(
&self,
querier: &QuerierWrapper<CQ>,
) -> StdResult<TokenInfoResponse> {
let query = self.encode_smart_query(Cw20QueryMsg::TokenInfo {})?;
querier.query(&query)
}
/// Get allowance of spender to use owner's account
pub fn allowance<T, U, CQ>(
&self,
querier: &QuerierWrapper<CQ>,
owner: T,
spender: U,
) -> StdResult<AllowanceResponse>
where
T: Into<String>,
U: Into<String>,
CQ: CustomQuery,
{
let query = self.encode_smart_query(Cw20QueryMsg::Allowance {
owner: owner.into(),
spender: spender.into(),
})?;
querier.query(&query)
}
/// Find info on who can mint, and how much
pub fn minter<CQ: CustomQuery>(
&self,
querier: &QuerierWrapper<CQ>,
) -> StdResult<Option<MinterResponse>> {
let query = self.encode_smart_query(Cw20QueryMsg::Minter {})?;
querier.query(&query)
}
/// returns true if the contract supports the allowance extension
pub fn has_allowance<CQ: CustomQuery>(&self, querier: &QuerierWrapper<CQ>) -> bool {
self.allowance(querier, self.addr(), self.addr()).is_ok()
}
/// returns true if the contract supports the mintable extension
pub fn is_mintable<CQ: CustomQuery>(&self, querier: &QuerierWrapper<CQ>) -> bool {
self.minter(querier).is_ok()
}
}