oil_api/state/referral.rs
1use serde::{Deserialize, Serialize};
2use solana_program::pubkey::Pubkey;
3use steel::*;
4
5use super::OilAccount;
6
7/// Referral account tracks a referrer's stats and pending rewards.
8///
9/// Anyone can create a Referral account to become a referrer.
10/// When a new driller signs up with their referral link, 0.5% of that
11/// driller's claimed rewards (both SOL and OIL) are credited to the
12/// referrer's pending balance.
13#[repr(C)]
14#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
15pub struct Referral {
16 /// The authority (wallet) of this referrer.
17 pub authority: Pubkey,
18
19 /// Total number of drillers referred by this referrer.
20 pub total_referred: u64,
21
22 /// Total SOL earned from referrals (lifetime, for stats).
23 pub total_sol_earned: u64,
24
25 /// Total OIL earned from referrals (lifetime, for stats).
26 pub total_oil_earned: u64,
27
28 /// Pending SOL rewards to claim.
29 pub pending_sol: u64,
30
31 /// Pending OIL rewards to claim.
32 pub pending_oil: u64,
33}
34
35impl Referral {
36 /// Claims pending SOL rewards.
37 /// Returns the amount of SOL that can be claimed and resets pending_sol to 0.
38 pub fn claim_sol(&mut self) -> u64 {
39 let amount = self.pending_sol;
40 self.pending_sol = 0;
41 amount
42 }
43
44 /// Claims pending OIL rewards.
45 /// Returns the amount of OIL that can be claimed and resets pending_oil to 0.
46 pub fn claim_oil(&mut self) -> u64 {
47 let amount = self.pending_oil;
48 self.pending_oil = 0;
49 amount
50 }
51
52 /// Calculates and credits SOL referral bonus (1.0% of total amount).
53 /// Returns the referral amount that should be transferred to this referral account.
54 ///
55 /// This function handles:
56 /// - Calculating 1.0% referral bonus
57 /// - Crediting pending_sol
58 /// - Updating total_sol_earned
59 pub fn credit_sol_referral(&mut self, total_amount: u64) -> u64 {
60 // Calculate referral bonus (1.0% of total claim)
61 let referral_amount = if total_amount > 0 {
62 total_amount / 100 // 1.0% = 1/100
63 } else {
64 0
65 };
66
67 // Credit referral account
68 if referral_amount > 0 {
69 self.pending_sol += referral_amount;
70 self.total_sol_earned += referral_amount;
71 }
72
73 referral_amount
74 }
75
76 /// Calculates and credits OIL referral bonus (1.0% of total amount).
77 /// Returns the referral amount that should be transferred to this referral account.
78 ///
79 /// This function handles:
80 /// - Calculating 1.0% referral bonus
81 /// - Crediting pending_oil
82 /// - Updating total_oil_earned
83 pub fn credit_oil_referral(&mut self, total_amount: u64) -> u64 {
84 // Calculate referral bonus (1.0% of total claim)
85 let referral_amount = if total_amount > 0 {
86 total_amount / 100 // 1.0% = 1/100
87 } else {
88 0
89 };
90
91 // Credit referral account
92 if referral_amount > 0 {
93 self.pending_oil += referral_amount;
94 self.total_oil_earned += referral_amount;
95 }
96
97 referral_amount
98 }
99}
100
101account!(OilAccount, Referral);