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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
use anyhow::anyhow;

use anyhow::Result;
use borsh::BorshDeserialize;
use ellipsis_transaction_utils::ParsedTransaction;
use itertools::Itertools;
use phoenix::program::MarketHeader;
use phoenix::program::MarketSizeParams;
use phoenix::program::PhoenixInstruction;
use phoenix::quantities::QuoteLots;
use phoenix::{
    program::cancel_multiple_orders::{CancelMultipleOrdersByIdParams, CancelUpToParams},
    program::events::PhoenixMarketEvent,
    program::instruction_builders::{
        create_cancel_all_orders_instruction, create_cancel_multiple_orders_by_id_instruction,
        create_cancel_up_to_instruction, create_new_order_instruction,
        create_withdraw_funds_instruction,
    },
    program::reduce_order::CancelOrderParams,
    quantities::{BaseLots, Ticks, WrapperU64},
    state::enums::{SelfTradeBehavior, Side},
    state::markets::FIFOOrderId,
    state::order_packet::OrderPacket,
    state::trader_state::TraderState,
};
use rand::{rngs::StdRng, Rng};
use solana_program::{instruction::Instruction, pubkey::Pubkey};
use solana_sdk::signature::Signature;
use std::str::FromStr;
use std::{
    collections::BTreeMap,
    fmt::Display,
    ops::{Div, Rem},
};

use crate::{market_event::Fill, orderbook::Orderbook};

const AUDIT_LOG_HEADER_LEN: usize = 92;

pub struct MarketState {
    /// State of the bids and offers in the market.
    pub orderbook: Orderbook<FIFOOrderId, PhoenixOrder>,
    /// Authorized makers in the market.
    pub traders: BTreeMap<Pubkey, TraderState>,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct RawPhoenixHeader {
    pub signature: Signature,
    pub instruction: u8,
    pub sequence_number: u64,
    pub timestamp: i64,
    pub slot: u64,
    pub market: Pubkey,
    pub signer: Pubkey,
}

#[derive(Clone, Debug, Default)]
pub struct RawPhoenixEvent {
    pub header: RawPhoenixHeader,
    pub batch: Vec<PhoenixMarketEvent>,
}

#[derive(Clone, Copy, Debug)]
pub struct PhoenixOrder {
    pub num_base_lots: u64,
    pub maker_id: Pubkey,
}

pub fn get_decimal_string<N: Display + Div + Rem + Copy + TryFrom<u64>>(
    amount: N,
    decimals: u32,
) -> String
where
    <N as Rem>::Output: std::fmt::Display,
    <N as Div>::Output: std::fmt::Display,
    <N as TryFrom<u64>>::Error: std::fmt::Debug,
{
    let scale = N::try_from(10_u64.pow(decimals)).unwrap();
    let lhs = amount / scale;
    let rhs = format!("{:0width$}", (amount % scale), width = decimals as usize).replace('-', ""); // remove negative sign from rhs
    let rhs = {
        let trim_zero = rhs.trim_end_matches('0');
        match trim_zero {
            "" => "0",
            _ => trim_zero,
        }
    };
    format!("{}.{}", lhs, rhs)
}

#[derive(Clone, Copy, Debug, Default)]
pub struct MarketMetadata {
    pub base_mint: Pubkey,
    pub quote_mint: Pubkey,
    pub base_decimals: u32,
    pub quote_decimals: u32,
    /// 10^base_decimals
    pub base_atoms_per_raw_base_unit: u64,
    /// 10^quote_decimals
    pub quote_atoms_per_quote_unit: u64,
    pub quote_atoms_per_quote_lot: u64,
    pub base_atoms_per_base_lot: u64,
    pub tick_size_in_quote_atoms_per_base_unit: u64,
    pub num_base_lots_per_base_unit: u64,
    /// The adjustment factor to convert from the raw base unit (i.e. 1 BONK token) to the Phoenix BaseUnit (which may be a multiple of whole tokens).
    /// The adjustment factor is almost always 1, unless one base token is worth less than one quote atom (i.e. 1e-6 USDC)
    pub raw_base_units_per_base_unit: u32,
    pub market_size_params: MarketSizeParams,
}

impl MarketMetadata {
    pub fn from_header(header: &MarketHeader) -> Result<Self> {
        let quote_atoms_per_quote_lot = header.get_quote_lot_size().into();
        let base_atoms_per_base_lot = header.get_base_lot_size().into();
        let quote_atoms_per_quote_unit = 10u64.pow(header.quote_params.decimals);
        let base_atoms_per_raw_base_unit = 10u64.pow(header.base_params.decimals);
        let base_mint = header.base_params.mint_key;
        let quote_mint = header.quote_params.mint_key;
        let tick_size_in_quote_atoms_per_base_unit =
            header.get_tick_size_in_quote_atoms_per_base_unit().into();
        // max(1) is only relevant for old markets where the raw_base_units_per_base_unit was not set
        let raw_base_units_per_base_unit = header.raw_base_units_per_base_unit.max(1);
        if base_atoms_per_raw_base_unit * raw_base_units_per_base_unit as u64
            % base_atoms_per_base_lot
            != 0
        {
            return Err(anyhow!(
                "Invalid base lot size (in base atoms per base lot)"
            ));
        }

        let num_base_lots_per_base_unit = (base_atoms_per_raw_base_unit
            * raw_base_units_per_base_unit as u64)
            / base_atoms_per_base_lot;

        Ok(MarketMetadata {
            base_mint,
            quote_mint,
            base_decimals: header.base_params.decimals,
            quote_decimals: header.quote_params.decimals,
            base_atoms_per_raw_base_unit,
            quote_atoms_per_quote_unit,
            tick_size_in_quote_atoms_per_base_unit,
            quote_atoms_per_quote_lot,
            base_atoms_per_base_lot,
            num_base_lots_per_base_unit,
            raw_base_units_per_base_unit,
            market_size_params: header.market_size_params,
        })
    }
}

impl MarketMetadata {
    /// Given a number of raw base units, returns the equivalent number of base lots (rounded down).
    pub fn raw_base_units_to_base_lots_rounded_down(&self, raw_base_units: f64) -> u64 {
        let base_units = raw_base_units / self.raw_base_units_per_base_unit as f64;
        (base_units * (self.num_base_lots_per_base_unit as f64)).floor() as u64
    }

    /// Given a number of raw base units, returns the equivalent number of base lots (rounded up).
    pub fn raw_base_units_to_base_lots_rounded_up(&self, raw_base_units: f64) -> u64 {
        let base_units = raw_base_units / self.raw_base_units_per_base_unit as f64;
        (base_units * (self.num_base_lots_per_base_unit as f64)).ceil() as u64
    }

    /// Given a number of base atoms, returns the equivalent number of base lots (rounded down).
    pub fn base_atoms_to_base_lots_rounded_down(&self, base_atoms: u64) -> u64 {
        base_atoms / self.base_atoms_per_base_lot
    }

    /// Given a number of base atoms, returns the equivalent number of base lots (rounded up).
    pub fn base_atoms_to_base_lots_rounded_up(&self, base_atoms: u64) -> u64 {
        1 + base_atoms.saturating_sub(1) / self.base_atoms_per_base_lot
    }

    /// Given a number of base lots, returns the equivalent number of base atoms.
    pub fn base_lots_to_base_atoms(&self, base_lots: u64) -> u64 {
        base_lots * self.base_atoms_per_base_lot
    }

    /// Given a number of quote units, returns the equivalent number of quote lots.
    pub fn quote_units_to_quote_lots(&self, quote_units: f64) -> u64 {
        (quote_units * (self.quote_atoms_per_quote_unit / self.quote_atoms_per_quote_lot) as f64)
            as u64
    }

    /// Given a number of quote atoms, returns the equivalent number of quote lots (rounded down).
    pub fn quote_atoms_to_quote_lots_rounded_down(&self, quote_atoms: u64) -> u64 {
        quote_atoms / self.quote_atoms_per_quote_lot
    }

    /// Given a number of quote atoms, returns the equivalent number of quote lots (rounded up).
    pub fn quote_atoms_to_quote_lots_rounded_up(&self, quote_atoms: u64) -> u64 {
        1 + quote_atoms.saturating_sub(1) / self.quote_atoms_per_quote_lot
    }

    /// Given a number of quote lots, returns the equivalent number of quote atoms.
    pub fn quote_lots_to_quote_atoms(&self, quote_lots: u64) -> u64 {
        quote_lots * self.quote_atoms_per_quote_lot
    }

    /// Given a number of base atoms, returns the equivalent number of raw base units.
    pub fn base_atoms_to_raw_base_units_as_float(&self, base_atoms: u64) -> f64 {
        base_atoms as f64 / self.base_atoms_per_raw_base_unit as f64
    }

    /// Given a number of quote atoms, returns the equivalent number of quote units.
    pub fn quote_atoms_to_quote_units_as_float(&self, quote_atoms: u64) -> f64 {
        quote_atoms as f64 / self.quote_atoms_per_quote_unit as f64
    }

    /// Given a number of base lots and price in ticks, returns the equivalent number of quote atoms
    /// for that price and number of base lots.
    pub fn base_lots_and_price_to_quote_atoms(&self, base_lots: u64, price_in_ticks: u64) -> u64 {
        base_lots * price_in_ticks * self.tick_size_in_quote_atoms_per_base_unit
            / self.num_base_lots_per_base_unit
    }

    /// Given a price in quote units per raw base unit (represented as a float), returns
    /// the corresponding number of ticks (rounded down)
    pub fn float_price_to_ticks_rounded_down(&self, price: f64) -> u64 {
        ((price
            * self.raw_base_units_per_base_unit as f64
            * self.quote_atoms_per_quote_unit as f64)
            / self.tick_size_in_quote_atoms_per_base_unit as f64) as u64
    }

    /// Given a price in quote units per raw base unit (represented as a float), returns
    /// the corresponding number of ticks (rounded up)
    pub fn float_price_to_ticks_rounded_up(&self, price: f64) -> u64 {
        ((price
            * self.raw_base_units_per_base_unit as f64
            * self.quote_atoms_per_quote_unit as f64)
            / self.tick_size_in_quote_atoms_per_base_unit as f64)
            .ceil() as u64
    }

    /// Given a number of ticks, returns the corresponding price in quote units per raw base unit (as a float)
    pub fn ticks_to_float_price(&self, ticks: u64) -> f64 {
        ticks as f64 * self.tick_size_in_quote_atoms_per_base_unit as f64
            / (self.quote_atoms_per_quote_unit as f64 * self.raw_base_units_per_base_unit as f64)
    }

    /// Returns the base lot size in raw base units (as a float)
    pub fn raw_base_units_per_base_lot(&self) -> f64 {
        self.base_atoms_per_base_lot as f64 / self.base_atoms_per_raw_base_unit as f64
    }

    /// Returns the tick size in quote units per raw base unit
    pub fn quote_units_per_raw_base_unit_per_tick(&self) -> f64 {
        self.tick_size_in_quote_atoms_per_base_unit as f64
            / (self.quote_atoms_per_quote_unit as f64 * self.raw_base_units_per_base_unit as f64)
    }
}

pub struct SDKClientCore {
    pub markets: BTreeMap<Pubkey, MarketMetadata>,
    pub trader: Pubkey,
}

/// Unit conversions
impl SDKClientCore {
    /// Given a market pubkey and a number of raw base units, returns the equivalent number of base lots (rounded down).
    pub fn raw_base_units_to_base_lots_rounded_down(
        &self,
        market_key: &Pubkey,
        raw_base_units: f64,
    ) -> Result<u64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.raw_base_units_to_base_lots_rounded_down(raw_base_units))
    }

    /// Given a market pubkey and a number of raw base units, returns the equivalent number of base lots (rounded up).
    pub fn raw_base_units_to_base_lots_rounded_up(
        &self,
        market_key: &Pubkey,
        raw_base_units: f64,
    ) -> Result<u64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.raw_base_units_to_base_lots_rounded_up(raw_base_units))
    }

    /// Given a market pubkey and a number of base atoms, returns the equivalent number of base lots (rounded down).
    pub fn base_atoms_to_base_lots_rounded_down(
        &self,
        market_key: &Pubkey,
        base_atoms: u64,
    ) -> Result<u64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.base_atoms_to_base_lots_rounded_down(base_atoms))
    }

    /// Given a market pubkey and a number of base atoms, returns the equivalent number of base lots (rounded up).
    pub fn base_atoms_to_base_lots_rounded_up(
        &self,
        market_key: &Pubkey,
        base_atoms: u64,
    ) -> Result<u64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.base_atoms_to_base_lots_rounded_up(base_atoms))
    }

    /// Given a market pubkey and a number of base lots, returns the equivalent number of base atoms.
    pub fn base_lots_to_base_atoms(&self, market_key: &Pubkey, base_lots: u64) -> Result<u64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.base_lots_to_base_atoms(base_lots))
    }

    /// Given a market pubkey and a number of quote units, returns the equivalent number of quote lots.
    pub fn quote_units_to_quote_lots(&self, market_key: &Pubkey, quote_units: f64) -> Result<u64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.quote_units_to_quote_lots(quote_units))
    }

    /// Given a market pubkey and a number of quote atoms, returns the equivalent number of quote lots (rounded down).
    pub fn quote_atoms_to_quote_lots_rounded_down(
        &self,
        market_key: &Pubkey,
        quote_atoms: u64,
    ) -> Result<u64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.quote_atoms_to_quote_lots_rounded_down(quote_atoms))
    }

    /// Given a market pubkey and a number of quote atoms, returns the equivalent number of quote lots (rounded up).
    pub fn quote_atoms_to_quote_lots_rounded_up(
        &self,
        market_key: &Pubkey,
        quote_atoms: u64,
    ) -> Result<u64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.quote_atoms_to_quote_lots_rounded_up(quote_atoms))
    }

    /// Given a market pubkey and a number of quote lots, returns the equivalent number of quote atoms.
    pub fn quote_lots_to_quote_atoms(&self, market_key: &Pubkey, quote_lots: u64) -> Result<u64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.quote_lots_to_quote_atoms(quote_lots))
    }

    /// Given a market pubkey and a number of base atoms, returns the equivalent number of raw base units.
    pub fn base_atoms_to_raw_base_units_as_float(
        &self,
        market_key: &Pubkey,
        base_atoms: u64,
    ) -> Result<f64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.base_atoms_to_raw_base_units_as_float(base_atoms))
    }

    /// Given a market pubkey and a number of quote atoms, returns the equivalent number of quote units.
    pub fn quote_atoms_to_quote_units_as_float(
        &self,
        market_key: &Pubkey,
        quote_atoms: u64,
    ) -> Result<f64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.quote_atoms_to_quote_units_as_float(quote_atoms))
    }

    /// Given a market pubkey and a fill event, returns the number of quote atoms filled.
    pub fn fill_event_to_quote_atoms(&self, market_key: &Pubkey, fill: &Fill) -> Result<u64> {
        let &Fill {
            base_lots_filled: base_lots,
            price_in_ticks,
            ..
        } = fill;
        self.base_lots_and_price_to_quote_atoms(market_key, base_lots, price_in_ticks)
    }

    /// Given a market pubkey, number of base lots, and price in ticks, returns the equivalent number of quote atoms
    /// for that price and number of base lots.
    pub fn base_lots_and_price_to_quote_atoms(
        &self,
        market_key: &Pubkey,
        base_lots: u64,
        price_in_ticks: u64,
    ) -> Result<u64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.base_lots_and_price_to_quote_atoms(base_lots, price_in_ticks))
    }

    /// Given a market pubkey and a price in quote units per raw base unit (represented as a float), returns
    /// the corresponding number of ticks (rounded down)
    pub fn float_price_to_ticks_rounded_down(
        &self,
        market_key: &Pubkey,
        price: f64,
    ) -> Result<u64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.float_price_to_ticks_rounded_down(price))
    }

    /// Given a market pubkey and a price in quote units per raw base unit (represented as a float), returns
    /// the corresponding number of ticks (rounded up)
    pub fn float_price_to_ticks_rounded_up(&self, market_key: &Pubkey, price: f64) -> Result<u64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.float_price_to_ticks_rounded_up(price))
    }

    /// Given a number of ticks, returns the corresponding price in quote units per raw base unit (as a float)
    pub fn ticks_to_float_price(&self, market_key: &Pubkey, ticks: u64) -> Result<f64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.ticks_to_float_price(ticks))
    }

    /// Given a market, returns the base lot size in raw base units (as a float)
    pub fn raw_base_units_per_base_lot(&self, market_key: &Pubkey) -> Result<f64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.raw_base_units_per_base_lot())
    }

    /// Given a market, returns the tick size in quote units per raw base unit
    pub fn quote_units_per_raw_base_unit_per_tick(&self, market_key: &Pubkey) -> Result<f64> {
        self.markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first"))
            .map(|m| m.quote_units_per_raw_base_unit_per_tick())
    }
}

impl SDKClientCore {
    /// Generate a random client order id
    pub fn get_next_client_order_id(&self, rng: &mut StdRng) -> u128 {
        rng.gen::<u128>()
    }

    pub fn get_market_metadata(&self, market_key: &Pubkey) -> &MarketMetadata {
        match self.markets.get(market_key) {
            Some(market_metadata) => market_metadata,
            None => panic!("Market not found! Please load in the market first."),
        }
    }

    pub fn parse_raw_phoenix_events(
        &self,
        sig: &Signature,
        events: Vec<Vec<u8>>,
    ) -> Option<Vec<RawPhoenixEvent>> {
        let mut market_events: Vec<RawPhoenixEvent> = vec![];

        for event in events.iter() {
            let header_event =
                PhoenixMarketEvent::try_from_slice(&event[..AUDIT_LOG_HEADER_LEN]).ok()?;
            let header = match header_event {
                PhoenixMarketEvent::Header(header) => Some(header),
                _ => {
                    panic!("Expected a header event");
                }
            }?;
            let offset = AUDIT_LOG_HEADER_LEN;
            let mut phoenix_event_bytes = (header.total_events as u32).to_le_bytes().to_vec();
            phoenix_event_bytes.extend_from_slice(&event[offset..]);
            let phoenix_events =
                match Vec::<PhoenixMarketEvent>::try_from_slice(&phoenix_event_bytes) {
                    Ok(v) => v,
                    Err(e) => {
                        println!("Error parsing events: {:?}", e);
                        return None;
                    }
                };

            market_events.push(RawPhoenixEvent {
                header: RawPhoenixHeader {
                    signature: *sig,
                    instruction: header.instruction,
                    sequence_number: header.sequence_number,
                    timestamp: header.timestamp,
                    slot: header.slot,
                    market: header.market,
                    signer: header.signer,
                },
                batch: phoenix_events,
            });
        }

        // This dedupes chunks with the same sequence number into a single list of events
        market_events = market_events
            .iter()
            .group_by(|event| event.header)
            .into_iter()
            .map(|(header, batches)| RawPhoenixEvent {
                header,
                batch: batches
                    .cloned()
                    .flat_map(|event| event.batch)
                    .collect::<Vec<_>>(),
            })
            .collect();

        Some(market_events)
    }

    pub fn parse_events_from_transaction(
        &self,
        tx: &ParsedTransaction,
    ) -> Option<Vec<RawPhoenixEvent>> {
        let sig = Signature::from_str(&tx.signature).ok()?;
        let mut event_list = vec![];
        for inner_ixs in tx.inner_instructions.iter() {
            for inner_ix in inner_ixs.iter() {
                let current_program_id = inner_ix.instruction.program_id.clone();
                if current_program_id != phoenix::id().to_string() {
                    continue;
                }
                if inner_ix.instruction.data.is_empty() {
                    continue;
                }
                let (tag, data) = match inner_ix.instruction.data.split_first() {
                    Some((tag, data)) => (*tag, data),
                    None => continue,
                };
                let ix_enum = match PhoenixInstruction::try_from(tag).ok() {
                    Some(ix) => ix,
                    None => continue,
                };
                if matches!(ix_enum, PhoenixInstruction::Log) {
                    event_list.push(data.to_vec());
                }
            }
        }
        self.parse_raw_phoenix_events(&sig, event_list)
    }
}

/// SDKClientCore instruction builders
impl SDKClientCore {
    pub fn get_ioc_ix(
        &self,
        market_key: &Pubkey,
        price: u64,
        side: Side,
        num_base_lots: u64,
    ) -> Result<Instruction> {
        self.get_ioc_generic_ix(
            market_key,
            price,
            side,
            num_base_lots,
            None,
            None,
            None,
            None,
            None,
            None,
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub fn get_ioc_generic_ix(
        &self,
        market_key: &Pubkey,
        price: u64,
        side: Side,
        num_base_lots: u64,
        self_trade_behavior: Option<SelfTradeBehavior>,
        match_limit: Option<u64>,
        client_order_id: Option<u128>,
        use_only_deposited_funds: Option<bool>,
        last_valid_slot: Option<u64>,
        last_valid_unix_timestamp_in_seconds: Option<u64>,
    ) -> Result<Instruction> {
        let market = self
            .markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first."))?;
        let num_quote_ticks_per_base_unit = price / market.tick_size_in_quote_atoms_per_base_unit;
        let self_trade_behavior = self_trade_behavior.unwrap_or(SelfTradeBehavior::CancelProvide);
        let client_order_id = client_order_id.unwrap_or(0);
        let use_only_deposited_funds = use_only_deposited_funds.unwrap_or(false);
        let order_packet = OrderPacket::ImmediateOrCancel {
            side,
            price_in_ticks: Some(Ticks::new(num_quote_ticks_per_base_unit)),
            num_base_lots: BaseLots::new(num_base_lots),
            num_quote_lots: QuoteLots::new(0),
            min_base_lots_to_fill: BaseLots::new(0),
            min_quote_lots_to_fill: QuoteLots::new(0),
            self_trade_behavior,
            match_limit,
            client_order_id,
            use_only_deposited_funds,
            last_valid_slot,
            last_valid_unix_timestamp_in_seconds,
        };
        Ok(create_new_order_instruction(
            &market_key.clone(),
            &self.trader,
            &market.base_mint,
            &market.quote_mint,
            &order_packet,
        ))
    }

    pub fn get_fok_sell_ix(
        &self,
        market_key: &Pubkey,
        price: u64,
        size_in_base_lots: u64,
    ) -> Result<Instruction> {
        self.get_fok_generic_ix(
            market_key,
            price,
            Side::Ask,
            size_in_base_lots,
            None,
            None,
            None,
            None,
        )
    }

    pub fn get_fok_buy_ix(
        &self,
        market_key: &Pubkey,
        price: u64,
        size_in_base_lots: u64,
    ) -> Result<Instruction> {
        self.get_fok_generic_ix(
            market_key,
            price,
            Side::Bid,
            size_in_base_lots,
            None,
            None,
            None,
            None,
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub fn get_fok_buy_generic_ix(
        &self,
        market_key: &Pubkey,
        price: u64,
        size_in_quote_lots: u64,
        self_trade_behavior: Option<SelfTradeBehavior>,
        match_limit: Option<u64>,
        client_order_id: Option<u128>,
        use_only_deposited_funds: Option<bool>,
    ) -> Result<Instruction> {
        self.get_fok_generic_ix(
            market_key,
            price,
            Side::Bid,
            size_in_quote_lots,
            self_trade_behavior,
            match_limit,
            client_order_id,
            use_only_deposited_funds,
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub fn get_fok_sell_generic_ix(
        &self,
        market_key: &Pubkey,
        price: u64,
        size_in_base_lots: u64,
        self_trade_behavior: Option<SelfTradeBehavior>,
        match_limit: Option<u64>,
        client_order_id: Option<u128>,
        use_only_deposited_funds: Option<bool>,
    ) -> Result<Instruction> {
        self.get_fok_generic_ix(
            market_key,
            price,
            Side::Ask,
            size_in_base_lots,
            self_trade_behavior,
            match_limit,
            client_order_id,
            use_only_deposited_funds,
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub fn get_fok_generic_ix(
        &self,
        market_key: &Pubkey,
        price: u64,
        side: Side,
        size: u64,
        self_trade_behavior: Option<SelfTradeBehavior>,
        match_limit: Option<u64>,
        client_order_id: Option<u128>,
        use_only_deposited_funds: Option<bool>,
    ) -> Result<Instruction> {
        let market = self
            .markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first."))?;
        let self_trade_behavior = self_trade_behavior.unwrap_or(SelfTradeBehavior::CancelProvide);
        let client_order_id = client_order_id.unwrap_or(0);
        let target_price_in_ticks = price / market.tick_size_in_quote_atoms_per_base_unit;
        let use_only_deposited_funds = use_only_deposited_funds.unwrap_or(false);
        match side {
            Side::Bid => {
                let quote_lot_budget = size / market.quote_atoms_per_quote_lot;
                Ok(create_new_order_instruction(
                    &market_key.clone(),
                    &self.trader,
                    &market.base_mint,
                    &market.quote_mint,
                    &OrderPacket::new_fok_buy_with_limit_price(
                        target_price_in_ticks,
                        quote_lot_budget,
                        self_trade_behavior,
                        match_limit,
                        client_order_id,
                        use_only_deposited_funds,
                    ),
                ))
            }
            Side::Ask => {
                let num_base_lots = size / market.base_atoms_per_base_lot;
                Ok(create_new_order_instruction(
                    &market_key.clone(),
                    &self.trader,
                    &market.base_mint,
                    &market.quote_mint,
                    &OrderPacket::new_fok_sell_with_limit_price(
                        target_price_in_ticks,
                        num_base_lots,
                        self_trade_behavior,
                        match_limit,
                        client_order_id,
                        use_only_deposited_funds,
                    ),
                ))
            }
        }
    }

    pub fn get_ioc_with_slippage_ix(
        &self,
        market_key: &Pubkey,
        lots_in: u64,
        min_lots_out: u64,
        side: Side,
    ) -> Result<Instruction> {
        let market = self
            .markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first."))?;
        let order_type = match side {
            Side::Bid => OrderPacket::new_ioc_buy_with_slippage(lots_in, min_lots_out),
            Side::Ask => OrderPacket::new_ioc_sell_with_slippage(lots_in, min_lots_out),
        };

        Ok(create_new_order_instruction(
            &market_key.clone(),
            &self.trader,
            &market.base_mint,
            &market.quote_mint,
            &order_type,
        ))
    }

    pub fn get_ioc_from_tick_price_ix(
        &self,
        market_key: &Pubkey,
        tick_price: u64,
        side: Side,
        size: u64,
    ) -> Result<Instruction> {
        let market = self
            .markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first."))?;
        Ok(create_new_order_instruction(
            &market_key.clone(),
            &self.trader,
            &market.base_mint,
            &market.quote_mint,
            &OrderPacket::new_ioc_by_lots(
                side,
                tick_price,
                size,
                SelfTradeBehavior::CancelProvide,
                None,
                0,
                false,
            ),
        ))
    }

    pub fn get_post_only_ix(
        &self,
        market_key: &Pubkey,
        price: u64,
        side: Side,
        size: u64,
    ) -> Result<Instruction> {
        self.get_post_only_generic_ix(
            market_key, price, side, size, None, None, None, None, None, None,
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub fn get_post_only_generic_ix(
        &self,
        market_key: &Pubkey,
        price: u64,
        side: Side,
        size: u64,
        client_order_id: Option<u128>,
        reject_post_only: Option<bool>,
        use_only_deposited_funds: Option<bool>,
        last_valid_slot: Option<u64>,
        last_valid_unix_timestamp_in_seconds: Option<u64>,
        fail_silently_on_insufficient_funds: Option<bool>,
    ) -> Result<Instruction> {
        let market = self
            .markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first."))?;
        let price_in_ticks = price / market.tick_size_in_quote_atoms_per_base_unit;
        let client_order_id = client_order_id.unwrap_or(0);
        let reject_post_only = reject_post_only.unwrap_or(false);
        let use_only_deposited_funds = use_only_deposited_funds.unwrap_or(false);
        let fail_silently_on_insufficient_funds =
            fail_silently_on_insufficient_funds.unwrap_or(false);
        let order_packet = OrderPacket::PostOnly {
            side,
            price_in_ticks: Ticks::new(price_in_ticks),
            num_base_lots: BaseLots::new(size),
            client_order_id,
            reject_post_only,
            use_only_deposited_funds,
            last_valid_slot,
            last_valid_unix_timestamp_in_seconds,
            fail_silently_on_insufficient_funds,
        };
        Ok(create_new_order_instruction(
            &market_key.clone(),
            &self.trader,
            &market.base_mint,
            &market.quote_mint,
            &order_packet,
        ))
    }

    pub fn get_post_only_ix_from_tick_price(
        &self,
        market_key: &Pubkey,
        tick_price: u64,
        side: Side,
        size: u64,
        client_order_id: u128,
        improve_price_on_cross: bool,
    ) -> Result<Instruction> {
        let market = self
            .markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first."))?;
        Ok(create_new_order_instruction(
            &market_key.clone(),
            &self.trader,
            &market.base_mint,
            &market.quote_mint,
            &if improve_price_on_cross {
                OrderPacket::new_adjustable_post_only_default_with_client_order_id(
                    side,
                    tick_price,
                    size,
                    client_order_id,
                )
            } else {
                OrderPacket::new_post_only_default_with_client_order_id(
                    side,
                    tick_price,
                    size,
                    client_order_id,
                )
            },
        ))
    }

    pub fn get_limit_order_ix(
        &self,
        market_key: &Pubkey,
        price: u64,
        side: Side,
        size: u64,
    ) -> Result<Instruction> {
        self.get_limit_order_generic_ix(
            market_key, price, side, size, None, None, None, None, None, None, None,
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub fn get_limit_order_generic_ix(
        &self,
        market_key: &Pubkey,
        price: u64,
        side: Side,
        size: u64,
        self_trade_behavior: Option<SelfTradeBehavior>,
        match_limit: Option<u64>,
        client_order_id: Option<u128>,
        use_only_deposited_funds: Option<bool>,
        last_valid_slot: Option<u64>,
        last_valid_unix_timestamp_in_seconds: Option<u64>,
        fail_silently_on_insufficient_funds: Option<bool>,
    ) -> Result<Instruction> {
        let market = self
            .markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first."))?;
        let num_quote_ticks_per_base_unit = price / market.tick_size_in_quote_atoms_per_base_unit;
        let self_trade_behavior = self_trade_behavior.unwrap_or(SelfTradeBehavior::DecrementTake);
        let client_order_id = client_order_id.unwrap_or(0);
        let use_only_deposited_funds = use_only_deposited_funds.unwrap_or(false);
        let fail_silently_on_insufficient_funds =
            fail_silently_on_insufficient_funds.unwrap_or(false);
        let order_packet = OrderPacket::Limit {
            side,
            price_in_ticks: Ticks::new(num_quote_ticks_per_base_unit),
            num_base_lots: BaseLots::new(size),
            self_trade_behavior,
            match_limit,
            client_order_id,
            use_only_deposited_funds,
            last_valid_slot,
            last_valid_unix_timestamp_in_seconds,
            fail_silently_on_insufficient_funds,
        };
        Ok(create_new_order_instruction(
            &market_key.clone(),
            &self.trader,
            &market.base_mint,
            &market.quote_mint,
            &order_packet,
        ))
    }

    pub fn get_limit_order_ix_from_tick_price(
        &self,
        market_key: &Pubkey,
        tick_price: u64,
        side: Side,
        size: u64,
        client_order_id: u128,
    ) -> Result<Instruction> {
        let market = self
            .markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first."))?;
        Ok(create_new_order_instruction(
            &market_key.clone(),
            &self.trader,
            &market.base_mint,
            &market.quote_mint,
            &OrderPacket::new_limit_order_default_with_client_order_id(
                side,
                tick_price,
                size,
                client_order_id,
            ),
        ))
    }

    pub fn get_cancel_ids_ix(
        &self,
        market_key: &Pubkey,
        ids: Vec<FIFOOrderId>,
    ) -> Result<Instruction> {
        let market = self
            .markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first."))?;
        let mut cancel_orders = vec![];
        for &FIFOOrderId {
            price_in_ticks,
            order_sequence_number,
            ..
        } in ids.iter()
        {
            cancel_orders.push(CancelOrderParams {
                side: Side::from_order_sequence_number(order_sequence_number),
                price_in_ticks: price_in_ticks.as_u64(),
                order_sequence_number,
            });
        }
        let cancel_multiple_orders = CancelMultipleOrdersByIdParams {
            orders: cancel_orders,
        };

        Ok(create_cancel_multiple_orders_by_id_instruction(
            &market_key.clone(),
            &self.trader,
            &market.base_mint,
            &market.quote_mint,
            &cancel_multiple_orders,
        ))
    }

    pub fn get_cancel_up_to_ix(
        &self,
        market_key: &Pubkey,
        tick_limit: Option<u64>,
        side: Side,
    ) -> Result<Instruction> {
        let market = self
            .markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first."))?;
        let params = CancelUpToParams {
            side,
            tick_limit,
            num_orders_to_search: None,
            num_orders_to_cancel: None,
        };

        Ok(create_cancel_up_to_instruction(
            &market_key.clone(),
            &self.trader,
            &market.base_mint,
            &market.quote_mint,
            &params,
        ))
    }

    pub fn get_cancel_all_ix(&self, market_key: &Pubkey) -> Result<Instruction> {
        let market = self
            .markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first."))?;
        Ok(create_cancel_all_orders_instruction(
            &market_key.clone(),
            &self.trader,
            &market.base_mint,
            &market.quote_mint,
        ))
    }

    pub fn get_withdraw_ix(&self, market_key: &Pubkey) -> Result<Instruction> {
        let market = self
            .markets
            .get(market_key)
            .ok_or_else(|| anyhow!("Market not found! Please load in the market first."))?;
        Ok(create_withdraw_funds_instruction(
            &market_key.clone(),
            &self.trader,
            &market.base_mint,
            &market.quote_mint,
        ))
    }
}