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
use {
    crate::state::*,
    anchor_lang::prelude::*,
    hpl_hive_control::state::{DelegateAuthority, Project},
    hpl_utils::traits::Default,
};

/// Accounts used in initialize multiplier instruction
#[derive(Accounts)]
pub struct InitMultipliers<'info> {
    /// StakingPool state account
    #[account(mut, has_one = project)]
    pub staking_pool: Account<'info, StakingPool>,

    /// Multiplier state account
    #[account(
        init, payer = payer,
        space = Multipliers::LEN,
        seeds = [
            b"multipliers",
            staking_pool.key().as_ref()
        ],
        bump,
    )]
    pub multipliers: Account<'info, Multipliers>,

    /// The wallet that holds authority for this action
    #[account()]
    pub authority: Signer<'info>,

    /// The wallet that pays for the rent
    #[account(mut)]
    pub payer: Signer<'info>,

    /// NATIVE SYSTEM PROGRAM
    pub system_program: Program<'info, System>,

    // HIVE CONTROL
    #[account()]
    pub project: Box<Account<'info, Project>>,
    #[account(has_one = authority)]
    pub delegate_authority: Option<Account<'info, DelegateAuthority>>,
    /// CHECK: This is not dangerous because we don't read or write from this account
    #[account(mut)]
    pub vault: AccountInfo<'info>,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct InitMultipliersArgs {
    decimals: u8,
}

/// Initialize multiplier state
pub fn init_multipliers(ctx: Context<InitMultipliers>, args: InitMultipliersArgs) -> Result<()> {
    let multipliers = &mut ctx.accounts.multipliers;
    multipliers.set_defaults();
    multipliers.bump = ctx.bumps["multipliers"];
    multipliers.decimals = args.decimals;
    multipliers.staking_pool = ctx.accounts.staking_pool.key();
    Ok(())
}

/// Accounts used in add multiplier instruction
#[derive(Accounts)]
pub struct AddMultiplier<'info> {
    /// StakingPool state account
    #[account(mut, has_one = project)]
    pub staking_pool: Account<'info, StakingPool>,

    /// Multiplier state account
    #[account(mut, has_one = staking_pool)]
    pub multipliers: Account<'info, Multipliers>,

    /// The wallet that pays for the rent
    #[account(mut)]
    pub authority: Signer<'info>,

    /// The wallet that pays for the rent
    #[account(mut)]
    pub payer: Signer<'info>,

    /// NATIVE SYSTEM PROGRAM
    pub system_program: Program<'info, System>,

    /// RENT SYSVAR
    pub rent_sysvar: Sysvar<'info, Rent>,

    // HIVE CONTROL
    #[account()]
    pub project: Box<Account<'info, Project>>,
    #[account(has_one = authority)]
    pub delegate_authority: Option<Account<'info, DelegateAuthority>>,
    /// CHECK: This is not dangerous because we don't read or write from this account
    #[account(mut)]
    pub vault: AccountInfo<'info>,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct AddMultiplierArgs {
    pub value: u64,
    pub multiplier_type: MultiplierType,
}

/// Initialize multiplier state
pub fn add_multiplier(ctx: Context<AddMultiplier>, args: AddMultiplierArgs) -> Result<()> {
    let multipliers = &mut ctx.accounts.multipliers;

    hpl_utils::reallocate(
        isize::try_from(Multiplier::LEN).unwrap(),
        multipliers.to_account_info(),
        ctx.accounts.payer.to_account_info(),
        &ctx.accounts.rent_sysvar,
        &ctx.accounts.system_program,
    )?;

    match args.multiplier_type {
        MultiplierType::StakeDuration { min_duration: _ } => {
            multipliers.duration_multipliers.push(Multiplier {
                value: args.value,
                multiplier_type: args.multiplier_type,
            });
            multipliers
                .duration_multipliers
                .sort_by_key(|x| match x.multiplier_type {
                    MultiplierType::StakeDuration { min_duration } => min_duration,
                    _ => 0,
                });
        }
        MultiplierType::NFTCount { min_count: _ } => {
            multipliers.count_multipliers.push(Multiplier {
                value: args.value,
                multiplier_type: args.multiplier_type,
            });
            multipliers
                .count_multipliers
                .sort_by_key(|x| match x.multiplier_type {
                    MultiplierType::NFTCount { min_count } => min_count,
                    _ => 0,
                });
        }
        MultiplierType::Creator { creator: _ } => {
            multipliers.creator_multipliers.push(Multiplier {
                value: args.value,
                multiplier_type: args.multiplier_type,
            });
            multipliers.creator_multipliers.sort_by_key(|x| x.value)
        }
        MultiplierType::Collection { collection: _ } => {
            multipliers.collection_multipliers.push(Multiplier {
                value: args.value,
                multiplier_type: args.multiplier_type,
            });
            multipliers
                .collection_multipliers
                .sort_by(|a, b| b.value.cmp(&a.value))
        }
    }

    Ok(())
}