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
//! State accounts.

use solana_farm_sdk::program::protocol::raydium::{
    raydium_stake, raydium_stake_v4, raydium_stake_v5, raydium_stake_v5_dev, raydium_v4,
};
use std::ops::Deref;
use vipers::unwrap_int;

use crate::*;

/// Number of milliBPS in 1.
pub const MILLIBPS_PER_WHOLE: u64 = 10_000 * 1_000;

/// A [UserInfo] manages user information related to a vault.
#[account]
#[derive(Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct UserInfo {
    // Public key of the vault
    pub vault: Pubkey,
    // Bump
    pub bump: u8,
    // User Account
    pub user_account: Pubkey,
    // User Info Manager - Should be the vault.
    pub manager: Pubkey,
    // Last Deposit Time
    pub last_deposit_at: i64,
    // Last Withdraw Time
    pub last_withdraw_at: i64,
    // Token A Added
    pub token_a_added: u64,
    // Token A Removed
    pub token_a_removed: u64,
    // Token B Added
    pub token_b_added: u64,
    // Token B Removed
    pub token_b_removed: u64,
    // Lp Tokens Debt
    pub lp_tokens_debt: u64,
}

impl UserInfo {
    pub fn update_deposit_time(&mut self) -> Result<()> {
        let clock = Clock::get()?;
        self.last_deposit_at = clock.unix_timestamp;
        Ok(())
    }

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

    pub fn add_liquidity(&mut self, token_a_added: u64, token_b_added: u64) -> Result<()> {
        if token_a_added > 0 {
            self.token_a_added = unwrap_int!(self.token_a_added.checked_add(token_a_added));
        }
        if token_b_added > 0 {
            self.token_b_added = unwrap_int!(self.token_b_added.checked_add(token_b_added));
        }
        if token_a_added > 0 || token_b_added > 0 {
            self.update_deposit_time()?;
        }
        Ok(())
    }

    pub fn remove_liquidity(&mut self, token_a_removed: u64, token_b_removed: u64) -> Result<()> {
        if token_a_removed > 0 {
            self.token_a_removed = unwrap_int!(self.token_a_removed.checked_add(token_a_removed));
        }
        if token_b_removed > 0 {
            self.token_b_removed = unwrap_int!(self.token_b_removed.checked_add(token_b_removed));
        }

        if token_a_removed > 0 || token_b_removed > 0 {
            self.update_withdraw_time()?;
        }
        Ok(())
    }

    pub fn add_lp_tokens_debt(&mut self, token_added: u64) -> Result<()> {
        self.lp_tokens_debt = unwrap_int!(self.lp_tokens_debt.checked_add(token_added));
        Ok(())
    }

    pub fn remove_lp_tokens_debt(&mut self, token_removed: u64) -> Result<()> {
        // safe to use unchecked sub
        if self.lp_tokens_debt <= token_removed {
            self.lp_tokens_debt = 0;
        } else {
            self.lp_tokens_debt = unwrap_int!(self.lp_tokens_debt.checked_sub(token_removed));
        }

        Ok(())
    }
}

/// A [Vault] manages tokens to stake into Raydium LPs.
///
#[account]
#[derive(Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Vault {
    /// Base of the strategy.
    pub base: Pubkey,
    /// Bump
    pub bump: u8,
    pub bump1: u8,
    pub bump2: u8,
    pub bump3: u8,

    /// Account which can manage the strategy.
    pub manager: Pubkey,

    // The vault mint.
    pub vault_lp_token_mint: Pubkey,

    // farm Mint
    pub farm_lp_mint: Pubkey,

    // Farm Reward Token Mints
    // These may be different than the pool mints.
    pub farm_reward_token_a_mint: Pubkey,
    pub farm_reward_token_b_mint: Pubkey,

    // For Pool Custody Accounts
    pub pool_lp_custody_account: Pubkey,
    pub pool_token_a_custody_account: Pubkey,
    pub pool_token_b_custody_account: Pubkey,

    // Farm Reward Custody Accounts
    pub farm_token_a_reward_custody_account: Pubkey,
    pub farm_token_b_reward_custody_account: Pubkey,

    // Pool Reserves
    pub pool_coin_token_account: Pubkey,
    pub pool_pc_token_account: Pubkey,

    /// Mint of pool.
    pub pool_token_a_mint: Pubkey,
    pub pool_token_b_mint: Pubkey,

    /// LP token mint.
    pub pool_lp_token_mint: Pubkey,

    pub amm: Pubkey,

    // Amm information
    pub amm_authority: Pubkey,
    pub amm_open_orders: Pubkey,
    pub amm_target: Pubkey,

    // Pool Information
    pub pool_withdraw_queue: Pubkey,
    pub pool_temp_lp_token_account: Pubkey,

    // Raydium Farm Id
    pub farm: Pubkey,

    // Raydium Farm Metadata
    pub farm_authority: Pubkey,
    pub farm_lp_token_account: Pubkey,
    pub farm_reward_token_a_account: Pubkey,
    pub farm_reward_token_b_account: Pubkey,

    // Serum information
    pub serum_market: Pubkey,
    pub serum_program_id: Pubkey,
    pub serum_coin_vault_account: Pubkey,
    pub serum_pc_vault_account: Pubkey,
    pub serum_vault_signer: Pubkey,

    // Raydium Pool Program ID
    pub pool_program_id: Pubkey,

    // Raydium Farm Program Id
    pub farm_program_id: Pubkey,
    pub farm_version: u64,

    // Vault Adminstrator Information
    pub deposits_allowed: bool,
    pub withdraws_allowed: bool,
    /// Withdraw fee in 1000th's of a basis point.
    pub fee_millibps: u64,
    pub external_fee_millibps: u64,

    // Crank Metadata
    pub crank_time: u64,
    pub crank_step: u64,
    pub min_crank_interval: u64,

    // Vault Information
    pub token_a_added: u64,
    pub token_a_removed: u64,
    pub token_b_added: u64,
    pub token_b_removed: u64,
    pub token_a_rewards: u64,
    pub token_b_rewards: u64,

    // stake info accounts
    pub stake_info_account: Pubkey,
    pub stake_info_account_v4: Pubkey,
    pub stake_info_account_v5: Pubkey,

    // fee accounts
    pub fees_account_a: Pubkey,
    pub fees_account_b: Pubkey,

    // serum information for swaps
    pub serum_bids: Pubkey,
    pub serum_asks: Pubkey,
    pub serum_event_queue: Pubkey,
    pub stake_info_account_v5_mainnet: Pubkey,
    pub bump4: u8,
    pub bump5: u8,
    pub stake_info_account_orca: Pubkey,
}

impl Vault {
    pub fn add_rewards(&mut self, token_a_rewards: u64, token_b_rewards: u64) -> Result<()> {
        if token_a_rewards > 0 {
            self.token_a_rewards = unwrap_int!(self.token_a_rewards.checked_add(token_a_rewards));
        }
        if token_b_rewards > 0 {
            self.token_b_rewards = unwrap_int!(self.token_b_rewards.checked_add(token_b_rewards));
        }
        Ok(())
    }

    pub fn add_liquidity(&mut self, token_a_added: u64, token_b_added: u64) -> Result<()> {
        if token_a_added > 0 {
            self.token_a_added = unwrap_int!(self.token_a_added.checked_add(token_a_added));
        }
        if token_b_added > 0 {
            self.token_b_added = unwrap_int!(self.token_b_added.checked_add(token_b_added));
        }
        Ok(())
    }

    pub fn remove_liquidity(&mut self, token_a_removed: u64, token_b_removed: u64) -> Result<()> {
        if token_a_removed > 0 {
            self.token_a_removed = unwrap_int!(self.token_a_removed.checked_add(token_a_removed));
        }
        if token_b_removed > 0 {
            self.token_b_removed = unwrap_int!(self.token_b_removed.checked_add(token_b_removed));
        }
        Ok(())
    }

    pub fn update_crank_time(&mut self) -> Result<()> {
        self.crank_time = clock::get_time_as_u64()?;
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AmmInfoV4(solana_farm_sdk::program::protocol::raydium::AmmInfoV4);

impl AmmInfoV4 {
    /// The length, in bytes, of the packed representation
    pub const LEN: usize = solana_farm_sdk::program::protocol::raydium::AmmInfoV4::LEN;

    /// Computes the minimum rent exempt balance of a [AMMAInfo].
    pub fn minimum_rent_exempt_balance() -> Result<u64> {
        Ok(Rent::get()?.minimum_balance(Self::LEN))
    }
}

impl anchor_lang::Id for AmmInfoV4 {
    fn id() -> Pubkey {
        raydium_v4::ID
        // raydium_dev_v4::ID
    }
}

impl Owner for AmmInfoV4 {
    fn owner() -> Pubkey {
        raydium_v4::ID
        // raydium_dev_v4::ID
    }
}

impl Deref for AmmInfoV4 {
    type Target = solana_farm_sdk::program::protocol::raydium::AmmInfoV4;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl anchor_lang::AccountSerialize for AmmInfoV4 {
    fn try_serialize<W: std::io::Write>(&self, _writer: &mut W) -> Result<()> {
        // no-op
        Ok(())
    }
}

impl anchor_lang::AccountDeserialize for AmmInfoV4 {
    fn try_deserialize(buf: &mut &[u8]) -> Result<Self> {
        AmmInfoV4::try_deserialize_unchecked(buf)
    }

    fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self> {
        Ok(solana_farm_sdk::program::protocol::raydium::AmmInfoV4::unpack(buf).map(AmmInfoV4)?)
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RaydiumUserStakeInfo(solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfo);

impl RaydiumUserStakeInfo {
    /// The length, in bytes, of the packed representation
    pub const LEN: usize = solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfo::LEN;

    /// Computes the minimum rent exempt balance of a [AMMAInfo].
    pub fn minimum_rent_exempt_balance() -> Result<u64> {
        Ok(Rent::get()?.minimum_balance(Self::LEN))
    }
}

impl anchor_lang::Id for RaydiumUserStakeInfo {
    fn id() -> Pubkey {
        raydium_stake::ID
    }
}

impl Owner for RaydiumUserStakeInfo {
    fn owner() -> Pubkey {
        raydium_stake::ID
    }
}

impl Deref for RaydiumUserStakeInfo {
    type Target = solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfo;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl anchor_lang::AccountSerialize for RaydiumUserStakeInfo {
    fn try_serialize<W: std::io::Write>(&self, _writer: &mut W) -> Result<()> {
        // no-op
        Ok(())
    }
}

impl anchor_lang::AccountDeserialize for RaydiumUserStakeInfo {
    fn try_deserialize(buf: &mut &[u8]) -> Result<Self> {
        RaydiumUserStakeInfo::try_deserialize_unchecked(buf)
    }

    fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self> {
        Ok(
            solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfo::unpack(buf)
                .map(RaydiumUserStakeInfo)?,
        )
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RaydiumUserStakeInfoV5Mainnet(
    solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfoV5,
);

impl RaydiumUserStakeInfoV5Mainnet {
    /// The length, in bytes, of the packed representation
    pub const LEN: usize = solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfoV5::LEN;

    /// Computes the minimum rent exempt balance of a [AMMAInfo].
    pub fn minimum_rent_exempt_balance() -> Result<u64> {
        Ok(Rent::get()?.minimum_balance(Self::LEN))
    }
}

impl anchor_lang::Id for RaydiumUserStakeInfoV5Mainnet {
    fn id() -> Pubkey {
        raydium_stake_v5::ID
    }
}

impl Owner for RaydiumUserStakeInfoV5Mainnet {
    fn owner() -> Pubkey {
        raydium_stake_v5::ID
    }
}

impl Deref for RaydiumUserStakeInfoV5Mainnet {
    type Target = solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfoV5;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl anchor_lang::AccountSerialize for RaydiumUserStakeInfoV5Mainnet {
    fn try_serialize<W: std::io::Write>(&self, _writer: &mut W) -> Result<()> {
        // no-op
        Ok(())
    }
}

impl anchor_lang::AccountDeserialize for RaydiumUserStakeInfoV5Mainnet {
    fn try_deserialize(buf: &mut &[u8]) -> Result<Self> {
        RaydiumUserStakeInfoV5Mainnet::try_deserialize_unchecked(buf)
    }

    fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self> {
        Ok(
            solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfoV5::unpack(buf)
                .map(RaydiumUserStakeInfoV5Mainnet)?,
        )
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct OrcaUserStakeInfo(solana_farm_sdk::program::protocol::orca::OrcaUserStakeInfo);

impl OrcaUserStakeInfo {
    /// The length, in bytes, of the packed representation
    pub const LEN: usize = solana_farm_sdk::program::protocol::orca::OrcaUserStakeInfo::LEN;

    /// Computes the minimum rent exempt balance of a [AMMAInfo].
    pub fn minimum_rent_exempt_balance() -> Result<u64> {
        Ok(Rent::get()?.minimum_balance(Self::LEN))
    }
}

impl anchor_lang::Id for OrcaUserStakeInfo {
    fn id() -> Pubkey {
        raydium_stake_v5::ID
    }
}

impl Owner for OrcaUserStakeInfo {
    fn owner() -> Pubkey {
        raydium_stake_v5::ID
    }
}

impl Deref for OrcaUserStakeInfo {
    type Target = solana_farm_sdk::program::protocol::orca::OrcaUserStakeInfo;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl anchor_lang::AccountSerialize for OrcaUserStakeInfo {
    fn try_serialize<W: std::io::Write>(&self, _writer: &mut W) -> Result<()> {
        // no-op
        Ok(())
    }
}

impl anchor_lang::AccountDeserialize for OrcaUserStakeInfo {
    fn try_deserialize(buf: &mut &[u8]) -> Result<Self> {
        OrcaUserStakeInfo::try_deserialize_unchecked(buf)
    }

    fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self> {
        Ok(
            solana_farm_sdk::program::protocol::orca::OrcaUserStakeInfo::unpack(buf)
                .map(OrcaUserStakeInfo)?,
        )
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RaydiumUserStakeInfoV4(
    solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfoV4,
);

impl RaydiumUserStakeInfoV4 {
    /// The length, in bytes, of the packed representation
    pub const LEN: usize = solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfoV4::LEN;

    /// Computes the minimum rent exempt balance of a [AMMAInfo].
    pub fn minimum_rent_exempt_balance() -> Result<u64> {
        Ok(Rent::get()?.minimum_balance(Self::LEN))
    }
}

impl anchor_lang::Id for RaydiumUserStakeInfoV4 {
    fn id() -> Pubkey {
        raydium_stake_v4::ID
    }
}

impl Owner for RaydiumUserStakeInfoV4 {
    fn owner() -> Pubkey {
        raydium_stake_v4::ID
    }
}

impl Deref for RaydiumUserStakeInfoV4 {
    type Target = solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfoV4;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl anchor_lang::AccountSerialize for RaydiumUserStakeInfoV4 {
    fn try_serialize<W: std::io::Write>(&self, _writer: &mut W) -> Result<()> {
        // no-op
        Ok(())
    }
}

impl anchor_lang::AccountDeserialize for RaydiumUserStakeInfoV4 {
    fn try_deserialize(buf: &mut &[u8]) -> Result<Self> {
        RaydiumUserStakeInfoV4::try_deserialize_unchecked(buf)
    }

    fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self> {
        Ok(
            solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfoV4::unpack(buf)
                .map(RaydiumUserStakeInfoV4)?,
        )
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RaydiumUserStakeInfoV5(
    solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfoV5,
);

impl RaydiumUserStakeInfoV5 {
    /// The length, in bytes, of the packed representation
    pub const LEN: usize = solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfoV5::LEN;

    /// Computes the minimum rent exempt balance of a [AMMAInfo].
    pub fn minimum_rent_exempt_balance() -> Result<u64> {
        Ok(Rent::get()?.minimum_balance(Self::LEN))
    }
}

impl anchor_lang::Id for RaydiumUserStakeInfoV5 {
    fn id() -> Pubkey {
        raydium_stake_v5_dev::ID
    }
}

impl Owner for RaydiumUserStakeInfoV5 {
    fn owner() -> Pubkey {
        raydium_stake_v5_dev::ID
    }
}

impl Deref for RaydiumUserStakeInfoV5 {
    type Target = solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfoV5;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl anchor_lang::AccountSerialize for RaydiumUserStakeInfoV5 {
    fn try_serialize<W: std::io::Write>(&self, _writer: &mut W) -> Result<()> {
        // no-op
        Ok(())
    }
}

impl anchor_lang::AccountDeserialize for RaydiumUserStakeInfoV5 {
    fn try_deserialize(buf: &mut &[u8]) -> Result<Self> {
        RaydiumUserStakeInfoV5::try_deserialize_unchecked(buf)
    }

    fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self> {
        Ok(
            solana_farm_sdk::program::protocol::raydium::RaydiumUserStakeInfoV5::unpack(buf)
                .map(RaydiumUserStakeInfoV5)?,
        )
    }
}