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
use anchor_lang::prelude::*;

use crate::{AccountPosition, Liquidation, Valuation};

event_groups! {
    PositionEvent {
        PositionRegistered,
        PositionClosed,
        PositionBalanceUpdated,
        PositionTouched
    }
}

#[event]
pub struct AccountCreated {
    pub margin_account: Pubkey,
    pub owner: Pubkey,
    pub seed: u16,
}

#[event]
pub struct AccountClosed {
    pub margin_account: Pubkey,
}

#[event]
pub struct VerifiedHealthy {
    pub margin_account: Pubkey,
}

#[event]
pub struct PositionRegistered {
    pub margin_account: Pubkey,
    pub authority: Pubkey,
    pub position: AccountPosition,
}

#[event]
pub struct PositionClosed {
    pub margin_account: Pubkey,
    pub authority: Pubkey,
    pub token: Pubkey,
}

#[event]
pub struct PositionMetadataRefreshed {
    pub margin_account: Pubkey,
    pub position: AccountPosition,
}

#[event]
pub struct PositionBalanceUpdated {
    pub position: AccountPosition,
}

#[event]
pub struct PositionTouched {
    pub position: AccountPosition,
}

#[event]
pub struct AccountingInvokeBegin {
    pub margin_account: Pubkey,
    pub adapter_program: Pubkey,
}

#[event]
pub struct AccountingInvokeEnd {}

#[event]
pub struct AdapterInvokeBegin {
    pub margin_account: Pubkey,
    pub adapter_program: Pubkey,
}

#[event]
pub struct AdapterInvokeEnd {}

#[event]
pub struct LiquidationBegun {
    pub margin_account: Pubkey,
    pub liquidator: Pubkey,
    pub liquidation: Pubkey,
    pub liquidation_data: Liquidation,
    pub valuation_summary: ValuationSummary,
}

#[event]
pub struct LiquidatorInvokeBegin {
    pub margin_account: Pubkey,
    pub adapter_program: Pubkey,
    pub liquidator: Pubkey,
}

#[event]
pub struct LiquidatorInvokeEnd {
    pub liquidation_data: Liquidation,
    pub valuation_summary: ValuationSummary,
}

#[event]
pub struct LiquidationEnded {
    pub margin_account: Pubkey,
    pub authority: Pubkey,
    pub timed_out: bool,
}

#[event]
pub struct TransferPosition {
    pub source_margin_account: Pubkey,
    pub target_margin_account: Pubkey,
    pub source_token_account: Pubkey,
    pub target_token_account: Pubkey,
    pub amount: u64,
}

#[derive(AnchorDeserialize, AnchorSerialize)]
pub struct ValuationSummary {
    pub equity: i128,
    pub liabilities: i128,
    pub required_collateral: i128,
    pub weighted_collateral: i128,
    pub effective_collateral: i128,
    pub available_collateral: i128,
    pub past_due: bool,
}

impl From<Valuation> for ValuationSummary {
    fn from(valuation: Valuation) -> Self {
        ValuationSummary {
            equity: valuation.equity.to_i128(),
            liabilities: valuation.liabilities.to_i128(),
            required_collateral: valuation.required_collateral.to_i128(),
            weighted_collateral: valuation.weighted_collateral.to_i128(),
            effective_collateral: valuation.effective_collateral.to_i128(),
            available_collateral: valuation.available_collateral().to_i128(),
            past_due: valuation.past_due(),
        }
    }
}

/// Allows you to return a single type that could actually be any of variety of events.
/// This cannot be done with traits because Box<Dyn $Name> is not possible because
/// AnchorSerialize prevents trait objects.
macro_rules! event_groups {
    ($($Name:ident{$($Variant:ident),+$(,)?})*) => {
        $(
        #[allow(clippy::enum_variant_names)]
        pub enum $Name {
            $($Variant($Variant),)+
        }

        impl $Name {
            pub fn emit(self) {
                match self {
                    $(Self::$Variant(item) => emit!(item),)+
                }
            }
        }

        $(impl From<$Variant> for $Name {
            fn from(item: $Variant) -> Self {
                Self::$Variant(item)
            }
        })+)+
    };
}
pub(crate) use event_groups;