gmsol-sdk 0.5.0

GMX-Solana is an extension of GMX on the Solana blockchain.
Documentation
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
use gmsol_utils::oracle::PriceProviderKind;
use solana_sdk::pubkey::Pubkey;

#[allow(unused_imports)]
use gmsol_programs::gmsol_store::accounts as store_accounts;

#[cfg(treasury)]
#[allow(unused_imports)]
use gmsol_programs::gmsol_treasury::accounts as treasury_accounts;

#[cfg(timelock)]
#[allow(unused_imports)]
use gmsol_programs::gmsol_timelock::accounts as timelock_accounts;

/// Nonce bytes.
pub type NonceBytes = [u8; 32];

/// Referral code bytes.
pub type ReferralCodeBytes = [u8; 8];

/// Seed for event authority.
pub const EVENT_AUTHORITY_SEED: &[u8] = b"__event_authority";

/// Seed for [`Store`](store_accounts::Store).
pub const STORE_SEED: &[u8] = b"data_store";

/// Seed for store wallet.
pub const STORE_WALLET_SEED: &[u8] = b"store_wallet";

/// Seed for market vault.
pub const MARKET_VAULT_SEED: &[u8] = b"market_vault";

/// Seed for market token mint.
pub const MAREKT_TOKEN_MINT_SEED: &[u8] = b"market_token_mint";

/// Seed for [`Market`](store_accounts::Market).
pub const MARKET_SEED: &[u8] = b"market";

/// Seed for [`Deposit`](store_accounts::Deposit).
pub const DEPOSIT_SEED: &[u8] = b"deposit";

/// Seed for first deposit receiver.
pub const FIRST_DEPOSIT_RECEIVER_SEED: &[u8] = b"first_deposit_receiver";

/// Seed for [`Withdrawal`](store_accounts::Withdrawal).
pub const WITHDRAWAL_SEED: &[u8] = b"withdrawal";

/// Seed for [`Shift`](store_accounts::Shift).
pub const SHIFT_SEED: &[u8] = b"shift";

/// Seed for [`Order`](store_accounts::Order).
pub const ORDER_SEED: &[u8] = b"order";

/// Seed for [`Position`](store_accounts::Position).
pub const POSITION_SEED: &[u8] = b"position";

/// Seed for claimable account.
pub const CLAIMABLE_ACCOUNT_SEED: &[u8] = b"claimable_account";

/// Seed for trade event buffer account.
pub const TRADE_DATA_SEED: &[u8] = b"trade_event_data";

/// Seed for [`User`](store_accounts::UserHeader).
pub const USER_SEED: &[u8] = b"user";

/// Seed for [`ReferralCodeV2`](store_accounts::ReferralCodeV2).
pub const REFERRAL_CODE_SEED: &[u8] = b"referral_code";

/// Seed for GLV token mint.
pub const GLV_TOKEN_SEED: &[u8] = b"glv_token";

/// Seed for [`Glv`](store_accounts::Glv).
pub const GLV_SEED: &[u8] = b"glv";

/// Seed for [`GlvDeposit`](store_accounts::GlvDeposit).
pub const GLV_DEPOSIT_SEED: &[u8] = b"glv_deposit";

/// Seed for [`GlvWithdrawal`](store_accounts::GlvWithdrawal).
pub const GLV_WITHDRAWAL_SEED: &[u8] = b"glv_withdrawal";

/// Seed for [`GtExchangeVault`](store_accounts::GtExchangeVault).
pub const GT_EXCHANGE_VAULT_SEED: &[u8] = b"gt_exchange_vault";

/// Seed for [`GtExchange`](store_accounts::GtExchange).
pub const GT_EXCHANGE_SEED: &[u8] = b"gt_exchange";

/// Seed for [`PriceFeed`](store_accounts::PriceFeed).
pub const PRICE_FEED_SEED: &[u8] = b"price_feed";

/// Seed for [`Config`](treasury_accounts::Config).
#[cfg(treasury)]
pub const TREASURY_CONFIG_SEED: &[u8] = b"config";

/// Seed for [`TreasuryVaultConfig`](treasury_accounts::TreasuryVaultConfig).
#[cfg(treasury)]
pub const TREASURY_VAULT_CONFIG_SEED: &[u8] = b"treasury_vault_config";

/// Seed for [`GtBank`](treasury_accounts::GtBank).
#[cfg(treasury)]
pub const GT_BANK_SEED: &[u8] = b"gt_bank";

/// Seed for treasury receiver.
#[cfg(treasury)]
pub const TREASURY_RECEIVER_SEED: &[u8] = b"receiver";

/// Seed for [`TimelockConfig`](timelock_accounts::TimelockConfig).
#[cfg(timelock)]
pub const TIMELOCK_CONFIG_SEED: &[u8] = b"timelock_config";

/// Seed for [`Executor`](timelock_accounts::Executor).
#[cfg(timelock)]
pub const TIMELOCK_EXECUTOR_SEED: &[u8] = b"timelock_executor";

/// Seed for the exeuctor wallet.
#[cfg(timelock)]
pub const TIMELOCK_EXECUTOR_WALLET_SEED: &[u8] = b"wallet";

fn to_seed(key: &str) -> [u8; 32] {
    use solana_sdk::hash::hash;
    hash(key.as_bytes()).to_bytes()
}

/// Find PDA for `event_authority` account.
pub fn find_event_authority_address(program_id: &Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[EVENT_AUTHORITY_SEED], program_id)
}

/// Find PDA for [`Store`](store_accounts::Store) account.
pub fn find_store_address(key: &str, store_program_id: &Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[STORE_SEED, &to_seed(key)], store_program_id)
}

/// Find PDA for store wallet account.
pub fn find_store_wallet_address(store: &Pubkey, store_program_id: &Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[STORE_WALLET_SEED, store.as_ref()], store_program_id)
}

/// Find PDA for market vault.
pub fn find_market_vault_address(
    store: &Pubkey,
    token: &Pubkey,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[MARKET_VAULT_SEED, store.as_ref(), token.as_ref()],
        store_program_id,
    )
}

/// Find PDA for Market token mint account.
pub fn find_market_token_address(
    store: &Pubkey,
    index_token: &Pubkey,
    long_token: &Pubkey,
    short_token: &Pubkey,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[
            MAREKT_TOKEN_MINT_SEED,
            store.as_ref(),
            index_token.as_ref(),
            long_token.as_ref(),
            short_token.as_ref(),
        ],
        store_program_id,
    )
}

/// Find PDA for [`Market`](store_accounts::Market) account.
pub fn find_market_address(
    store: &Pubkey,
    token: &Pubkey,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[MARKET_SEED, store.as_ref(), token.as_ref()],
        store_program_id,
    )
}

/// Find PDA for deposit.
pub fn find_deposit_address(
    store: &Pubkey,
    user: &Pubkey,
    nonce: &NonceBytes,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[DEPOSIT_SEED, store.as_ref(), user.as_ref(), nonce],
        store_program_id,
    )
}

/// Find PDA for first deposit receiver.
pub fn find_first_deposit_receiver_address(store_program_id: &Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[FIRST_DEPOSIT_RECEIVER_SEED], store_program_id)
}

/// Find PDA for withdrawal.
pub fn find_withdrawal_address(
    store: &Pubkey,
    user: &Pubkey,
    nonce: &NonceBytes,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[WITHDRAWAL_SEED, store.as_ref(), user.as_ref(), nonce],
        store_program_id,
    )
}

/// Find PDA for shift.
pub fn find_shift_address(
    store: &Pubkey,
    owner: &Pubkey,
    nonce: &NonceBytes,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[SHIFT_SEED, store.as_ref(), owner.as_ref(), nonce],
        store_program_id,
    )
}

/// Find PDA for [`Order`](store_accounts::Order) account.
pub fn find_order_address(
    store: &Pubkey,
    owner: &Pubkey,
    nonce: &NonceBytes,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[ORDER_SEED, store.as_ref(), owner.as_ref(), nonce.as_ref()],
        store_program_id,
    )
}

/// Find PDA for position.
pub fn find_position_address(
    store: &Pubkey,
    owner: &Pubkey,
    market_token: &Pubkey,
    collateral_token: &Pubkey,
    is_long: bool,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    // See the definition of `PositionKind`.
    let kind = if is_long { 1 } else { 2 };
    Pubkey::find_program_address(
        &[
            POSITION_SEED,
            store.as_ref(),
            owner.as_ref(),
            market_token.as_ref(),
            collateral_token.as_ref(),
            &[kind],
        ],
        store_program_id,
    )
}

/// Find PDA for claimable account.
pub fn find_claimable_account_address(
    store: &Pubkey,
    mint: &Pubkey,
    user: &Pubkey,
    time_key: &[u8],
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[
            CLAIMABLE_ACCOUNT_SEED,
            store.as_ref(),
            mint.as_ref(),
            user.as_ref(),
            time_key,
        ],
        store_program_id,
    )
}

/// Find PDA for trade event buffer.
pub fn find_trade_event_buffer_address(
    store: &Pubkey,
    authority: &Pubkey,
    index: u16,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[
            TRADE_DATA_SEED,
            store.as_ref(),
            authority.as_ref(),
            &index.to_le_bytes(),
        ],
        store_program_id,
    )
}

/// Find PDA for [`User`](store_accounts::UserHeader) account.
pub fn find_user_address(
    store: &Pubkey,
    owner: &Pubkey,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[USER_SEED, store.as_ref(), owner.as_ref()],
        store_program_id,
    )
}

/// Find PDA for referral code account.
pub fn find_referral_code_address(
    store: &Pubkey,
    code: ReferralCodeBytes,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[REFERRAL_CODE_SEED, store.as_ref(), &code],
        store_program_id,
    )
}

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

/// Find PDA GLV account.
pub fn find_glv_address(glv_token: &Pubkey, program_id: &Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[GLV_SEED, glv_token.as_ref()], program_id)
}

/// Find PDA for a GLV deposit account.
pub fn find_glv_deposit_address(
    store: &Pubkey,
    owner: &Pubkey,
    nonce: &NonceBytes,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[GLV_DEPOSIT_SEED, store.as_ref(), owner.as_ref(), nonce],
        store_program_id,
    )
}

/// Find PDA for a GLV withdrawal account.
pub fn find_glv_withdrawal_address(
    store: &Pubkey,
    owner: &Pubkey,
    nonce: &NonceBytes,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[GLV_WITHDRAWAL_SEED, store.as_ref(), owner.as_ref(), nonce],
        store_program_id,
    )
}

/// Find PDA for GT exchange vault account.
pub fn find_gt_exchange_vault_address(
    store: &Pubkey,
    time_window_index: i64,
    time_window: u32,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[
            GT_EXCHANGE_VAULT_SEED,
            store.as_ref(),
            &time_window_index.to_le_bytes(),
            &time_window.to_le_bytes(),
        ],
        store_program_id,
    )
}

/// Find PDA for GT exchange account.
pub fn find_gt_exchange_address(
    vault: &Pubkey,
    owner: &Pubkey,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[GT_EXCHANGE_SEED, vault.as_ref(), owner.as_ref()],
        store_program_id,
    )
}

/// Fint PDA for custom price feed account.
pub fn find_price_feed_address(
    store: &Pubkey,
    authority: &Pubkey,
    index: u16,
    provider: PriceProviderKind,
    token: &Pubkey,
    store_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[
            PRICE_FEED_SEED,
            store.as_ref(),
            authority.as_ref(),
            &index.to_le_bytes(),
            &[provider as u8],
            token.as_ref(),
        ],
        store_program_id,
    )
}

/// Find PDA for global treasury config.
#[cfg(treasury)]
pub fn find_treasury_config_address(store: &Pubkey, treasury_program_id: &Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[TREASURY_CONFIG_SEED, store.as_ref()], treasury_program_id)
}

/// Find PDA for treasury vault config.
#[cfg(treasury)]
pub fn find_treasury_vault_config_address(
    config: &Pubkey,
    index: u16,
    treasury_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[
            TREASURY_VAULT_CONFIG_SEED,
            config.as_ref(),
            &index.to_le_bytes(),
        ],
        treasury_program_id,
    )
}

/// Find PDA for GT bank.
#[cfg(treasury)]
pub fn find_gt_bank_address(
    treasury_vault_config: &Pubkey,
    gt_exchange_vault: &Pubkey,
    treasury_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[
            GT_BANK_SEED,
            treasury_vault_config.as_ref(),
            gt_exchange_vault.as_ref(),
        ],
        treasury_program_id,
    )
}

/// Find PDA for treasury receiver.
#[cfg(treasury)]
pub fn find_treasury_receiver_address(
    config: &Pubkey,
    treasury_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[TREASURY_RECEIVER_SEED, config.as_ref()],
        treasury_program_id,
    )
}

/// Find PDA for timelock config.
#[cfg(timelock)]
pub fn find_timelock_config_address(store: &Pubkey, timelock_program_id: &Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[TIMELOCK_CONFIG_SEED, store.as_ref()], timelock_program_id)
}

/// Find PDA for executor.
#[cfg(timelock)]
pub fn find_executor_address(
    store: &Pubkey,
    role: &str,
    timelock_program_id: &Pubkey,
) -> crate::Result<(Pubkey, u8)> {
    Ok(Pubkey::find_program_address(
        &[
            TIMELOCK_EXECUTOR_SEED,
            store.as_ref(),
            &crate::utils::fixed_str::fixed_str_to_bytes::<
                { gmsol_programs::constants::MAX_ROLE_NAME_LEN },
            >(role)?,
        ],
        timelock_program_id,
    ))
}

/// Find PDA for executor wallet.
#[cfg(timelock)]
pub fn find_executor_wallet_address(
    executor: &Pubkey,
    timelock_program_id: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[TIMELOCK_EXECUTOR_WALLET_SEED, executor.as_ref()],
        timelock_program_id,
    )
}