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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//! Phoenix is a limit order book exchange on the Solana blockchain.
//!
//! It exposes a set of instructions to create, cancel, and fill orders.
//! Each event that modifies the state of the book is recorded in an event log which can
//! be queried from a transaction signature after each transaction is confirmed. This
//! allows clients to build their own order book and trade history.
//!
//! The program is able to atomically match orders and settle trades on chain. This
//! is because each market has a fixed set of users that are allowed to place limit
//! orders on the book. Users who swap against the book will have their funds settle
//! instantaneously, while the funds of users who place orders on the book will be
//! immediately available for withdraw post fill.
//!

#[macro_use]
mod log;
pub mod program;
pub mod quantities;
// Note this mod is private and only exists for the purposes of IDL generation
mod shank_structs;
pub mod state;

use crate::program::processor::*;

use borsh::BorshSerialize;
// You need to import Pubkey prior to using the declare_id macro
use ellipsis_macros::declare_id;
use solana_program::{program::set_return_data, pubkey::Pubkey};

use program::{
    assert_with_msg, event_recorder::EventRecorder, PhoenixInstruction, PhoenixLogContext,
    PhoenixMarketContext,
};
use solana_program::{
    account_info::{next_account_info, AccountInfo},
    entrypoint::ProgramResult,
    program_error::ProgramError,
};
use state::markets::MarketEvent;

#[cfg(not(feature = "no-entrypoint"))]
use solana_security_txt::security_txt;

#[cfg(not(feature = "no-entrypoint"))]
security_txt! {
    // Required fields
    name: "Phoenix V1",
    project_url: "https://ellipsislabs.xyz/",
    contacts: "email:maintainers@ellipsislabs.xyz",
    policy: "https://github.com/Ellipsis-Labs/phoenix-v1/blob/master/SECURITY.md",
    // Optional Fields
    preferred_languages: "en",
    source_code: "https://github.com/Ellipsis-Labs/phoenix-v1",
    auditors: "contact@osec.io"
}

declare_id!("PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY");

/// This is a static PDA with seeds: [b"log"]
/// If the program id changes, this will also need to be updated
pub mod phoenix_log_authority {
    // You need to import Pubkey prior to using the declare_pda macro
    use ellipsis_macros::declare_pda;
    use solana_program::pubkey::Pubkey;

    // This creates a static PDA with seeds: [b"log"]
    // The address of the PDA is 7aDTsspkQNGKmrexAN7FLx9oxU3iPczSSvHNggyuqYkR
    // The bump seed is stored in a variable called bump()
    declare_pda!(
        "7aDTsspkQNGKmrexAN7FLx9oxU3iPczSSvHNggyuqYkR",
        "PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY",
        "log"
    );

    #[test]
    fn check_pda() {
        use crate::phoenix_log_authority;
        use solana_program::pubkey::Pubkey;
        assert_eq!(
            phoenix_log_authority::ID,
            Pubkey::create_program_address(
                &["log".as_ref(), &[phoenix_log_authority::bump()]],
                &super::id()
            )
            .unwrap()
        );
    }
}

#[cfg(not(feature = "no-entrypoint"))]
solana_program::entrypoint!(process_instruction);

pub fn process_instruction(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    instruction_data: &[u8],
) -> ProgramResult {
    let (tag, data) = instruction_data
        .split_first()
        .ok_or(ProgramError::InvalidInstructionData)?;

    let instruction =
        PhoenixInstruction::try_from(*tag).or(Err(ProgramError::InvalidInstructionData))?;

    // This is a special instruction that is only used for recording
    // inner instruction data from recursive CPI calls.
    //
    // Market events can be searched by querying the transaction hash and parsing
    // the inner instruction data according to a pre-defined schema.
    //
    // Only the log authority is allowed to call this instruction.
    if let PhoenixInstruction::Log = instruction {
        let authority = next_account_info(&mut accounts.iter())?;
        assert_with_msg(
            authority.is_signer,
            ProgramError::MissingRequiredSignature,
            "Log authority must sign through CPI",
        )?;
        assert_with_msg(
            authority.key == &phoenix_log_authority::id(),
            ProgramError::InvalidArgument,
            "Invalid log authority",
        )?;
        return Ok(());
    }

    let (program_accounts, accounts) = accounts.split_at(4);
    let accounts_iter = &mut program_accounts.iter();
    let phoenix_log_context = PhoenixLogContext::load(accounts_iter)?;
    let market_context = if instruction == PhoenixInstruction::InitializeMarket {
        PhoenixMarketContext::load_init(accounts_iter)?
    } else {
        PhoenixMarketContext::load(accounts_iter)?
    };

    let mut event_recorder = EventRecorder::new(phoenix_log_context, &market_context, instruction)?;

    let mut record_event_fn = |e: MarketEvent<Pubkey>| event_recorder.add_event(e);
    let mut order_ids = Vec::new();

    match instruction {
        PhoenixInstruction::InitializeMarket => {
            phoenix_log!("PhoenixInstruction::Initialize");
            initialize::process_initialize_market(program_id, &market_context, accounts, data)?
        }
        PhoenixInstruction::Swap => {
            phoenix_log!("PhoenixInstruction::Swap");
            new_order::process_swap(
                program_id,
                &market_context,
                accounts,
                data,
                &mut record_event_fn,
            )?;
        }
        PhoenixInstruction::SwapWithFreeFunds => {
            phoenix_log!("PhoenixInstruction::SwapWithFreeFunds");
            new_order::process_swap_with_free_funds(
                program_id,
                &market_context,
                accounts,
                data,
                &mut record_event_fn,
            )?;
        }
        PhoenixInstruction::PlaceLimitOrder => {
            phoenix_log!("PhoenixInstruction::PlaceLimitOrder");
            new_order::process_place_limit_order(
                program_id,
                &market_context,
                accounts,
                data,
                &mut record_event_fn,
                &mut order_ids,
            )?
        }
        PhoenixInstruction::PlaceLimitOrderWithFreeFunds => {
            phoenix_log!("PhoenixInstruction::PlaceLimitOrderWithFreeFunds");
            new_order::process_place_limit_order_with_free_funds(
                program_id,
                &market_context,
                accounts,
                data,
                &mut record_event_fn,
                &mut order_ids,
            )?;
        }
        PhoenixInstruction::PlaceMultiplePostOnlyOrders => {
            phoenix_log!("PhoenixInstruction::PlaceMultiplePostOnlyOrders");
            new_order::process_place_multiple_post_only_orders(
                program_id,
                &market_context,
                accounts,
                data,
                &mut record_event_fn,
                &mut order_ids,
            )?;
        }
        PhoenixInstruction::PlaceMultiplePostOnlyOrdersWithFreeFunds => {
            phoenix_log!("PhoenixInstruction::PlaceMultiplePostOnlyOrdersWithFreeFunds");
            new_order::process_place_multiple_post_only_orders_with_free_funds(
                program_id,
                &market_context,
                accounts,
                data,
                &mut record_event_fn,
                &mut order_ids,
            )?;
        }
        PhoenixInstruction::ReduceOrder => {
            phoenix_log!("PhoenixInstruction::ReduceOrder");
            reduce_order::process_reduce_order(
                program_id,
                &market_context,
                accounts,
                data,
                true,
                &mut record_event_fn,
            )?
        }
        PhoenixInstruction::ReduceOrderWithFreeFunds => {
            phoenix_log!("PhoenixInstruction::ReduceOrderWithFreeFunds");
            reduce_order::process_reduce_order(
                program_id,
                &market_context,
                accounts,
                data,
                false,
                &mut record_event_fn,
            )?
        }
        PhoenixInstruction::CancelAllOrders => {
            phoenix_log!("PhoenixInstruction::CancelAllOrders");
            cancel_multiple_orders::process_cancel_all_orders(
                program_id,
                &market_context,
                accounts,
                data,
                true,
                &mut record_event_fn,
            )?
        }
        PhoenixInstruction::CancelAllOrdersWithFreeFunds => {
            phoenix_log!("PhoenixInstruction::CancelAllOrdersWithFreeFunds");
            cancel_multiple_orders::process_cancel_all_orders(
                program_id,
                &market_context,
                accounts,
                data,
                false,
                &mut record_event_fn,
            )?
        }
        PhoenixInstruction::CancelUpTo => {
            phoenix_log!("PhoenixInstruction::CancelMultipleOrders");
            cancel_multiple_orders::process_cancel_up_to(
                program_id,
                &market_context,
                accounts,
                data,
                true,
                &mut record_event_fn,
            )?
        }
        PhoenixInstruction::CancelUpToWithFreeFunds => {
            phoenix_log!("PhoenixInstruction::CancelUpToWithFreeFunds");
            cancel_multiple_orders::process_cancel_up_to(
                program_id,
                &market_context,
                accounts,
                data,
                false,
                &mut record_event_fn,
            )?
        }
        PhoenixInstruction::CancelMultipleOrdersById => {
            phoenix_log!("PhoenixInstruction::CancelMultipleOrdersById");
            cancel_multiple_orders::process_cancel_multiple_orders_by_id(
                program_id,
                &market_context,
                accounts,
                data,
                true,
                &mut record_event_fn,
            )?
        }
        PhoenixInstruction::CancelMultipleOrdersByIdWithFreeFunds => {
            phoenix_log!("PhoenixInstruction::CancelMultipleOrdersByIdWithFreeFunds");
            cancel_multiple_orders::process_cancel_multiple_orders_by_id(
                program_id,
                &market_context,
                accounts,
                data,
                false,
                &mut record_event_fn,
            )?
        }
        PhoenixInstruction::WithdrawFunds => {
            phoenix_log!("PhoenixInstruction::WithdrawFunds");
            withdraw::process_withdraw_funds(program_id, &market_context, accounts, data)?;
        }
        PhoenixInstruction::DepositFunds => {
            phoenix_log!("PhoenixInstruction::DepositFunds");
            deposit::process_deposit_funds(program_id, &market_context, accounts, data)?
        }
        PhoenixInstruction::ForceCancelOrders => {
            phoenix_log!("PhoenixInstruction::ForceCancelOrders");
            governance::process_force_cancel_orders(
                program_id,
                &market_context,
                accounts,
                data,
                &mut record_event_fn,
            )?
        }
        PhoenixInstruction::EvictSeat => {
            phoenix_log!("PhoenixInstruction::EvictSeat");
            governance::process_evict_seat(program_id, &market_context, accounts, data)?
        }
        PhoenixInstruction::ClaimAuthority => {
            phoenix_log!("PhoenixInstruction::ClaimAuthority");
            governance::process_claim_authority(program_id, &market_context, data)?
        }
        PhoenixInstruction::NameSuccessor => {
            phoenix_log!("PhoenixInstruction::NameSuccessor");
            governance::process_name_successor(program_id, &market_context, data)?
        }
        PhoenixInstruction::ChangeMarketStatus => {
            phoenix_log!("PhoenixInstruction::ChangeMarketStatus");
            governance::process_change_market_status(program_id, &market_context, accounts, data)?
        }
        PhoenixInstruction::RequestSeatAuthorized => {
            phoenix_log!("PhoenixInstruction::RequestSeatAuthorized");
            manage_seat::process_request_seat_authorized(
                program_id,
                &market_context,
                accounts,
                data,
            )?
        }
        PhoenixInstruction::RequestSeat => {
            phoenix_log!("PhoenixInstruction::RequestSeat");
            manage_seat::process_request_seat(program_id, &market_context, accounts, data)?
        }
        PhoenixInstruction::ChangeSeatStatus => {
            phoenix_log!("PhoenixInstruction::ChangeSeatStatus");
            manage_seat::process_change_seat_status(program_id, &market_context, accounts, data)?;
        }
        PhoenixInstruction::CollectFees => {
            phoenix_log!("PhoenixInstruction::CollectFees");
            fees::process_collect_fees(
                program_id,
                &market_context,
                accounts,
                data,
                &mut record_event_fn,
            )?
        }
        PhoenixInstruction::ChangeFeeRecipient => {
            phoenix_log!("PhoenixInstruction::ChangeFeeRecipient");
            fees::process_change_fee_recipient(program_id, &market_context, accounts, data)?
        }
        _ => unreachable!(),
    }
    event_recorder.increment_market_sequence_number_and_flush(market_context.market_info)?;
    // We set the order ids at the end of the instruction because the return data gets cleared after
    // every CPI call.
    if !order_ids.is_empty() {
        set_return_data(order_ids.try_to_vec()?.as_ref());
    }
    Ok(())
}