Skip to main content

oil_api/state/
referral.rs

1use serde::{Deserialize, Serialize};
2use solana_program::pubkey::Pubkey;
3use solana_program::program_error::ProgramError;
4use solana_program::log::sol_log;
5use steel::*;
6
7use super::OilAccount;
8use crate::consts::REFERRAL;
9
10/// Referral account tracks a referrer's stats and pending rewards.
11#[repr(C)]
12#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
13pub struct Referral {
14    /// The authority (wallet) of this referrer.
15    pub authority: Pubkey,
16
17    /// Total number of miners referred by this referrer.
18    pub total_referred: u64,
19
20    /// Total SOL earned from referrals (lifetime, for stats).
21    pub total_sol_earned: u64,
22
23    /// Total OIL earned from referrals (lifetime, for stats).
24    pub total_oil_earned: u64,
25
26    /// Pending SOL rewards to claim.
27    pub pending_sol: u64,
28
29    /// Pending OIL rewards to claim.
30    pub pending_oil: u64,
31}
32
33impl Referral {
34    pub fn claim_sol(&mut self) -> u64 {
35        let amount = self.pending_sol;
36        self.pending_sol = 0;
37        amount
38    }
39
40    pub fn claim_oil(&mut self) -> u64 {
41        let amount = self.pending_oil;
42        self.pending_oil = 0;
43        amount
44    }
45
46    pub fn credit_sol_referral(&mut self, total_amount: u64) -> u64 {
47        // Calculate referral bonus (0.5% of total claim)
48        let referral_amount = if total_amount > 0 {
49            total_amount / 200 // 0.5% = 1/200
50        } else {
51            0
52        };
53
54        // Credit referral account
55        if referral_amount > 0 {
56            self.pending_sol += referral_amount;
57            self.total_sol_earned += referral_amount;
58        }
59
60        referral_amount
61    }
62
63    pub fn credit_oil_referral(&mut self, total_amount: u64) -> u64 {
64        // Calculate referral bonus (0.5% of total claim)
65        let referral_amount = if total_amount > 0 {
66            total_amount / 200 // 0.5% = 1/200
67        } else {
68            0
69        };
70
71        // Credit referral account
72        if referral_amount > 0 {
73            self.pending_oil += referral_amount;
74            self.total_oil_earned += referral_amount;
75        }
76
77        referral_amount
78    }
79
80    pub fn process_new_miner_referral<'a>(
81        referral_info_opt: Option<&AccountInfo<'a>>,
82        referrer: Pubkey,
83        authority: Pubkey,
84    ) -> Result<(), ProgramError> {
85        // Only process if referrer is valid (not default and not self-referral)
86        if referrer == Pubkey::default() || referrer == authority {
87            return Ok(());
88        }
89        
90        // Referral account must be provided
91        let referral_info = referral_info_opt.ok_or(ProgramError::NotEnoughAccountKeys)?;
92        
93        // Validate referral account
94        referral_info
95            .is_writable()?
96            .has_seeds(&[REFERRAL, &referrer.to_bytes()], &crate::ID)?;
97        
98        // Referral account must exist
99        if referral_info.data_is_empty() {
100            return Err(ProgramError::InvalidAccountData);
101        }
102        
103        // Increment total_referred
104        let referral = referral_info.as_account_mut::<Referral>(&crate::ID)?;
105        referral.total_referred += 1;
106        sol_log(&format!("Referral: {} now has {} referrals", referrer, referral.total_referred));
107        
108        Ok(())
109    }
110}
111
112account!(OilAccount, Referral);