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 {
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 let referral_amount = if total_amount > 0 {
49 total_amount / 200 } else {
51 0
52 };
53
54 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 let referral_amount = if total_amount > 0 {
66 total_amount / 200 } else {
68 0
69 };
70
71 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 if referrer == Pubkey::default() || referrer == authority {
87 return Ok(());
88 }
89
90 let referral_info = referral_info_opt.ok_or(ProgramError::NotEnoughAccountKeys)?;
92
93 referral_info
95 .is_writable()?
96 .has_seeds(&[REFERRAL, &referrer.to_bytes()], &crate::ID)?;
97
98 if referral_info.data_is_empty() {
100 return Err(ProgramError::InvalidAccountData);
101 }
102
103 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);