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
169
170
171
172
173
use crate::state::markets::MarketEvent;
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::pubkey::Pubkey;

#[derive(Debug, Copy, Clone, BorshDeserialize, BorshSerialize)]
pub struct AuditLogHeader {
    pub instruction: u8,
    pub sequence_number: u64,
    pub timestamp: i64,
    pub slot: u64,
    pub market: Pubkey,
    pub signer: Pubkey,
    pub total_events: u16,
}

#[derive(Debug, Copy, Clone, BorshDeserialize, BorshSerialize)]
pub struct FillEvent {
    pub index: u16,
    pub maker_id: Pubkey,
    pub order_sequence_number: u64,
    pub price_in_ticks: u64,
    pub base_lots_filled: u64,
    pub base_lots_remaining: u64,
}

#[derive(Debug, Copy, Clone, BorshDeserialize, BorshSerialize)]
pub struct ReduceEvent {
    pub index: u16,
    pub order_sequence_number: u64,
    pub price_in_ticks: u64,
    pub base_lots_removed: u64,
    pub base_lots_remaining: u64,
}

#[derive(Debug, Copy, Clone, BorshDeserialize, BorshSerialize)]
pub struct PlaceEvent {
    pub index: u16,
    pub order_sequence_number: u64,
    pub client_order_id: u128,
    pub price_in_ticks: u64,
    pub base_lots_placed: u64,
}

#[derive(Debug, Copy, Clone, BorshDeserialize, BorshSerialize)]
pub struct EvictEvent {
    pub index: u16,
    pub maker_id: Pubkey,
    pub order_sequence_number: u64,
    pub price_in_ticks: u64,
    pub base_lots_evicted: u64,
}

#[derive(Debug, Copy, Clone, BorshDeserialize, BorshSerialize)]
pub struct FillSummaryEvent {
    pub index: u16,
    pub client_order_id: u128,
    pub total_base_lots_filled: u64,
    pub total_quote_lots_filled: u64,
    pub total_fee_in_quote_lots: u64,
}

#[derive(Debug, Copy, Clone, BorshDeserialize, BorshSerialize)]
pub struct FeeEvent {
    pub index: u16,
    pub fees_collected_in_quote_lots: u64,
}

#[derive(Debug, Copy, Clone, BorshDeserialize, BorshSerialize)]
pub enum PhoenixMarketEvent {
    Uninitialized,
    Header(AuditLogHeader),
    Fill(FillEvent),
    Place(PlaceEvent),
    Reduce(ReduceEvent),
    Evict(EvictEvent),
    FillSummary(FillSummaryEvent),
    Fee(FeeEvent),
}

impl Default for PhoenixMarketEvent {
    fn default() -> Self {
        Self::Uninitialized
    }
}

impl PhoenixMarketEvent {
    pub fn set_index(&mut self, i: u16) {
        match self {
            Self::Fill(FillEvent { index, .. }) => *index = i,
            Self::Place(PlaceEvent { index, .. }) => *index = i,
            Self::Reduce(ReduceEvent { index, .. }) => *index = i,
            Self::FillSummary(FillSummaryEvent { index, .. }) => *index = i,
            Self::Evict(EvictEvent { index, .. }) => *index = i,
            Self::Fee(FeeEvent { index, .. }) => *index = i,
            _ => panic!("Cannot set index on uninitialized or header event"),
        }
    }
}

impl From<MarketEvent<Pubkey>> for PhoenixMarketEvent {
    fn from(e: MarketEvent<Pubkey>) -> Self {
        match e {
            MarketEvent::<Pubkey>::Fill {
                maker_id,
                order_sequence_number,
                price_in_ticks,
                base_lots_filled,
                base_lots_remaining,
            } => Self::Fill(FillEvent {
                maker_id,
                order_sequence_number,
                price_in_ticks: price_in_ticks.into(),
                base_lots_filled: base_lots_filled.into(),
                base_lots_remaining: base_lots_remaining.into(),
                index: 0,
            }),
            MarketEvent::<Pubkey>::Place {
                order_sequence_number,
                client_order_id,
                price_in_ticks,
                base_lots_placed,
            } => Self::Place(PlaceEvent {
                order_sequence_number,
                client_order_id,
                price_in_ticks: price_in_ticks.into(),
                base_lots_placed: base_lots_placed.into(),
                index: 0,
            }),
            MarketEvent::<Pubkey>::Reduce {
                order_sequence_number,
                price_in_ticks,
                base_lots_removed,
                base_lots_remaining,
            } => Self::Reduce(ReduceEvent {
                order_sequence_number,
                price_in_ticks: price_in_ticks.into(),
                base_lots_removed: base_lots_removed.into(),
                base_lots_remaining: base_lots_remaining.into(),
                index: 0,
            }),
            MarketEvent::<Pubkey>::Evict {
                maker_id,
                order_sequence_number,
                price_in_ticks,
                base_lots_evicted,
            } => Self::Evict(EvictEvent {
                maker_id,
                order_sequence_number,
                price_in_ticks: price_in_ticks.into(),
                base_lots_evicted: base_lots_evicted.into(),
                index: 0,
            }),
            MarketEvent::<Pubkey>::FillSummary {
                client_order_id,
                total_base_lots_filled,
                total_quote_lots_filled,
                total_fee_in_quote_lots,
            } => Self::FillSummary(FillSummaryEvent {
                client_order_id,
                total_base_lots_filled: total_base_lots_filled.into(),
                total_quote_lots_filled: total_quote_lots_filled.into(),
                total_fee_in_quote_lots: total_fee_in_quote_lots.into(),
                index: 0,
            }),
            MarketEvent::<Pubkey>::Fee {
                fees_collected_in_quote_lots,
            } => Self::Fee(FeeEvent {
                fees_collected_in_quote_lots: fees_collected_in_quote_lots.into(),
                index: 0,
            }),
        }
    }
}