gmsol-store 0.5.0

GMX-Solana is an extension of GMX on the Solana blockchain.
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
use anchor_lang::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};
use gmsol_model::{
    action::decrease_position::{DecreasePositionReport, DecreasePositionSwapType},
    price::Price,
};
use gmsol_utils::InitSpace as _;

use crate::{
    events::{EventEmitter, GtUpdated, OrderRemoved},
    utils::pubkey::optional_address,
    CoreError,
};

use super::{
    common::{
        action::{Action, ActionHeader, ActionSigner, Closable},
        swap::SwapActionParams,
        token::TokenAndAccount,
    },
    user::UserHeader,
    Oracle, Seed, Store,
};

pub use gmsol_utils::order::{OrderKind, OrderSide};

/// Update Order Params.
#[derive(AnchorSerialize, AnchorDeserialize, Clone, InitSpace, Copy)]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct UpdateOrderParams {
    /// Size delta in USD.
    pub size_delta_value: Option<u128>,
    /// Acceptable price.
    pub acceptable_price: Option<u128>,
    /// Trigger price.
    pub trigger_price: Option<u128>,
    /// Min output amount.
    pub min_output: Option<u128>,
    /// Valid from this timestamp.
    pub valid_from_ts: Option<i64>,
}

impl UpdateOrderParams {
    /// Is empty.
    pub fn is_empty(&self) -> bool {
        self.size_delta_value.is_none()
            && self.acceptable_price.is_none()
            && self.trigger_price.is_none()
            && self.min_output.is_none()
            && self.valid_from_ts.is_none()
    }
}

/// Transfer Out.
#[zero_copy]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(BorshSerialize, BorshDeserialize, Default, InitSpace)]
pub struct TransferOut {
    /// Executed.
    executed: u8,
    #[cfg_attr(feature = "debug", debug(skip))]
    padding_0: [u8; 7],
    /// Final output token.
    pub final_output_token: u64,
    /// Secondary output token.
    pub secondary_output_token: u64,
    /// Long token.
    pub long_token: u64,
    /// Short token.
    pub short_token: u64,
    /// Long token amount for claimable account of user.
    pub long_token_for_claimable_account_of_user: u64,
    /// Short token amount for cliamable account of user.
    pub short_token_for_claimable_account_of_user: u64,
    /// Long token amount for claimable account of holding.
    pub long_token_for_claimable_account_of_holding: u64,
    /// Short token amount for claimable account of holding.
    pub short_token_for_claimable_account_of_holding: u64,
}

#[cfg(test)]
impl From<crate::events::EventTransferOut> for TransferOut {
    fn from(event: crate::events::EventTransferOut) -> Self {
        let crate::events::EventTransferOut {
            executed,
            padding_0,
            final_output_token,
            secondary_output_token,
            long_token,
            short_token,
            long_token_for_claimable_account_of_user,
            short_token_for_claimable_account_of_user,
            long_token_for_claimable_account_of_holding,
            short_token_for_claimable_account_of_holding,
        } = event;

        Self {
            executed,
            padding_0,
            final_output_token,
            secondary_output_token,
            long_token,
            short_token,
            long_token_for_claimable_account_of_user,
            short_token_for_claimable_account_of_user,
            long_token_for_claimable_account_of_holding,
            short_token_for_claimable_account_of_holding,
        }
    }
}

/// Recevier Kind.
pub enum CollateralReceiver {
    Collateral,
    ClaimableForHolding,
    ClaimableForUser,
}

impl TransferOut {
    const EXECUTED: u8 = u8::MAX;
    const NOT_EXECUTED: u8 = 0;

    /// Return whether the order is executed.
    pub fn executed(&self) -> bool {
        !self.executed == Self::NOT_EXECUTED
    }

    /// Return whether the output for user is empty.
    pub fn is_user_output_empty(&self) -> bool {
        self.final_output_token == 0
            && self.secondary_output_token == 0
            && self.long_token == 0
            && self.short_token == 0
            && self.long_token_for_claimable_account_of_user == 0
            && self.short_token_for_claimable_account_of_user == 0
    }

    pub(crate) fn set_executed(&mut self, executed: bool) -> &mut Self {
        self.executed = if executed {
            Self::EXECUTED
        } else {
            Self::NOT_EXECUTED
        };
        self
    }

    pub(crate) fn new_failed() -> Self {
        Self {
            executed: Self::NOT_EXECUTED,
            ..Default::default()
        }
    }

    pub(crate) fn total_long_token_amount(&self) -> Result<u64> {
        self.long_token
            .checked_add(self.long_token_for_claimable_account_of_user)
            .and_then(|a| a.checked_add(self.long_token_for_claimable_account_of_holding))
            .ok_or_else(|| error!(CoreError::TokenAmountOverflow))
    }

    pub(crate) fn total_short_token_amount(&self) -> Result<u64> {
        self.short_token
            .checked_add(self.short_token_for_claimable_account_of_user)
            .and_then(|a| a.checked_add(self.short_token_for_claimable_account_of_holding))
            .ok_or_else(|| error!(CoreError::TokenAmountOverflow))
    }

    pub(crate) fn transfer_out(&mut self, is_secondary: bool, amount: u64) -> Result<()> {
        if amount == 0 {
            return Ok(());
        }
        if is_secondary {
            self.secondary_output_token = self
                .secondary_output_token
                .checked_add(amount)
                .ok_or_else(|| error!(CoreError::TokenAmountOverflow))?;
        } else {
            self.final_output_token = self
                .final_output_token
                .checked_add(amount)
                .ok_or_else(|| error!(CoreError::TokenAmountOverflow))?;
        }
        Ok(())
    }

    pub(crate) fn transfer_out_funding_amounts(
        &mut self,
        long_amount: &u128,
        short_amount: &u128,
    ) -> Result<()> {
        self.transfer_out_collateral(
            true,
            CollateralReceiver::Collateral,
            (*long_amount)
                .try_into()
                .map_err(|_| error!(CoreError::TokenAmountOverflow))?,
        )?;
        self.transfer_out_collateral(
            false,
            CollateralReceiver::Collateral,
            (*short_amount)
                .try_into()
                .map_err(|_| error!(CoreError::TokenAmountOverflow))?,
        )?;
        Ok(())
    }

    pub(crate) fn process_claimable_collateral_for_decrease(
        &mut self,
        report: &DecreasePositionReport<u128, i128>,
    ) -> Result<()> {
        let for_holding = report.claimable_collateral_for_holding();
        require!(
            *for_holding.output_token_amount() == 0,
            CoreError::ClaimableCollateralForHoldingCannotBeInOutputTokens,
        );

        let is_output_token_long = report.is_output_token_long();
        let is_secondary_token_long = report.is_secondary_output_token_long();

        let secondary_amount = (*for_holding.secondary_output_token_amount())
            .try_into()
            .map_err(|_| error!(CoreError::TokenAmountOverflow))?;
        self.transfer_out_collateral(
            is_secondary_token_long,
            CollateralReceiver::ClaimableForHolding,
            secondary_amount,
        )?;

        let for_user = report.claimable_collateral_for_user();
        self.transfer_out_collateral(
            is_output_token_long,
            CollateralReceiver::ClaimableForUser,
            (*for_user.output_token_amount())
                .try_into()
                .map_err(|_| error!(CoreError::TokenAmountOverflow))?,
        )?;
        self.transfer_out_collateral(
            is_secondary_token_long,
            CollateralReceiver::ClaimableForUser,
            (*for_user.secondary_output_token_amount())
                .try_into()
                .map_err(|_| error!(CoreError::TokenAmountOverflow))?,
        )?;
        Ok(())
    }

    pub(crate) fn transfer_out_collateral(
        &mut self,
        is_long: bool,
        to: CollateralReceiver,
        amount: u64,
    ) -> Result<()> {
        if amount == 0 {
            return Ok(());
        }
        match to {
            CollateralReceiver::Collateral => {
                if is_long {
                    self.long_token = self
                        .long_token
                        .checked_add(amount)
                        .ok_or_else(|| error!(CoreError::TokenAmountOverflow))?;
                } else {
                    self.short_token = self
                        .short_token
                        .checked_add(amount)
                        .ok_or_else(|| error!(CoreError::TokenAmountOverflow))?;
                }
            }
            CollateralReceiver::ClaimableForHolding => {
                if is_long {
                    self.long_token_for_claimable_account_of_holding = self
                        .long_token_for_claimable_account_of_holding
                        .checked_add(amount)
                        .ok_or_else(|| error!(CoreError::TokenAmountOverflow))?;
                } else {
                    self.short_token_for_claimable_account_of_holding = self
                        .short_token_for_claimable_account_of_holding
                        .checked_add(amount)
                        .ok_or_else(|| error!(CoreError::TokenAmountOverflow))?;
                }
            }
            CollateralReceiver::ClaimableForUser => {
                if is_long {
                    self.long_token_for_claimable_account_of_user = self
                        .long_token_for_claimable_account_of_user
                        .checked_add(amount)
                        .ok_or_else(|| error!(CoreError::TokenAmountOverflow))?;
                } else {
                    self.short_token_for_claimable_account_of_user = self
                        .short_token_for_claimable_account_of_user
                        .checked_add(amount)
                        .ok_or_else(|| error!(CoreError::TokenAmountOverflow))?;
                }
            }
        }
        Ok(())
    }
}

/// Order.
#[account(zero_copy)]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Order {
    /// Action header.
    pub(crate) header: ActionHeader,
    /// Market token.
    pub(crate) market_token: Pubkey,
    /// Token accounts.
    pub(crate) tokens: OrderTokenAccounts,
    /// Swap params.
    pub(crate) swap: SwapActionParams,
    #[cfg_attr(feature = "debug", debug(skip))]
    padding_0: [u8; 4],
    /// Order params.
    pub(crate) params: OrderActionParams,
    pub(crate) gt_reward: u64,
    #[cfg_attr(feature = "debug", debug(skip))]
    padding_1: [u8; 8],
    #[cfg_attr(feature = "debug", debug(skip))]
    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
    reserved: [u8; 128],
}

impl Seed for Order {
    /// Seed.
    const SEED: &'static [u8] = b"order";
}

impl gmsol_utils::InitSpace for Order {
    const INIT_SPACE: usize = std::mem::size_of::<Self>();
}

impl Action for Order {
    const MIN_EXECUTION_LAMPORTS: u64 = 300_000;

    fn header(&self) -> &ActionHeader {
        &self.header
    }
}

impl Closable for Order {
    type ClosedEvent = OrderRemoved;

    fn to_closed_event(&self, address: &Pubkey, reason: &str) -> Result<Self::ClosedEvent> {
        OrderRemoved::new(
            self.header.id,
            self.header.store,
            *address,
            self.params.kind()?,
            self.market_token,
            self.header.owner,
            self.header.action_state()?,
            reason,
        )
    }
}

impl Order {
    /// Get rent for position cut.
    pub(crate) fn position_cut_rent(is_pure: bool, include_execution_fee: bool) -> Result<u64> {
        use anchor_spl::token::TokenAccount;

        let rent = Rent::get()?;
        let amount = rent.minimum_balance(Self::INIT_SPACE + 8)
            + rent.minimum_balance(TokenAccount::LEN) * if is_pure { 1 } else { 2 }
            + if include_execution_fee {
                Self::MIN_EXECUTION_LAMPORTS
            } else {
                0
            };

        Ok(amount)
    }

    /// Get signer.
    pub fn signer(&self) -> ActionSigner {
        self.header.signer(Self::SEED)
    }

    /// Validate that current timestamp >= `valid_from_ts`.
    pub fn validate_valid_from_ts(&self) -> Result<()> {
        if self.params.kind()?.is_market() {
            return Ok(());
        }
        require_gte!(Clock::get()?.unix_timestamp, self.params.valid_from_ts);
        Ok(())
    }

    /// Validate trigger price.
    pub fn validate_trigger_price(&self, index_price: &Price<u128>) -> Result<()> {
        let params = &self.params;
        let kind = params.kind()?;
        let is_long = params.side()?.is_long();
        let trigger_price = &params.trigger_price;
        match kind {
            OrderKind::LimitIncrease => {
                if is_long {
                    require_gte!(
                        trigger_price,
                        index_price.pick_price(true),
                        CoreError::InvalidTriggerPrice
                    );
                } else {
                    require_gte!(
                        index_price.pick_price(false),
                        trigger_price,
                        CoreError::InvalidTriggerPrice
                    );
                }
            }
            OrderKind::LimitDecrease => {
                if is_long {
                    require_gte!(
                        index_price.pick_price(false),
                        trigger_price,
                        CoreError::InvalidTriggerPrice
                    );
                } else {
                    require_gte!(
                        trigger_price,
                        index_price.pick_price(true),
                        CoreError::InvalidTriggerPrice
                    );
                }
            }
            OrderKind::StopLossDecrease => {
                if is_long {
                    require_gte!(
                        trigger_price,
                        index_price.pick_price(false),
                        CoreError::InvalidTriggerPrice
                    );
                } else {
                    require_gte!(
                        index_price.pick_price(true),
                        trigger_price,
                        CoreError::InvalidTriggerPrice
                    );
                }
            }
            OrderKind::LimitSwap => {
                // NOTE: For limit swap orders, the trigger price can be substituted by the min output amount,
                // so validatoin is not required. In fact, we should prohibit the creation of limit swap orders
                // with a trigger price.
            }
            OrderKind::MarketSwap
            | OrderKind::MarketIncrease
            | OrderKind::MarketDecrease
            | OrderKind::Liquidation
            | OrderKind::AutoDeleveraging => {}
            _ => return err!(CoreError::UnknownOrderKind),
        }

        Ok(())
    }

    /// Validate output amount.
    pub fn validate_output_amount(&self, output_amount: u128) -> Result<()> {
        require_gte!(
            output_amount,
            self.params.min_output,
            CoreError::InsufficientOutputAmount
        );
        Ok(())
    }

    #[inline(never)]
    pub(crate) fn validate_decrease_output_amounts(
        &self,
        oracle: &Oracle,
        output_token: &Pubkey,
        output_amount: u64,
        secondary_output_token: &Pubkey,
        secondary_output_amount: u64,
    ) -> Result<()> {
        let mut total = 0u128;
        {
            let price = oracle.get_primary_price(output_token, false)?.min;
            let output_value = u128::from(output_amount).saturating_mul(price);
            total = total.saturating_add(output_value);
        }
        {
            let price = oracle.get_primary_price(secondary_output_token, false)?.min;
            let output_value = u128::from(secondary_output_amount).saturating_mul(price);
            total = total.saturating_add(output_value);
        }

        // We use the `min_output_amount` as min output value.
        self.validate_output_amount(total)?;
        Ok(())
    }

    /// Get secondary output token (pnl token).
    pub fn secondary_output_token(&self) -> Result<Pubkey> {
        if self.params.side()?.is_long() {
            self.tokens.long_token.token()
        } else {
            self.tokens.short_token.token()
        }
        .ok_or_else(|| error!(CoreError::MissingPoolTokens))
    }

    /// Get order params.
    pub fn params(&self) -> &OrderActionParams {
        &self.params
    }

    /// Get swap params.
    pub fn swap(&self) -> &SwapActionParams {
        &self.swap
    }

    /// Get market token.
    pub fn market_token(&self) -> &Pubkey {
        &self.market_token
    }

    /// Get token accounts.
    pub fn tokens(&self) -> &OrderTokenAccounts {
        &self.tokens
    }

    /// Process GT.
    /// CHECK: the order must have been successfully executed.
    #[inline(never)]
    pub(crate) fn unchecked_process_gt(
        &mut self,
        store: &mut Store,
        user: &mut UserHeader,
        paid_fee_value: u128,
        event_emitter: &EventEmitter,
    ) -> Result<()> {
        if paid_fee_value == 0 {
            msg!("[GT] GT is not minted unless an order fee is paid");
            return Ok(());
        }

        // Ignore the overflowed value.
        let next_paid_fee_value = user.gt.paid_fee_value().saturating_add(paid_fee_value);
        let minted_fee_value = user.gt.minted_fee_value();

        require_gte!(
            next_paid_fee_value,
            minted_fee_value,
            CoreError::InvalidUserAccount
        );

        let value_to_mint_for = next_paid_fee_value.saturating_sub(minted_fee_value);

        let (minted, delta_minted_value, minting_cost) =
            store.gt().get_mint_amount(value_to_mint_for)?;

        let next_minted_value = minted_fee_value
            .checked_add(delta_minted_value)
            .ok_or_else(|| error!(CoreError::ValueOverflow))?;

        store.gt_mut().mint_to(user, minted)?;

        self.gt_reward = minted;
        user.gt.paid_fee_value = next_paid_fee_value;
        user.gt.minted_fee_value = next_minted_value;

        event_emitter
            .emit_cpi(&GtUpdated::minted(
                minting_cost,
                minted,
                store.gt(),
                Some(user),
            ))
            .expect("failed to emit GT minted event");

        Ok(())
    }

    pub(crate) fn update(&mut self, id: u64, params: &UpdateOrderParams) -> Result<()> {
        let current = &mut self.params;
        require!(current.is_updatable()?, CoreError::InvalidArgument);
        require!(!params.is_empty(), CoreError::InvalidArgument);

        self.header.id = id;

        if let Some(size_delta_value) = params.size_delta_value {
            current.size_delta_value = size_delta_value;
        }

        if let Some(acceptable_price) = params.acceptable_price {
            current.acceptable_price = acceptable_price;
        }

        if let Some(trigger_price) = params.trigger_price {
            current.trigger_price = trigger_price;
        }

        if let Some(min_output) = params.min_output {
            if matches!(current.kind()?, OrderKind::LimitSwap) {
                require_neq!(min_output, 0, CoreError::InvalidArgument);
            }
            current.min_output = min_output;
        }

        if let Some(ts) = params.valid_from_ts {
            current.valid_from_ts = ts;
        }

        self.header.updated()?;

        Ok(())
    }
}

/// Token accounts for Order.
#[zero_copy]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OrderTokenAccounts {
    /// Initial collateral.
    pub(crate) initial_collateral: TokenAndAccount,
    /// Final output token.
    pub(crate) final_output_token: TokenAndAccount,
    /// Long token.
    pub(crate) long_token: TokenAndAccount,
    /// Short token.
    pub(crate) short_token: TokenAndAccount,
    #[cfg_attr(feature = "debug", debug(skip))]
    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
    reserved: [u8; 128],
}

impl OrderTokenAccounts {
    /// Get initial collateral token info.
    ///
    /// Only available for increase and swap orders.
    pub fn initial_collateral(&self) -> &TokenAndAccount {
        &self.initial_collateral
    }

    /// Get final output token info.
    ///
    /// Only available for decrease and swap orders.
    pub fn final_output_token(&self) -> &TokenAndAccount {
        &self.final_output_token
    }

    /// Get long token info.
    ///
    /// Only available for increase and decrease orders.
    pub fn long_token(&self) -> &TokenAndAccount {
        &self.long_token
    }

    /// Get short token info.
    ///
    //// Only available for increase and decrease orders.
    pub fn short_token(&self) -> &TokenAndAccount {
        &self.short_token
    }
}

/// Order params.
#[zero_copy]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OrderActionParams {
    /// Kind.
    kind: u8,
    /// Order side.
    side: u8,
    /// Decrease position swap type.
    decrease_position_swap_type: u8,
    #[cfg_attr(feature = "debug", debug(skip))]
    padding_1: [u8; 5],
    /// Collateral/Output token.
    collateral_token: Pubkey,
    /// Position address.
    position: Pubkey,
    /// Initial collateral delta amount.
    pub(crate) initial_collateral_delta_amount: u64,
    /// Size delta value.
    pub(crate) size_delta_value: u128,
    /// Min output amount or value.
    /// - Used as amount for swap orders.
    /// - Used as value for decrease position orders.
    min_output: u128,
    /// Trigger price (in unit price).
    pub(crate) trigger_price: u128,
    /// Acceptable price (in unit price).
    pub(crate) acceptable_price: u128,
    pub(crate) valid_from_ts: i64,
    #[cfg_attr(feature = "debug", debug(skip))]
    padding_2: [u8; 8],
    #[cfg_attr(feature = "debug", debug(skip))]
    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
    reserved: [u8; 64],
}

impl OrderActionParams {
    const DEFAULT_VALID_FROM_TS: i64 = 0;

    pub(crate) fn init_swap(
        &mut self,
        kind: OrderKind,
        collateral_token: Pubkey,
        swap_in_amount: u64,
        min_output: Option<u128>,
        valid_from_ts: Option<i64>,
    ) -> Result<()> {
        self.kind = kind.into();
        self.collateral_token = collateral_token;
        self.initial_collateral_delta_amount = swap_in_amount;
        match kind {
            OrderKind::MarketSwap => {
                self.min_output = min_output.unwrap_or(0);
                self.valid_from_ts = Self::DEFAULT_VALID_FROM_TS;
            }
            OrderKind::LimitSwap => {
                let Some(min_output) = min_output else {
                    return err!(CoreError::InvalidMinOutputAmount);
                };
                require!(min_output != 0, CoreError::Internal);
                self.min_output = min_output;

                self.valid_from_ts = valid_from_ts.unwrap_or(Self::DEFAULT_VALID_FROM_TS);
            }
            _ => {
                return err!(CoreError::Internal);
            }
        }
        Ok(())
    }

    #[allow(clippy::too_many_arguments)]
    pub(crate) fn init_increase(
        &mut self,
        is_long: bool,
        kind: OrderKind,
        position: Pubkey,
        collateral_token: Pubkey,
        initial_collateral_delta_amount: u64,
        size_delta_value: u128,
        trigger_price: Option<u128>,
        acceptable_price: Option<u128>,
        min_output: Option<u128>,
        valid_from_ts: Option<i64>,
    ) -> Result<()> {
        self.kind = kind.into();
        self.side = if is_long {
            OrderSide::Long
        } else {
            OrderSide::Short
        }
        .into();
        self.collateral_token = collateral_token;
        self.initial_collateral_delta_amount = initial_collateral_delta_amount;
        self.size_delta_value = size_delta_value;
        self.position = position;
        self.min_output = min_output.unwrap_or(0);
        match acceptable_price {
            Some(price) => {
                self.acceptable_price = price;
            }
            None => {
                if is_long {
                    self.acceptable_price = u128::MAX;
                } else {
                    self.acceptable_price = u128::MIN;
                }
            }
        }
        match kind {
            OrderKind::MarketIncrease => {
                require!(trigger_price.is_none(), CoreError::InvalidTriggerPrice);
                self.valid_from_ts = Self::DEFAULT_VALID_FROM_TS;
            }
            OrderKind::LimitIncrease => {
                let Some(price) = trigger_price else {
                    return err!(CoreError::InvalidTriggerPrice);
                };
                self.trigger_price = price;
                self.valid_from_ts = valid_from_ts.unwrap_or(Self::DEFAULT_VALID_FROM_TS);
            }
            _ => {
                return err!(CoreError::Internal);
            }
        }
        Ok(())
    }

    #[allow(clippy::too_many_arguments)]
    pub(crate) fn init_decrease(
        &mut self,
        is_long: bool,
        kind: OrderKind,
        position: Pubkey,
        collateral_token: Pubkey,
        initial_collateral_delta_amount: u64,
        size_delta_value: u128,
        trigger_price: Option<u128>,
        acceptable_price: Option<u128>,
        min_output: Option<u128>,
        swap_type: DecreasePositionSwapType,
        valid_from_ts: Option<i64>,
    ) -> Result<()> {
        self.kind = kind.into();
        self.side = if is_long {
            OrderSide::Long
        } else {
            OrderSide::Short
        }
        .into();
        self.decrease_position_swap_type = swap_type.into();
        self.position = position;
        self.collateral_token = collateral_token;
        self.initial_collateral_delta_amount = initial_collateral_delta_amount;
        self.size_delta_value = size_delta_value;
        self.min_output = min_output.unwrap_or(0);
        match acceptable_price {
            Some(price) => {
                self.acceptable_price = price;
            }
            None => {
                if is_long {
                    self.acceptable_price = u128::MIN;
                } else {
                    self.acceptable_price = u128::MAX;
                }
            }
        }
        match kind {
            OrderKind::MarketDecrease | OrderKind::Liquidation | OrderKind::AutoDeleveraging => {
                require!(trigger_price.is_none(), CoreError::InvalidTriggerPrice);
                self.valid_from_ts = Self::DEFAULT_VALID_FROM_TS;
            }
            OrderKind::LimitDecrease | OrderKind::StopLossDecrease => {
                let Some(price) = trigger_price else {
                    return err!(CoreError::InvalidTriggerPrice);
                };
                self.trigger_price = price;
                self.valid_from_ts = valid_from_ts.unwrap_or(Self::DEFAULT_VALID_FROM_TS);
            }
            _ => {
                return err!(CoreError::Internal);
            }
        }
        Ok(())
    }

    /// Get order kind.
    pub fn kind(&self) -> Result<OrderKind> {
        self.kind
            .try_into()
            .map_err(|_| error!(CoreError::UnknownOrderKind))
    }

    /// Get decrease position swap type.
    pub fn decrease_position_swap_type(&self) -> Result<DecreasePositionSwapType> {
        let ty = self
            .decrease_position_swap_type
            .try_into()
            .map_err(|_| CoreError::UnknownDecreasePositionSwapType)?;
        Ok(ty)
    }

    /// Return whether the order is updatable.
    pub fn is_updatable(&self) -> Result<bool> {
        let kind = self.kind()?;
        Ok(matches!(
            kind,
            OrderKind::LimitSwap
                | OrderKind::LimitIncrease
                | OrderKind::LimitDecrease
                | OrderKind::StopLossDecrease
        ))
    }

    /// Get order side.
    pub fn side(&self) -> Result<OrderSide> {
        self.side
            .try_into()
            .map_err(|_| error!(CoreError::UnknownOrderSide))
    }

    /// Get position address.
    pub fn position(&self) -> Option<&Pubkey> {
        optional_address(&self.position)
    }

    /// Get initial collateral delta amount.
    pub fn amount(&self) -> u64 {
        self.initial_collateral_delta_amount
    }

    /// Get size delta in value.
    pub fn size(&self) -> u128 {
        self.size_delta_value
    }

    /// Get accetable price (unit price).
    pub fn acceptable_price(&self) -> u128 {
        self.acceptable_price
    }

    /// Get trigger price (unit price).
    pub fn trigger_price(&self) -> u128 {
        self.trigger_price
    }

    /// Get min output.
    pub fn min_output(&self) -> u128 {
        self.min_output
    }

    /// Get valid from ts.
    pub fn valid_from_ts(&self) -> i64 {
        self.valid_from_ts
    }
}