lockup 1.1.2

Saber token lockup.
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! A token lockup program for linear release with cliff.
#![deny(rustdoc::all)]
#![allow(rustdoc::missing_doc_code_examples)]
#![allow(deprecated)]

/// Returns the program-derived-address seeds used for creating the associated
/// account.
macro_rules! associated_seeds {
    ($state:expr, $($with:expr),+) => {
        &[
            b"anchor".as_ref(),
            $($with),+,
            &[$state.nonce()],
        ]
    };
}

use anchor_lang::prelude::*;
use anchor_lang::{accounts::cpi_state::CpiState, solana_program::pubkey::PUBKEY_BYTES};
use anchor_spl::token::{self, Mint, Token, TokenAccount};
use mint_proxy::mint_proxy::MintProxy;
use mint_proxy::MinterInfo;
use vipers::prelude::*;

pub mod calculator;

declare_id!("LockKXdYQVMbhhckwH3BxoYJ9FYatcZjwNGEuCwY33Q");

/// Saber token lockup program.
#[program]
pub mod lockup {
    use super::*;

    #[state]
    pub struct Lockup {
        /// Owner that controls/creates the lockup.
        pub owner: Pubkey,
        /// Next owner.
        pub pending_owner: Pubkey,
    }

    impl Lockup {
        /// Initializes the [Lockup].
        pub fn new(ctx: Context<Initialize>) -> Result<Lockup> {
            Ok(Lockup {
                owner: ctx.accounts.auth.owner.key(),
                pending_owner: Pubkey::default(),
            })
        }

        /// Creates a new [Release].
        #[access_control(check_auth(self, &ctx.accounts.auth))]
        pub fn create_release(
            &self,
            ctx: Context<CreateRelease>,
            release_amount: u64,
            start_ts: i64,
            end_ts: i64,
        ) -> Result<()> {
            require!(release_amount != 0, InvalidDepositAmount);
            require!(is_valid_schedule(start_ts, end_ts), InvalidSchedule);

            // minter_info validations
            require!(
                *ctx.accounts.minter_info.to_account_info().owner
                    == ctx.accounts.mint_proxy_program.key(),
                MinterInfoProgramMismatch
            );
            require!(
                ctx.accounts.minter_info.allowance >= release_amount,
                MinterAllowanceTooLow
            );
            require!(
                ctx.accounts.minter_info.minter == ctx.accounts.release.key(),
                MinterUnauthorized
            );

            let release = &mut ctx.accounts.release;
            release.beneficiary = ctx.accounts.beneficiary.key();
            release.mint = ctx.accounts.mint.key();
            release.mint_proxy_program = ctx.accounts.mint_proxy_program.key();
            release.minter_info = ctx.accounts.minter_info.key();
            release.start_balance = release_amount;
            release.end_ts = end_ts;
            release.start_ts = start_ts;
            release.created_ts = Clock::get()?.unix_timestamp;
            release.outstanding = release_amount;
            release.__nonce = *unwrap_int!(ctx.bumps.get("release"));

            emit!(ReleaseCreatedEvent {
                beneficiary: release.beneficiary,
                mint: release.mint,
                release_amount,
                created_at: release.created_ts,
                start_at: release.start_ts,
                end_at: release.end_ts,
            });

            Ok(())
        }

        /// Revokes a [Release].
        #[access_control(check_auth(self, &ctx.accounts.auth))]
        pub fn revoke_release(&self, ctx: Context<RevokeRelease>) -> Result<()> {
            require!(
                ctx.accounts.release.outstanding == ctx.accounts.release.start_balance,
                ReleaseAlreadyRedeemedFrom
            );
            Ok(())
        }

        /// Transfers ownership of the [Lockup] to another account.
        #[access_control(check_auth(self, &ctx.accounts))]
        pub fn transfer_ownership(&mut self, ctx: Context<Auth>, next_owner: Pubkey) -> Result<()> {
            self.pending_owner = next_owner;
            Ok(())
        }

        /// Accepts the new ownership of the [Lockup].
        pub fn accept_ownership(&mut self, ctx: Context<Auth>) -> Result<()> {
            require!(ctx.accounts.owner.is_signer, Unauthorized);
            require!(
                self.pending_owner == ctx.accounts.owner.key(),
                PendingOwnerMismatch
            );
            self.owner = self.pending_owner;
            self.pending_owner = Pubkey::default();
            Ok(())
        }

        /// Withdraws all available [Release] tokens.
        pub fn withdraw(&self, ctx: Context<Withdraw>) -> Result<()> {
            ctx.accounts.validate()?;

            // calculate amount to withdraw
            let release = &ctx.accounts.release;
            let amount =
                calculator::available_for_withdrawal(release, Clock::get()?.unix_timestamp);

            // Short circuit if withdraw amount is zero.
            if amount == 0 {
                return Ok(());
            }

            require!(
                ctx.accounts.minter_info.allowance >= amount,
                MinterAllowanceTooLow
            );

            // Mint rewards
            let cpi_accounts = mint_proxy::cpi::accounts::PerformMint {
                proxy_mint_authority: ctx.accounts.proxy_mint_authority.to_account_info(),
                minter: ctx.accounts.release.to_account_info(),
                token_mint: ctx.accounts.token_mint.to_account_info(),
                destination: ctx.accounts.token_account.to_account_info(),
                minter_info: ctx.accounts.minter_info.to_account_info(),
                token_program: ctx.accounts.token_program.to_account_info(),
            };
            let beneficiary_key = ctx.accounts.beneficiary.key().to_bytes();
            let seeds = associated_seeds!(ctx.accounts.release, &beneficiary_key);
            let signer_seeds = &[&seeds[..]];
            let cpi_program = ctx.accounts.mint_proxy_program.to_account_info();
            let cpi_state_context =
                CpiContext::new_with_signer(cpi_program, cpi_accounts, signer_seeds);
            mint_proxy::invoke_perform_mint(
                cpi_state_context,
                ctx.accounts.mint_proxy_state.to_account_info(),
                amount,
            )?;

            // Bookkeeping.
            let release = &mut ctx.accounts.release;
            release.outstanding = unwrap_int!(release.outstanding.checked_sub(amount));

            emit!(WithdrawEvent {
                beneficiary: release.beneficiary,
                mint: release.mint,
                outstanding_amount: release.outstanding,
                withdraw_amount: amount,
                timestamp: Clock::get()?.unix_timestamp
            });

            Ok(())
        }

        /// Withdraws tokens from the [Release] with an amount.
        pub fn withdraw_with_amount(&self, ctx: Context<Withdraw>, amount: u64) -> Result<()> {
            // Short circuit if withdraw amount is zero.
            if amount == 0 {
                return Ok(());
            }

            ctx.accounts.validate()?;

            let amount_released = calculator::available_for_withdrawal(
                &ctx.accounts.release,
                Clock::get()?.unix_timestamp,
            );
            // Has the given amount released?
            require!(amount <= amount_released, InsufficientWithdrawalBalance);
            // Enough mint allowance for mint?
            require!(
                ctx.accounts.minter_info.allowance >= amount,
                MinterAllowanceTooLow
            );

            // Mint rewards
            let cpi_accounts = mint_proxy::cpi::accounts::PerformMint {
                proxy_mint_authority: ctx.accounts.proxy_mint_authority.to_account_info(),
                minter: ctx.accounts.release.to_account_info(),
                token_mint: ctx.accounts.token_mint.to_account_info(),
                destination: ctx.accounts.token_account.to_account_info(),
                minter_info: ctx.accounts.minter_info.to_account_info(),
                token_program: ctx.accounts.token_program.to_account_info(),
            };
            let beneficiary_key = ctx.accounts.beneficiary.key().to_bytes();
            let seeds = associated_seeds!(ctx.accounts.release, &beneficiary_key);
            let signer_seeds = &[&seeds[..]];
            let cpi_program = ctx.accounts.mint_proxy_program.to_account_info();
            let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer_seeds);
            mint_proxy::invoke_perform_mint(
                cpi_ctx,
                ctx.accounts.mint_proxy_state.to_account_info(),
                amount,
            )?;

            let release = &mut ctx.accounts.release;
            // Bookkeeping.
            release.outstanding = unwrap_int!(release.outstanding.checked_sub(amount));

            emit!(WithdrawEvent {
                beneficiary: release.beneficiary,
                mint: release.mint,
                outstanding_amount: release.outstanding,
                withdraw_amount: amount,
                timestamp: Clock::get()?.unix_timestamp
            });

            Ok(())
        }
    }

    /// Convenience function for UI's to calculate the withdrawable amount.
    pub fn available_for_withdrawal(ctx: Context<AvailableForWithdrawal>) -> Result<()> {
        let available = calculator::available_for_withdrawal(
            &ctx.accounts.release,
            ctx.accounts.clock.unix_timestamp,
        );
        // Log as string so that JS can read as a BN.
        msg!(&format!("{{ \"result\": \"{}\" }}", available));
        Ok(())
    }
}

#[derive(Accounts)]
pub struct Auth<'info> {
    pub owner: Signer<'info>,
}

#[derive(Accounts)]
pub struct Initialize<'info> {
    pub auth: Auth<'info>,
    pub mint_proxy_state: CpiState<'info, MintProxy>,
    pub mint_proxy_program: Program<'info, mint_proxy::program::MintProxy>,
}

#[derive(Accounts)]
pub struct CreateRelease<'info> {
    /// Authentication for authority of the [lockup::Lockup].
    pub auth: Auth<'info>,
    /// Minter info account.
    pub minter_info: Account<'info, MinterInfo>,
    /// Account able to withdraw from the [Release].
    /// CHECK: Arbitrary.
    pub beneficiary: UncheckedAccount<'info>,
    /// [Release] account.
    #[account(
        init,
        seeds = [
            b"anchor".as_ref(),
            beneficiary.key().as_ref()
        ],
        bump,
        space = 8 + Release::LEN,
        payer = payer
    )]
    pub release: Account<'info, Release>,
    /// Token to be released.
    pub mint: Account<'info, Mint>,
    /// Mint proxy program.
    pub mint_proxy_program: Program<'info, mint_proxy::program::MintProxy>,
    /// Payer for the [Release] account creation.
    #[account(mut)]
    pub payer: Signer<'info>,
    /// System program.
    pub system_program: Program<'info, System>,
    /// Rent sysvar.
    pub rent: Sysvar<'info, Rent>,
}

#[derive(Accounts)]
pub struct RevokeRelease<'info> {
    /// Authentication for authority of the [lockup::Lockup].
    pub auth: Auth<'info>,
    /// [Release] account.
    #[account(mut, close = payer)]
    pub release: Account<'info, Release>,
    /// Recipient of the [Release] account lamports.
    /// CHECK: Arbitrary.
    pub payer: UncheckedAccount<'info>,
}

#[derive(Accounts)]
pub struct Withdraw<'info> {
    /// Mint authority of the proxy.
    /// CHECK: Arbitrary.
    pub proxy_mint_authority: UncheckedAccount<'info>,
    /// Mint of the token unlocked.
    #[account(mut)]
    pub token_mint: Account<'info, Mint>,
    /// Owner of the [Release].
    pub beneficiary: Signer<'info>,
    /// [Release].
    #[account(mut, has_one = beneficiary)]
    pub release: Account<'info, Release>,
    /// Beneficiary token account.
    #[account(mut)]
    pub token_account: Account<'info, TokenAccount>,
    /// Token program.
    pub token_program: Program<'info, Token>,
    /// Clock sysvar, now unused and may be any account.
    /// CHECK: Arbitrary.
    pub unused_clock: UncheckedAccount<'info>,
    /// Minter info.
    #[account(mut)]
    pub minter_info: Account<'info, MinterInfo>,
    /// Mint proxy program.
    pub mint_proxy_program: Program<'info, mint_proxy::program::MintProxy>,
    /// Mint proxy state.
    pub mint_proxy_state: CpiState<'info, mint_proxy::mint_proxy::MintProxy>,
}

impl<'info> Withdraw<'info> {
    fn validate(&self) -> Result<()> {
        // proxy_mint_authority validations
        assert_keys_eq!(
            self.proxy_mint_authority,
            self.mint_proxy_state.proxy_mint_authority,
            ProxyMintAuthorityMismatch
        );

        // token_mint validations
        require!(self.token_mint.key() == self.release.mint, InvalidTokenMint);
        require!(
            self.token_mint.key() == self.mint_proxy_state.token_mint,
            MintProxyMintMismatch
        );

        // beneficiary validations
        require!(
            self.beneficiary.key() == self.release.beneficiary,
            InvalidBeneficiary,
        );
        require!(self.beneficiary.is_signer, InvalidBeneficiary);

        // release validations
        require!(
            self.release.key() == self.minter_info.minter,
            ReleaseMismatch,
        );

        // token_account validations
        require!(
            self.token_account.mint == self.release.mint,
            DestinationMintMismatch,
        );

        // token_program validations
        require!(self.token_program.key() == token::ID, TokenProgramMismatch,);

        // minter_info validations
        require!(
            self.minter_info.key() == self.release.minter_info,
            MinterInfoMismatch
        );

        // mint_proxy_program validations
        require!(
            self.mint_proxy_program.key() == self.release.mint_proxy_program,
            InvalidMintProxyProgram
        );

        Ok(())
    }
}

#[derive(Accounts)]
pub struct AvailableForWithdrawal<'info> {
    pub release: Account<'info, Release>,
    pub clock: Sysvar<'info, Clock>,
}

/// Contains information about a beneficiary and the tokens it can claim
/// + its release schedule.
#[account]
#[derive(Default)]
pub struct Release {
    /// The owner of this [Release] account.
    pub beneficiary: Pubkey,
    /// The mint of the SPL token locked up.
    pub mint: Pubkey,
    /// The mint proxy program.
    pub mint_proxy_program: Pubkey,
    /// The [mint_proxy::MinterInfo].
    pub minter_info: Pubkey,
    /// The outstanding SBR deposit backing this release account. All
    /// withdrawals will deduct this balance.
    pub outstanding: u64,
    /// The starting balance of this release account, i.e., how much was
    /// originally deposited.
    pub start_balance: u64,
    /// The unix timestamp at which this release account was created.
    pub created_ts: i64,
    /// The time at which release begins.
    pub start_ts: i64,
    /// The time at which all tokens are released.
    pub end_ts: i64,
    /// Nonce field to the struct to hold the bump seed for the program derived address,
    /// sourced from `<https://github.com/project-serum/anchor/blob/ec6888a3b9f702bc41bd3266e7dd70116df3549c/lang/attribute/account/src/lib.rs#L220-L221.>`.
    __nonce: u8,
}

impl Release {
    pub const LEN: usize = PUBKEY_BYTES * 4 + 8 + 8 + 8 + 8 + 8 + 1;

    /// Gets the nonce.
    pub fn nonce(&self) -> u8 {
        self.__nonce
    }
}

fn check_auth(lockup: &Lockup, auth: &Auth) -> Result<()> {
    require!(
        auth.owner.is_signer && lockup.owner == auth.owner.key(),
        Unauthorized
    );
    Ok(())
}

#[event]
pub struct ReleaseCreatedEvent {
    #[index]
    pub beneficiary: Pubkey,
    #[index]
    pub mint: Pubkey,

    pub release_amount: u64,
    pub created_at: i64,
    pub start_at: i64,
    pub end_at: i64,
}

#[event]
pub struct WithdrawEvent {
    #[index]
    pub beneficiary: Pubkey,
    #[index]
    pub mint: Pubkey,

    pub outstanding_amount: u64,
    pub withdraw_amount: u64,
    pub timestamp: i64,
}

#[error_code]
pub enum ErrorCode {
    #[msg("The provided beneficiary was not valid.")]
    InvalidBeneficiary,
    #[msg("The release deposit amount must be greater than zero.")]
    InvalidDepositAmount,
    #[msg("The Whitelist entry is not a valid program address.")]
    InvalidProgramAddress,
    #[msg("Invalid release schedule given.")]
    InvalidSchedule,
    #[msg("The provided token mint did not match the mint on the release account.")]
    InvalidTokenMint,
    #[msg("Insufficient withdrawal balance.")]
    InsufficientWithdrawalBalance,
    #[msg("Unauthorized access.")]
    Unauthorized,
    #[msg("Pending owner mismatch.")]
    PendingOwnerMismatch,
    #[msg("The mint proxy program provided was not valid.")]
    InvalidMintProxyProgram,
    #[msg("The Release must be an authorized minter on the mint proxy.")]
    MinterUnauthorized,
    #[msg("The minter info is not owned by the expected mint proxy.")]
    MinterInfoProgramMismatch,
    #[msg("The minter must have an allowance of at least the release amount.")]
    MinterAllowanceTooLow,
    #[msg("Minter info mismatch")]
    MinterInfoMismatch,

    #[msg("Release mismatch")]
    ReleaseMismatch,
    #[msg("Proxy mint authority mismatch")]
    ProxyMintAuthorityMismatch,
    #[msg("Mint proxy mint mismatch")]
    MintProxyMintMismatch,
    #[msg("Withdraw destination mint mismatch")]
    DestinationMintMismatch,
    #[msg("Token program mismatch")]
    TokenProgramMismatch,
    #[msg("Release already redeemed from")]
    ReleaseAlreadyRedeemedFrom,

    #[msg("U64 overflow.")]
    U64Overflow,
}

pub fn is_valid_schedule(start_ts: i64, end_ts: i64) -> bool {
    end_ts > start_ts
}