oil_api/state/
well.rs

1use serde::{Deserialize, Serialize};
2use steel::*;
3
4use crate::state::well_pda;
5
6use super::{OilAccount, Auction};
7
8/// Well account (one per well)
9/// PDA: [WELL, well_id]
10/// Tracks current auction state for a well
11#[repr(C)]
12#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
13pub struct Well {
14    /// Well ID (0-3) - which well this is for
15    pub well_id: u64,
16    
17    /// Current epoch ID (increments each auction: 0, 1, 2, 3, etc.)
18    /// Starts at 0, increments when bid happens
19    pub epoch_id: u64,
20    
21    /// Current bidder/owner (Pubkey::default() if unowned)
22    pub current_bidder: Pubkey,
23    
24    /// Initial price for current epoch (in lamports)
25    /// Doubles from current price when bid happens
26    pub init_price: u64,
27    
28    /// Mining per second (MPS) - current mining rate (OIL per second, in atomic units)
29    /// This is the base rate adjusted for halvings
30    pub mps: u64,
31    
32    /// Epoch start time (timestamp when current epoch started)
33    pub epoch_start_time: u64,
34    
35    /// Accumulated OIL mined by current owner (not yet claimed)
36    pub accumulated_oil: u64,
37    
38    /// Last time accumulated_oil was updated
39    pub last_update_time: u64,
40    
41    /// Number of halvings that have occurred (for rate calculation)
42    pub halving_count: u64,
43    
44    /// Total OIL ever mined from this well (lifetime)
45    pub lifetime_oil_mined: u64,
46    
47    /// Flag indicating if owned by pool (1) or solo owner (0)
48    /// When pool wins (pool_total >= current_price), this is set to 1
49    pub is_pool_owned: u64,
50    
51    /// Total OIL mined by current operator (doesn't reset when claimed, only when ownership changes)
52    /// Repurposed from buffer_a
53    pub operator_total_oil_mined: u64,
54    
55    /// Last epoch ID that was synced (similar to checkpoint_id in Driller)
56    /// Used to enforce sync_auction_state before set_bid (like checkpoint before deploy)
57    /// Repurposed from buffer_b
58    pub last_synced_epoch_id: u64,
59    
60    /// Buffer field (for future use)
61    pub buffer_c: u64,
62    
63    /// Buffer field (for future use)
64    pub buffer_d: u64,
65    
66    /// Buffer field (for future use)
67    pub buffer_e: u64,
68}
69
70impl Well {
71    pub fn pda(well_id: u64) -> (Pubkey, u8) {
72        well_pda(well_id)
73    }
74
75    /// Calculate current price for this epoch (Dutch auction - price decreases over time)
76    pub fn current_price(&self, auction: &Auction, clock: &Clock) -> u64 {
77        // If well has no owner (never been bid on), show starting price
78        use solana_program::pubkey::Pubkey;
79        if self.current_bidder == Pubkey::default() && self.is_pool_owned == 0 {
80            return self.init_price; // Return starting price for unowned wells
81        }
82        
83        let elapsed = clock.unix_timestamp.saturating_sub(self.epoch_start_time as i64);
84        let duration = auction.auction_duration_seconds as i64;
85        
86        if elapsed >= duration {
87            return 0; // Auction expired, free to claim
88        }
89        
90        // Linear decay: price = init_price * (1 - elapsed / duration)
91        // Applies to both solo-owned and pool-owned wells
92        let remaining = duration - elapsed;
93        (self.init_price as u128 * remaining as u128 / duration as u128) as u64
94    }
95
96    /// Update accumulated OIL for this epoch state
97    pub fn update_accumulated_oil(&mut self, clock: &Clock) {
98        // Skip if no owner
99        if self.current_bidder == Pubkey::default() && self.is_pool_owned == 0 {
100            return;
101        }
102        
103        let last_update = self.last_update_time as i64;
104        let elapsed = clock.unix_timestamp.saturating_sub(last_update);
105        if elapsed <= 0 {
106            return;
107        }
108        
109        // Calculate OIL mined: rate * time
110        let oil_mined = self.mps
111            .checked_mul(elapsed as u64)
112            .unwrap_or(0);
113        
114        self.accumulated_oil = self.accumulated_oil
115            .checked_add(oil_mined)
116            .unwrap_or(u64::MAX);
117        
118        self.lifetime_oil_mined = self.lifetime_oil_mined
119            .checked_add(oil_mined)
120            .unwrap_or(u64::MAX);
121        
122        // Track total mined by current operator (persists even after claiming)
123        self.operator_total_oil_mined = self.operator_total_oil_mined
124            .checked_add(oil_mined)
125            .unwrap_or(u64::MAX);
126        
127        self.last_update_time = clock.unix_timestamp as u64;
128    }
129
130    /// Check and apply halving if needed, updating mining rate
131    /// Time-based halving: halvings occur every 28 days (halving_period_seconds)
132    /// Uses 50% reduction (multiply by 0.5) per halving, matching Macaron's model
133    pub fn check_and_apply_halving(&mut self, auction: &mut Auction, clock: &Clock) {
134        // Check if we should apply halvings based on current time
135        let current_time = clock.unix_timestamp as u64;
136        let halvings_to_apply = auction.should_apply_halving(current_time);
137        
138        if halvings_to_apply > 0 {
139            // Apply halvings: 50% reduction (multiply by 0.5) per halving
140            // Formula: new_rate = old_rate * 0.5 = old_rate / 2
141            for _ in 0..halvings_to_apply {
142                self.mps = self.mps / 2; // 50% reduction (multiply by 0.5)
143                self.halving_count += 1;
144            }
145            
146            // Update auction last_halving_time to current time
147            // This ensures we don't apply the same halving multiple times
148            auction.last_halving_time = current_time;
149        }
150    }
151}
152
153account!(OilAccount, Well);
154