buddy_link/cpi/
validate_referrer.rs

1use crate::constants::BL_PROGRAM_ID;
2use crate::instruction;
3use crate::utils::{get_account_info_or_default, get_key_or_none};
4use anchor_lang::prelude::*;
5use anchor_lang::{Accounts, Key, ToAccountInfo};
6use solana_program::entrypoint::ProgramResult;
7use solana_program::program::invoke;
8
9#[derive(Accounts)]
10pub struct ValidateReferrer<'info> {
11    /// CHECK: The buddylink program
12    #[account(executable, address = BL_PROGRAM_ID)]
13    pub buddy_link_program: AccountInfo<'info>,
14
15    /// CHECK: Payer of the transaction
16    #[account(mut, signer)]
17    pub payer: AccountInfo<'info>,
18    /// CHECK: Authority
19    #[account(mut)]
20    pub authority: AccountInfo<'info>,
21
22    /// CHECK: Buddy Link Profile of the referee.
23    #[account()]
24    pub referee_buddy_profile: AccountInfo<'info>,
25    /// CHECK: Buddy Link Paid buddy of the referee (could be the same as above).
26    #[account()]
27    pub referee_buddy: AccountInfo<'info>,
28    /// CHECK: Referee treasury (is owned by above).
29    #[account(mut)]
30    pub referee_treasury: AccountInfo<'info>,
31    /// CHECK: Referee member (account of the referee within your organization).
32    #[account()]
33    pub referee_member: AccountInfo<'info>,
34
35    /// CHECK: Referrer member (account of the referrer within your organization) (if you want to validate the referrer of the current referee).
36    #[account()]
37    pub referrer_member: Option<AccountInfo<'info>>,
38    /// CHECK: Referrer treasury (treasury that owns the referrer member ) (if you want to validate the referrer of the current referee).
39    #[account()]
40    pub referrer_treasury: Option<AccountInfo<'info>>,
41    /// CHECK: Referrer treasury for reward (treasury that is linked to the current mint, could be the same as above) (if you want to validate the referrer of the current referee).
42    #[account()]
43    pub referrer_treasury_for_reward: Option<AccountInfo<'info>>,
44    /// CHECK: Token account linked to the mint (if you want to validate the referral tree with a specific mint)
45    #[account()]
46    pub referrer_token_account: Option<AccountInfo<'info>>,
47    /// CHECK: The mint
48    #[account()]
49    pub mint: Option<AccountInfo<'info>>,
50}
51
52pub fn validate_referrer<'info>(ctx: CpiContext<'_, '_, '_, 'info, ValidateReferrer<'info>>) -> ProgramResult {
53    let instruction = instruction::validate_referrer(
54        ctx.accounts.payer.key(),
55        ctx.accounts.authority.key(),
56        get_key_or_none(&ctx.accounts.mint),
57        get_key_or_none(&ctx.accounts.referrer_token_account),
58        get_key_or_none(&ctx.accounts.referrer_member),
59        get_key_or_none(&ctx.accounts.referrer_treasury),
60        get_key_or_none(&ctx.accounts.referrer_treasury_for_reward),
61        ctx.accounts.referee_buddy_profile.key(),
62        ctx.accounts.referee_buddy.key(),
63        ctx.accounts.referee_treasury.key(),
64        ctx.accounts.referee_member.key(),
65    );
66
67    let default_account_info = ctx.accounts.buddy_link_program.to_account_info();
68
69    let account_infos = [
70        ctx.accounts.payer.to_account_info(),
71        ctx.accounts.authority.to_account_info(),
72        ctx.accounts.referee_buddy_profile.to_account_info(),
73        ctx.accounts.referee_buddy.to_account_info(),
74        ctx.accounts.referee_treasury.to_account_info(),
75        ctx.accounts.referee_member.to_account_info(),
76        get_account_info_or_default(&ctx.accounts.referrer_member, &default_account_info),
77        get_account_info_or_default(&ctx.accounts.referrer_treasury, &default_account_info),
78        get_account_info_or_default(
79            &ctx.accounts.referrer_treasury_for_reward,
80            &default_account_info,
81        ),
82        get_account_info_or_default(&ctx.accounts.mint, &default_account_info),
83        get_account_info_or_default(&ctx.accounts.referrer_token_account, &default_account_info),
84    ];
85
86    invoke(&instruction, &account_infos)
87}