oil_api/state/
miner.rs

1use serde::{Deserialize, Serialize};
2use steel::*;
3
4use crate::state::{miner_pda, Treasury};
5
6use super::OilAccount;
7
8
9#[repr(C)]
10#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
11pub struct Miner {
12    /// The authority of this miner account.
13    pub authority: Pubkey,
14
15    /// The miner's prospects in the current round.
16    pub deployed: [u64; 25],
17
18    /// The cumulative amount of SOL deployed on each square prior to this miner's move.
19    pub cumulative: [u64; 25],
20
21    /// SOL witheld in reserve to pay for checkpointing.
22    pub checkpoint_fee: u64,
23
24    /// The last round that this miner checkpointed.
25    pub checkpoint_id: u64,
26
27    /// The last time this miner claimed OIL rewards.
28    pub last_claim_oil_at: i64,
29
30    /// The last time this miner claimed SOL rewards.
31    pub last_claim_sol_at: i64,
32
33    /// The rewards factor last time rewards were updated on this miner account.
34    pub rewards_factor: Numeric,
35
36    /// The amount of SOL this miner can claim.
37    pub rewards_sol: u64,
38
39    /// The amount of OIL this miner can claim.
40    pub rewards_oil: u64,
41
42    /// The amount of OIL this miner has earned from claim fees.
43    pub refined_oil: u64,
44
45    /// The ID of the round this miner last played in.
46    pub round_id: u64,
47
48    /// The pooled deployed amount of this miner.
49    pub pooled_deployed: u64,
50
51    /// OIL rewards from auction wells (not yet claimed)
52    /// Accumulated when ownership changes (pre-minted) or when claiming current ownership (minted on-demand)
53    pub auction_rewards_oil: u64,
54
55    /// SOL rewards from auction wells (not yet claimed)
56    /// Includes: outbid refunds, pool refunds from abandoned pools
57    pub auction_rewards_sol: u64,
58
59    /// The rewards factor last time auction rewards were updated on this rig account.
60    pub auction_rewards_factor: Numeric,
61
62    /// The amount of OIL this rig has earned from auction claim fees (refined OIL).
63    pub auction_refined_oil: u64,
64
65    /// The last time this rig claimed OIL rewards.
66    pub last_claim_auction_oil_at: i64,
67
68    /// The last time this rig claimed SOL rewards.
69    pub last_claim_auction_sol_at: i64,
70
71    /// The total amount of SOL this miner has mined across all blocks.
72    pub lifetime_rewards_sol: u64,
73
74    /// The total amount of OIL this miner has mined across all blocks.
75    pub lifetime_rewards_oil: u64,
76
77    /// The total amount of OIL this miner has deployed across all rounds.
78    pub lifetime_deployed: u64,
79
80    /// The pubkey of the referrer who referred this miner.
81    /// If this is Pubkey::default(), the miner has no referrer (either old account or no referral used).
82    pub referrer: Pubkey,
83}
84
85impl Miner {
86    pub fn pda(&self) -> (Pubkey, u8) {
87        miner_pda(self.authority)
88    }
89
90    pub fn claim_oil(&mut self, clock: &Clock, treasury: &mut Treasury) -> u64 {
91        self.update_rewards(treasury);
92        let refined_oil = self.refined_oil;
93        let rewards_oil = self.rewards_oil;
94        let mut amount = refined_oil + rewards_oil;
95        self.refined_oil = 0;
96        self.rewards_oil = 0;
97
98        // Charge a 10% fee and share with miners who haven't claimed yet.
99        // Check block_total_unclaimed BEFORE subtracting this miner's rewards_oil
100        // to ensure fee is charged even if this is the only miner with unclaimed oil.
101        if treasury.block_total_unclaimed > 0 {
102            let fee = rewards_oil / 10;
103            amount -= fee;
104            treasury.block_rewards_factor += Numeric::from_fraction(fee, treasury.block_total_unclaimed);
105            treasury.block_total_refined += fee;
106            self.lifetime_rewards_oil -= fee;
107        }
108        
109        treasury.block_total_unclaimed -= rewards_oil;
110        treasury.block_total_refined -= refined_oil;
111        self.last_claim_oil_at = clock.unix_timestamp;
112
113        amount
114    }
115
116    pub fn claim_sol(&mut self, clock: &Clock) -> u64 {
117        let amount = self.rewards_sol;
118        self.rewards_sol = 0;
119        self.last_claim_sol_at = clock.unix_timestamp;
120        amount
121    }
122
123    pub fn update_rewards(&mut self, treasury: &Treasury) {
124        // Accumulate rewards, weighted by stake balance.
125        if treasury.block_rewards_factor > self.rewards_factor {
126            let accumulated_rewards = treasury.block_rewards_factor - self.rewards_factor;
127            if accumulated_rewards < Numeric::ZERO {
128                panic!("Accumulated rewards is negative");
129            }
130            let personal_rewards = accumulated_rewards * Numeric::from_u64(self.rewards_oil);
131            self.refined_oil += personal_rewards.to_u64();
132            self.lifetime_rewards_oil += personal_rewards.to_u64();
133        }
134
135        // Update this miner account's last seen rewards factor.
136        self.rewards_factor = treasury.block_rewards_factor;
137    }
138
139    pub fn update_auction_rewards(&mut self, treasury: &Treasury) {
140        // Accumulate auction rewards, weighted by unclaimed auction OIL.
141        if treasury.auction_rewards_factor > self.auction_rewards_factor {
142            let accumulated_rewards = treasury.auction_rewards_factor - self.auction_rewards_factor;
143            if accumulated_rewards < Numeric::ZERO {
144                panic!("Accumulated auction rewards is negative");
145            }
146            let personal_rewards = accumulated_rewards * Numeric::from_u64(self.auction_rewards_oil);
147            self.auction_refined_oil += personal_rewards.to_u64();
148        }
149
150        // Update this miner account's last seen auction rewards factor.
151        self.auction_rewards_factor = treasury.auction_rewards_factor;
152    }
153}
154
155account!(OilAccount, Miner);