oil_api/state/
rig.rs

1use serde::{Deserialize, Serialize};
2use steel::*;
3
4use crate::state::{rig_pda, Treasury};
5
6use super::OilAccount;
7
8/// Rig account for auction-based mining
9/// Similar to Driller but focused on auction wells
10#[repr(C)]
11#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
12pub struct Rig {
13    /// The authority of this rig account (owner's wallet).
14    pub authority: Pubkey,
15
16    /// OIL rewards from auction wells (not yet claimed)
17    /// Accumulated when ownership changes (pre-minted) or when claiming current ownership (minted on-demand)
18    pub auction_rewards_oil: u64,
19
20    /// SOL rewards from auction wells (not yet claimed)
21    /// Includes: outbid refunds, pool refunds from abandoned pools
22    pub auction_rewards_sol: u64,
23
24    /// Current pool contributions per well (in lamports)
25    /// [well_0, well_1, well_2, well_3]
26    /// 0 = not contributing to this well
27    pub auction_pool_contributions: [u64; 4],
28
29    /// Epoch ID per well that user contributed to
30    /// [epoch_0, epoch_1, epoch_2, epoch_3]
31    /// 0 = not contributing to this well
32    /// Used to determine if pool is still active (compare with Well.epoch_id)
33    pub auction_pool_epochs: [u64; 4],
34
35    /// The rewards factor last time auction rewards were updated on this rig account.
36    pub auction_rewards_factor: Numeric,
37
38    /// The amount of OIL this rig has earned from auction claim fees (refined OIL).
39    pub auction_refined_oil: u64,
40
41    /// The last time this rig claimed OIL rewards.
42    pub last_claim_oil_at: i64,
43
44    /// The last time this rig claimed SOL rewards.
45    pub last_claim_sol_at: i64,
46
47    /// The total amount of OIL this rig has mined across all auction wells (lifetime).
48    pub lifetime_rewards_oil: u64,
49
50    /// The total amount of SOL this rig has earned across all auction wells (lifetime).
51    /// Includes: outbid refunds, pool refunds, etc.
52    pub lifetime_rewards_sol: u64,
53
54    /// Last epoch ID per well that this rig synced (similar to checkpoint_id in Driller)
55    /// [epoch_0, epoch_1, epoch_2, epoch_3]
56    /// 0 = not synced for this well
57    /// Used to enforce sync_auction_state before set_bid/join_auction_pool (like checkpoint before deploy)
58    /// Ensures each user syncs their own refunds before participating in auctions
59    pub last_synced_epoch_id: [u64; 4],
60}
61
62impl Rig {
63    pub fn pda(&self) -> (Pubkey, u8) {
64        rig_pda(self.authority)
65    }
66
67    pub fn update_auction_rewards(&mut self, treasury: &Treasury) {
68        // Accumulate auction rewards, weighted by unclaimed auction OIL.
69        if treasury.auction_driller_rewards_factor > self.auction_rewards_factor {
70            let accumulated_rewards = treasury.auction_driller_rewards_factor - self.auction_rewards_factor;
71            if accumulated_rewards < Numeric::ZERO {
72                panic!("Accumulated auction rewards is negative");
73            }
74            let personal_rewards = accumulated_rewards * Numeric::from_u64(self.auction_rewards_oil);
75            self.auction_refined_oil += personal_rewards.to_u64();
76        }
77
78        // Update this rig account's last seen auction rewards factor.
79        self.auction_rewards_factor = treasury.auction_driller_rewards_factor;
80    }
81
82    /// Credit a pool refund to this rig account
83    /// Called when a pool the user contributed to is abandoned
84    /// Updates rig state: credits refund amount and clears pool tracking fields
85    pub fn credit_pool_refund(&mut self, well_id: usize, refund_amount: u64) {
86        // Credit refund to rig's auction_rewards_sol
87        self.auction_rewards_sol += refund_amount;
88        
89        // Clear rig tracking fields (pool is abandoned, user is no longer contributing)
90        self.auction_pool_contributions[well_id] = 0;
91        self.auction_pool_epochs[well_id] = 0;
92    }
93}
94
95account!(OilAccount, Rig);