1use std::fmt::Display;
3
4use cosmwasm_schema::cw_serde;
5use cosmwasm_std::{coin, coins, Addr, BankMsg, Coin, CosmosMsg, QuerierWrapper, StdResult};
6
7use crate::query::KujiraQuery;
8
9#[cw_serde]
10#[derive(Hash, Eq, PartialOrd, Ord)]
11pub struct Denom(String);
12
13impl Denom {
14 pub fn coins<T>(&self, amount: &T) -> Vec<Coin>
15 where
16 T: Clone + Into<u128>,
17 {
18 coins(amount.clone().into(), self.0.clone())
19 }
20
21 pub fn coin<T>(&self, amount: &T) -> Coin
22 where
23 T: Clone + Into<u128>,
24 {
25 coin(amount.clone().into(), self.0.clone())
26 }
27
28 pub fn send<T, M>(&self, to: &Addr, amount: &T) -> CosmosMsg<M>
29 where
30 T: Into<u128> + Clone,
31 {
32 CosmosMsg::Bank(BankMsg::Send {
33 to_address: to.to_string(),
34 amount: self.coins(amount),
35 })
36 }
37
38 pub fn query_balance(&self, q: QuerierWrapper<KujiraQuery>, addr: &Addr) -> StdResult<Coin> {
39 q.query_balance(addr.clone(), self.0.to_string())
40 }
41
42 pub fn as_bytes(&self) -> &[u8] {
43 self.0.as_bytes()
44 }
45
46 pub fn from_cw20(value: cw20::Denom) -> Self {
47 match value {
48 cw20::Denom::Native(x) => Self::from(x),
49 cw20::Denom::Cw20(_) => panic!("CW20 Unsupported"),
50 }
51 }
52}
53
54impl Display for Denom {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 self.0.fmt(f)
57 }
58}
59
60impl<T> From<T> for Denom
61where
62 T: Into<String>,
63{
64 fn from(t: T) -> Self {
65 Self(t.into())
66 }
67}
68
69impl AsRef<str> for Denom {
70 fn as_ref(&self) -> &str {
71 self.0.as_ref()
72 }
73}