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
//! SNIP-20 token interface definitions from a contract.

mod interface;
pub use interface::*;

use crate::{
    core::ContractLink,
    cosmwasm_std::{
        Addr, Binary, CosmosMsg, QuerierWrapper,
        StdResult, Uint128, Coin, StdError
    },
    scrt::{vk, BLOCK_SIZE, to_cosmos_msg},
};

/// SNIP-20 token wrapper to easily call methods
/// on contracts that implement the standard.
#[derive(Clone, Debug)]
pub struct ISnip20 {
    pub link: ContractLink<Addr>,
    padding: Option<String>,
    memo: Option<String>,
    funds: Vec<Coin>,
    block_size: usize,
}

impl ISnip20 {
    #[inline]
    pub fn new(address: Addr, code_hash: String) -> Self {
        Self {
            link: ContractLink { address, code_hash },
            padding: None,
            memo: None,
            block_size: BLOCK_SIZE,
            funds: vec![]
        }
    }

    #[inline]
    pub fn memo(mut self, memo: String) -> Self {
        self.memo = Some(memo);

        self
    }

    #[inline]
    pub fn block_size(mut self, size: usize) -> Self {
        self.block_size = size;

        self
    }

    #[inline]
    pub fn padding(mut self, padding: String) -> Self {
        self.padding = Some(padding);

        self
    }

    #[inline]
    pub fn add_funds(mut self, coin: Coin) -> Self {
        self.funds.push(coin);

        self
    }

    #[inline]
    pub fn mint(
        mut self, recipient: String,
        amount: Uint128,
        decoys: Option<Vec<String>>,
        entropy: Option<Binary>
    ) -> StdResult<CosmosMsg> {
        let padding = self.padding.take();
        let memo = self.memo.take();

        self.cosmos_msg(&ExecuteMsg::Mint {
            recipient,
            amount,
            memo,
            decoys,
            entropy,
            padding
        })
    }

    #[inline]
    pub fn set_minters(mut self, minters: Vec<String>) -> StdResult<CosmosMsg> {
        let padding = self.padding.take();

        self.cosmos_msg(&ExecuteMsg::SetMinters {
            minters,
            padding
        })
    }

    #[inline]
    pub fn send(
        mut self,
        recipient: String,
        amount: Uint128,
        msg: Option<Binary>,
        recipient_code_hash: Option<String>,
        decoys: Option<Vec<String>>,
        entropy: Option<Binary>
    ) -> StdResult<CosmosMsg> {
        let padding = self.padding.take();
        let memo = self.memo.take();

        self.cosmos_msg(&ExecuteMsg::Send {
            recipient,
            recipient_code_hash,
            amount,
            msg,
            memo,
            decoys,
            entropy,
            padding
        })
    }

    #[inline]
    pub fn send_from(
        mut self,
        owner: String,
        recipient: String,
        amount: Uint128,
        msg: Option<Binary>,
        recipient_code_hash: Option<String>,
        decoys: Option<Vec<String>>,
        entropy: Option<Binary>
    ) -> StdResult<CosmosMsg> {
        let padding = self.padding.take();
        let memo = self.memo.take();

        self.cosmos_msg(&ExecuteMsg::SendFrom {
            owner,
            recipient,
            recipient_code_hash,
            amount,
            msg,
            memo,
            entropy,
            decoys,
            padding
        })
    }

    #[inline]
    pub fn register_receive(mut self, code_hash: String) -> StdResult<CosmosMsg> {
        let padding = self.padding.take();

        self.cosmos_msg(&ExecuteMsg::RegisterReceive {
            code_hash,
            padding
        })
    }

    #[inline]
    pub fn transfer(
        mut self,
        recipient: String,
        amount: Uint128,
        decoys: Option<Vec<String>>,
        entropy: Option<Binary>
    ) -> StdResult<CosmosMsg> {
        let padding = self.padding.take();
        let memo = self.memo.take();

        self.cosmos_msg(&ExecuteMsg::Transfer {
            recipient,
            amount,
            memo,
            entropy,
            decoys,
            padding
        })
    }

    #[inline]
    pub fn transfer_from(
        mut self,
        owner: String,
        recipient: String,
        amount: Uint128,
        decoys: Option<Vec<String>>,
        entropy: Option<Binary>
    ) -> StdResult<CosmosMsg> {
        let padding = self.padding.take();
        let memo = self.memo.take();

        self.cosmos_msg(&ExecuteMsg::TransferFrom {
            owner,
            recipient,
            amount,
            memo,
            entropy,
            decoys,
            padding
        })
    }

    #[inline]
    pub fn batch_transfer(
        mut self,
        actions: Vec<TransferAction>,
        entropy: Option<Binary>
    ) -> StdResult<CosmosMsg> {
        let padding = self.padding.take();

        self.cosmos_msg(&ExecuteMsg::BatchTransfer {
            actions,
            entropy,
            padding
        })
    }

    #[inline]
    #[cfg(feature = "vk")]
    pub fn set_viewing_key(mut self, key: impl Into<String>) -> StdResult<CosmosMsg> {
        let padding = self.padding.take();

        self.cosmos_msg(&vk::auth::ExecuteMsg::SetViewingKey {
            key: key.into(),
            padding
        })
    }

    #[inline]
    pub fn increase_allowance(
        mut self,
        spender: String,
        amount: Uint128,
        expiration: Option<u64>,
    ) -> StdResult<CosmosMsg> {
        let padding = self.padding.take();

        self.cosmos_msg(&ExecuteMsg::IncreaseAllowance {
            spender,
            amount,
            expiration,
            padding
        })
    }

    pub fn query_balance(
        self,
        querier: QuerierWrapper,
        address: impl Into<String>,
        key: impl Into<String>
    ) -> StdResult<Uint128> {
        let resp: QueryAnswer = querier.query_wasm_smart(
            self.link.code_hash,
            self.link.address,
            &QueryMsg::Balance {
                address: address.into(),
                key: key.into()
            }
        )?;

        match resp {
            QueryAnswer::Balance { amount } => Ok(amount),
            _ => Err(StdError::generic_err("SNIP-20: expecting Balance response."))
        }
    }

    pub fn query_token_info(self, querier: QuerierWrapper) -> StdResult<TokenInfo> {
        let resp: QueryAnswer = querier.query_wasm_smart(
            self.link.code_hash,
            self.link.address,
            &QueryMsg::TokenInfo { }
        )?;

        match resp {
            QueryAnswer::TokenInfo(info) => Ok(info),
            _ => Err(StdError::generic_err("SNIP-20: expecting TokenInfo response."))
        }
    }

    #[inline]
    fn cosmos_msg(self, msg: &impl serde::Serialize) -> StdResult<CosmosMsg> {
        to_cosmos_msg(
            self.link.address.into_string(),
            self.link.code_hash,
            msg
        )
    }
}

impl From<ContractLink<Addr>> for ISnip20 {
    #[inline]
    fn from(link: ContractLink<Addr>) -> Self {
        Self::new(link.address, link.code_hash)
    }
}