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    /// Total contributed FOGO for current epoch (tracks native SOL balance in Well PDA's system account)
49    /// Incremented on each contribution, decremented when pool bids
50    /// Reset to 0 when epoch ends
51    pub total_contributed: u64,
52    
53    /// Pool bid cost - stores the bid_amount when pool bids
54    /// Used to calculate original_total when pool gets outbid
55    /// Reset to 0 when epoch ends
56    pub pool_bid_cost: u64,
57}
58
59impl Well {
60    pub fn pda(well_id: u64) -> (Pubkey, u8) {
61        well_pda(well_id)
62    }
63
64    pub fn current_price(&self, auction: &Auction, clock: &Clock) -> u64 {
65        use crate::consts::AUCTION_FLOOR_PRICE;
66        
67        // If well has no owner (never been bid on), show starting price
68        use solana_program::pubkey::Pubkey;
69        if self.current_bidder == Pubkey::default() {
70            return self.init_price; // Return starting price for unowned wells
71        }
72        
73        let elapsed = clock.unix_timestamp.saturating_sub(self.epoch_start_time as i64);
74        let duration = auction.auction_duration_seconds as i64;
75        
76        if elapsed >= duration {
77            return AUCTION_FLOOR_PRICE; // Auction expired, price is at floor
78        }
79        
80        // Linear decay: price = floor + (init_price - floor) * (remaining / duration)
81        let remaining = duration - elapsed;
82        let price_range = self.init_price.saturating_sub(AUCTION_FLOOR_PRICE);
83        let decayed_amount = (price_range as u128 * remaining as u128 / duration as u128) as u64;
84        AUCTION_FLOOR_PRICE + decayed_amount
85    }
86
87    pub fn update_accumulated_oil(&mut self, clock: &Clock) {
88        // Skip if no owner
89        use solana_program::pubkey::Pubkey;
90        if self.current_bidder == Pubkey::default() {
91            return;
92        }
93        
94        let last_update = self.last_update_time as i64;
95        let elapsed = clock.unix_timestamp.saturating_sub(last_update);
96        if elapsed <= 0 {
97            return;
98        }
99        
100        // Calculate OIL mined: rate * time
101        let oil_mined = self.mps
102            .checked_mul(elapsed as u64)
103            .unwrap_or(0);
104        
105        self.accumulated_oil = self.accumulated_oil
106            .checked_add(oil_mined)
107            .unwrap_or(u64::MAX);
108        
109        self.lifetime_oil_mined = self.lifetime_oil_mined
110            .checked_add(oil_mined)
111            .unwrap_or(u64::MAX);
112        
113        // Track total mined by current operator (persists even after claiming)
114        self.operator_total_oil_mined = self.operator_total_oil_mined
115            .checked_add(oil_mined)
116            .unwrap_or(u64::MAX);
117        
118        self.last_update_time = clock.unix_timestamp as u64;
119    }
120
121    pub fn check_and_apply_halving(&mut self, auction: &mut Auction, clock: &Clock) {
122        // Check if we should apply halvings based on current time
123        let current_time = clock.unix_timestamp as u64;
124        let halvings_to_apply = auction.should_apply_halving(current_time);
125        
126        if halvings_to_apply > 0 {
127            // Apply halvings: 50% reduction (multiply by 0.5) per halving
128            for _ in 0..halvings_to_apply {
129                self.mps = self.mps / 2; // 50% reduction (multiply by 0.5)
130                self.halving_count += 1;
131            }
132            
133            // Update auction last_halving_time to current time
134            auction.last_halving_time = current_time;
135        }
136    }
137}
138
139account!(OilAccount, Well);
140