ore_api/state/
stake.rs

1use serde::{Deserialize, Serialize};
2use steel::*;
3
4use crate::state::{stake_pda, Treasury};
5
6use super::OreAccount;
7
8#[repr(C)]
9#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
10pub struct Stake {
11    /// The authority of this miner account.
12    pub authority: Pubkey,
13
14    /// The balance of this stake account.
15    pub balance: u64,
16
17    /// The timestamp of last claim.
18    pub last_claim_at: i64,
19
20    /// The timestamp the last time this staker deposited.
21    pub last_deposit_at: i64,
22
23    /// The timestamp the last time this staker withdrew.
24    pub last_withdraw_at: i64,
25
26    /// The rewards factor last time rewards were updated on this stake account.
27    pub rewards_factor: Numeric,
28
29    /// The amount of ORE this staker can claim.
30    pub rewards: u64,
31
32    /// The total amount of ORE this staker has earned over its lifetime.
33    pub lifetime_rewards: u64,
34
35    /// Flag indicating whether this staker is associated with a Solana Seeker.
36    pub is_seeker: u64,
37}
38
39impl Stake {
40    pub fn pda(&self) -> (Pubkey, u8) {
41        stake_pda(self.authority)
42    }
43
44    pub fn claim(&mut self, amount: u64, clock: &Clock, treasury: &Treasury) -> u64 {
45        self.update_rewards(treasury);
46        let amount = self.rewards.min(amount);
47        self.rewards -= amount;
48        self.last_claim_at = clock.unix_timestamp;
49        amount
50    }
51
52    pub fn deposit(
53        &mut self,
54        amount: u64,
55        clock: &Clock,
56        treasury: &mut Treasury,
57        sender: &TokenAccount,
58    ) -> u64 {
59        self.update_rewards(treasury);
60        let amount = sender.amount().min(amount);
61        self.balance += amount;
62        self.last_deposit_at = clock.unix_timestamp;
63        treasury.total_staked += amount;
64        amount
65    }
66
67    pub fn withdraw(&mut self, amount: u64, clock: &Clock, treasury: &mut Treasury) -> u64 {
68        self.update_rewards(treasury);
69        let amount = self.balance.min(amount);
70        self.balance -= amount;
71        self.last_withdraw_at = clock.unix_timestamp;
72        treasury.total_staked -= amount;
73        amount
74    }
75
76    pub fn update_rewards(&mut self, treasury: &Treasury) {
77        // Accumulate rewards, weighted by stake balance.
78        if treasury.stake_rewards_factor > self.rewards_factor {
79            let accumulated_rewards = treasury.stake_rewards_factor - self.rewards_factor;
80            if accumulated_rewards < Numeric::ZERO {
81                panic!("Accumulated rewards is negative");
82            }
83            let personal_rewards = accumulated_rewards * Numeric::from_u64(self.balance);
84            self.rewards += personal_rewards.to_u64();
85            self.lifetime_rewards += personal_rewards.to_u64();
86        }
87
88        // Update this stake account's last seen rewards factor.
89        self.rewards_factor = treasury.stake_rewards_factor;
90    }
91}
92
93account!(OreAccount, Stake);