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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]

#[cfg(test)]
mod integration_tests;

use std::fmt::{self};

use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
    to_binary, Addr, BankMsg, Coin, CosmosMsg, CustomQuery, Deps, QuerierWrapper, StdError,
    StdResult, Uint128, WasmMsg,
};

use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
pub enum DenomError {
    #[error(transparent)]
    Std(#[from] StdError),

    #[error("invalid cw20 - did not respond to `TokenInfo` query: {err}")]
    InvalidCw20 { err: StdError },

    #[error("invalid native denom. length must be between in [3, 128], got ({len})")]
    NativeDenomLength { len: usize },

    #[error("expected alphabetic ascii character in native denomination")]
    NonAlphabeticAscii,

    #[error("invalid character ({c}) in native denom")]
    InvalidCharacter { c: char },
}

/// A denom that has been checked to point to a valid asset. This enum
/// should never be constructed literally and should always be built
/// by calling `into_checked` on an `UncheckedDenom` instance.
#[cw_serde]
pub enum CheckedDenom {
    /// A native (bank module) asset.
    Native(String),
    /// A cw20 asset.
    Cw20(Addr),
}

/// A denom that has not been checked to confirm it points to a valid
/// asset.
#[cw_serde]
pub enum UncheckedDenom {
    /// A native (bank module) asset.
    Native(String),
    /// A cw20 asset.
    Cw20(String),
}

impl UncheckedDenom {
    /// Converts an unchecked denomination into a checked one. In the
    /// case of native denominations, it is checked that the
    /// denomination is valid according to the [default SDK rules]. In
    /// the case of cw20 denominations the it is checked that the
    /// specified address is valid and that that address responds to a
    /// `TokenInfo` query without erroring and returns a valid
    /// `cw20::TokenInfoResponse`.
    ///
    /// [default SDK rules]: https://github.com/cosmos/cosmos-sdk/blob/7728516abfab950dc7a9120caad4870f1f962df5/types/coin.go#L865-L867
    pub fn into_checked(self, deps: Deps) -> Result<CheckedDenom, DenomError> {
        match self {
            Self::Native(denom) => validate_native_denom(denom),
            Self::Cw20(addr) => {
                let addr = deps.api.addr_validate(&addr)?;
                let _info: cw20::TokenInfoResponse = deps
                    .querier
                    .query_wasm_smart(addr.clone(), &cw20::Cw20QueryMsg::TokenInfo {})
                    .map_err(|err| DenomError::InvalidCw20 { err })?;
                Ok(CheckedDenom::Cw20(addr))
            }
        }
    }
}

impl CheckedDenom {
    /// Is the `CheckedDenom` this cw20?
    ///
    /// # Example
    ///
    /// ```
    /// use cosmwasm_std::{Addr, coin};
    /// use cw_denom::CheckedDenom;
    ///
    /// let cw20 = Addr::unchecked("fleesp");
    /// assert!(CheckedDenom::Cw20(Addr::unchecked("fleesp")).is_cw20(&cw20));
    /// assert!(!CheckedDenom::Native("fleesp".to_string()).is_cw20(&cw20));
    /// ```
    pub fn is_cw20(&self, cw20: &Addr) -> bool {
        match self {
            CheckedDenom::Native(_) => false,
            CheckedDenom::Cw20(a) => a == cw20,
        }
    }

    /// Is the `CheckedDenom` this native denom?
    ///
    /// # Example
    ///
    /// ```
    /// use cosmwasm_std::{Addr, coin};
    /// use cw_denom::CheckedDenom;
    ///
    /// let coin = coin(10, "floob");
    /// assert!(CheckedDenom::Native("floob".to_string()).is_native(&coin.denom));
    /// assert!(!CheckedDenom::Cw20(Addr::unchecked("floob")).is_native(&coin.denom));
    /// ```
    pub fn is_native(&self, denom: &str) -> bool {
        match self {
            CheckedDenom::Native(n) => n == denom,
            CheckedDenom::Cw20(_) => false,
        }
    }

    /// Queries WHO's balance for the denomination.
    pub fn query_balance<C: CustomQuery>(
        &self,
        querier: &QuerierWrapper<C>,
        who: &Addr,
    ) -> StdResult<Uint128> {
        match self {
            CheckedDenom::Native(denom) => Ok(querier.query_balance(who, denom)?.amount),
            CheckedDenom::Cw20(address) => {
                let balance: cw20::BalanceResponse = querier.query_wasm_smart(
                    address,
                    &cw20::Cw20QueryMsg::Balance {
                        address: who.to_string(),
                    },
                )?;
                Ok(balance.balance)
            }
        }
    }

    /// Gets a `CosmosMsg` that, when executed, will transfer AMOUNT
    /// tokens to WHO. AMOUNT being zero will cause the message
    /// execution to fail.
    pub fn get_transfer_to_message(&self, who: &Addr, amount: Uint128) -> StdResult<CosmosMsg> {
        Ok(match self {
            CheckedDenom::Native(denom) => BankMsg::Send {
                to_address: who.to_string(),
                amount: vec![Coin {
                    amount,
                    denom: denom.to_string(),
                }],
            }
            .into(),
            CheckedDenom::Cw20(address) => WasmMsg::Execute {
                contract_addr: address.to_string(),
                msg: to_binary(&cw20::Cw20ExecuteMsg::Transfer {
                    recipient: who.to_string(),
                    amount,
                })?,
                funds: vec![],
            }
            .into(),
        })
    }
}

/// Follows cosmos SDK validation logic. Specifically, the regex
/// string `[a-zA-Z][a-zA-Z0-9/:._-]{2,127}`.
///
/// <https://github.com/cosmos/cosmos-sdk/blob/7728516abfab950dc7a9120caad4870f1f962df5/types/coin.go#L865-L867>
pub fn validate_native_denom(denom: String) -> Result<CheckedDenom, DenomError> {
    if denom.len() < 3 || denom.len() > 128 {
        return Err(DenomError::NativeDenomLength { len: denom.len() });
    }
    let mut chars = denom.chars();
    // Really this means that a non utf-8 character is in here, but
    // non-ascii is also correct.
    let first = chars.next().ok_or(DenomError::NonAlphabeticAscii)?;
    if !first.is_ascii_alphabetic() {
        return Err(DenomError::NonAlphabeticAscii);
    }

    for c in chars {
        if !(c.is_ascii_alphanumeric() || c == '/' || c == ':' || c == '.' || c == '_' || c == '-')
        {
            return Err(DenomError::InvalidCharacter { c });
        }
    }

    Ok(CheckedDenom::Native(denom))
}

// Useful for returning these in response objects when updating the
// config or doing a withdrawal.
impl fmt::Display for CheckedDenom {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Native(inner) => write!(f, "{inner}"),
            Self::Cw20(inner) => write!(f, "{inner}"),
        }
    }
}

#[cfg(test)]
mod tests {
    use cosmwasm_std::{
        testing::{mock_dependencies, MockQuerier},
        to_binary, Addr, ContractResult, QuerierResult, StdError, SystemError, Uint128, WasmQuery,
    };

    use super::*;

    const CW20_ADDR: &str = "cw20";

    fn token_info_mock_querier(works: bool) -> impl Fn(&WasmQuery) -> QuerierResult {
        move |query: &WasmQuery| -> QuerierResult {
            match query {
                WasmQuery::Smart { contract_addr, .. } => {
                    if *contract_addr == CW20_ADDR {
                        if works {
                            QuerierResult::Ok(ContractResult::Ok(
                                to_binary(&cw20::TokenInfoResponse {
                                    name: "coin".to_string(),
                                    symbol: "symbol".to_string(),
                                    decimals: 6,
                                    total_supply: Uint128::new(10),
                                })
                                .unwrap(),
                            ))
                        } else {
                            QuerierResult::Err(SystemError::NoSuchContract {
                                addr: CW20_ADDR.to_string(),
                            })
                        }
                    } else {
                        unimplemented!()
                    }
                }
                _ => unimplemented!(),
            }
        }
    }

    #[test]
    fn test_into_checked_cw20_valid() {
        let mut querier = MockQuerier::default();
        querier.update_wasm(token_info_mock_querier(true));

        let mut deps = mock_dependencies();
        deps.querier = querier;

        let unchecked = UncheckedDenom::Cw20(CW20_ADDR.to_string());
        let checked = unchecked.into_checked(deps.as_ref()).unwrap();

        assert_eq!(checked, CheckedDenom::Cw20(Addr::unchecked(CW20_ADDR)))
    }

    #[test]
    fn test_into_checked_cw20_invalid() {
        let mut querier = MockQuerier::default();
        querier.update_wasm(token_info_mock_querier(false));

        let mut deps = mock_dependencies();
        deps.querier = querier;

        let unchecked = UncheckedDenom::Cw20(CW20_ADDR.to_string());
        let err = unchecked.into_checked(deps.as_ref()).unwrap_err();
        assert_eq!(
            err,
            DenomError::InvalidCw20 {
                err: StdError::GenericErr {
                    msg: format!("Querier system error: No such contract: {CW20_ADDR}",)
                }
            }
        )
    }

    #[test]
    fn test_into_checked_cw20_addr_invalid() {
        let mut querier = MockQuerier::default();
        querier.update_wasm(token_info_mock_querier(true));

        let mut deps = mock_dependencies();
        deps.querier = querier;

        let unchecked = UncheckedDenom::Cw20("HasCapitalsSoShouldNotValidate".to_string());
        let err = unchecked.into_checked(deps.as_ref()).unwrap_err();
        assert_eq!(
            err,
            DenomError::Std(StdError::GenericErr {
                msg: "Invalid input: address not normalized".to_string()
            })
        )
    }

    #[test]
    fn test_validate_native_denom_invalid() {
        let invalids = [
            // Too short.
            "ab".to_string(),
            // Too long.
            (0..129).map(|_| "a").collect::<String>(),
            // Starts with non alphabetic character.
            "1abc".to_string(),
            // Contains invalid character.
            "abc~d".to_string(),
            // Too short, also empty.
            "".to_string(),
            // Weird unicode start.
            "🥵abc".to_string(),
            // Weird unocide in non-head position.
            "ab:12🥵a".to_string(),
            // Comma is not a valid seperator.
            "ab,cd".to_string(),
        ];

        for invalid in invalids {
            assert!(validate_native_denom(invalid).is_err())
        }

        // Check that we're getting the errors we expect.
        assert_eq!(
            validate_native_denom("".to_string()),
            Err(DenomError::NativeDenomLength { len: 0 })
        );
        // Should check length before contents for better runtime.
        assert_eq!(
            validate_native_denom("1".to_string()),
            Err(DenomError::NativeDenomLength { len: 1 })
        );
        assert_eq!(
            validate_native_denom("🥵abc".to_string()),
            Err(DenomError::NonAlphabeticAscii)
        );
        // The regex that the SDK specifies works on ASCII characters
        // (not unicode classes), so this emoji has a "length" that is
        // greater than one (counted in terms of ASCII characters). As
        // such, we expect to fail on character validation and not
        // length.
        assert_eq!(
            validate_native_denom("🥵".to_string()),
            Err(DenomError::NonAlphabeticAscii)
        );
        assert_eq!(
            validate_native_denom("a🥵abc".to_string()),
            Err(DenomError::InvalidCharacter { c: '🥵' })
        );
    }

    #[test]
    fn test_validate_native_denom_valid() {
        let valids = [
            "ujuno",
            "uosmo",
            "IBC/A59A9C955F1AB8B76671B00C1A0482C64A6590352944BB5880E5122358F7E1CE",
            "wasm.juno123/channel-1/badkids",
        ];
        for valid in valids {
            validate_native_denom(valid.to_string()).unwrap();
        }
    }

    #[test]
    fn test_display() {
        let denom = CheckedDenom::Native("hello".to_string());
        assert_eq!(denom.to_string(), "hello".to_string());
        let denom = CheckedDenom::Cw20(Addr::unchecked("hello"));
        assert_eq!(denom.to_string(), "hello".to_string());
    }
}