oil_api/state/
referral.rs1use 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#[repr(C)]
12#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
13pub struct Referral {
14 pub authority: Pubkey,
16
17 pub total_referred: u64,
19
20 pub total_sol_earned: u64,
22
23 pub total_oil_earned: u64,
25
26 pub pending_sol: u64,
28
29 pub pending_oil: u64,
31}
32
33impl Referral {
34 pub fn claim_sol(&mut self) -> u64 {
36 let amount = self.pending_sol;
37 self.pending_sol = 0;
38 amount
39 }
40
41 pub fn claim_oil(&mut self) -> u64 {
43 let amount = self.pending_oil;
44 self.pending_oil = 0;
45 amount
46 }
47
48 pub fn credit_sol_referral(&mut self, total_amount: u64) -> u64 {
50 let referral_amount = if total_amount > 0 {
52 total_amount / 200 } else {
54 0
55 };
56
57 if referral_amount > 0 {
59 self.pending_sol += referral_amount;
60 self.total_sol_earned += referral_amount;
61 }
62
63 referral_amount
64 }
65
66 pub fn credit_oil_referral(&mut self, total_amount: u64) -> u64 {
68 let referral_amount = if total_amount > 0 {
70 total_amount / 200 } else {
72 0
73 };
74
75 if referral_amount > 0 {
77 self.pending_oil += referral_amount;
78 self.total_oil_earned += referral_amount;
79 }
80
81 referral_amount
82 }
83
84 pub fn process_new_miner_referral<'a>(
86 referral_info_opt: Option<&AccountInfo<'a>>,
87 referrer: Pubkey,
88 authority: Pubkey,
89 ) -> Result<(), ProgramError> {
90 if referrer == Pubkey::default() || referrer == authority {
92 return Ok(());
93 }
94
95 let referral_info = referral_info_opt.ok_or(ProgramError::NotEnoughAccountKeys)?;
97
98 referral_info
100 .is_writable()?
101 .has_seeds(&[REFERRAL, &referrer.to_bytes()], &crate::ID)?;
102
103 if referral_info.data_is_empty() {
105 return Err(ProgramError::InvalidAccountData);
106 }
107
108 let referral = referral_info.as_account_mut::<Referral>(&crate::ID)?;
110 referral.total_referred += 1;
111 sol_log(&format!("Referral: {} now has {} referrals", referrer, referral.total_referred));
112
113 Ok(())
114 }
115}
116
117account!(OilAccount, Referral);