levana_perpswap_cosmos/contracts/position_token/
events.rs

1//! Events emitted by the position token contract
2use crate::prelude::*;
3use cosmwasm_std::{Addr, Event};
4use cw_utils::Expiration;
5
6/// New NFT was minted
7#[derive(Debug)]
8pub struct MintEvent {
9    /// Position token ID
10    pub token_id: String,
11    /// Owner of the position
12    pub owner: Addr,
13}
14
15impl From<MintEvent> for Event {
16    fn from(src: MintEvent) -> Self {
17        Event::new("mint").add_attributes(vec![
18            ("token_id", src.token_id.to_string()),
19            ("owner", src.owner.to_string()),
20        ])
21    }
22}
23
24impl TryFrom<Event> for MintEvent {
25    type Error = anyhow::Error;
26
27    fn try_from(evt: Event) -> anyhow::Result<Self> {
28        Ok(MintEvent {
29            token_id: evt.string_attr("token_id")?,
30            owner: evt.unchecked_addr_attr("owner")?,
31        })
32    }
33}
34
35/// NFT was burned, aka a position was closed
36#[derive(Debug)]
37pub struct BurnEvent {
38    /// Position that was closed
39    pub token_id: String,
40}
41
42impl From<BurnEvent> for Event {
43    fn from(src: BurnEvent) -> Self {
44        Event::new("burn").add_attributes(vec![("token_id", src.token_id)])
45    }
46}
47
48impl TryFrom<Event> for BurnEvent {
49    type Error = anyhow::Error;
50
51    fn try_from(evt: Event) -> anyhow::Result<Self> {
52        Ok(BurnEvent {
53            token_id: evt.string_attr("token_id")?,
54        })
55    }
56}
57
58// converting expiration back into an event is painful
59// so these are just unidirectional for now
60
61/// Approval was granted
62#[derive(Debug)]
63pub struct ApprovalEvent {
64    /// Position
65    pub token_id: String,
66    /// Who can spend it
67    pub spender: Addr,
68    /// When it expires
69    pub expires: Expiration,
70}
71
72impl From<ApprovalEvent> for Event {
73    fn from(src: ApprovalEvent) -> Self {
74        Event::new("approval").add_attributes(vec![
75            ("token_id", src.token_id.to_string()),
76            ("spender", src.spender.to_string()),
77            ("expires", src.expires.to_string()),
78        ])
79    }
80}
81
82/// Approval was revoked
83#[derive(Debug)]
84pub struct RevokeEvent {
85    /// Position ID
86    pub token_id: String,
87    /// Whose spend permissions were revoked
88    pub spender: Addr,
89}
90
91impl From<RevokeEvent> for Event {
92    fn from(src: RevokeEvent) -> Self {
93        Event::new("revoke").add_attributes(vec![
94            ("token_id", src.token_id.to_string()),
95            ("spender", src.spender.to_string()),
96        ])
97    }
98}
99
100/// An operator was granted spend permissions on all positions for a wallet
101#[derive(Debug)]
102pub struct ApproveAllEvent {
103    /// Who is the operator
104    pub operator: Addr,
105    /// When does the permission expire
106    pub expires: Expiration,
107}
108
109impl From<ApproveAllEvent> for Event {
110    fn from(src: ApproveAllEvent) -> Self {
111        Event::new("approve-all").add_attributes(vec![
112            ("operator", src.operator.to_string()),
113            ("expires", src.expires.to_string()),
114        ])
115    }
116}
117
118/// Revoke all permissions for an operator
119#[derive(Debug)]
120pub struct RevokeAllEvent {
121    /// Operator to revoke
122    pub operator: Addr,
123}
124
125impl From<RevokeAllEvent> for Event {
126    fn from(src: RevokeAllEvent) -> Self {
127        Event::new("revoke-all").add_attributes(vec![("operator", src.operator.to_string())])
128    }
129}
130
131/// NFT was transferred
132#[derive(Debug)]
133pub struct TransferEvent {
134    /// New owner
135    pub recipient: Addr,
136    /// Position ID
137    pub token_id: String,
138}
139
140impl From<TransferEvent> for Event {
141    fn from(src: TransferEvent) -> Self {
142        Event::new("transfer").add_attributes(vec![
143            ("recipient", src.recipient.to_string()),
144            ("token_id", src.token_id),
145        ])
146    }
147}