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
use crate::error::AccessError;
use bonfida_utils::BorshSize;
use borsh::{BorshDeserialize, BorshSerialize};
use bytemuck::{cast_slice, from_bytes, from_bytes_mut, try_cast_slice_mut, Pod, Zeroable};
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::FromPrimitive;
use solana_program::account_info::AccountInfo;
use solana_program::clock::Clock;
use solana_program::entrypoint::ProgramResult;
use solana_program::msg;
use solana_program::program_error::ProgramError;
use solana_program::pubkey::Pubkey;
use solana_program::sysvar::Sysvar;
use std::cell::RefMut;
use std::convert::TryInto;
use std::mem::size_of;
use std::ops::DerefMut;

/// ACCESS token mint
pub const ACCESS_MINT: Pubkey =
    solana_program::pubkey!("5MAYDfq5yxtudAhtfyuMBuHZjgAbaS9tbEyEQYAhDS5y");

#[allow(missing_docs)]
pub const SECONDS_IN_DAY: u64 = if cfg!(feature = "days-to-sec-15m") {
    15 * 60
} else if cfg!(feature = "days-to-sec-10s") {
    10
} else {
    3600 * 24
};

/// Percentage of the staking rewards going to stakers
pub const STAKER_MULTIPLIER: u64 = 50;

/// Percentage of the staking rewards going to the pool owner
pub const OWNER_MULTIPLIER: u64 = 100 - STAKER_MULTIPLIER;

/// Length of the circular buffer (stores balances for 1 year)
pub const STAKE_BUFFER_LEN: u64 = 274; // 9 Months

/// Max pending unstake requests
pub const MAX_UNSTAKE_REQUEST: usize = 10;

/// Fees charged on staking instruction in % (i.e FEES = 1 <-> 1% fee charged)
pub const FEES: u64 = 2;

#[derive(BorshSerialize, BorshDeserialize, BorshSize, PartialEq, FromPrimitive, ToPrimitive, Debug)]
#[repr(u8)]
#[allow(missing_docs)]
pub enum Tag {
    Uninitialized,
    StakePool,
    InactiveStakePool,
    StakeAccount,
    // Bond accounts are inactive until the buyer transfered the funds
    InactiveBondAccount,
    BondAccount,
    CentralState,
    Deleted,
    // Accounts frozen by the central state authority
    FrozenStakePool,
    FrozenStakeAccount,
    FrozenBondAccount,
}

impl Tag {
    /// Freeze or unfreeze an account tag
    pub fn opposite(&self) -> Result<Tag, ProgramError> {
        let tag = match self {
            Tag::StakePool => Tag::FrozenStakePool,
            Tag::StakeAccount => Tag::FrozenStakeAccount,
            Tag::BondAccount => Tag::FrozenBondAccount,
            Tag::FrozenStakePool => Tag::StakePool,
            Tag::FrozenStakeAccount => Tag::StakeAccount,
            Tag::FrozenBondAccount => Tag::BondAccount,
            _ => return Err(AccessError::InvalidTagChange.into()),
        };

        Ok(tag)
    }
}

#[derive(BorshSerialize, BorshDeserialize, BorshSize, Copy, Clone, Pod, Zeroable, Debug)]
#[repr(C)]
#[allow(missing_docs)]
pub struct StakePoolHeader {
    /// Tag
    pub tag: u8,

    /// Stake pool nonce
    pub nonce: u8,

    /// Updated by a trustless cranker
    pub current_day_idx: u16,

    /// Padding
    pub _padding: [u8; 4],

    /// Minimum amount to stake to get access to the pool
    pub minimum_stake_amount: u64,

    /// Total amount staked in the pool
    pub total_staked: u64,

    /// Last time the stake pool owner claimed as an offset from the central state's creation time
    pub last_claimed_offset: u64,

    /// The % of pool rewards going to stakers
    pub stakers_part: u64,

    /// Owner of the stake pool
    pub owner: [u8; 32],

    /// Stake pool vault
    pub vault: [u8; 32],
}

#[allow(missing_docs)]
pub struct StakePool<H, B> {
    pub header: H,
    /// Circular buffer of length STAKE_BUFFER_LEN storing (inflation * pool_total_staked / total_staked) in FP32 format
    pub balances: B,
}

/// The Rewards structure that is held in the stake pools circular buffer.
/// The two fields represent the share that is owed to the pool owner and the stakers respectively.
/// The values are stored in the FP32 format.
#[derive(Pod, Clone, Copy, Zeroable, Debug)]
#[repr(C)]
pub struct RewardsTuple {
    pub(crate) pool_reward: u128,
    pub(crate) stakers_reward: u128,
}

#[allow(missing_docs)]
pub type StakePoolRef<'a> = StakePool<RefMut<'a, StakePoolHeader>, RefMut<'a, [RewardsTuple]>>;

#[allow(missing_docs)]
pub type StakePoolHeaped = StakePool<Box<StakePoolHeader>, Box<[RewardsTuple]>>;

#[allow(missing_docs)]
impl<'a> StakePoolRef<'a> {
    pub fn get_checked<'b: 'a>(
        account_info: &'a AccountInfo<'b>,
        allowed_tags: Vec<Tag>,
    ) -> Result<Self, ProgramError> {
        let (header, balances) = RefMut::map_split(account_info.data.borrow_mut(), |s| {
            let (hd, rem) = s.split_at_mut(size_of::<StakePoolHeader>());
            (
                from_bytes_mut::<StakePoolHeader>(hd),
                try_cast_slice_mut(rem).unwrap(),
            )
        });

        let tag = FromPrimitive::from_u8(header.tag).ok_or(ProgramError::InvalidAccountData)?;
        if !allowed_tags.contains(&tag) {
            return Err(AccessError::DataTypeMismatch.into());
        }

        Ok(StakePool { header, balances })
    }
}

#[allow(missing_docs)]
impl StakePoolHeaped {
    pub fn from_buffer(buf: &[u8]) -> Self {
        println!("StakePoolHeaped::from_buffer: buf.len() = {}", buf.len());
        let (header, balances) = buf.split_at(size_of::<StakePoolHeader>());
        println!("StakePoolHeaped::from_buffer: header.len() = {}", header.len());
        let header = from_bytes::<StakePoolHeader>(header);
        println!("StakePoolHeaped::from_buffer: header = {:?}", header);
        let balances = cast_slice::<_, RewardsTuple>(balances);
        println!("StakePoolHeaped::from_buffer: balances.len() = {}", balances.len());
        Self {
            header: Box::new(*header),
            balances: Box::from(balances),
        }
    }
}

#[allow(missing_docs)]
impl<H: DerefMut<Target = StakePoolHeader>, B: DerefMut<Target = [RewardsTuple]>> StakePool<H, B> {
    pub fn push_balances_buff(
        &mut self,
        current_offset: u64,
        last_crank_offset: u64,
        rewards: RewardsTuple,
    ) -> Result<(), ProgramError> {
        let nb_days_passed = current_offset - last_crank_offset;
        for i in 1..nb_days_passed {
            self.balances[(((self.header.current_day_idx as u64)
                .checked_add(i)
                .ok_or(AccessError::Overflow)?)
                % STAKE_BUFFER_LEN) as usize] = RewardsTuple {
                pool_reward: 0,
                stakers_reward: 0,
            };
        }
        self.header.current_day_idx = self
            .header
            .current_day_idx
            .checked_add(
                nb_days_passed
                    .try_into()
                    .map_err(|_| AccessError::Overflow)?,
            )
            .ok_or(AccessError::Overflow)?;

        self.balances[(((self.header.current_day_idx - 1) as u64) % STAKE_BUFFER_LEN) as usize] =
            rewards;
        Ok(())
    }

    pub fn create_key(
        nonce: &u8,
        owner: &Pubkey,
        program_id: &Pubkey,
    ) -> Result<Pubkey, ProgramError> {
        let seeds: &[&[u8]] = &[StakePoolHeader::SEED, &owner.to_bytes(), &[*nonce]];
        Pubkey::create_program_address(seeds, program_id).map_err(|_| ProgramError::InvalidSeeds)
    }
}

#[allow(missing_docs)]
impl StakePool<(), ()> {
    pub fn find_key(owner: &Pubkey, program_id: &Pubkey) -> (Pubkey, u8) {
        let seeds: &[&[u8]] = &[StakePoolHeader::SEED, &owner.to_bytes()];
        Pubkey::find_program_address(seeds, program_id)
    }
}

#[allow(missing_docs)]
impl StakePoolHeader {
    pub const SEED: &'static [u8; 10] = b"stake_pool";

    pub fn new(
        owner: Pubkey,
        nonce: u8,
        vault: Pubkey,
        minimum_stake_amount: u64,
    ) -> Result<Self, ProgramError> {
        Ok(Self {
            tag: Tag::InactiveStakePool as u8,
            total_staked: 0,
            current_day_idx: 0,
            _padding: [0; 4],
            last_claimed_offset: 0,
            owner: owner.to_bytes(),
            nonce,
            vault: vault.to_bytes(),
            minimum_stake_amount,
            stakers_part: STAKER_MULTIPLIER,
        })
    }

    pub fn close(&mut self) {
        self.tag = Tag::Deleted as u8
    }

    pub fn deposit(&mut self, amount: u64) -> ProgramResult {
        self.total_staked = self
            .total_staked
            .checked_add(amount)
            .ok_or(AccessError::Overflow)?;
        Ok(())
    }

    pub fn withdraw(&mut self, amount: u64) -> ProgramResult {
        self.total_staked = self
            .total_staked
            .checked_sub(amount)
            .ok_or(AccessError::Overflow)?;
        Ok(())
    }
}

#[derive(BorshSerialize, BorshDeserialize, BorshSize)]
#[allow(missing_docs)]
pub struct StakeAccount {
    /// Tag
    pub tag: Tag,

    /// Owner of the stake account
    pub owner: Pubkey,

    /// Amount staked in the account
    pub stake_amount: u64,

    /// Stake pool to which the account belongs to
    pub stake_pool: Pubkey,

    /// Offset of a last day where rewards were claimed from the contract creation date
    pub last_claimed_offset: u64,

    /// Minimum stakeable amount of the pool when the account
    /// was created
    pub pool_minimum_at_creation: u64,
}

#[allow(missing_docs)]
impl StakeAccount {
    pub const SEED: &'static [u8; 13] = b"stake_account";

    pub fn new(owner: Pubkey, stake_pool: Pubkey, pool_minimum_at_creation: u64) -> Self {
        Self {
            tag: Tag::StakeAccount,
            owner,
            stake_amount: 0,
            stake_pool,
            last_claimed_offset: 0,
            pool_minimum_at_creation,
        }
    }

    pub fn create_key(
        nonce: &u8,
        owner: &Pubkey,
        stake_pool: &Pubkey,
        program_id: &Pubkey,
    ) -> Result<Pubkey, ProgramError> {
        let seeds: &[&[u8]] = &[
            StakeAccount::SEED,
            &owner.to_bytes(),
            &stake_pool.to_bytes(),
            &[*nonce],
        ];
        Pubkey::create_program_address(seeds, program_id).map_err(|_| ProgramError::InvalidSeeds)
    }

    pub fn find_key(owner: &Pubkey, stake_pool: &Pubkey, program_id: &Pubkey) -> (Pubkey, u8) {
        let seeds: &[&[u8]] = &[
            StakeAccount::SEED,
            &owner.to_bytes(),
            &stake_pool.to_bytes(),
        ];
        Pubkey::find_program_address(seeds, program_id)
    }

    pub fn save(&self, mut dst: &mut [u8]) -> ProgramResult {
        self.serialize(&mut dst)
            .map_err(|_| ProgramError::InvalidAccountData)
    }

    pub fn from_account_info(a: &AccountInfo) -> Result<StakeAccount, ProgramError> {
        let mut data = &a.data.borrow() as &[u8];
        if data[0] != Tag::StakeAccount as u8 && data[0] != Tag::Uninitialized as u8 {
            return Err(AccessError::DataTypeMismatch.into());
        }
        let result = StakeAccount::deserialize(&mut data)?;
        Ok(result)
    }

    pub fn close(&mut self) {
        self.tag = Tag::Deleted
    }

    pub fn deposit(&mut self, amount: u64) -> ProgramResult {
        self.stake_amount = self
            .stake_amount
            .checked_add(amount)
            .ok_or(AccessError::Overflow)?;
        Ok(())
    }

    pub fn withdraw(&mut self, amount: u64) -> ProgramResult {
        self.stake_amount = self
            .stake_amount
            .checked_sub(amount)
            .ok_or(AccessError::Overflow)?;
        Ok(())
    }
}

#[derive(BorshSerialize, BorshDeserialize, BorshSize)]
#[allow(missing_docs)]
pub struct CentralState {
    /// Tag
    pub tag: Tag,

    /// Central state nonce
    pub signer_nonce: u8,

    /// Daily inflation in token amount, inflation is paid from
    /// the reserve owned by the central state
    pub daily_inflation: u64,

    /// Mint of the token being emitted
    pub token_mint: Pubkey,

    /// Authority
    /// The public key that can change the inflation
    pub authority: Pubkey,

    /// Creation timestamp
    pub creation_time: i64,

    /// Total amount of staked tokens
    pub total_staked: u64,

    /// The daily total_staked snapshot to calculate correctly calculate the pool rewards
    pub total_staked_snapshot: u64,

    /// The offset of the total_staked_snapshot from the creation_time in days
    pub last_snapshot_offset: u64,
}

impl CentralState {
    #[allow(missing_docs)]
    pub fn new(
        signer_nonce: u8,
        daily_inflation: u64,
        token_mint: Pubkey,
        authority: Pubkey,
        total_staked: u64,
    ) -> Result<Self, ProgramError> {
        Ok(Self {
            tag: Tag::CentralState,
            signer_nonce,
            daily_inflation,
            token_mint,
            authority,
            creation_time: Clock::get()?.unix_timestamp,
            total_staked,
            total_staked_snapshot: 0,
            last_snapshot_offset: 0,
        })
    }
    #[allow(missing_docs)]
    pub fn create_key(signer_nonce: &u8, program_id: &Pubkey) -> Result<Pubkey, ProgramError> {
        let signer_seeds: &[&[u8]] = &[&program_id.to_bytes(), &[*signer_nonce]];
        Pubkey::create_program_address(signer_seeds, program_id)
            .map_err(|_| ProgramError::InvalidSeeds)
    }
    #[allow(missing_docs)]
    pub fn find_key(program_id: &Pubkey) -> (Pubkey, u8) {
        Pubkey::find_program_address(&[&program_id.to_bytes()], program_id)
    }
    #[allow(missing_docs)]
    pub fn save(&self, mut dst: &mut [u8]) -> ProgramResult {
        self.serialize(&mut dst)
            .map_err(|_| ProgramError::InvalidAccountData)
    }
    #[allow(missing_docs)]
    pub fn from_account_info(a: &AccountInfo) -> Result<CentralState, ProgramError> {
        let mut data = &a.data.borrow() as &[u8];
        if data[0] != Tag::CentralState as u8 && data[0] != Tag::Uninitialized as u8 {
            return Err(AccessError::DataTypeMismatch.into());
        }
        let result = CentralState::deserialize(&mut data)?;
        Ok(result)
    }
    #[allow(missing_docs)]
    pub fn get_current_offset(&self) -> Result<u64, ProgramError> {
        let current_time = Clock::get()?.unix_timestamp as u64;
        Ok((current_time - self.creation_time as u64) / SECONDS_IN_DAY)
    }
}

/// Number of sellers who need to agree for a bond to be sold
pub const BOND_SIGNER_THRESHOLD: u64 = 1;

/// List of authorized bond sellers
pub const AUTHORIZED_BOND_SELLERS: [Pubkey; 1] = [solana_program::pubkey!(
    "3Nrq6mCNL5i8Qk4APhggbwXismcsF23gNVDEaKycZBL8"
)];

#[derive(BorshSerialize, BorshDeserialize, BorshSize)]
#[allow(missing_docs)]
pub struct BondAccount {
    // Tag
    pub tag: Tag,

    // Owner of the bond
    pub owner: Pubkey,

    // Total amount sold
    pub total_amount_sold: u64,

    // Total staked tokens
    pub total_staked: u64,

    // Total quote token
    pub total_quote_amount: u64,

    // Quote mint used to buy the bond
    pub quote_mint: Pubkey,

    // Seller token account (i.e destination of the quote tokens)
    pub seller_token_account: Pubkey,

    // Unlock start date
    pub unlock_start_date: i64,

    // Unlock period
    // time interval at which the tokens unlock
    pub unlock_period: i64,

    // Unlock amount
    // amount unlocked at every unlock_period
    pub unlock_amount: u64,

    // Last unlock date
    pub last_unlock_time: i64,

    // Total amount unlocked (metric)
    pub total_unlocked_amount: u64,

    // Minimum stakeable amount of the pool when the account
    // was created
    pub pool_minimum_at_creation: u64,

    // Stake pool to which the account belongs to
    pub stake_pool: Pubkey,

    // Last offset of the from the contract creation time in days
    pub last_claimed_offset: u64,

    // Sellers who signed for the sell of the bond account
    pub sellers: Vec<Pubkey>,
}

#[allow(missing_docs)]
impl BondAccount {
    pub const SEED: &'static [u8; 12] = b"bond_account";

    pub fn create_key(owner: &Pubkey, total_amount_sold: u64, program_id: &Pubkey) -> (Pubkey, u8) {
        let seeds: &[&[u8]] = &[
            BondAccount::SEED,
            &owner.to_bytes(),
            &total_amount_sold.to_le_bytes(),
        ];
        Pubkey::find_program_address(seeds, program_id)
    }

    #[allow(clippy::too_many_arguments)]
    pub fn new(
        owner: Pubkey,
        total_amount_sold: u64,
        total_quote_amount: u64,
        quote_mint: Pubkey,
        seller_token_account: Pubkey,
        unlock_start_date: i64,
        unlock_period: i64,
        unlock_amount: u64,
        last_unlock_time: i64,
        pool_minimum_at_creation: u64,
        stake_pool: Pubkey,
        seller: Pubkey,
    ) -> Self {
        let sellers = vec![seller];
        Self {
            tag: Tag::InactiveBondAccount,
            owner,
            total_amount_sold,
            total_staked: total_amount_sold,
            total_quote_amount,
            quote_mint,
            seller_token_account,
            unlock_start_date,
            unlock_period,
            unlock_amount,
            last_unlock_time,
            total_unlocked_amount: 0,
            stake_pool,
            last_claimed_offset: 0,
            sellers,
            pool_minimum_at_creation,
        }
    }

    pub fn save(&self, mut dst: &mut [u8]) -> ProgramResult {
        self.serialize(&mut dst)
            .map_err(|_| ProgramError::InvalidAccountData)
    }

    pub fn is_active(&self) -> bool {
        self.tag == Tag::BondAccount
    }

    pub fn activate(&mut self, current_offset: u64) -> ProgramResult {
        self.tag = Tag::BondAccount;
        self.last_claimed_offset = current_offset;
        let current_time = Clock::get()?.unix_timestamp;
        self.last_unlock_time = std::cmp::max(current_time, self.unlock_start_date);
        Ok(())
    }

    pub fn from_account_info(
        a: &AccountInfo,
        allow_inactive: bool,
    ) -> Result<BondAccount, ProgramError> {
        let mut data = &a.data.borrow() as &[u8];
        let tag = if allow_inactive {
            Tag::InactiveBondAccount
        } else {
            Tag::BondAccount
        };
        if data[0] != tag as u8 && data[0] != Tag::Uninitialized as u8 {
            return Err(AccessError::DataTypeMismatch.into());
        }
        let result = BondAccount::deserialize(&mut data)?;
        Ok(result)
    }

    pub fn calc_unlock_amount(&self, missed_periods: u64) -> Result<u64, ProgramError> {
        msg!("Missed periods {}", missed_periods);
        let cumulated_unlock_amnt = (missed_periods)
            .checked_mul(self.unlock_amount)
            .ok_or(AccessError::Overflow)?;
        msg!(
            "Unlock amount {} Total amount {}",
            cumulated_unlock_amnt,
            self.total_amount_sold
        );

        Ok(std::cmp::min(
            cumulated_unlock_amnt,
            self.total_amount_sold
                .checked_sub(self.total_unlocked_amount)
                .ok_or(AccessError::Overflow)?,
        ))
    }
}