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
use std::ops::Deref;

use anchor_lang::prelude::*;
use anchor_spl::token::{Mint, Token, TokenAccount};

use crate::{
    constants,
    events::{EventEmitter, TradeData, TradeEventRef},
    ops::{
        execution_fee::PayExecutionFeeOperation,
        market::{MarketTransferInOperation, MarketTransferOutOperation},
        order::{
            ExecuteOrderOperation, ProcessTransferOutOperation, RemovePosition,
            ShouldSendTradeEvent,
        },
    },
    states::{
        common::{
            action::{ActionExt, ActionSigner},
            swap::SwapActionParamsExt,
        },
        feature::ActionDisabledFlag,
        order::{Order, TransferOut},
        position::Position,
        user::UserHeader,
        Chainlink, Market, Oracle, Seed, Store, TokenMapHeader, TokenMapLoader,
    },
    utils::{internal, pubkey::DEFAULT_PUBKEY},
    CoreError,
};

/// The accounts definition for [`prepare_trade_event_buffer`](crate::gmsol_store::prepare_trade_event_buffer).
#[derive(Accounts)]
#[instruction(index: u16)]
pub struct PrepareTradeEventBuffer<'info> {
    /// Authority.
    #[account(mut)]
    pub authority: Signer<'info>,
    /// Store.
    pub store: AccountLoader<'info, Store>,
    /// Trade Event Buffer.
    #[account(
        init_if_needed,
        payer = authority,
        // The "zero-copy" init space of `TradeData` is used here.
        space = 8 + <TradeData as gmsol_utils::InitSpace>::INIT_SPACE,
        seeds = [TradeData::SEED, store.key().as_ref(), authority.key().as_ref(), &index.to_le_bytes()],
        bump,
    )]
    pub event: AccountLoader<'info, TradeData>,
    /// System Program.
    pub system_program: Program<'info, System>,
}

pub(crate) fn prepare_trade_event_buffer(
    ctx: Context<PrepareTradeEventBuffer>,
    _index: u16,
) -> Result<()> {
    match ctx.accounts.event.load_init() {
        Ok(mut event) => {
            require_keys_eq!(event.authority, DEFAULT_PUBKEY, CoreError::Internal);
            event.store = ctx.accounts.store.key();
            event.authority = ctx.accounts.authority.key();
        }
        Err(Error::AnchorError(err)) => {
            if err.error_code_number != ErrorCode::AccountDiscriminatorAlreadySet as u32 {
                return Err(Error::AnchorError(err));
            }
        }
        Err(err) => {
            return Err(err);
        }
    }
    ctx.accounts.event.exit(&crate::ID)?;
    require_keys_eq!(
        ctx.accounts.event.load()?.store,
        ctx.accounts.store.key(),
        CoreError::PermissionDenied
    );
    require_keys_eq!(
        ctx.accounts.event.load()?.authority,
        ctx.accounts.authority.key(),
        CoreError::PermissionDenied
    );
    Ok(())
}

pub(crate) fn get_pnl_token(
    position: &Option<AccountLoader<'_, Position>>,
    market: &Market,
) -> Result<Pubkey> {
    let is_long = position
        .as_ref()
        .ok_or_else(|| error!(CoreError::PositionIsRequired))?
        .load()?
        .try_is_long()?;
    if is_long {
        Ok(market.meta().long_token_mint)
    } else {
        Ok(market.meta.short_token_mint)
    }
}

pub(crate) fn check_delegation(account: &TokenAccount, target: Pubkey) -> Result<bool> {
    let is_matched = account
        .delegate
        .map(|delegate| delegate == target)
        .ok_or_else(|| error!(CoreError::NoDelegatedAuthorityIsSet))?;
    Ok(is_matched)
}

pub(crate) fn validated_recent_timestamp(config: &Store, timestamp: i64) -> Result<i64> {
    let recent_time_window = config.amount.recent_time_window;
    let expiration_time = timestamp.saturating_add_unsigned(recent_time_window);
    let clock = Clock::get()?;
    if timestamp <= clock.unix_timestamp && clock.unix_timestamp <= expiration_time {
        Ok(timestamp)
    } else {
        err!(CoreError::InvalidArgument)
    }
}

/// The accounts definition for [`execute_increase_or_swap_order`](crate::gmsol_store::execute_increase_or_swap_order) instruction.
///
/// Remaining accounts expected by this instruction:
///   - 0..M. `[]` M feed accounts, where M represents the total number of tokens in the
///     swap params.
///   - M..M+N. `[writable]` N market accounts, where N represents the total number of unique
///     markets excluding the current market in the swap params.
#[event_cpi]
#[derive(Accounts)]
#[instruction(recent_timestamp: i64)]
pub struct ExecuteIncreaseOrSwapOrder<'info> {
    /// Authority.
    pub authority: Signer<'info>,
    /// Store.
    #[account(mut, has_one = token_map)]
    pub store: AccountLoader<'info, Store>,
    /// Token Map.
    #[account(has_one = store)]
    pub token_map: AccountLoader<'info, TokenMapHeader>,
    /// Oracle buffer to use.
    #[account(mut, has_one = store)]
    pub oracle: AccountLoader<'info, Oracle>,
    /// Market.
    #[account(mut, has_one = store)]
    pub market: AccountLoader<'info, Market>,
    /// The owner of the order.
    /// CHECK: only used to receive fund.
    #[account(mut)]
    pub owner: UncheckedAccount<'info>,
    /// User Account.
    #[account(
        mut,
        constraint = user.load()?.is_initialized() @ CoreError::InvalidUserAccount,
        has_one = owner,
        has_one = store,
        seeds = [UserHeader::SEED, store.key().as_ref(), owner.key().as_ref()],
        bump = user.load()?.bump,
    )]
    pub user: AccountLoader<'info, UserHeader>,
    /// Order to execute.
    #[account(
        mut,
        constraint = order.load()?.header.store == store.key() @ CoreError::StoreMismatched,
        constraint = order.load()?.header.market == market.key() @ CoreError::MarketMismatched,
        constraint = order.load()?.header.owner == owner.key() @ CoreError::OwnerMismatched,
        constraint = order.load()?.params.position().copied() == position.as_ref().map(|p| p.key()) @ CoreError::PositionMismatched,
        constraint = order.load()?.tokens.initial_collateral.account() == initial_collateral_token_escrow.as_ref().map(|a| a.key()) @ CoreError::TokenAccountMismatched,
        constraint = order.load()?.tokens.final_output_token.account() == final_output_token_escrow.as_ref().map(|a| a.key()) @ CoreError::TokenAccountMismatched,
        constraint = order.load()?.tokens.long_token.account() == long_token_escrow.as_ref().map(|a| a.key())@ CoreError::TokenAccountMismatched,
        constraint = order.load()?.tokens.short_token.account() == short_token_escrow.as_ref().map(|a| a.key())@ CoreError::TokenAccountMismatched,
    )]
    pub order: AccountLoader<'info, Order>,
    #[account(
        mut,
        constraint = position.load()?.owner == order.load()?.header.owner,
        constraint = position.load()?.store == store.key(),
        seeds = [
            Position::SEED,
            store.key().as_ref(),
            order.load()?.header.owner.as_ref(),
            position.load()?.market_token.as_ref(),
            position.load()?.collateral_token.as_ref(),
            &[position.load()?.kind],
        ],
        bump = position.load()?.bump,
    )]
    pub position: Option<AccountLoader<'info, Position>>,
    /// Trade event buffer.
    #[account(mut, has_one = store, has_one = authority)]
    pub event: Option<AccountLoader<'info, TradeData>>,
    /// Initial collateral token.
    pub initial_collateral_token: Option<Box<Account<'info, Mint>>>,
    /// Final output token.
    pub final_output_token: Option<Box<Account<'info, Mint>>>,
    /// Long token.
    pub long_token: Option<Box<Account<'info, Mint>>>,
    /// Short token.
    pub short_token: Option<Box<Account<'info, Mint>>>,
    /// The escrow account for initial collateral tokens.
    #[account(
        mut,
        associated_token::mint = initial_collateral_token,
        associated_token::authority = order,
    )]
    pub initial_collateral_token_escrow: Option<Box<Account<'info, TokenAccount>>>,
    /// The escrow account for final output tokens.
    #[account(
        mut,
        associated_token::mint = final_output_token,
        associated_token::authority = order,
    )]
    pub final_output_token_escrow: Option<Box<Account<'info, TokenAccount>>>,
    /// The escrow account for long tokens.
    #[account(
        mut,
        associated_token::mint = long_token,
        associated_token::authority = order,
    )]
    pub long_token_escrow: Option<Box<Account<'info, TokenAccount>>>,
    /// The escrow account for short tokens.
    #[account(
        mut,
        associated_token::mint = short_token,
        associated_token::authority = order,
    )]
    pub short_token_escrow: Option<Box<Account<'info, TokenAccount>>>,
    /// Initial collatearl token vault.
    #[account(
        mut,
        token::mint = initial_collateral_token,
        token::authority = store,
        seeds = [
            constants::MARKET_VAULT_SEED,
            store.key().as_ref(),
            initial_collateral_token_vault.mint.as_ref(),
        ],
        bump,
    )]
    pub initial_collateral_token_vault: Option<Box<Account<'info, TokenAccount>>>,
    /// Final output token vault.
    #[account(
        mut,
        token::mint = final_output_token,
        token::authority = store,
        seeds = [
            constants::MARKET_VAULT_SEED,
            store.key().as_ref(),
            final_output_token_vault.mint.as_ref(),
        ],
        bump,
    )]
    pub final_output_token_vault: Option<Box<Account<'info, TokenAccount>>>,
    /// Long token vault.
    #[account(
        mut,
        token::mint = long_token,
        token::authority = store,
        seeds = [
            constants::MARKET_VAULT_SEED,
            store.key().as_ref(),
            long_token_vault.mint.as_ref(),
        ],
        bump,
    )]
    pub long_token_vault: Option<Box<Account<'info, TokenAccount>>>,
    /// Short token vault.
    #[account(
        mut,
        token::mint = short_token,
        token::authority = store,
        seeds = [
            constants::MARKET_VAULT_SEED,
            store.key().as_ref(),
            short_token_vault.mint.as_ref(),
        ],
        bump,
    )]
    pub short_token_vault: Option<Box<Account<'info, TokenAccount>>>,
    /// The token program.
    pub token_program: Program<'info, Token>,
    /// The system program.
    pub system_program: Program<'info, System>,
    /// Chainlink Program.
    pub chainlink_program: Option<Program<'info, Chainlink>>,
}

#[inline(never)]
pub(crate) fn unchecked_execute_increase_or_swap_order<'info>(
    mut ctx: Context<'_, '_, 'info, 'info, ExecuteIncreaseOrSwapOrder<'info>>,
    _recent_timestamp: i64,
    execution_fee: u64,
    throw_on_execution_error: bool,
) -> Result<()> {
    let accounts = &mut ctx.accounts;

    let kind = accounts.order.load()?.params().kind()?;

    // Validate the order kind.
    require!(
        kind.is_increase_position() || kind.is_swap(),
        CoreError::InvalidArgument
    );

    // Validate feature enabled.
    accounts.store.load()?.validate_feature_enabled(
        kind.try_into()
            .map_err(CoreError::from)
            .map_err(|err| error!(err))?,
        ActionDisabledFlag::Execute,
    )?;

    let remaining_accounts = ctx.remaining_accounts;
    let signer = accounts.order.load()?.signer();

    let event_authority = accounts.event_authority.clone();
    let event_emitter = EventEmitter::new(&event_authority, ctx.bumps.event_authority);

    accounts.transfer_tokens_in(&signer, remaining_accounts, &event_emitter)?;

    let (is_position_removed, transfer_out, should_send_trade_event) =
        accounts.perform_execution(remaining_accounts, throw_on_execution_error, &event_emitter)?;

    if transfer_out.executed() {
        accounts.order.load_mut()?.header.completed()?;
        accounts.process_transfer_out(remaining_accounts, &transfer_out, &event_emitter)?;
    } else {
        accounts.order.load_mut()?.header.cancelled()?;
        accounts.transfer_tokens_out(remaining_accounts, &event_emitter)?;
    }

    if should_send_trade_event {
        let event_loader = accounts.event.clone();
        let event = event_loader
            .as_ref()
            .ok_or_else(|| error!(CoreError::EventBufferNotProvided))?
            .load()?;
        let event = TradeEventRef::from(&*event);
        event_emitter.emit_cpi(&event)?;
    }

    if is_position_removed {
        msg!("[Position] the position is removed");
    }

    // It must be placed at the end to be executed correctly.
    ctx.accounts.pay_execution_fee(execution_fee)?;

    Ok(())
}

impl<'info> internal::Authentication<'info> for ExecuteIncreaseOrSwapOrder<'info> {
    fn authority(&self) -> &Signer<'info> {
        &self.authority
    }

    fn store(&self) -> &AccountLoader<'info, Store> {
        &self.store
    }
}

impl<'info> ExecuteIncreaseOrSwapOrder<'info> {
    #[inline(never)]
    fn transfer_tokens_in(
        &self,
        signer: &ActionSigner,
        remaining_accounts: &'info [AccountInfo<'info>],
        event_emitter: &EventEmitter<'_, 'info>,
    ) -> Result<()> {
        if let Some(escrow) = self.initial_collateral_token_escrow.as_ref() {
            let store = &self.store.key();
            let market = self
                .order
                .load()?
                .swap
                .find_and_unpack_first_market(store, true, remaining_accounts)?
                .unwrap_or(self.market.clone());
            let vault = self
                .initial_collateral_token_vault
                .as_ref()
                .ok_or_else(|| error!(CoreError::TokenAccountNotProvided))?;
            let amount = self.order.load()?.params.initial_collateral_delta_amount;
            MarketTransferInOperation::builder()
                .store(&self.store)
                .from_authority(self.order.to_account_info())
                .token_program(self.token_program.to_account_info())
                .signer_seeds(&signer.as_seeds())
                .market(&market)
                .from(escrow.to_account_info())
                .vault(vault)
                .amount(amount)
                .event_emitter(*event_emitter)
                .build()
                .execute()?;
        }
        Ok(())
    }

    #[inline(never)]
    fn transfer_tokens_out(
        &self,
        remaining_accounts: &'info [AccountInfo<'info>],
        event_emitter: &EventEmitter<'_, 'info>,
    ) -> Result<()> {
        if let Some(escrow) = self.initial_collateral_token_escrow.as_ref() {
            let store = &self.store.key();
            let market = self
                .order
                .load()?
                .swap
                .find_and_unpack_first_market(store, true, remaining_accounts)?
                .unwrap_or(self.market.clone());
            let vault = self
                .initial_collateral_token_vault
                .as_ref()
                .ok_or_else(|| error!(CoreError::TokenAccountNotProvided))?;
            let token = self
                .initial_collateral_token
                .as_ref()
                .ok_or_else(|| error!(CoreError::TokenMintNotProvided))?;
            let amount = self.order.load()?.params.initial_collateral_delta_amount;
            MarketTransferOutOperation::builder()
                .store(&self.store)
                .token_program(self.token_program.to_account_info())
                .market(&market)
                .to(escrow.to_account_info())
                .vault(vault.to_account_info())
                .amount(amount)
                .decimals(token.decimals)
                .token_mint(token.to_account_info())
                .event_emitter(*event_emitter)
                .build()
                .execute()?;
        }
        Ok(())
    }

    #[inline(never)]
    fn perform_execution(
        &mut self,
        remaining_accounts: &'info [AccountInfo<'info>],
        throw_on_execution_error: bool,
        event_emitter: &EventEmitter<'_, 'info>,
    ) -> Result<(RemovePosition, Box<TransferOut>, ShouldSendTradeEvent)> {
        // Note: We only need the tokens here, the feeds are not necessary.
        let feeds = self
            .order
            .load()?
            .swap
            .to_feeds(&self.token_map.load_token_map()?)
            .map_err(CoreError::from)?;
        let ops = ExecuteOrderOperation::builder()
            .store(&self.store)
            .market(&self.market)
            .owner(self.owner.to_account_info())
            .user(&self.user)
            .order(&self.order)
            .position(self.position.as_ref())
            .event(self.event.as_ref())
            .throw_on_execution_error(throw_on_execution_error)
            .executor(self.authority.to_account_info())
            .event_emitter(*event_emitter);

        self.oracle.load_mut()?.with_prices(
            &self.store,
            &self.token_map,
            &feeds.tokens,
            remaining_accounts,
            self.chainlink_program.as_ref(),
            |oracle, remaining_accounts| {
                ops.oracle(oracle)
                    .remaining_accounts(remaining_accounts)
                    .build()
                    .execute()
            },
        )
    }

    #[inline(never)]
    fn process_transfer_out(
        &self,
        remaining_accounts: &'info [AccountInfo<'info>],
        transfer_out: &TransferOut,
        event_emitter: &EventEmitter<'_, 'info>,
    ) -> Result<()> {
        let is_pnl_token_long_token = self.order.load()?.params.side()?.is_long();
        let final_output_market = self
            .order
            .load()?
            .swap
            .find_and_unpack_last_market(&self.store.key(), true, remaining_accounts)?
            .unwrap_or(self.market.clone());
        ProcessTransferOutOperation::builder()
            .token_program(self.token_program.to_account_info())
            .store(&self.store)
            .market(&self.market)
            .is_pnl_token_long_token(is_pnl_token_long_token)
            .final_output_token(self.final_output_token.as_deref())
            .final_output_market(&final_output_market)
            .final_output_token_account(
                self.final_output_token_escrow
                    .as_ref()
                    .map(|a| a.to_account_info()),
            )
            .final_output_token_vault(self.final_output_token_vault.as_deref())
            .long_token(self.long_token.as_deref())
            .long_token_account(self.long_token_escrow.as_ref().map(|a| a.to_account_info()))
            .long_token_vault(self.long_token_vault.as_deref())
            .short_token(self.short_token.as_deref())
            .short_token_account(
                self.short_token_escrow
                    .as_ref()
                    .map(|a| a.to_account_info()),
            )
            .short_token_vault(self.short_token_vault.as_deref())
            .claimable_long_token_account_for_user(None)
            .claimable_short_token_account_for_user(None)
            .claimable_pnl_token_account_for_holding(None)
            .transfer_out(transfer_out)
            .event_emitter(*event_emitter)
            .build()
            .execute()?;
        Ok(())
    }

    #[inline(never)]
    fn pay_execution_fee(&self, execution_fee: u64) -> Result<()> {
        let execution_lamports = self.order.load()?.execution_lamports(execution_fee);
        PayExecutionFeeOperation::builder()
            .payer(self.order.to_account_info())
            .receiver(self.authority.to_account_info())
            .execution_lamports(execution_lamports)
            .build()
            .execute()?;
        Ok(())
    }
}

/// The accounts definition for [`execute_decrease_order`](crate::gmsol_store::execute_decrease_order)
/// instruction.
///
/// Remaining accounts expected by this instruction:
///   - 0..M. `[]` M feed accounts, where M represents the total number of tokens in the
///     swap params.
///   - M..M+N. `[writable]` N market accounts, where N represents the total number of unique
///     markets excluding the current market in the swap params.
#[event_cpi]
#[derive(Accounts)]
#[instruction(recent_timestamp: i64)]
pub struct ExecuteDecreaseOrder<'info> {
    /// Authority.
    pub authority: Signer<'info>,
    /// Store.
    #[account(mut, has_one = token_map)]
    pub store: AccountLoader<'info, Store>,
    /// Token Map.
    #[account(has_one = store)]
    pub token_map: AccountLoader<'info, TokenMapHeader>,
    /// Oracle buffer to use.
    #[account(mut, has_one = store)]
    pub oracle: AccountLoader<'info, Oracle>,
    /// Market.
    #[account(mut, has_one = store)]
    pub market: AccountLoader<'info, Market>,
    /// The owner of the order.
    /// CHECK: only used to receive fund.
    #[account(mut)]
    pub owner: UncheckedAccount<'info>,
    /// User Account.
    #[account(
        mut,
        constraint = user.load()?.is_initialized() @ CoreError::InvalidUserAccount,
        has_one = owner,
        has_one = store,
        seeds = [UserHeader::SEED, store.key().as_ref(), owner.key().as_ref()],
        bump = user.load()?.bump,
    )]
    pub user: AccountLoader<'info, UserHeader>,
    /// Order to execute.
    #[account(
        mut,
        constraint = order.load()?.header.store == store.key() @ CoreError::StoreMismatched,
        constraint = order.load()?.header.market == market.key() @ CoreError::MarketMismatched,
        constraint = order.load()?.header.owner == owner.key() @ CoreError::OwnerMismatched,
        constraint = order.load()?.params.position().copied() == Some(position.key()) @ CoreError::PositionMismatched,
        constraint = order.load()?.tokens.final_output_token.account() == Some(final_output_token_escrow.key()) @ CoreError::TokenAccountMismatched,
        constraint = order.load()?.tokens.long_token.account() == Some(long_token_escrow.key()) @ CoreError::TokenAccountMismatched,
        constraint = order.load()?.tokens.short_token.account() == Some(short_token_escrow.key()) @ CoreError::TokenAccountMismatched,
    )]
    pub order: AccountLoader<'info, Order>,
    #[account(
        mut,
        constraint = position.load()?.owner == order.load()?.header.owner,
        constraint = position.load()?.store == store.key(),
        seeds = [
            Position::SEED,
            store.key().as_ref(),
            order.load()?.header.owner.as_ref(),
            position.load()?.market_token.as_ref(),
            position.load()?.collateral_token.as_ref(),
            &[position.load()?.kind],
        ],
        bump = position.load()?.bump,
    )]
    pub position: AccountLoader<'info, Position>,
    /// Trade event buffer.
    #[account(mut, has_one = store, has_one = authority)]
    pub event: AccountLoader<'info, TradeData>,
    /// Final output token.
    pub final_output_token: Box<Account<'info, Mint>>,
    /// Long token.
    pub long_token: Box<Account<'info, Mint>>,
    /// Short token.
    pub short_token: Box<Account<'info, Mint>>,
    /// The escrow account for final output tokens.
    #[account(
        mut,
        associated_token::mint = final_output_token,
        associated_token::authority = order,
    )]
    pub final_output_token_escrow: Box<Account<'info, TokenAccount>>,
    /// The escrow account for long tokens.
    #[account(
        mut,
        associated_token::mint = long_token,
        associated_token::authority = order,
    )]
    pub long_token_escrow: Box<Account<'info, TokenAccount>>,
    /// The escrow account for short tokens.
    #[account(
        mut,
        associated_token::mint = short_token,
        associated_token::authority = order,
    )]
    pub short_token_escrow: Box<Account<'info, TokenAccount>>,
    /// Final output token vault.
    #[account(
        mut,
        token::mint = final_output_token,
        token::authority = store,
        seeds = [
            constants::MARKET_VAULT_SEED,
            store.key().as_ref(),
            final_output_token_vault.mint.as_ref(),
        ],
        bump,
    )]
    pub final_output_token_vault: Box<Account<'info, TokenAccount>>,
    /// Long token vault.
    #[account(
        mut,
        token::mint = long_token,
        token::authority = store,
        seeds = [
            constants::MARKET_VAULT_SEED,
            store.key().as_ref(),
            long_token_vault.mint.as_ref(),
        ],
        bump,
    )]
    pub long_token_vault: Box<Account<'info, TokenAccount>>,
    /// Short token vault.
    #[account(
        mut,
        token::mint = short_token,
        token::authority = store,
        seeds = [
            constants::MARKET_VAULT_SEED,
            store.key().as_ref(),
            short_token_vault.mint.as_ref(),
        ],
        bump,
    )]
    pub short_token_vault: Box<Account<'info, TokenAccount>>,
    #[account(
        mut,
        token::mint = market.load()?.meta().long_token_mint,
        token::authority = store,
        constraint = check_delegation(&claimable_long_token_account_for_user, order.load()?.header.owner)?,
        seeds = [
            constants::CLAIMABLE_ACCOUNT_SEED,
            store.key().as_ref(),
            market.load()?.meta().long_token_mint.as_ref(),
            order.load()?.header.owner.as_ref(),
            &store.load()?.claimable_time_key(validated_recent_timestamp(store.load()?.deref(), recent_timestamp)?)?,
        ],
        bump,
    )]
    pub claimable_long_token_account_for_user: Box<Account<'info, TokenAccount>>,
    #[account(
        mut,
        token::mint = market.load()?.meta().short_token_mint,
        token::authority = store,
        constraint = check_delegation(&claimable_short_token_account_for_user, order.load()?.header.owner)?,
        seeds = [
            constants::CLAIMABLE_ACCOUNT_SEED,
            store.key().as_ref(),
            market.load()?.meta().short_token_mint.as_ref(),
            order.load()?.header.owner.as_ref(),
            &store.load()?.claimable_time_key(validated_recent_timestamp(store.load()?.deref(), recent_timestamp)?)?,
        ],
        bump,
    )]
    pub claimable_short_token_account_for_user: Box<Account<'info, TokenAccount>>,
    #[account(
        mut,
        token::mint = get_pnl_token(&Some(position.clone()), market.load()?.deref())?,
        token::authority = store,
        constraint = check_delegation(&claimable_pnl_token_account_for_holding, store.load()?.address.holding)?,
        seeds = [
            constants::CLAIMABLE_ACCOUNT_SEED,
            store.key().as_ref(),
            get_pnl_token(&Some(position.clone()), market.load()?.deref())?.as_ref(),
            store.load()?.address.holding.as_ref(),
            &store.load()?.claimable_time_key(validated_recent_timestamp(store.load()?.deref(), recent_timestamp)?)?,
        ],
        bump,
    )]
    pub claimable_pnl_token_account_for_holding: Box<Account<'info, TokenAccount>>,
    /// The token program.
    pub token_program: Program<'info, Token>,
    /// The system program.
    pub system_program: Program<'info, System>,
    /// Chainlink Program.
    pub chainlink_program: Option<Program<'info, Chainlink>>,
}

pub(crate) fn unchecked_execute_decrease_order<'info>(
    mut ctx: Context<'_, '_, 'info, 'info, ExecuteDecreaseOrder<'info>>,
    _recent_timestamp: i64,
    execution_fee: u64,
    throw_on_execution_error: bool,
) -> Result<()> {
    let accounts = &mut ctx.accounts;
    let remaining_accounts = ctx.remaining_accounts;

    let kind = accounts.order.load()?.params().kind()?;

    // Validate the order kind.
    require!(kind.is_decrease_position(), CoreError::InvalidArgument);

    // Validate feature enabled.
    accounts.store.load()?.validate_feature_enabled(
        kind.try_into()
            .map_err(CoreError::from)
            .map_err(|err| error!(err))?,
        ActionDisabledFlag::Execute,
    )?;

    let event_authority = accounts.event_authority.clone();
    let event_emitter = EventEmitter::new(&event_authority, ctx.bumps.event_authority);
    let (is_position_removed, transfer_out, should_send_trade_event) =
        accounts.perform_execution(remaining_accounts, throw_on_execution_error, &event_emitter)?;

    if transfer_out.executed() {
        accounts.order.load_mut()?.header.completed()?;
        accounts.process_transfer_out(remaining_accounts, &transfer_out, &event_emitter)?;
    } else {
        accounts.order.load_mut()?.header.cancelled()?;
    }

    if should_send_trade_event {
        let event_loader = accounts.event.clone();
        let event = event_loader.load()?;
        let event = TradeEventRef::from(&*event);
        event_emitter.emit_cpi(&event)?;
    }

    if is_position_removed {
        msg!("[Position] the position is removed");
    }

    // It must be placed at the end to be executed correctly.
    ctx.accounts.pay_execution_fee(execution_fee)?;

    Ok(())
}

impl<'info> internal::Authentication<'info> for ExecuteDecreaseOrder<'info> {
    fn authority(&self) -> &Signer<'info> {
        &self.authority
    }

    fn store(&self) -> &AccountLoader<'info, Store> {
        &self.store
    }
}

impl<'info> ExecuteDecreaseOrder<'info> {
    #[inline(never)]
    fn perform_execution(
        &mut self,
        remaining_accounts: &'info [AccountInfo<'info>],
        throw_on_execution_error: bool,
        event_emitter: &EventEmitter<'_, 'info>,
    ) -> Result<(RemovePosition, Box<TransferOut>, ShouldSendTradeEvent)> {
        // Note: We only need the tokens here, the feeds are not necessary.
        let feeds = self
            .order
            .load()?
            .swap
            .to_feeds(&self.token_map.load_token_map()?)
            .map_err(CoreError::from)?;
        let ops = ExecuteOrderOperation::builder()
            .store(&self.store)
            .market(&self.market)
            .owner(self.owner.to_account_info())
            .user(&self.user)
            .order(&self.order)
            .position(Some(&self.position))
            .event(Some(&self.event))
            .throw_on_execution_error(throw_on_execution_error)
            .executor(self.authority.to_account_info())
            .event_emitter(*event_emitter);

        self.oracle.load_mut()?.with_prices(
            &self.store,
            &self.token_map,
            &feeds.tokens,
            remaining_accounts,
            self.chainlink_program.as_ref(),
            #[inline(never)]
            |oracle, remaining_accounts| {
                ops.oracle(oracle)
                    .remaining_accounts(remaining_accounts)
                    .build()
                    .execute()
            },
        )
    }

    #[inline(never)]
    fn process_transfer_out(
        &self,
        remaining_accounts: &'info [AccountInfo<'info>],
        transfer_out: &TransferOut,
        event_emitter: &EventEmitter<'_, 'info>,
    ) -> Result<()> {
        let is_pnl_token_long_token = self.order.load()?.params.side()?.is_long();
        let final_output_market = self
            .order
            .load()?
            .swap
            .find_and_unpack_last_market(&self.store.key(), true, remaining_accounts)?
            .unwrap_or(self.market.clone());
        ProcessTransferOutOperation::builder()
            .token_program(self.token_program.to_account_info())
            .store(&self.store)
            .market(&self.market)
            .is_pnl_token_long_token(is_pnl_token_long_token)
            .final_output_market(&final_output_market)
            .final_output_token(Some(&self.final_output_token))
            .final_output_token_account(Some(self.final_output_token_escrow.to_account_info()))
            .final_output_token_vault(Some(&*self.final_output_token_vault))
            .long_token(Some(&self.long_token))
            .long_token_account(Some(self.long_token_escrow.to_account_info()))
            .long_token_vault(Some(&*self.long_token_vault))
            .short_token(Some(&self.short_token))
            .short_token_account(Some(self.short_token_escrow.to_account_info()))
            .short_token_vault(Some(&*self.short_token_vault))
            .claimable_long_token_account_for_user(Some(
                self.claimable_long_token_account_for_user.to_account_info(),
            ))
            .claimable_short_token_account_for_user(Some(
                self.claimable_short_token_account_for_user
                    .to_account_info(),
            ))
            .claimable_pnl_token_account_for_holding(Some(
                self.claimable_pnl_token_account_for_holding
                    .to_account_info(),
            ))
            .transfer_out(transfer_out)
            .event_emitter(*event_emitter)
            .build()
            .execute()?;
        Ok(())
    }

    #[inline(never)]
    fn pay_execution_fee(&self, execution_fee: u64) -> Result<()> {
        let execution_lamports = self.order.load()?.execution_lamports(execution_fee);
        PayExecutionFeeOperation::builder()
            .payer(self.order.to_account_info())
            .receiver(self.authority.to_account_info())
            .execution_lamports(execution_lamports)
            .build()
            .execute()?;
        Ok(())
    }
}