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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use anchor_lang::prelude::*;
use enumflags2::bitflags;
use static_assertions as sa;

#[account]
pub struct GlobalConfigAccount {
    pub dflow_admin: Pubkey,
}

#[account]
pub struct RecoveryStateAccount {
    pub mint: Pubkey,
}

pub enum AuctionStateAccountEvent {
    Initialized = 0,
    NewBestBid = 1,
    NewAuctionWinner = 2,
    NextOrderIdIncremented = 3,
    AuctionStateChanged = 4,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AuctionStatus {
    Trading = 0,
    Halted = 1,
    Expired = 2,
    Draining = 3,
}

#[derive(Debug, Clone, Copy)]
pub enum Side {
    Ask = -1,
    Bid = 1
}

#[derive(AnchorSerialize, AnchorDeserialize, Debug, Clone, Eq, PartialEq)]
pub struct CurrencyPair {
    pub base: Pubkey,
    pub base_oracle: Pubkey,
    pub quote: Pubkey,
    pub quote_oracle: Pubkey,
}
sa::const_assert_eq!(std::mem::size_of::<CurrencyPair>(), 128);

#[derive(AnchorSerialize, AnchorDeserialize, Debug, Clone, Copy)]
pub struct MantissaExp {
    pub mantissa: i64,
    pub exponent: i32,
}

#[account]
pub struct AuctionMapper {
    pub auction_ids: Vec<u64>,
}

#[account]
pub struct AuctionStateAccount {
    pub event: u8,
    pub status: u8,
    pub supported_pairs_count: u8,
    pub auction_winner: Option<Pubkey>,
    pub auction_leader: Option<Pubkey>,
    pub bid_mint: Pubkey,
    pub vote_reward_mint: Pubkey,
    pub auction_owner: Pubkey,
    pub vote_size: u64,
    pub auction_id: u64,
    // The epoch currently accepting bids in an ongoing auction
    // The epoch that new orders are submitted to is this `epoch - 1`
    pub epoch: u64,
    pub min_notional_order_size: u64,
    pub max_notional_order_size: u64,
    pub notional_decimals: u8,
    pub best_bid: u64,
    pub winning_bid: u64,
    pub batch_notional_size: u64,
    pub next_order_id: u64,
    pub vote_period: u32,
    pub claim_period: u32,
    pub num_open_epochs: u32,
    pub pairs: Vec<CurrencyPair>,
}
sa::const_assert_eq!(std::mem::size_of::<AuctionStateAccount>(), 280);

#[account]
pub struct AuctionEpochState {
    pub routed_notional: u64,
    pub bid_size: u64,
    pub epoch: u64,
    pub winner: Option<Pubkey>,
    pub num_open_orders: u16,
    pub num_unsettled_fills: u16,
    pub owner: Pubkey,
}

#[derive(AnchorSerialize, AnchorDeserialize, Debug, Copy, Clone, Default)]
pub struct OrderDetails {
    pub y_mint: Pubkey,
    pub min_fill_price: u64,
}

#[account]
#[derive(Default)]
pub struct FillRecordAccount {
    // Return lamports for creating this account to this key
    pub market_maker_account_owner: Pubkey,
    pub retail_account_owner: Pubkey,
    // This is the order associated with this FillRecordAccount
    pub vault_meta_account: Pubkey,
    pub fill_x_amount: u64,
    pub fill_notional: u64,
    pub order_details: OrderDetails,
    pub auction_id: u64,
    pub fill_timestamp: u64,
    pub retail_nonce: u64,
    pub fill_nonce: u16,
    pub fair_votes: u32,
    pub unfair_votes: u32,
}

#[account]
pub struct MarketMakerDataAccount {
    pub owner: Pubkey,
    pub encryption_pub_key: [u8; 32],
    pub max_orders_supported: u64,
    pub events_head: u8,
    pub events: Vec<MarketMakerEvent>,
    pub orders: Vec<Pubkey>,
}

pub enum MarketMakerEventKind {
    None = 0,
    OrderPlaced = 1,
    OrderCancelled = 2,
    OrderPartiallyFilled = 3,
    OrderFilled = 4,
}

pub const MARKET_MAKER_EVENT_DATA_LENGTH: usize = 128;

#[derive(AnchorSerialize, AnchorDeserialize, Debug, Copy, Clone)]
pub struct MarketMakerEvent {
    pub event: u8,
    pub data: [u8; MARKET_MAKER_EVENT_DATA_LENGTH],
}

pub enum RetailDataAccountEvent {
    Initialized = 0,
    NewOrder = 1,
    OrderCancelled = 2,
    OrderFilled = 3,
    OrderPartiallyFilled = 4,
    AccountClosed = 5,
}

#[account]
pub struct RetailDataAccount {
    pub retail_account_owner: Pubkey,
    pub event: u8,
    pub nonce: u64,
    pub num_unsettled_fills: u64,
    pub max_orders_supported: u64,
    pub orders: Vec<Pubkey>,
}

#[account]
pub struct VaultMetaAccount {
    pub retail_encryption_pub_key: [u8; 32],
    pub mm_encryption_pub_key: [u8; 32],
    pub retail_signing_pub_key: [u8; 32],
    pub retail_signature: [u8; 64],
    pub vault_token_account: Pubkey,
    pub rebate_receiver_token_account: Pubkey,
    pub market_maker_data_account: Pubkey,
    pub retail_data_account: Pubkey,
    pub retail_account_owner: Pubkey,
    pub retail_x_token_account: Pubkey,
    pub x_mint: Pubkey,
    pub deposit_notional: u64,
    pub deposit_amount: u64,
    pub filled_x_amount: u64,
    pub filled_y_amount: u64,
    pub auction_id: u64,
    pub auction_epoch: u64,
    pub nonce: u64,
    pub order_id: u64,
    pub order_type: u8,
    pub fill_nonce: u16,
    pub basket_prices: Vec<MantissaExp>,
    pub encrypted_order_details: String,
}

#[account]
pub struct VoteRecordAccount {
    pub vote: u8,
}

#[account]
pub struct BidRecordAccount {
    pub bid_size: u64,
}

#[account]
pub struct SignatoryRecord {
    pub signatory_server: Pubkey,
    pub stake_size: u64,
}

#[account]
#[derive(Default)]
pub struct SignatoryState {
    pub stake_mint: Pubkey,
}

#[bitflags]
#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(u16)]
pub enum Role {
    AuctionOwner = 1 << 0,
    Arbiter = 1 << 1,
    SignatoryServer = 1 << 2,
    MarketMaker = 1 << 3,
}

#[account]
pub struct WhitelistEntry {
    pub principal: Pubkey,
    pub role_flags: u16,
}