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_block_oil_at: i64,
29
30    /// The last time this miner claimed SOL rewards.
31    pub last_claim_block_sol_at: i64,
32
33    /// The rewards factor last time rewards were updated on this miner account.
34    pub block_rewards_factor: Numeric,
35
36    /// The amount of SOL this miner can claim.
37    pub block_rewards_sol: u64,
38
39    /// The amount of OIL this miner can claim.
40    pub block_rewards_oil: u64,
41
42    /// The amount of OIL this miner has earned from claim fees.
43    pub block_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    pub lifetime_bid: u64,
81
82    /// The pubkey of the referrer who referred this miner.
83    /// If this is Pubkey::default(), the miner has no referrer (either old account or no referral used).
84    pub referrer: Pubkey,
85}
86
87impl Miner {
88    pub fn pda(&self) -> (Pubkey, u8) {
89        miner_pda(self.authority)
90    }
91
92    pub fn claim_oil(&mut self, clock: &Clock, treasury: &mut Treasury) -> u64 {
93        self.update_rewards(treasury);
94        let refined_oil = self.block_refined_oil;
95        let rewards_oil = self.block_rewards_oil;
96        let mut amount = refined_oil + rewards_oil;
97        self.block_refined_oil = 0;
98        self.block_rewards_oil = 0;
99
100        // Charge a 10% fee and share with miners who haven't claimed yet.
101        // Check block_total_unclaimed BEFORE subtracting this miner's rewards_oil
102        // to ensure fee is charged even if this is the only miner with unclaimed oil.
103        if treasury.block_total_unclaimed > 0 {
104            let fee = rewards_oil / 10;
105            amount -= fee;
106            treasury.block_rewards_factor += Numeric::from_fraction(fee, treasury.block_total_unclaimed);
107            treasury.block_total_refined += fee;
108            self.lifetime_rewards_oil -= fee;
109        }
110        
111        treasury.block_total_unclaimed -= rewards_oil;
112        treasury.block_total_refined -= refined_oil;
113        self.last_claim_block_oil_at = clock.unix_timestamp;
114
115        amount
116    }
117
118    pub fn claim_sol(&mut self, clock: &Clock) -> u64 {
119        let amount = self.block_rewards_sol;
120        self.block_rewards_sol = 0;
121        self.last_claim_block_sol_at = clock.unix_timestamp;
122        amount
123    }
124
125    pub fn update_rewards(&mut self, treasury: &Treasury) {
126        // Accumulate rewards, weighted by stake balance.
127        if treasury.block_rewards_factor > self.block_rewards_factor {
128            let accumulated_rewards = treasury.block_rewards_factor - self.block_rewards_factor;
129            if accumulated_rewards < Numeric::ZERO {
130                panic!("Accumulated rewards is negative");
131            }
132            let personal_rewards = accumulated_rewards * Numeric::from_u64(self.block_rewards_oil);
133            self.block_refined_oil += personal_rewards.to_u64();
134            self.lifetime_rewards_oil += personal_rewards.to_u64();
135        }
136
137        // Update this miner account's last seen rewards factor.
138        self.block_rewards_factor = treasury.block_rewards_factor;
139    }
140
141    pub fn update_auction_rewards(&mut self, treasury: &Treasury) {
142        // Accumulate auction rewards, weighted by unclaimed auction OIL.
143        if treasury.auction_rewards_factor > self.auction_rewards_factor {
144            let accumulated_rewards = treasury.auction_rewards_factor - self.auction_rewards_factor;
145            if accumulated_rewards < Numeric::ZERO {
146                panic!("Accumulated auction rewards is negative");
147            }
148            let personal_rewards = accumulated_rewards * Numeric::from_u64(self.auction_rewards_oil);
149            self.auction_refined_oil += personal_rewards.to_u64();
150        }
151
152        // Update this miner account's last seen auction rewards factor.
153        self.auction_rewards_factor = treasury.auction_rewards_factor;
154    }
155}
156
157account!(OilAccount, Miner);