Skip to main content

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#[repr(C)]
10#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
11pub struct Well {
12    /// Well ID (0-3) - which well this is for
13    pub well_id: u64,
14    
15    /// Current epoch ID (increments each auction: 0, 1, 2, 3, etc.)
16    pub epoch_id: u64,
17    
18    /// Current bidder/owner (Pubkey::default() if unowned)
19    pub current_bidder: Pubkey,
20    
21    /// Initial price for current epoch (in lamports)
22    pub init_price: u64,
23    
24    /// Mining per second (MPS) - current mining rate (OIL per second, in atomic units)
25    pub mps: u64,
26    
27    /// Epoch start time (timestamp when current epoch started)
28    pub epoch_start_time: u64,
29    
30    /// Accumulated OIL mined by current owner (not yet claimed)
31    pub accumulated_oil: u64,
32    
33    /// Last time accumulated_oil was updated
34    pub last_update_time: u64,
35    
36    /// Number of halvings that have occurred (for rate calculation)
37    pub halving_count: u64,
38    
39    /// Total OIL ever mined from this well (lifetime)
40    pub lifetime_oil_mined: u64,
41    
42    /// Total OIL mined by current operator (doesn't reset when claimed, only when ownership changes)
43    pub operator_total_oil_mined: u64,
44    
45    /// Buffer field (for future use) - previously is_pool_owned
46    pub buffer_c: u64,
47    
48    /// Buffer field (for future use)
49    pub buffer_d: u64,
50    
51    /// Buffer field (for future use)
52    pub buffer_e: u64,
53}
54
55impl Well {
56    pub fn pda(well_id: u64) -> (Pubkey, u8) {
57        well_pda(well_id)
58    }
59
60    pub fn current_price(&self, auction: &Auction, clock: &Clock) -> u64 {
61        use crate::consts::AUCTION_FLOOR_PRICE;
62        
63        // If well has no owner (never been bid on), show starting price
64        use solana_program::pubkey::Pubkey;
65        if self.current_bidder == Pubkey::default() {
66            return self.init_price; // Return starting price for unowned wells
67        }
68        
69        let elapsed = clock.unix_timestamp.saturating_sub(self.epoch_start_time as i64);
70        let duration = auction.auction_duration_seconds as i64;
71        
72        if elapsed >= duration {
73            return AUCTION_FLOOR_PRICE; // Auction expired, price is at floor
74        }
75        
76        // Linear decay: price = floor + (init_price - floor) * (remaining / duration)
77        let remaining = duration - elapsed;
78        let price_range = self.init_price.saturating_sub(AUCTION_FLOOR_PRICE);
79        let decayed_amount = (price_range as u128 * remaining as u128 / duration as u128) as u64;
80        AUCTION_FLOOR_PRICE + decayed_amount
81    }
82
83    pub fn update_accumulated_oil(&mut self, clock: &Clock) {
84        // Skip if no owner
85        use solana_program::pubkey::Pubkey;
86        if self.current_bidder == Pubkey::default() {
87            return;
88        }
89        
90        let last_update = self.last_update_time as i64;
91        let elapsed = clock.unix_timestamp.saturating_sub(last_update);
92        if elapsed <= 0 {
93            return;
94        }
95        
96        // Calculate OIL mined: rate * time
97        let oil_mined = self.mps
98            .checked_mul(elapsed as u64)
99            .unwrap_or(0);
100        
101        self.accumulated_oil = self.accumulated_oil
102            .checked_add(oil_mined)
103            .unwrap_or(u64::MAX);
104        
105        self.lifetime_oil_mined = self.lifetime_oil_mined
106            .checked_add(oil_mined)
107            .unwrap_or(u64::MAX);
108        
109        // Track total mined by current operator (persists even after claiming)
110        self.operator_total_oil_mined = self.operator_total_oil_mined
111            .checked_add(oil_mined)
112            .unwrap_or(u64::MAX);
113        
114        self.last_update_time = clock.unix_timestamp as u64;
115    }
116
117    pub fn check_and_apply_halving(&mut self, auction: &mut Auction, clock: &Clock) {
118        // Check if we should apply halvings based on current time
119        let current_time = clock.unix_timestamp as u64;
120        let halvings_to_apply = auction.should_apply_halving(current_time);
121        
122        if halvings_to_apply > 0 {
123            // Apply halvings: 50% reduction (multiply by 0.5) per halving
124            for _ in 0..halvings_to_apply {
125                self.mps = self.mps / 2; // 50% reduction (multiply by 0.5)
126                self.halving_count += 1;
127            }
128            
129            // Update auction last_halving_time to current time
130            auction.last_halving_time = current_time;
131        }
132    }
133}
134
135account!(OilAccount, Well);
136