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
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
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
use std::{
    borrow::{Borrow, BorrowMut},
    collections::BTreeSet,
};

use anchor_lang::prelude::*;
use anchor_spl::token_interface;
use gmsol_utils::{glv::MAX_GLV_MARKET_FLAGS, InitSpace};

use crate::{
    constants,
    events::{GlvDepositRemoved, GlvWithdrawalRemoved, ShiftRemoved},
    states::{Deposit, Market},
    CoreError,
};

use super::{
    common::{
        action::{Action, ActionHeader, Closable},
        swap::{unpack_markets, HasSwapParams, SwapActionParams},
        token::{TokenAndAccount, TokensCollector},
    },
    deposit::DepositActionParams,
    shift, Seed, Shift, TokenMapAccess,
};

pub use gmsol_utils::glv::GlvMarketFlag;

const MAX_ALLOWED_NUMBER_OF_MARKETS: usize = 96;

/// Glv.
#[account(zero_copy)]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
pub struct Glv {
    version: u8,
    /// Bump seed.
    pub(crate) bump: u8,
    bump_bytes: [u8; 1],
    #[cfg_attr(feature = "debug", debug(skip))]
    padding_0: [u8; 3],
    /// Index.
    pub(crate) index: u16,
    /// Store.
    pub store: Pubkey,
    pub(crate) glv_token: Pubkey,
    pub(crate) long_token: Pubkey,
    pub(crate) short_token: Pubkey,
    shift_last_executed_at: i64,
    pub(crate) min_tokens_for_first_deposit: u64,
    shift_min_interval_secs: u32,
    #[cfg_attr(feature = "debug", debug(skip))]
    padding_1: [u8; 4],
    shift_max_price_impact_factor: u128,
    shift_min_value: u128,
    #[cfg_attr(feature = "debug", debug(skip))]
    reserved: [u8; 256],
    /// Market config map with market token addresses as keys.
    markets: GlvMarkets,
}

gmsol_utils::fixed_map!(
    GlvMarkets,
    Pubkey,
    crate::utils::pubkey::to_bytes,
    GlvMarketConfig,
    MAX_ALLOWED_NUMBER_OF_MARKETS,
    12
);

impl Default for Glv {
    fn default() -> Self {
        use bytemuck::Zeroable;

        Self::zeroed()
    }
}

impl Seed for Glv {
    const SEED: &'static [u8] = b"glv";
}

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

impl Glv {
    /// GLV token seed.
    pub const GLV_TOKEN_SEED: &'static [u8] = b"glv_token";

    /// Max allowed number of markets.
    pub const MAX_ALLOWED_NUMBER_OF_MARKETS: usize = MAX_ALLOWED_NUMBER_OF_MARKETS;

    /// Find GLV token address.
    pub fn find_glv_token_pda(store: &Pubkey, index: u16, program_id: &Pubkey) -> (Pubkey, u8) {
        Pubkey::find_program_address(
            &[Self::GLV_TOKEN_SEED, store.as_ref(), &index.to_le_bytes()],
            program_id,
        )
    }

    /// Find GLV address.
    pub fn find_glv_pda(glv_token: &Pubkey, program_id: &Pubkey) -> (Pubkey, u8) {
        Pubkey::find_program_address(&[Self::SEED, glv_token.as_ref()], program_id)
    }

    pub(crate) fn signer_seeds(&self) -> [&[u8]; 3] {
        [Self::SEED, self.glv_token().as_ref(), &self.bump_bytes]
    }

    pub(crate) fn vec_signer_seeds(&self) -> Vec<Vec<u8>> {
        vec![
            Self::SEED.to_vec(),
            self.glv_token.to_bytes().to_vec(),
            self.bump_bytes.to_vec(),
        ]
    }

    /// Initialize the [`Glv`] account.
    ///
    /// # CHECK
    /// - The [`Glv`] account must be uninitialized.
    /// - The `bump` must be the bump deriving the address of the [`Glv`] account.
    /// - The `glv_token` must be used to derive the address of the [`Glv`] account.
    /// - The market tokens must be valid and unique, and their corresponding markets
    ///   must use the given tokens as long token and short token.
    /// - The `store` must be the address of the store owning the corresponding markets.
    ///
    /// # Errors
    /// - The `glv_token` address must be derived from [`GLV_TOKEN_SEED`](Self::GLV_TOKEN_SEED), `store` and `index`.
    /// - The total number of the market tokens must not exceed the max allowed number of markets.
    pub(crate) fn unchecked_init(
        &mut self,
        bump: u8,
        index: u16,
        store: &Pubkey,
        glv_token: &Pubkey,
        long_token: &Pubkey,
        short_token: &Pubkey,
        market_tokens: &BTreeSet<Pubkey>,
    ) -> Result<()> {
        let expected_glv_token = Self::find_glv_token_pda(store, index, &crate::ID).0;
        require_keys_eq!(expected_glv_token, *glv_token, CoreError::InvalidArgument);

        self.version = 0;
        self.bump = bump;
        self.bump_bytes = [bump];
        self.index = index;
        self.store = *store;
        self.glv_token = *glv_token;
        self.long_token = *long_token;
        self.short_token = *short_token;

        self.shift_min_interval_secs = constants::DEFAULT_GLV_MIN_SHIFT_INTERVAL_SECS;
        self.shift_max_price_impact_factor = constants::DEFAULT_GLV_MAX_SHIFT_PRICE_IMPACT_FACTOR;
        self.shift_min_value = constants::DEFAULT_GLV_MIN_SHIFT_VALUE;

        require_gte!(
            Self::MAX_ALLOWED_NUMBER_OF_MARKETS,
            market_tokens.len(),
            CoreError::ExceedMaxLengthLimit
        );

        for market_token in market_tokens {
            self.markets
                .insert_with_options(market_token, Default::default(), true)?;
        }
        Ok(())
    }

    pub(crate) fn process_and_validate_markets_for_init<'info>(
        markets: &'info [AccountInfo<'info>],
        store: &Pubkey,
    ) -> Result<(Pubkey, Pubkey, BTreeSet<Pubkey>)> {
        let mut tokens = None;

        let mut market_tokens = BTreeSet::default();
        for market in unpack_markets(markets) {
            let market = market?;
            let market = market.load()?;
            let meta = market.validated_meta(store)?;
            match &mut tokens {
                Some((long_token, short_token)) => {
                    require_keys_eq!(
                        *long_token,
                        meta.long_token_mint,
                        CoreError::TokenMintMismatched
                    );
                    require_keys_eq!(
                        *short_token,
                        meta.short_token_mint,
                        CoreError::TokenMintMismatched
                    );
                }
                none => {
                    *none = Some((meta.long_token_mint, meta.short_token_mint));
                }
            }
            require!(
                market_tokens.insert(meta.market_token_mint),
                CoreError::InvalidArgument
            );
        }

        if let Some((long_token, short_token)) = tokens {
            require_eq!(markets.len(), market_tokens.len(), CoreError::Internal);
            Ok((long_token, short_token, market_tokens))
        } else {
            err!(CoreError::InvalidArgument)
        }
    }

    /// Get the version of the [`Glv`] account format.
    pub fn version(&self) -> u8 {
        self.version
    }

    /// Get the index of the glv token.
    pub fn index(&self) -> u16 {
        self.index
    }

    /// Get the store address.
    pub fn store(&self) -> &Pubkey {
        &self.store
    }

    /// Get the GLV token address.
    pub fn glv_token(&self) -> &Pubkey {
        &self.glv_token
    }

    /// Get the long token address.
    pub fn long_token(&self) -> &Pubkey {
        &self.long_token
    }

    /// Get the short token address.
    pub fn short_token(&self) -> &Pubkey {
        &self.short_token
    }

    pub(crate) fn update_config(&mut self, params: &UpdateGlvParams) -> Result<()> {
        if let Some(amount) = params.min_tokens_for_first_deposit {
            require_neq!(
                self.min_tokens_for_first_deposit,
                amount,
                CoreError::PreconditionsAreNotMet
            );
            self.min_tokens_for_first_deposit = amount;
        }

        if let Some(secs) = params.shift_min_interval_secs {
            require_neq!(
                self.shift_min_interval_secs,
                secs,
                CoreError::PreconditionsAreNotMet
            );
            self.shift_min_interval_secs = secs;
        }

        if let Some(factor) = params.shift_max_price_impact_factor {
            require_neq!(
                self.shift_max_price_impact_factor,
                factor,
                CoreError::PreconditionsAreNotMet
            );
            self.shift_max_price_impact_factor = factor;
        }

        if let Some(value) = params.shift_min_value {
            require_neq!(
                self.shift_min_value,
                value,
                CoreError::PreconditionsAreNotMet
            );
            self.shift_min_value = value;
        }

        Ok(())
    }

    pub(crate) fn insert_market(&mut self, store: &Pubkey, market: &Market) -> Result<()> {
        let meta = market.validated_meta(store)?;

        require_keys_eq!(
            meta.long_token_mint,
            self.long_token,
            CoreError::InvalidArgument
        );

        require_keys_eq!(
            meta.short_token_mint,
            self.short_token,
            CoreError::InvalidArgument
        );

        let market_token = meta.market_token_mint;
        self.markets
            .insert_with_options(&market_token, GlvMarketConfig::default(), true)?;

        Ok(())
    }

    /// Remove market from the GLV.
    ///
    /// # CHECK
    /// - The balance of the vault must be zero.
    pub(crate) fn unchecked_remove_market(&mut self, market_token: &Pubkey) -> Result<()> {
        let config = self
            .market_config(market_token)
            .ok_or_else(|| error!(CoreError::NotFound))?;

        require!(
            !config.get_flag(GlvMarketFlag::IsDepositAllowed),
            CoreError::PreconditionsAreNotMet
        );

        require!(
            self.markets.remove(market_token).is_some(),
            CoreError::Internal
        );

        Ok(())
    }

    /// Get all market tokens.
    pub fn market_tokens(&self) -> impl Iterator<Item = Pubkey> + '_ {
        self.markets
            .entries()
            .map(|(key, _)| Pubkey::new_from_array(*key))
    }

    /// Get the total number of markets.
    pub fn num_markets(&self) -> usize {
        self.markets.len()
    }

    /// Return whether the given market token is contained in this GLV.
    pub fn contains(&self, market_token: &Pubkey) -> bool {
        self.markets.get(market_token).is_some()
    }

    /// Get [`GlvMarketConfig`] for the given market.
    pub fn market_config(&self, market_token: &Pubkey) -> Option<&GlvMarketConfig> {
        self.markets.get(market_token)
    }

    pub(crate) fn update_market_config(
        &mut self,
        market_token: &Pubkey,
        max_amount: Option<u64>,
        max_value: Option<u128>,
    ) -> Result<()> {
        let config = self
            .markets
            .get_mut(market_token)
            .ok_or_else(|| error!(CoreError::NotFound))?;
        if let Some(amount) = max_amount {
            config.max_amount = amount;
        }
        if let Some(value) = max_value {
            config.max_value = value;
        }
        Ok(())
    }

    pub(crate) fn toggle_market_config_flag(
        &mut self,
        market_token: &Pubkey,
        flag: GlvMarketFlag,
        enable: bool,
    ) -> Result<bool> {
        self.markets
            .get_mut(market_token)
            .ok_or_else(|| error!(CoreError::NotFound))?
            .toggle_flag(flag, enable)
    }

    /// Create a new [`TokensCollector`].
    pub fn tokens_collector(&self, action: Option<&impl HasSwapParams>) -> TokensCollector {
        TokensCollector::new(action, self.num_markets())
    }

    /// Split remaining accounts.
    pub(crate) fn validate_and_split_remaining_accounts<'info>(
        &self,
        store: &Pubkey,
        remaining_accounts: &'info [AccountInfo<'info>],
        action: Option<&impl HasSwapParams>,
        token_map: &impl TokenMapAccess,
    ) -> Result<SplitAccountsForGlv<'info>> {
        let len = self.num_markets();

        let markets_end = len;
        let market_tokens_end = markets_end + len;

        require_gte!(
            remaining_accounts.len(),
            market_tokens_end,
            CoreError::InvalidArgument
        );

        let markets = &remaining_accounts[0..markets_end];
        let market_tokens = &remaining_accounts[markets_end..market_tokens_end];
        let remaining_accounts = &remaining_accounts[market_tokens_end..];

        let mut tokens_collector = self.tokens_collector(action);

        for idx in 0..len {
            let market = &markets[idx];
            let market_token = &market_tokens[idx];
            let expected_market_token = Pubkey::new_from_array(
                *self
                    .markets
                    .get_entry_by_index(idx)
                    .expect("never out of range")
                    .0,
            );

            require_keys_eq!(
                market_token.key(),
                expected_market_token,
                CoreError::MarketTokenMintMismatched
            );

            {
                let mint = Account::<anchor_spl::token::Mint>::try_from(market_token)?;
                require!(
                    mint.mint_authority == Some(*store).into(),
                    CoreError::StoreMismatched
                );
            }

            {
                let market = AccountLoader::<Market>::try_from(market)?;
                let market = market.load()?;
                let meta = market.validated_meta(store)?;
                require_keys_eq!(
                    meta.market_token_mint,
                    expected_market_token,
                    CoreError::MarketTokenMintMismatched
                );
                tokens_collector.insert_token(&meta.index_token_mint);
            }
        }

        Ok(SplitAccountsForGlv {
            markets,
            market_tokens,
            remaining_accounts,
            tokens: tokens_collector
                .into_vec(token_map)
                .map_err(CoreError::from)?,
        })
    }

    pub(crate) fn validate_market_token_balance(
        &self,
        market_token: &Pubkey,
        new_balance: u64,
        market_pool_value: &i128,
        market_token_supply: &u128,
    ) -> Result<()> {
        let config = self
            .markets
            .get(market_token)
            .ok_or_else(|| error!(CoreError::NotFound))?;
        config.validate_balance(new_balance, market_pool_value, market_token_supply)
    }

    pub(crate) fn update_market_token_balance(
        &mut self,
        market_token: &Pubkey,
        new_balance: u64,
    ) -> Result<()> {
        let config = self
            .markets
            .get_mut(market_token)
            .ok_or_else(|| error!(CoreError::NotFound))?;
        config.update_balance(new_balance);
        Ok(())
    }

    pub(crate) fn validate_shift_interval(&self) -> Result<()> {
        let interval = self.shift_min_interval_secs;
        if interval == 0 {
            Ok(())
        } else {
            let current = Clock::get()?.unix_timestamp;
            let after = self
                .shift_last_executed_at
                .checked_add(interval as i64)
                .ok_or_else(|| error!(CoreError::ValueOverflow))?;
            require_gte!(current, after, CoreError::GlvShiftIntervalNotYetPassed);
            Ok(())
        }
    }

    pub(crate) fn validate_shift_price_impact(
        &self,
        from_market_token_value: u128,
        to_market_token_value: u128,
    ) -> Result<()> {
        use gmsol_model::utils::div_to_factor;

        if from_market_token_value < to_market_token_value {
            Ok(())
        } else {
            let max_factor = self.shift_max_price_impact_factor;
            let diff = from_market_token_value.abs_diff(to_market_token_value);
            let effective_price_impact_factor = div_to_factor::<_, { constants::MARKET_DECIMALS }>(
                &diff,
                &from_market_token_value,
                false,
            )
            .ok_or_else(|| error!(CoreError::Internal))?;
            require_gte!(
                max_factor,
                effective_price_impact_factor,
                CoreError::GlvShiftMaxPriceImpactExceeded
            );
            Ok(())
        }
    }

    pub(crate) fn validate_shift_value(&self, from_market_token_value: u128) -> Result<()> {
        require_gte!(
            from_market_token_value,
            self.shift_min_value,
            CoreError::GlvShiftValueNotLargeEnough
        );
        Ok(())
    }

    pub(crate) fn update_shift_last_executed_ts(&mut self) -> Result<()> {
        let clock = Clock::get()?;
        self.shift_last_executed_at = clock.unix_timestamp;
        Ok(())
    }
}

#[cfg(feature = "utils")]
impl Glv {
    /// Get last shift executed ts.
    pub fn shift_last_executed_at(&self) -> i64 {
        self.shift_last_executed_at
    }

    /// Get min shift interval.
    pub fn shift_min_interval_secs(&self) -> u32 {
        self.shift_min_interval_secs
    }

    /// Get max shift price impact factor.
    pub fn shift_max_price_impact_factor(&self) -> u128 {
        self.shift_max_price_impact_factor
    }

    /// Get min shift vaule.
    pub fn shift_min_value(&self) -> u128 {
        self.shift_min_value
    }

    /// Get min tokens for first deposit.
    pub fn min_tokens_for_first_deposit(&self) -> u64 {
        self.min_tokens_for_first_deposit
    }
}

/// GLV Update Params.
#[derive(AnchorSerialize, AnchorDeserialize, Default)]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct UpdateGlvParams {
    /// Minimum amount for the first GLV deposit.
    pub min_tokens_for_first_deposit: Option<u64>,
    /// Minimum shift interval seconds.
    pub shift_min_interval_secs: Option<u32>,
    /// Maximum price impact factor after shift.
    pub shift_max_price_impact_factor: Option<u128>,
    /// Minimum shift value.
    pub shift_min_value: Option<u128>,
}

impl UpdateGlvParams {
    /// Returns whether the update is empty.
    pub fn is_empty(&self) -> bool {
        self.min_tokens_for_first_deposit.is_none()
            && self.shift_min_interval_secs.is_none()
            && self.shift_max_price_impact_factor.is_none()
            && self.shift_min_value.is_none()
    }

    pub(crate) fn validate(&self) -> Result<()> {
        require!(!self.is_empty(), CoreError::InvalidArgument);
        Ok(())
    }
}

gmsol_utils::flags!(GlvMarketFlag, MAX_GLV_MARKET_FLAGS, u8);

/// Market Config for GLV.
#[zero_copy]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
pub struct GlvMarketConfig {
    max_amount: u64,
    flags: GlvMarketFlagContainer,
    #[cfg_attr(feature = "debug", debug(skip))]
    padding_0: [u8; 7],
    max_value: u128,
    balance: u64,
    #[cfg_attr(feature = "debug", debug(skip))]
    padding_1: [u8; 8],
}

impl Default for GlvMarketConfig {
    fn default() -> Self {
        use bytemuck::Zeroable;

        Self::zeroed()
    }
}

impl GlvMarketConfig {
    fn validate_balance(
        &self,
        new_balance: u64,
        market_pool_value: &i128,
        market_token_supply: &u128,
    ) -> Result<()> {
        if self.max_amount == 0 && self.max_value == 0 {
            return Ok(());
        }

        if self.max_amount > 0 {
            require_gte!(
                self.max_amount,
                new_balance,
                CoreError::ExceedMaxGlvMarketTokenBalanceAmount
            );
        }

        if self.max_value > 0 {
            if market_pool_value.is_negative() {
                return err!(CoreError::GlvNegativeMarketPoolValue);
            }

            let value = gmsol_model::utils::market_token_amount_to_usd(
                &(new_balance as u128),
                &market_pool_value.unsigned_abs(),
                market_token_supply,
            )
            .ok_or_else(|| error!(CoreError::FailedToCalculateGlvValueForMarket))?;
            require_gte!(
                self.max_value,
                value,
                CoreError::ExceedMaxGlvMarketTokenBalanceValue
            );
        }

        Ok(())
    }

    fn update_balance(&mut self, new_balance: u64) {
        self.balance = new_balance;
    }

    /// Get balance.
    pub fn balance(&self) -> u64 {
        self.balance
    }

    pub(crate) fn toggle_flag(&mut self, flag: GlvMarketFlag, enable: bool) -> Result<bool> {
        let current = self.flags.get_flag(flag);
        require_neq!(current, enable, CoreError::PreconditionsAreNotMet);
        Ok(self.flags.set_flag(flag, enable))
    }

    /// Get flag.
    pub fn get_flag(&self, flag: GlvMarketFlag) -> bool {
        self.flags.get_flag(flag)
    }
}

#[cfg(feature = "utils")]
impl GlvMarketConfig {
    /// Get max amount.
    pub fn max_amount(&self) -> u64 {
        self.max_amount
    }

    /// Get max value.
    pub fn max_value(&self) -> u128 {
        self.max_value
    }
}

pub(crate) struct SplitAccountsForGlv<'info> {
    pub(crate) markets: &'info [AccountInfo<'info>],
    pub(crate) market_tokens: &'info [AccountInfo<'info>],
    pub(crate) remaining_accounts: &'info [AccountInfo<'info>],
    pub(crate) tokens: Vec<Pubkey>,
}

/// Glv Deposit.
#[account(zero_copy)]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GlvDeposit {
    /// Header.
    pub(crate) header: ActionHeader,
    /// Token accounts.
    pub(crate) tokens: GlvDepositTokenAccounts,
    /// Params.
    pub(crate) params: GlvDepositActionParams,
    /// Swap params.
    pub(crate) swap: SwapActionParams,
    #[cfg_attr(feature = "debug", debug(skip))]
    padding_1: [u8; 4],
    #[cfg_attr(feature = "debug", debug(skip))]
    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
    reserved: [u8; 128],
}

impl Action for GlvDeposit {
    const MIN_EXECUTION_LAMPORTS: u64 = 200_000;

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

impl Closable for GlvDeposit {
    type ClosedEvent = GlvDepositRemoved;

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

impl Seed for GlvDeposit {
    const SEED: &'static [u8] = b"glv_deposit";
}

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

impl GlvDeposit {
    /// Validate the GLV deposit before execution.
    ///
    /// # CHECK
    /// - This deposit must have been initialized.
    /// - The `glv` and `glv_token` must match.
    /// - The `market_token` must be a valid token account.
    /// - The `glv_token` must be a valid token account.
    ///
    /// # Errors
    /// - The address of `market_token` must match the market token address of this deposit.
    /// - The address of `glv_token` must match the glv token address of this deposit.
    pub(crate) fn unchecked_validate_for_execution(
        &self,
        glv_token: &InterfaceAccount<token_interface::Mint>,
        glv: &Glv,
    ) -> Result<()> {
        require_keys_eq!(
            glv_token.key(),
            self.tokens.glv_token(),
            CoreError::TokenMintMismatched,
        );

        let supply = glv_token.supply;

        if supply == 0 {
            Self::validate_first_deposit(
                &self.header().receiver(),
                self.params.min_glv_token_amount,
                glv,
            )?;
        }

        Ok(())
    }

    pub(crate) fn is_market_deposit_required(&self) -> bool {
        self.params.deposit.initial_long_token_amount != 0
            || self.params.deposit.initial_short_token_amount != 0
    }

    /// Get first deposit receiver.
    #[inline]
    pub fn first_deposit_receiver() -> Pubkey {
        Deposit::first_deposit_receiver()
    }

    fn validate_first_deposit(receiver: &Pubkey, min_amount: u64, glv: &Glv) -> Result<()> {
        let min_tokens_for_first_deposit = glv.min_tokens_for_first_deposit;

        // Skip first deposit check if the amount is zero.
        if min_tokens_for_first_deposit == 0 {
            return Ok(());
        }

        require_keys_eq!(
            *receiver,
            Self::first_deposit_receiver(),
            CoreError::InvalidReceiverForFirstDeposit
        );

        require_gte!(
            min_amount,
            min_tokens_for_first_deposit,
            CoreError::NotEnoughGlvTokenAmountForFirstDeposit,
        );

        Ok(())
    }

    pub(crate) fn validate_output_amount(&self, amount: u64) -> Result<()> {
        require_gte!(
            amount,
            self.params.min_glv_token_amount,
            CoreError::InsufficientOutputAmount
        );

        Ok(())
    }

    /// Get token infos.
    pub fn tokens(&self) -> &GlvDepositTokenAccounts {
        &self.tokens
    }
}

impl HasSwapParams for GlvDeposit {
    fn swap(&self) -> &SwapActionParams {
        &self.swap
    }
}

/// Token and accounts.
#[zero_copy]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GlvDepositTokenAccounts {
    /// Initial long token and account.
    pub initial_long_token: TokenAndAccount,
    /// Initial short token and account.
    pub initial_short_token: TokenAndAccount,
    /// Market token and account.
    pub(crate) market_token: TokenAndAccount,
    /// GLV token and account.
    pub(crate) glv_token: TokenAndAccount,
    #[cfg_attr(feature = "debug", debug(skip))]
    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
    reserved: [u8; 128],
}

impl GlvDepositTokenAccounts {
    /// Get market token.
    pub fn market_token(&self) -> Pubkey {
        self.market_token
            .token()
            .expect("uninitialized GLV Deposit account")
    }

    /// Get market token account.
    pub fn market_token_account(&self) -> Pubkey {
        self.market_token
            .account()
            .expect("uninitalized GLV Deposit account")
    }

    /// Get GLV token.
    pub fn glv_token(&self) -> Pubkey {
        self.glv_token
            .token()
            .expect("uninitialized GLV Deposit account")
    }

    /// Get GLV token account.
    pub fn glv_token_account(&self) -> Pubkey {
        self.glv_token
            .account()
            .expect("uninitalized GLV Deposit account")
    }
}

/// GLV Deposit Params.
#[zero_copy]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GlvDepositActionParams {
    /// Deposit params.
    pub(crate) deposit: DepositActionParams,
    /// The amount of market tokens to deposit.
    pub(crate) market_token_amount: u64,
    /// The minimum acceptable amount of glv tokens to receive.
    pub(crate) min_glv_token_amount: u64,
    #[cfg_attr(feature = "debug", debug(skip))]
    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
    reserved: [u8; 64],
}

/// Glv Withdrawal.
#[account(zero_copy)]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GlvWithdrawal {
    /// Header.
    pub(crate) header: ActionHeader,
    /// Token accounts.
    pub(crate) tokens: GlvWithdrawalTokenAccounts,
    /// Params.
    pub(crate) params: GlvWithdrawalActionParams,
    /// Swap params.
    pub(crate) swap: SwapActionParams,
    #[cfg_attr(feature = "debug", debug(skip))]
    padding_1: [u8; 4],
    #[cfg_attr(feature = "debug", debug(skip))]
    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
    reserved: [u8; 128],
}

impl GlvWithdrawal {
    /// Get tokens.
    pub fn tokens(&self) -> &GlvWithdrawalTokenAccounts {
        &self.tokens
    }

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

impl Action for GlvWithdrawal {
    const MIN_EXECUTION_LAMPORTS: u64 = 200_000;

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

impl Closable for GlvWithdrawal {
    type ClosedEvent = GlvWithdrawalRemoved;

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

impl Seed for GlvWithdrawal {
    const SEED: &'static [u8] = b"glv_withdrawal";
}

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

impl HasSwapParams for GlvWithdrawal {
    fn swap(&self) -> &SwapActionParams {
        &self.swap
    }
}

/// Token and accounts.
#[zero_copy]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GlvWithdrawalTokenAccounts {
    /// Final ong token and account.
    pub(crate) final_long_token: TokenAndAccount,
    /// Final short token and account.
    pub(crate) final_short_token: TokenAndAccount,
    /// Market token and account.
    pub(crate) market_token: TokenAndAccount,
    /// GLV token and account.
    pub(crate) glv_token: TokenAndAccount,
    #[cfg_attr(feature = "debug", debug(skip))]
    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
    reserved: [u8; 128],
}

impl GlvWithdrawalTokenAccounts {
    /// Get market token.
    pub fn market_token(&self) -> Pubkey {
        self.market_token
            .token()
            .expect("uninitialized GLV Withdrawal account")
    }

    /// Get market token account.
    pub fn market_token_account(&self) -> Pubkey {
        self.market_token
            .account()
            .expect("uninitalized GLV Withdrawal account")
    }

    /// Get GLV token.
    pub fn glv_token(&self) -> Pubkey {
        self.glv_token
            .token()
            .expect("uninitialized GLV Withdrawal account")
    }

    /// Get GLV token account.
    pub fn glv_token_account(&self) -> Pubkey {
        self.glv_token
            .account()
            .expect("uninitalized GLV Withdrawal account")
    }

    /// Get final long token.
    pub fn final_long_token(&self) -> Pubkey {
        self.final_long_token
            .token()
            .expect("uninitialized GLV Withdrawal account")
    }

    /// Get final long token account.
    pub fn final_long_token_account(&self) -> Pubkey {
        self.final_long_token
            .account()
            .expect("uninitalized GLV Withdrawal account")
    }

    /// Get final short token.
    pub fn final_short_token(&self) -> Pubkey {
        self.final_short_token
            .token()
            .expect("uninitialized GLV Withdrawal account")
    }

    /// Get final short token account.
    pub fn final_short_token_account(&self) -> Pubkey {
        self.final_short_token
            .account()
            .expect("uninitalized GLV Withdrawal account")
    }
}

/// GLV Withdrawal Params.
#[zero_copy]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GlvWithdrawalActionParams {
    /// The amount of GLV tokens to burn.
    pub(crate) glv_token_amount: u64,
    /// The minimum acceptable amount of final long tokens to receive.
    pub min_final_long_token_amount: u64,
    /// The minimum acceptable amount of final short tokens to receive.
    pub min_final_short_token_amount: u64,
    #[cfg_attr(feature = "debug", debug(skip))]
    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
    reserved: [u8; 64],
}

/// Glv Shift.
#[account(zero_copy)]
#[cfg_attr(feature = "debug", derive(derive_more::Debug))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GlvShift {
    pub(crate) shift: Shift,
    #[cfg_attr(feature = "debug", debug(skip))]
    #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
    reserved: [u8; 128],
}

impl Action for GlvShift {
    const MIN_EXECUTION_LAMPORTS: u64 = 0;

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

impl Closable for GlvShift {
    type ClosedEvent = ShiftRemoved;

    fn to_closed_event(&self, address: &Pubkey, reason: &str) -> Result<Self::ClosedEvent> {
        let header = self.header();
        let tokens = self.tokens();
        ShiftRemoved::new(
            header.id,
            header.store,
            *address,
            tokens.from_market_token(),
            header.owner,
            header.action_state()?,
            reason,
        )
    }
}

impl Seed for GlvShift {
    const SEED: &'static [u8] = Shift::SEED;
}

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

impl GlvShift {
    /// Get the GLV address.
    pub fn glv(&self) -> &Pubkey {
        &self.shift.header.owner
    }

    /// Get token infos.
    pub fn tokens(&self) -> &shift::ShiftTokenAccounts {
        self.shift.tokens()
    }

    pub(crate) fn header_mut(&mut self) -> &mut ActionHeader {
        &mut self.shift.header
    }

    /// Get the funder.
    pub fn funder(&self) -> &Pubkey {
        self.shift.header().rent_receiver()
    }
}

impl Borrow<Shift> for GlvShift {
    fn borrow(&self) -> &Shift {
        &self.shift
    }
}

impl BorrowMut<Shift> for GlvShift {
    fn borrow_mut(&mut self) -> &mut Shift {
        &mut self.shift
    }
}