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
#![deny(missing_docs)]

//! A program for liquid staking with a single validator

pub mod error;
pub mod inline_mpl_token_metadata;
pub mod instruction;
pub mod processor;
pub mod state;

#[cfg(not(feature = "no-entrypoint"))]
pub mod entrypoint;

// export current sdk types for downstream users building with a different sdk
// version
pub use solana_program;
use solana_program::{pubkey::Pubkey, stake};

solana_program::declare_id!("SVSPxpvHdN29nkVg9rPapPNDddN5DipNLRUFhyjFThE");

const POOL_PREFIX: &[u8] = b"pool";
const POOL_STAKE_PREFIX: &[u8] = b"stake";
const POOL_MINT_PREFIX: &[u8] = b"mint";
const POOL_MINT_AUTHORITY_PREFIX: &[u8] = b"mint_authority";
const POOL_STAKE_AUTHORITY_PREFIX: &[u8] = b"stake_authority";
const POOL_MPL_AUTHORITY_PREFIX: &[u8] = b"mpl_authority";

const MINT_DECIMALS: u8 = 9;

const VOTE_STATE_DISCRIMINATOR_END: usize = 4;
const VOTE_STATE_AUTHORIZED_WITHDRAWER_START: usize = 36;
const VOTE_STATE_AUTHORIZED_WITHDRAWER_END: usize = 68;

fn find_pool_address_and_bump(program_id: &Pubkey, vote_account_address: &Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[POOL_PREFIX, vote_account_address.as_ref()], program_id)
}

fn find_pool_stake_address_and_bump(program_id: &Pubkey, pool_address: &Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[POOL_STAKE_PREFIX, pool_address.as_ref()], program_id)
}

fn find_pool_mint_address_and_bump(program_id: &Pubkey, pool_address: &Pubkey) -> (Pubkey, u8) {
    Pubkey::find_program_address(&[POOL_MINT_PREFIX, pool_address.as_ref()], program_id)
}

fn find_pool_stake_authority_address_and_bump(
    program_id: &Pubkey,
    pool_address: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[POOL_STAKE_AUTHORITY_PREFIX, pool_address.as_ref()],
        program_id,
    )
}

fn find_pool_mint_authority_address_and_bump(
    program_id: &Pubkey,
    pool_address: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[POOL_MINT_AUTHORITY_PREFIX, pool_address.as_ref()],
        program_id,
    )
}

fn find_pool_mpl_authority_address_and_bump(
    program_id: &Pubkey,
    pool_address: &Pubkey,
) -> (Pubkey, u8) {
    Pubkey::find_program_address(
        &[POOL_MPL_AUTHORITY_PREFIX, pool_address.as_ref()],
        program_id,
    )
}

fn find_default_deposit_account_address_and_seed(
    pool_address: &Pubkey,
    user_wallet_address: &Pubkey,
) -> (Pubkey, String) {
    let pool_address_str = pool_address.to_string();
    let seed = format!("svsp{}", &pool_address_str[0..28]);
    let address =
        Pubkey::create_with_seed(user_wallet_address, &seed, &stake::program::id()).unwrap();

    (address, seed)
}

/// Find the canonical pool address for a given vote account.
pub fn find_pool_address(program_id: &Pubkey, vote_account_address: &Pubkey) -> Pubkey {
    find_pool_address_and_bump(program_id, vote_account_address).0
}

/// Find the canonical stake account address for a given pool account.
pub fn find_pool_stake_address(program_id: &Pubkey, pool_address: &Pubkey) -> Pubkey {
    find_pool_stake_address_and_bump(program_id, pool_address).0
}

/// Find the canonical token mint address for a given pool account.
pub fn find_pool_mint_address(program_id: &Pubkey, pool_address: &Pubkey) -> Pubkey {
    find_pool_mint_address_and_bump(program_id, pool_address).0
}

/// Find the canonical stake authority address for a given pool account.
pub fn find_pool_stake_authority_address(program_id: &Pubkey, pool_address: &Pubkey) -> Pubkey {
    find_pool_stake_authority_address_and_bump(program_id, pool_address).0
}

/// Find the canonical mint authority address for a given pool account.
pub fn find_pool_mint_authority_address(program_id: &Pubkey, pool_address: &Pubkey) -> Pubkey {
    find_pool_mint_authority_address_and_bump(program_id, pool_address).0
}

/// Find the canonical MPL authority address for a given pool account.
pub fn find_pool_mpl_authority_address(program_id: &Pubkey, pool_address: &Pubkey) -> Pubkey {
    find_pool_mpl_authority_address_and_bump(program_id, pool_address).0
}

/// Find the address of the default intermediate account that holds activating
/// user stake before deposit.
pub fn find_default_deposit_account_address(
    pool_address: &Pubkey,
    user_wallet_address: &Pubkey,
) -> Pubkey {
    find_default_deposit_account_address_and_seed(pool_address, user_wallet_address).0
}