levana_perpswap_cosmos/contracts/cw20/
events.rs

1use cosmwasm_std::{Addr, Event, Uint128};
2use cw_utils::Expiration;
3
4use super::entry::{EmbeddedLogo, Logo};
5
6#[derive(Debug)]
7pub struct TransferEvent {
8    pub owner: Addr,
9    pub recipient: Addr,
10    pub amount: Uint128,
11    pub by: Option<Addr>,
12}
13
14impl From<TransferEvent> for Event {
15    fn from(src: TransferEvent) -> Self {
16        let event = Event::new("transfer").add_attributes(vec![
17            ("recipient", src.recipient.to_string()),
18            ("amount", src.amount.to_string()),
19            ("owner", src.owner.to_string()),
20        ]);
21
22        if let Some(by) = src.by {
23            event.add_attribute("by", by.to_string())
24        } else {
25            event
26        }
27    }
28}
29
30#[derive(Debug)]
31pub struct BurnEvent {
32    pub owner: Addr,
33    pub amount: Uint128,
34    pub by: Option<Addr>,
35}
36
37impl From<BurnEvent> for Event {
38    fn from(src: BurnEvent) -> Self {
39        let event = Event::new("burn").add_attributes(vec![
40            ("amount", src.amount.to_string()),
41            ("owner", src.owner.to_string()),
42        ]);
43
44        if let Some(by) = src.by {
45            event.add_attribute("by", by.to_string())
46        } else {
47            event
48        }
49    }
50}
51
52#[derive(Debug)]
53pub struct SendEvent {
54    pub owner: Addr,
55    pub contract: Addr,
56    pub amount: Uint128,
57    pub by: Option<Addr>,
58}
59
60impl From<SendEvent> for Event {
61    fn from(src: SendEvent) -> Self {
62        let event = Event::new("send").add_attributes(vec![
63            ("contract", src.contract.to_string()),
64            ("amount", src.amount.to_string()),
65            ("owner", src.owner.to_string()),
66        ]);
67
68        if let Some(by) = src.by {
69            event.add_attribute("by", by.to_string())
70        } else {
71            event
72        }
73    }
74}
75
76#[derive(Debug)]
77pub struct MintEvent {
78    pub owner: Addr,
79    pub recipient: Addr,
80    pub amount: Uint128,
81}
82
83impl From<MintEvent> for Event {
84    fn from(src: MintEvent) -> Self {
85        Event::new("mint").add_attributes(vec![
86            ("owner", src.owner.to_string()),
87            ("amount", src.amount.to_string()),
88            ("recipient", src.recipient.to_string()),
89        ])
90    }
91}
92
93#[derive(Debug)]
94pub struct AllowanceChangeEvent {
95    pub kind: AllowanceChangeKind,
96    pub owner: Addr,
97    pub spender: Addr,
98    pub amount: Uint128,
99    pub expires: Option<Expiration>,
100}
101
102#[derive(Debug)]
103pub enum AllowanceChangeKind {
104    Increase,
105    Decrease,
106}
107
108impl AllowanceChangeKind {
109    pub fn as_str(&self) -> &'static str {
110        match self {
111            Self::Increase => "increase",
112            Self::Decrease => "decrease",
113        }
114    }
115}
116
117impl From<AllowanceChangeEvent> for Event {
118    fn from(src: AllowanceChangeEvent) -> Self {
119        let event = Event::new("allowance-change").add_attributes(vec![
120            ("kind", src.kind.as_str().to_string()),
121            ("owner", src.owner.to_string()),
122            ("spender", src.spender.to_string()),
123            ("amount", src.amount.to_string()),
124        ]);
125
126        match src.expires {
127            Some(expires) => event.add_attribute("expires", expires.to_string()),
128            None => event,
129        }
130    }
131}
132
133#[derive(Debug)]
134pub struct MinterChangeEvent {
135    pub minter: Addr,
136}
137
138impl From<MinterChangeEvent> for Event {
139    fn from(src: MinterChangeEvent) -> Self {
140        Event::new("minter-change").add_attribute("minter", src.minter.into_string())
141    }
142}
143
144#[derive(Debug)]
145pub struct MarketingChangeEvent {
146    pub project: Option<String>,
147    pub description: Option<String>,
148    pub marketing: Option<String>,
149}
150
151impl From<MarketingChangeEvent> for Event {
152    fn from(src: MarketingChangeEvent) -> Self {
153        let event = Event::new("marketing-change");
154
155        let event = match src.project {
156            Some(x) => event.add_attribute("project", x),
157            None => event,
158        };
159
160        let event = match src.description {
161            Some(x) => event.add_attribute("description", x),
162            None => event,
163        };
164
165        match src.marketing {
166            Some(x) => event.add_attribute("marketing", x),
167            None => event,
168        }
169    }
170}
171
172#[derive(Debug)]
173pub struct LogoChangeEvent<'a> {
174    pub logo: &'a Logo,
175}
176
177impl<'a> From<LogoChangeEvent<'a>> for Event {
178    fn from(src: LogoChangeEvent) -> Self {
179        let event = Event::new("logo-change");
180
181        match src.logo {
182            Logo::Url(url) => event.add_attribute("kind", "url").add_attribute("url", url),
183            Logo::Embedded(embedded) => event.add_attribute("kind", "embedded").add_attribute(
184                "embedded-kind",
185                match embedded {
186                    EmbeddedLogo::Svg(_) => "svg",
187                    EmbeddedLogo::Png(_) => "png",
188                },
189            ),
190        }
191    }
192}