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    pub auction_rewards_oil: u64,
53
54    /// SOL rewards from auction wells (not yet claimed)
55    pub auction_rewards_sol: u64,
56
57    /// The rewards factor last time auction rewards were updated on this rig account.
58    pub auction_rewards_factor: Numeric,
59
60    /// The amount of OIL this rig has earned from auction claim fees (refined OIL).
61    pub auction_refined_oil: u64,
62
63    /// The last time this rig claimed OIL rewards.
64    pub last_claim_auction_oil_at: i64,
65
66    /// The last time this rig claimed SOL rewards.
67    pub last_claim_auction_sol_at: i64,
68
69    /// The total amount of SOL this miner has mined across all blocks.
70    pub lifetime_rewards_sol: u64,
71
72    /// The total amount of OIL this miner has mined across all blocks.
73    pub lifetime_rewards_oil: u64,
74
75    /// The total amount of OIL this miner has deployed across all rounds.
76    pub lifetime_deployed: u64,
77
78    pub lifetime_bid: u64,
79
80    /// The pubkey of the referrer who referred this miner.
81    pub referrer: Pubkey,
82
83    /// Total stake score across all stake accounts for this miner.
84    pub total_stake_score: u64,
85
86    pub is_seeker: u64,
87
88    pub buffer_a: u64,
89}
90
91impl Miner {
92    pub fn pda(&self) -> (Pubkey, u8) {
93        miner_pda(self.authority)
94    }
95
96    pub fn initialize(&mut self, authority: Pubkey) {
97        self.authority = authority;
98        self.deployed = [0; 25];
99        self.cumulative = [0; 25];
100        self.checkpoint_fee = 0;
101        self.checkpoint_id = 0;
102        self.last_claim_block_oil_at = 0;
103        self.last_claim_block_sol_at = 0;
104        self.block_rewards_factor = Numeric::ZERO;
105        self.block_rewards_sol = 0;
106        self.block_rewards_oil = 0;
107        self.block_refined_oil = 0;
108        self.round_id = 0;
109        self.pooled_deployed = 0;
110        self.is_seeker = 0;
111        self.buffer_a = 0;
112        self.auction_rewards_oil = 0;
113        self.auction_rewards_sol = 0;
114        self.auction_rewards_factor = Numeric::ZERO;
115        self.auction_refined_oil = 0;
116        self.last_claim_auction_oil_at = 0;
117        self.last_claim_auction_sol_at = 0;
118        self.lifetime_rewards_sol = 0;
119        self.lifetime_rewards_oil = 0;
120        self.lifetime_deployed = 0;
121        self.lifetime_bid = 0;
122        self.referrer = Pubkey::default();
123        self.total_stake_score = 0;
124    }
125
126    pub fn claim_oil(&mut self, clock: &Clock, treasury: &mut Treasury) -> u64 {
127        self.update_rewards(treasury);
128        let refined_oil = self.block_refined_oil;
129        let rewards_oil = self.block_rewards_oil;
130        let mut amount = refined_oil + rewards_oil;
131        self.block_refined_oil = 0;
132        self.block_rewards_oil = 0;
133
134        // Charge a 10% fee and share with miners who haven't claimed yet.
135        // Check block_total_unclaimed BEFORE subtracting this miner's rewards_oil
136        // to ensure fee is charged even if this is the only miner with unclaimed oil.
137        if treasury.block_total_unclaimed > 0 {
138            let fee = rewards_oil / 10;
139            amount -= fee;
140            treasury.block_rewards_factor += Numeric::from_fraction(fee, treasury.block_total_unclaimed);
141            treasury.block_total_refined += fee;
142            self.lifetime_rewards_oil -= fee;
143        }
144        
145        treasury.block_total_unclaimed -= rewards_oil;
146        treasury.block_total_refined -= refined_oil;
147        self.last_claim_block_oil_at = clock.unix_timestamp;
148
149        amount
150    }
151
152    pub fn claim_sol(&mut self, clock: &Clock) -> u64 {
153        let amount = self.block_rewards_sol;
154        self.block_rewards_sol = 0;
155        self.last_claim_block_sol_at = clock.unix_timestamp;
156        amount
157    }
158
159    pub fn update_rewards(&mut self, treasury: &Treasury) {
160        // Accumulate rewards, weighted by stake balance.
161        if treasury.block_rewards_factor > self.block_rewards_factor {
162            let accumulated_rewards = treasury.block_rewards_factor - self.block_rewards_factor;
163            if accumulated_rewards < Numeric::ZERO {
164                panic!("Accumulated rewards is negative");
165            }
166            let personal_rewards = accumulated_rewards * Numeric::from_u64(self.block_rewards_oil);
167            self.block_refined_oil += personal_rewards.to_u64();
168            self.lifetime_rewards_oil += personal_rewards.to_u64();
169        }
170
171        // Update this miner account's last seen rewards factor.
172        self.block_rewards_factor = treasury.block_rewards_factor;
173    }
174
175    pub fn update_auction_rewards(&mut self, treasury: &Treasury) {
176        // Accumulate auction rewards, weighted by unclaimed auction OIL.
177        if treasury.auction_rewards_factor > self.auction_rewards_factor {
178            let accumulated_rewards = treasury.auction_rewards_factor - self.auction_rewards_factor;
179            if accumulated_rewards < Numeric::ZERO {
180                panic!("Accumulated auction rewards is negative");
181            }
182            let personal_rewards = accumulated_rewards * Numeric::from_u64(self.auction_rewards_oil);
183            self.auction_refined_oil += personal_rewards.to_u64();
184        }
185
186        // Update this miner account's last seen auction rewards factor.
187        self.auction_rewards_factor = treasury.auction_rewards_factor;
188    }
189}
190
191account!(OilAccount, Miner);