levana_perpswap_cosmos/contracts/factory/
events.rs

1//! Events emitted by the factory contract
2use crate::prelude::*;
3use cosmwasm_std::{Addr, Event};
4
5/// Event when the factory instantiates a new contract.
6#[derive(Debug)]
7pub struct InstantiateEvent {
8    /// Kind of contract instantiated
9    pub kind: NewContractKind,
10    /// Market ID associated with new contract
11    pub market_id: MarketId,
12    /// Address of the contract
13    pub addr: Addr,
14}
15
16/// The type of a newly instantiate contract
17#[derive(Debug, Clone, Copy)]
18pub enum NewContractKind {
19    /// The market
20    Market,
21    /// LP liquidity token proxy
22    Lp,
23    /// xLP liquidity token proxy
24    Xlp,
25    /// Position token NFT proxy
26    Position,
27}
28
29impl NewContractKind {
30    fn as_str(self) -> &'static str {
31        match self {
32            NewContractKind::Market => "market",
33            NewContractKind::Lp => "lp",
34            NewContractKind::Xlp => "xlp",
35            NewContractKind::Position => "position",
36        }
37    }
38}
39
40impl FromStr for NewContractKind {
41    type Err = anyhow::Error;
42
43    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
44        match s {
45            "market" => Ok(NewContractKind::Market),
46            "lp" => Ok(NewContractKind::Lp),
47            "xlp" => Ok(NewContractKind::Xlp),
48            "position" => Ok(NewContractKind::Position),
49            _ => Err(anyhow::anyhow!("Unknown contract kind: {s:?}")),
50        }
51    }
52}
53
54impl From<InstantiateEvent> for Event {
55    fn from(
56        InstantiateEvent {
57            kind,
58            market_id,
59            addr,
60        }: InstantiateEvent,
61    ) -> Self {
62        Event::new("instantiate")
63            .add_attribute("kind", kind.as_str())
64            .add_attribute("market-id", market_id.to_string())
65            .add_attribute("addr", addr)
66    }
67}
68
69impl TryFrom<Event> for InstantiateEvent {
70    type Error = anyhow::Error;
71
72    fn try_from(evt: Event) -> anyhow::Result<Self> {
73        Ok(InstantiateEvent {
74            kind: evt.string_attr("kind")?.parse()?,
75            market_id: evt.string_attr("market-id")?.parse()?,
76            addr: evt.unchecked_addr_attr("addr")?,
77        })
78    }
79}