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
use std::collections::BTreeSet;

use anchor_lang::prelude::*;
use anchor_spl::{
    associated_token::AssociatedToken,
    token_2022::Token2022,
    token_interface::{self, Mint},
};
use gmsol_utils::InitSpace;

use crate::{
    constants,
    states::{
        glv::{Glv, UpdateGlvParams},
        Market, Seed, Store,
    },
    utils::{internal, token::is_associated_token_account_with_program_id},
    CoreError,
};

/// The accounts definition for [`initialize_glv`](crate::gmsol_store::initialize_glv) instruction.
///
/// Remaining accounts expected by this instruction:
///
///   - 0..N. `[]` N unique market accounts.
///   - N..2N. `[]` N corresponding market token accounts. Should be sorted by addresses.
///   - 2N..3N. `[writable]` N corresponding market token vault accounts,
///     which will be initialized as the associated token accounts of the GLV.
///     This vault accounts should be sorted by market token addresses.
#[derive(Accounts)]
#[instruction(index: u16)]
pub struct InitializeGlv<'info> {
    /// Authority.
    #[account(mut)]
    pub authority: Signer<'info>,
    /// Store.
    pub store: AccountLoader<'info, Store>,
    /// Glv token.
    #[account(
        init,
        payer = authority,
        mint::authority = store,
        mint::decimals = constants::MARKET_TOKEN_DECIMALS,
        seeds = [
            Glv::GLV_TOKEN_SEED,
            store.key().as_ref(),
            &index.to_le_bytes(),
        ],
        bump,
        owner = token_program.key(),
    )]
    pub glv_token: InterfaceAccount<'info, Mint>,
    /// Glv account.
    #[account(
        init,
        payer = authority,
        space = 8 + Glv::INIT_SPACE,
        seeds = [
            Glv::SEED,
            glv_token.key().as_ref(),
        ],
        bump,
    )]
    pub glv: AccountLoader<'info, Glv>,
    pub system_program: Program<'info, System>,
    pub token_program: Program<'info, Token2022>,
    pub market_token_program: Interface<'info, token_interface::TokenInterface>,
    pub associated_token_program: Program<'info, AssociatedToken>,
}

/// Initialize GLV token and account.
///
/// # CHECK
/// - Only MARKET_KEEPER is allowed to call this function.
pub(crate) fn unchecked_initialize_glv<'info>(
    ctx: Context<'_, '_, 'info, 'info, InitializeGlv<'info>>,
    index: u16,
    length: usize,
) -> Result<()> {
    let remaining_accounts = ctx.remaining_accounts;

    require_gte!(
        Glv::MAX_ALLOWED_NUMBER_OF_MARKETS,
        length,
        CoreError::ExceedMaxLengthLimit
    );

    require_gt!(length, 0, CoreError::InvalidArgument);

    let markets_end = length;
    let market_tokens_end = markets_end + length;
    let vaults_end = market_tokens_end + length;

    require_gte!(
        remaining_accounts.len(),
        vaults_end,
        ErrorCode::AccountNotEnoughKeys
    );

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

    let store = ctx.accounts.store.key();
    let (long_token, short_token, expected_market_tokens) =
        Glv::process_and_validate_markets_for_init(markets, &store)?;

    ctx.accounts
        .initialize_vaults(&expected_market_tokens, market_tokens, vaults)?;

    ctx.accounts.glv.load_init()?.unchecked_init(
        ctx.bumps.glv,
        index,
        &store,
        &ctx.accounts.glv_token.key(),
        &long_token,
        &short_token,
        &expected_market_tokens,
    )?;

    Ok(())
}

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

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

impl<'info> InitializeGlv<'info> {
    fn initialize_vaults(
        &self,
        expected_market_tokens: &BTreeSet<Pubkey>,
        market_tokens: &'info [AccountInfo<'info>],
        vaults: &'info [AccountInfo<'info>],
    ) -> Result<()> {
        use anchor_spl::associated_token::{create_idempotent, Create};

        require_eq!(
            expected_market_tokens.len(),
            vaults.len(),
            CoreError::Internal
        );

        require_eq!(
            expected_market_tokens.len(),
            market_tokens.len(),
            CoreError::Internal
        );

        let glv = self.glv.key();
        let token_program_id = self.market_token_program.key;

        for ((expected_market_token, market_token), vault) in
            expected_market_tokens.iter().zip(market_tokens).zip(vaults)
        {
            require!(
                is_associated_token_account_with_program_id(
                    vault.key,
                    &glv,
                    expected_market_token,
                    token_program_id,
                ),
                ErrorCode::AccountNotAssociatedTokenAccount
            );

            create_idempotent(CpiContext::new(
                self.associated_token_program.to_account_info(),
                Create {
                    payer: self.authority.to_account_info(),
                    associated_token: vault.clone(),
                    authority: self.glv.to_account_info(),
                    mint: market_token.clone(),
                    system_program: self.system_program.to_account_info(),
                    token_program: self.market_token_program.to_account_info(),
                },
            ))?;
        }

        Ok(())
    }
}

/// The accounts definition for [`update_glv_market_config`](crate::gmsol_store::update_glv_market_config) instruction.
#[derive(Accounts)]
pub struct UpdateGlvMarketConfig<'info> {
    /// Authority.
    pub authority: Signer<'info>,
    /// Store.
    pub store: AccountLoader<'info, Store>,
    /// GLV.
    #[account(
        mut,
        has_one = store,
        constraint = glv.load()?.contains(&market_token.key()) @ CoreError::InvalidArgument,
    )]
    pub glv: AccountLoader<'info, Glv>,
    /// Market token.
    pub market_token: Box<Account<'info, anchor_spl::token::Mint>>,
}

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

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

/// Update the config for the given market.
///
/// # CHECK
/// - Only MARKET_KEEPER is allowed to call this function.
pub(crate) fn unchecked_update_glv_market_config(
    ctx: Context<UpdateGlvMarketConfig>,
    max_amount: Option<u64>,
    max_value: Option<u128>,
) -> Result<()> {
    require!(
        max_amount.is_some() || max_value.is_some(),
        CoreError::InvalidArgument
    );
    let mut glv = ctx.accounts.glv.load_mut()?;
    glv.update_market_config(&ctx.accounts.market_token.key(), max_amount, max_value)?;
    Ok(())
}

/// Toggle flag of the given market.
///
/// # CHECK
/// - Only MARKET_KEEPER is allowed to call this function.
pub(crate) fn unchecked_toggle_glv_market_flag(
    ctx: Context<UpdateGlvMarketConfig>,
    flag: &str,
    enable: bool,
) -> Result<()> {
    let flag = flag
        .parse()
        .map_err(|_| error!(CoreError::InvalidArgument))?;
    let mut glv = ctx.accounts.glv.load_mut()?;
    let market_token = ctx.accounts.market_token.key();
    let previous = glv.toggle_market_config_flag(&market_token, flag, enable)?;
    msg!(
        "[GLV] toggled market flag {}: {} -> {}",
        flag,
        previous,
        enable
    );
    Ok(())
}

/// The accounts definition for [`update_glv_config`](crate::gmsol_store::update_glv_config) instruction.
#[derive(Accounts)]
pub struct UpdateGlvConfig<'info> {
    /// Authority.
    pub authority: Signer<'info>,
    /// Store.
    pub store: AccountLoader<'info, Store>,
    /// GLV to update.
    #[account(mut, has_one = store)]
    pub glv: AccountLoader<'info, Glv>,
}

/// Update thte config of GLV.
///
/// # CHECK
/// - Only MARKET_KEEPER can use.
pub(crate) fn unchecked_update_glv(
    ctx: Context<UpdateGlvConfig>,
    params: &UpdateGlvParams,
) -> Result<()> {
    params.validate()?;
    ctx.accounts.glv.load_mut()?.update_config(params)?;
    Ok(())
}

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

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

/// The accounts definition for [`insert_glv_market`](crate::gmsol_store::insert_glv_market) instruction.
#[derive(Accounts)]
pub struct InsertGlvMarket<'info> {
    /// Authority.
    #[account(mut)]
    pub authority: Signer<'info>,
    /// Store.
    pub store: AccountLoader<'info, Store>,
    /// GLV to modify.
    #[account(
        mut,
        has_one = store,
    )]
    pub glv: AccountLoader<'info, Glv>,
    /// Market token.
    #[account(
        mint::authority = store,
        mint::token_program = token_program,
        constraint = !glv.load()?.contains(&market_token.key()) @ CoreError::PreconditionsAreNotMet,
    )]
    pub market_token: InterfaceAccount<'info, token_interface::Mint>,
    /// Market.
    #[account(
        has_one = store,
        constraint = market.load()?.meta().market_token_mint == market_token.key() @ CoreError::MarketTokenMintMismatched,
    )]
    pub market: AccountLoader<'info, Market>,
    /// Vault.
    #[account(
        init_if_needed,
        payer = authority,
        associated_token::mint = market_token,
        associated_token::authority = glv,
        associated_token::token_program = token_program,
    )]
    pub vault: InterfaceAccount<'info, token_interface::TokenAccount>,
    /// System program.
    pub system_program: Program<'info, System>,
    /// Token program for market token.
    pub token_program: Interface<'info, token_interface::TokenInterface>,
    /// Associated token program.
    pub associated_token_program: Program<'info, AssociatedToken>,
}

/// Insert a new market to the GLV.
///
/// # CHECK
/// - Only MARKET_KEEPER can use.
pub(crate) fn unchecked_insert_glv_market(ctx: Context<InsertGlvMarket>) -> Result<()> {
    let mut glv = ctx.accounts.glv.load_mut()?;
    let market = ctx.accounts.market.load()?;

    glv.insert_market(&ctx.accounts.store.key(), &market)?;

    Ok(())
}

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

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

/// The accounts definition for [`remove_glv_market`](crate::gmsol_store::remove_glv_market) instruction.
#[derive(Accounts)]
pub struct RemoveGlvMarket<'info> {
    /// Authority.
    #[account(mut)]
    pub authority: Signer<'info>,
    /// Store.
    pub store: AccountLoader<'info, Store>,
    /// The store wallet.
    #[account(mut, seeds = [Store::WALLET_SEED, store.key().as_ref()], bump)]
    pub store_wallet: SystemAccount<'info>,
    /// GLV to modify.
    #[account(
        mut,
        has_one = store,
    )]
    pub glv: AccountLoader<'info, Glv>,
    /// Market token.
    #[account(
        mint::authority = store,
        constraint = glv.load()?.contains(&market_token.key()) @ CoreError::PreconditionsAreNotMet,
    )]
    pub market_token: InterfaceAccount<'info, token_interface::Mint>,
    /// Vault.
    #[account(
        mut,
        associated_token::mint = market_token,
        associated_token::authority = glv,
    )]
    pub vault: InterfaceAccount<'info, token_interface::TokenAccount>,
    /// Store wallet ATA.
    #[account(
        init_if_needed,
        payer = authority,
        associated_token::mint = market_token,
        associated_token::authority = store_wallet,
    )]
    pub store_wallet_ata: InterfaceAccount<'info, token_interface::TokenAccount>,
    /// Token program.
    pub token_program: Interface<'info, token_interface::TokenInterface>,
    /// Associated token program.
    pub associated_token_program: Program<'info, AssociatedToken>,
    /// System program.
    pub system_program: Program<'info, System>,
}

/// Remove a new market from the GLV.
///
/// # CHECK
/// - Only MARKET_KEEPER can use.
pub(crate) fn unchecked_remove_glv_market(ctx: Context<RemoveGlvMarket>) -> Result<()> {
    use anchor_spl::token_interface::{close_account, CloseAccount};

    let market_token = ctx.accounts.market_token.key();
    require_eq!(
        ctx.accounts
            .glv
            .load()?
            .market_config(&market_token)
            .ok_or_else(|| error!(CoreError::NotFound))?
            .balance(),
        0,
        CoreError::PreconditionsAreNotMet
    );
    ctx.accounts
        .glv
        .load_mut()?
        .unchecked_remove_market(&market_token)?;

    {
        let glv = ctx.accounts.glv.load()?;
        let signer = glv.signer_seeds();

        let amount = ctx.accounts.vault.amount;

        if amount != 0 {
            let decimals = ctx.accounts.market_token.decimals;
            token_interface::transfer_checked(
                CpiContext::new(
                    ctx.accounts.token_program.to_account_info(),
                    token_interface::TransferChecked {
                        from: ctx.accounts.vault.to_account_info(),
                        mint: ctx.accounts.market_token.to_account_info(),
                        to: ctx.accounts.store_wallet_ata.to_account_info(),
                        authority: ctx.accounts.glv.to_account_info(),
                    },
                )
                .with_signer(&[&signer]),
                amount,
                decimals,
            )?;
        }

        close_account(
            CpiContext::new(
                ctx.accounts.token_program.to_account_info(),
                CloseAccount {
                    account: ctx.accounts.vault.to_account_info(),
                    destination: ctx.accounts.store_wallet.to_account_info(),
                    authority: ctx.accounts.glv.to_account_info(),
                },
            )
            .with_signer(&[&signer]),
        )?;
    }

    Ok(())
}

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

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