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
use borsh::BorshDeserialize;
use phoenix::{
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,
},
program::{
cancel_multiple_orders::{CancelMultipleOrdersByIdParams, CancelUpToParams},
EvictEvent, ExpiredOrderEvent, FeeEvent, FillEvent, FillSummaryEvent, PlaceEvent,
TimeInForceEvent,
},
program::{reduce_order::CancelOrderParams, ReduceEvent},
quantities::WrapperU64,
state::enums::{SelfTradeBehavior, Side},
state::markets::FIFOOrderId,
state::order_packet::OrderPacket,
state::trader_state::TraderState,
};
use rand::{rngs::StdRng, Rng};
use solana_sdk::signature::Signature;
use std::{
collections::BTreeMap,
fmt::Display,
ops::{Deref, Div, Rem},
sync::{Arc, Mutex},
};
use anyhow;
use solana_program::{instruction::Instruction, pubkey::Pubkey};
use crate::{
market_event::{
Evict, Fill, FillSummary, MarketEventDetails, PhoenixEvent, Place, Reduce, TimeInForce,
},
orderbook::Orderbook,
};
const AUDIT_LOG_HEADER_LEN: usize = 92;
pub struct MarketState {
pub orderbook: Orderbook<FIFOOrderId, PhoenixOrder>,
pub traders: BTreeMap<Pubkey, TraderState>,
}
#[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('-', ""); let rhs = {
let trim_zero = rhs.trim_end_matches('0');
match trim_zero {
"" => "0",
_ => trim_zero,
}
};
format!("{}.{}", lhs, rhs)
}
#[derive(Clone, Copy, Debug)]
pub struct MarketMetadata {
pub base_mint: Pubkey,
pub quote_mint: Pubkey,
pub base_decimals: u32,
pub quote_decimals: u32,
pub base_multiplier: u64,
pub quote_multiplier: u64,
pub quote_lot_size: u64,
pub base_lot_size: u64,
pub tick_size_in_quote_atoms_per_base_unit: u64,
pub num_base_lots_per_base_unit: u64,
pub raw_base_units_per_base_unit: u32,
}
pub struct SDKClientCore {
pub markets: BTreeMap<Pubkey, MarketMetadata>,
pub rng: Arc<Mutex<StdRng>>,
pub active_market_key: Pubkey,
pub trader: Pubkey,
}
impl Deref for SDKClientCore {
type Target = MarketMetadata;
fn deref(&self) -> &Self::Target {
self.markets.get(&self.active_market_key).unwrap()
}
}
impl SDKClientCore {
pub fn raw_base_units_to_base_lots(&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
}
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
}
pub fn base_atoms_to_base_lots(&self, base_atoms: u64) -> u64 {
base_atoms / self.base_lot_size }
pub fn base_lots_to_base_atoms(&self, base_lots: u64) -> u64 {
base_lots * self.base_lot_size }
pub fn quote_units_to_quote_lots(&self, quote_units: f64) -> u64 {
(quote_units * self.quote_multiplier as f64 / self.quote_lot_size as f64) as u64
}
pub fn quote_atoms_to_quote_lots(&self, quote_atoms: u64) -> u64 {
quote_atoms / self.quote_lot_size
}
pub fn quote_lots_to_quote_atoms(&self, quote_lots: u64) -> u64 {
quote_lots * self.quote_lot_size
}
pub fn base_atoms_to_base_unit_as_float(&self, base_atoms: u64) -> f64 {
base_atoms as f64 / self.base_multiplier as f64
}
pub fn quote_atoms_to_quote_unit_as_float(&self, quote_atoms: u64) -> f64 {
quote_atoms as f64 / self.quote_multiplier as f64
}
pub fn print_quote_amount(&self, quote_amount: u64) {
println!("{}", get_decimal_string(quote_amount, self.quote_decimals));
}
pub fn print_base_amount(&self, base_amount: u64) {
println!("{}", get_decimal_string(base_amount, self.base_decimals));
}
pub fn fill_event_to_quote_amount(&self, fill: &Fill) -> u64 {
let &Fill {
base_lots_filled: base_lots,
price_in_ticks,
..
} = fill;
self.order_to_quote_amount(base_lots, price_in_ticks)
}
pub fn order_to_quote_amount(&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
}
pub fn float_price_to_ticks(&self, price: f64) -> u64 {
((price * self.raw_base_units_per_base_unit as f64 * self.quote_multiplier as f64)
/ self.tick_size_in_quote_atoms_per_base_unit as f64) as u64
}
pub fn float_price_to_ticks_rounded_up(&self, price: f64) -> u64 {
((price * self.raw_base_units_per_base_unit as f64 * self.quote_multiplier as f64)
/ self.tick_size_in_quote_atoms_per_base_unit as f64)
.ceil() as u64
}
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_multiplier as f64
}
pub fn base_lots_to_base_units_multiplier(&self) -> f64 {
1.0 / self.num_base_lots_per_base_unit as f64
}
pub fn ticks_to_float_price_multiplier(&self) -> f64 {
self.tick_size_in_quote_atoms_per_base_unit as f64 / self.quote_multiplier as f64
}
}
impl SDKClientCore {
pub fn get_next_client_order_id(&self) -> u128 {
self.rng.lock().unwrap().gen::<u128>()
}
pub fn change_active_market(&mut self, market: &Pubkey) -> anyhow::Result<()> {
if self.markets.get(market).is_some() {
self.active_market_key = *market;
Ok(())
} else {
Err(anyhow::Error::msg("Market not found"))
}
}
pub fn get_active_market_metadata(&self) -> &MarketMetadata {
self.markets.get(&self.active_market_key).unwrap()
}
pub fn parse_phoenix_events(
&self,
sig: &Signature,
events: Vec<Vec<u8>>,
) -> Option<Vec<PhoenixEvent>> {
let mut market_events: Vec<PhoenixEvent> = 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;
}
};
let mut trade_direction = None;
for phoenix_event in phoenix_events {
match phoenix_event {
PhoenixMarketEvent::Fill(FillEvent {
index,
maker_id,
order_sequence_number,
price_in_ticks,
base_lots_filled,
base_lots_remaining,
}) => {
let side_filled = Side::from_order_sequence_number(order_sequence_number);
market_events.push(PhoenixEvent {
market: header.market,
sequence_number: header.sequence_number,
slot: header.slot,
timestamp: header.timestamp,
signature: *sig,
signer: header.signer,
event_index: index as u64,
details: MarketEventDetails::Fill(Fill {
order_sequence_number,
maker: maker_id,
taker: header.signer,
price_in_ticks,
base_lots_filled,
base_lots_remaining,
side_filled: Side::from_order_sequence_number(
order_sequence_number,
),
is_full_fill: base_lots_remaining == 0,
}),
});
if trade_direction.is_none() {
trade_direction = match side_filled {
Side::Bid => Some(-1),
Side::Ask => Some(1),
}
}
}
PhoenixMarketEvent::Reduce(ReduceEvent {
index,
order_sequence_number,
price_in_ticks,
base_lots_removed,
base_lots_remaining,
}) => market_events.push(PhoenixEvent {
market: header.market,
sequence_number: header.sequence_number,
slot: header.slot,
timestamp: header.timestamp,
signature: *sig,
signer: header.signer,
event_index: index as u64,
details: MarketEventDetails::Reduce(Reduce {
order_sequence_number,
maker: header.signer,
price_in_ticks,
base_lots_removed,
base_lots_remaining,
is_full_cancel: base_lots_remaining == 0,
}),
}),
PhoenixMarketEvent::Place(PlaceEvent {
index,
order_sequence_number,
client_order_id,
price_in_ticks,
base_lots_placed,
}) => market_events.push(PhoenixEvent {
market: header.market,
sequence_number: header.sequence_number,
slot: header.slot,
timestamp: header.timestamp,
signature: *sig,
signer: header.signer,
event_index: index as u64,
details: MarketEventDetails::Place(Place {
order_sequence_number,
client_order_id,
maker: header.signer,
price_in_ticks,
base_lots_placed,
}),
}),
PhoenixMarketEvent::Evict(EvictEvent {
index,
maker_id,
order_sequence_number,
price_in_ticks,
base_lots_evicted,
}) => market_events.push(PhoenixEvent {
market: header.market,
sequence_number: header.sequence_number,
slot: header.slot,
timestamp: header.timestamp,
signature: *sig,
signer: header.signer,
event_index: index as u64,
details: MarketEventDetails::Evict(Evict {
order_sequence_number,
maker: maker_id,
price_in_ticks,
base_lots_evicted,
}),
}),
PhoenixMarketEvent::FillSummary(FillSummaryEvent {
index,
client_order_id,
total_base_lots_filled,
total_quote_lots_filled,
total_fee_in_quote_lots,
}) => market_events.push(PhoenixEvent {
market: header.market,
sequence_number: header.sequence_number,
slot: header.slot,
timestamp: header.timestamp,
signature: *sig,
signer: header.signer,
event_index: index as u64,
details: MarketEventDetails::FillSummary(FillSummary {
client_order_id,
total_base_filled: total_base_lots_filled * self.base_lot_size,
total_quote_filled_including_fees: total_quote_lots_filled
* self.quote_lot_size,
total_quote_fees: total_fee_in_quote_lots * self.quote_lot_size,
trade_direction: trade_direction.unwrap_or(0),
}),
}),
PhoenixMarketEvent::Fee(FeeEvent {
index,
fees_collected_in_quote_lots,
}) => market_events.push(PhoenixEvent {
market: header.market,
sequence_number: header.sequence_number,
slot: header.slot,
timestamp: header.timestamp,
signature: *sig,
signer: header.signer,
event_index: index as u64,
details: MarketEventDetails::Fee(
fees_collected_in_quote_lots * self.quote_lot_size,
),
}),
PhoenixMarketEvent::TimeInForce(TimeInForceEvent {
index,
order_sequence_number,
last_valid_slot,
last_valid_unix_timestamp_in_seconds,
}) => market_events.push(PhoenixEvent {
market: header.market,
sequence_number: header.sequence_number,
slot: header.slot,
timestamp: header.timestamp,
signature: *sig,
signer: header.signer,
event_index: index as u64,
details: MarketEventDetails::TimeInForce(TimeInForce {
order_sequence_number,
last_valid_slot,
last_valid_unix_timestamp_in_seconds,
}),
}),
PhoenixMarketEvent::ExpiredOrder(ExpiredOrderEvent {
index,
maker_id,
order_sequence_number,
price_in_ticks,
base_lots_removed,
}) => market_events.push(PhoenixEvent {
market: header.market,
sequence_number: header.sequence_number,
slot: header.slot,
timestamp: header.timestamp,
signature: *sig,
signer: header.signer,
event_index: index as u64,
details: MarketEventDetails::Reduce(Reduce {
order_sequence_number,
maker: maker_id,
price_in_ticks,
base_lots_removed,
base_lots_remaining: 0,
is_full_cancel: true,
}),
}),
_ => {
println!("Unknown event: {:?}", phoenix_event);
}
}
}
}
Some(market_events)
}
pub fn get_ioc_ix(&self, price: u64, side: Side, num_base_lots: u64) -> Instruction {
self.get_ioc_generic_ix(price, side, num_base_lots, None, None, None, None)
}
#[allow(clippy::too_many_arguments)]
pub fn get_ioc_generic_ix(
&self,
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>,
) -> Instruction {
let num_quote_ticks_per_base_unit = price / self.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);
create_new_order_instruction(
&self.active_market_key.clone(),
&self.trader,
&self.base_mint,
&self.quote_mint,
&OrderPacket::new_ioc_by_lots(
side,
num_quote_ticks_per_base_unit,
num_base_lots,
self_trade_behavior,
match_limit,
client_order_id,
use_only_deposited_funds,
),
)
}
pub fn get_fok_sell_ix(&self, price: u64, size_in_base_lots: u64) -> Instruction {
self.get_fok_generic_ix(price, Side::Ask, size_in_base_lots, None, None, None, None)
}
pub fn get_fok_buy_generic_ix(
&self,
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>,
) -> Instruction {
self.get_fok_generic_ix(
price,
Side::Bid,
size_in_quote_lots,
self_trade_behavior,
match_limit,
client_order_id,
use_only_deposited_funds,
)
}
pub fn get_fok_sell_generic_ix(
&self,
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>,
) -> Instruction {
self.get_fok_generic_ix(
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,
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>,
) -> Instruction {
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 / self.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 / self.quote_lot_size;
create_new_order_instruction(
&self.active_market_key.clone(),
&self.trader,
&self.base_mint,
&self.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 / self.base_lot_size;
create_new_order_instruction(
&self.active_market_key.clone(),
&self.trader,
&self.base_mint,
&self.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,
lots_in: u64,
min_lots_out: u64,
side: Side,
) -> Instruction {
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),
};
create_new_order_instruction(
&self.active_market_key.clone(),
&self.trader,
&self.base_mint,
&self.quote_mint,
&order_type,
)
}
pub fn get_ioc_from_tick_price_ix(
&self,
tick_price: u64,
side: Side,
size: u64,
) -> Instruction {
create_new_order_instruction(
&self.active_market_key.clone(),
&self.trader,
&self.base_mint,
&self.quote_mint,
&OrderPacket::new_ioc_by_lots(
side,
tick_price,
size,
SelfTradeBehavior::CancelProvide,
None,
self.rng.lock().unwrap().gen::<u128>(),
false,
),
)
}
pub fn get_post_only_ix(&self, price: u64, side: Side, size: u64) -> Instruction {
self.get_post_only_generic_ix(price, side, size, None, None, None)
}
pub fn get_post_only_generic_ix(
&self,
price: u64,
side: Side,
size: u64,
client_order_id: Option<u128>,
reject_post_only: Option<bool>,
use_only_deposited_funds: Option<bool>,
) -> Instruction {
let price_in_ticks = price / self.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);
create_new_order_instruction(
&self.active_market_key.clone(),
&self.trader,
&self.base_mint,
&self.quote_mint,
&OrderPacket::new_post_only(
side,
price_in_ticks,
size,
client_order_id,
reject_post_only,
use_only_deposited_funds,
),
)
}
pub fn get_post_only_ix_from_tick_price(
&self,
tick_price: u64,
side: Side,
size: u64,
client_order_id: u128,
improve_price_on_cross: bool,
) -> Instruction {
create_new_order_instruction(
&self.active_market_key.clone(),
&self.trader,
&self.base_mint,
&self.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, price: u64, side: Side, size: u64) -> Instruction {
self.get_limit_order_generic_ix(price, side, size, None, None, None, None)
}
#[allow(clippy::too_many_arguments)]
pub fn get_limit_order_generic_ix(
&self,
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>,
) -> Instruction {
let num_quote_ticks_per_base_unit = price / self.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);
create_new_order_instruction(
&self.active_market_key.clone(),
&self.trader,
&self.base_mint,
&self.quote_mint,
&OrderPacket::new_limit_order(
side,
num_quote_ticks_per_base_unit,
size,
self_trade_behavior,
match_limit,
client_order_id,
use_only_deposited_funds,
),
)
}
pub fn get_limit_order_ix_from_tick_price(
&self,
tick_price: u64,
side: Side,
size: u64,
client_order_id: u128,
) -> Instruction {
create_new_order_instruction(
&self.active_market_key.clone(),
&self.trader,
&self.base_mint,
&self.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, ids: Vec<FIFOOrderId>) -> Instruction {
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,
};
create_cancel_multiple_orders_by_id_instruction(
&self.active_market_key.clone(),
&self.trader,
&self.base_mint,
&self.quote_mint,
&cancel_multiple_orders,
)
}
pub fn get_cancel_up_to_ix(&self, tick_limit: Option<u64>, side: Side) -> Instruction {
let params = CancelUpToParams {
side,
tick_limit,
num_orders_to_search: None,
num_orders_to_cancel: None,
};
create_cancel_up_to_instruction(
&self.active_market_key.clone(),
&self.trader,
&self.base_mint,
&self.quote_mint,
¶ms,
)
}
pub fn get_cancel_all_ix(&self) -> Instruction {
create_cancel_all_orders_instruction(
&self.active_market_key.clone(),
&self.trader,
&self.base_mint,
&self.quote_mint,
)
}
}