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
#![cfg_attr(feature = "no-entrypoint", allow(dead_code))]
// #![feature(trivial_bounds)]

use anchor_lang::prelude::*;
use solana_program::pubkey::Pubkey;

use instructions::*;
use solana_security_txt::security_txt;
pub mod errors;
pub mod instructions;
pub mod state;
pub mod utils;

declare_id!("JAiLsp5k6ANQAy9UcJC9TfTWieY5Afo42ykihpTeVk8L");
// declare_id!("5ai4CtMebtFxvgK6fDJXfSGn9K6aLbRe1uxxToZWt9dR");

#[cfg(not(feature = "no-entrypoint"))]
security_txt! {
    name: "Fracture Staking",
    project_url: "https://thefracture.io/",
    contacts: "email:dev@thefracture.io,twitter:@TheFracture_",
    policy: "private",
    preferred_languages: "en",
    source_code: "private"
}

#[program]
mod jungle {
    use super::*;

    /// Initializes the jungle
    pub fn initialize_jungle(
        ctx: Context<InitializeJungle>,
        bumps: InitializeJungleBumps,
        maximum_weekly_multiplier: u64,
        weekly_multiplier: u64,
        maximum_holdings_multiplier: u64,
        holdings_multiplier: u64,
        start: i64,
        root: [u8; 32],
    ) -> Result<()> {
        instructions::init_jungle::handler(
            ctx,
            bumps,
            maximum_weekly_multiplier,
            weekly_multiplier,
            maximum_holdings_multiplier,
            holdings_multiplier,
            start,
            root,
        )
    }

    /// Sets the jungle parameters
    pub fn set_jungle(
        ctx: Context<SetJungle>,
        maximum_weekly_multiplier: u64,
        weekly_multiplier: u64,
        maximum_holdings_multiplier: u64,
        holdings_multiplier: u64,
        start: i64,
        root: [u8; 32],
    ) -> Result<()> {
        instructions::set_jungle::handler(
            ctx,
            maximum_weekly_multiplier,
            weekly_multiplier,
            maximum_holdings_multiplier,
            holdings_multiplier,
            start,
            root,
        )
    }

    /// New Dummy Animal
    pub fn init_dummy(ctx: Context<InitDummy>, bump: u8) -> Result<()> {
        instructions::init_dummy::handler(ctx, bump)
    }

    /// Add Reward Token
    pub fn add_reward_token(
        ctx: Context<AddRewardToken>,
        bumps: u8,
        decimals: u8,
        value: u64,
    ) -> Result<()> {
        instructions::add_reward_token::handler(ctx, bumps, decimals, value)
    }

    /// Update Reward Token
    pub fn update_reward_token(
        ctx: Context<UpdateRewardToken>,
        decimals: u8,
        value: u64,
    ) -> Result<()> {
        instructions::update_reward_token::handler(ctx, decimals, value)
    }

    /// Init Staker
    pub fn init_staker(ctx: Context<InitStaker>, bumps: u8) -> Result<()> {
        instructions::init_staker::handler(ctx, bumps)
    }

    /// Init Transaction Batch
    pub fn init_transaction_batch(ctx: Context<InitTransactionBatch>) -> Result<()> {
        instructions::init_transaction_batch::handler(ctx)
    }

    /// Stake an animal
    pub fn stake_animal<'info>(
        ctx: Context<'_, '_, '_, 'info, StakeAnimal<'info>>,
        bumps: StakeAnimalBumps,
        // proof: Vec<CompressedProofElement>,
        emissions_per_day: u64,
        faction: u64,
        // authorization_data: Option<AuthorizationDataLocal>,
        rules_acc_present: bool,
    ) -> Result<()> {
        instructions::stake_animal::handler(
            ctx,
            bumps,
            // proof,
            emissions_per_day,
            faction,
            // authorization_data,
            rules_acc_present,
        )
    }

    /// Unstake a staked animal
    pub fn unstake_animal<'info>(
        ctx: Context<'_, '_, '_, 'info, UnstakeAnimal<'info>>,
        authorization_data: Option<AuthorizationDataLocal>,
        rules_acc_present: bool,
    ) -> Result<()> {
        instructions::unstake_animal::handler(ctx, authorization_data, rules_acc_present)
    }

    /// Claim staking rewards
    pub fn claim_staking(ctx: Context<ClaimStaking>) -> Result<()> {
        instructions::claim_staking::handler(ctx)
    }

    /// Bulk Claim
    pub fn bulk_claim(ctx: Context<BulkClaim>) -> Result<()> {
        instructions::bulk_claim::handler(ctx)
    }

    /// Withdraw rewards from the vault
    pub fn swap_points(ctx: Context<SwapPoints>, amount: u64) -> Result<()> {
        instructions::swap_points::handler(ctx, amount)
    }

    /// Burn points from the vault
    pub fn burn_points(ctx: Context<BurnPoints>, amount: u64) -> Result<()> {
        instructions::burn_points::handler(ctx, amount)
    }
    /// Burn points from the vault
    // pub fn fix_bridged(
    //     ctx: Context<FixBridged>,
    //     transaction_index: u32
    // ) -> Result<()> {
    //     instructions::fix_bridged::handler(
    //         ctx,
    //         transaction_index
    //     )
    // }
    /// Transfer Point to someone
    pub fn transfer_points(ctx: Context<TransferPoints>, amount: u64) -> Result<()> {
        instructions::transfer_points::handler(ctx, amount)
    }

    /// Reward Point to staker
    pub fn reward_points(ctx: Context<RewardPoints>, amount: u64) -> Result<()> {
        instructions::reward_point::handler(ctx, amount)
    }

    /// Unstake a staked animal
    pub fn force_unstake<'info>(
        ctx: Context<'_, '_, '_, 'info, ForceUnstake<'info>>,
        authorization_data: Option<AuthorizationDataLocal>,
        rules_acc_present: bool,
    ) -> Result<()> {
        instructions::force_unstake::handler(ctx, authorization_data, rules_acc_present)
    }

    // /// Initialize admin
    // pub fn init_admin(ctx: Context<InitAdmin>, bump: u8) -> Result<()> {
    //     instructions::init_admin::handler(ctx, bump)
    // }
}

#[account]
pub struct Admin {
    pub bump: u8,
    pub jungle_key: Pubkey,
}
impl Admin {
    pub const LEN: usize = 8 + 1 + 32;
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default)]
pub struct InitializeJungleBumps {
    pub jungle: u8,
    pub escrow: u8,
}
/// The global state of the program
#[account]
#[derive(Default)]
pub struct Jungle {
    /// The identifier
    pub key: Pubkey,

    /// The owner of the program
    pub owner: Pubkey,

    /// The bump used to generate PDAs
    pub bumps: InitializeJungleBumps,

    /// The PDA owning the community fund
    pub escrow: Pubkey,

    /// Array of rewards tokens mint
    pub reward_tokens: Vec<Reward>,

    /// The total animals currently staked.
    pub animals_staked: u64,

    // Maximum Weekly Multiplier
    pub maximum_weekly_multiplier: u64,

    // Base Weekly Multiplier
    pub weekly_multiplier: u64,

    // Maximum Holdings Multiplier
    pub maximum_holdings_multiplier: u64,

    // Base Holdings Multiplier
    pub holdings_multiplier: u64,

    /// The time the staking starts (in seconds since 1970)
    pub start: i64,

    /// The root of the merkle tree used to know if a token is part of the collection
    pub root: [u8; 32],
}

#[derive(AnchorSerialize, AnchorDeserialize, Copy, Clone, Default)]
pub struct Reward {
    /// Bumps used to create associated token account
    pub bumps: u8,

    /// Mint of the reward token
    pub mint: Pubkey,

    /// The account associated to the reward token
    pub account: Pubkey,

    /// Decimal points of the reward token
    pub decimals: u8,

    /// Points -> Token Worth
    pub value: u64,
}
impl Reward {
    pub const LEN: usize = 8 + 40 + 40 + 1 + 8;
}

#[derive(AnchorSerialize, AnchorDeserialize, Copy, Clone, Default)]
pub struct StakeAnimalBumps {
    pub animal: u8,
    pub deposit: u8,
    pub staker_info: u8,
}

/// The staking account linked to the NFT
#[account]
#[derive(Default)]
pub struct Animal {
    /// Bump used to create this PDA
    pub bumps: StakeAnimalBumps,

    /// The mint of the NFT
    pub mint: Pubkey,

    /// Owner of the animal
    pub staker: Pubkey,

    /// How rare the animal is
    pub emissions_per_day: u64,

    /// The wallet to which fees are given
    pub faction: u8,

    /// Last time the owner claimed rewards
    pub last_claim: i64,

    /// Last time the owner claimed rewards
    pub staked_at: i64,
}

impl Animal {
    pub const LEN: usize = 8 + 2 + 40 + 40 + 8 + 1 + 8 + 8;
}

#[account]
#[derive(Default)]
pub struct StakerInfo {
    /// Bumps used to create this PDA
    pub bumps: u8,

    /// Staker Account
    pub staker: Pubkey,

    /// Number of animals staked
    pub holdings: u64,

    /// Pending Rewards
    pub tokens: u64,
    // /// Transaction History
    // pub transaction_batches: Vec<Pubkey>,
}
impl StakerInfo {
    pub const LEN: usize = 8 + 1 + 40 + 8 + 8 + 40 * 254; // 10225
}

#[account]
#[derive(Default)]
pub struct TransactionBatch {
    /// Authority to change
    pub staker: Pubkey,

    /// Transaction History
    pub transactions: Vec<Transaction>,
}
impl TransactionBatch {
    pub const LEN: usize = 8 + 40 + Transaction::LEN * 254; // 10208
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default)]
pub struct Transaction {
    pub timestamp: i64,      // seconds since 1970
    pub amount: u64,         // amount of tokens
    pub description: String, // rewards claimed, paid for InAppPurchase etc
}

impl Transaction {
    pub const LEN: usize = 8 + 8 + 24; // 40
}