Skip to main content

klend_interface/instructions/
referrer.rs

1use borsh::BorshSerialize;
2use solana_instruction::Instruction;
3use solana_pubkey::Pubkey;
4
5use crate::{discriminators, util::*, KLEND_PROGRAM_ID, SYSTEM_PROGRAM_ID, SYSVAR_RENT_ID};
6
7// ---------------------------------------------------------------------------
8// init_referrer_token_state
9// ---------------------------------------------------------------------------
10
11pub struct InitReferrerTokenStateAccounts {
12    pub payer: Pubkey,
13    pub lending_market: Pubkey,
14    pub reserve: Pubkey,
15    pub referrer: Pubkey,
16    pub referrer_token_state: Pubkey,
17}
18
19pub fn init_referrer_token_state(accounts: InitReferrerTokenStateAccounts) -> Instruction {
20    let data = discriminators::INIT_REFERRER_TOKEN_STATE.to_vec();
21
22    Instruction {
23        program_id: KLEND_PROGRAM_ID,
24        accounts: vec![
25            signer_writable(accounts.payer),
26            readonly(accounts.lending_market),
27            readonly(accounts.reserve),
28            readonly(accounts.referrer),
29            writable(accounts.referrer_token_state),
30            readonly(SYSVAR_RENT_ID),
31            readonly(SYSTEM_PROGRAM_ID),
32        ],
33        data,
34    }
35}
36
37// ---------------------------------------------------------------------------
38// init_user_metadata
39// ---------------------------------------------------------------------------
40
41pub struct InitUserMetadataAccounts {
42    pub owner: Pubkey,
43    pub fee_payer: Pubkey,
44    pub user_metadata: Pubkey,
45    pub referrer_user_metadata: Option<Pubkey>,
46}
47
48pub fn init_user_metadata(
49    accounts: InitUserMetadataAccounts,
50    user_lookup_table: Pubkey,
51) -> Instruction {
52    #[derive(BorshSerialize)]
53    struct Args {
54        user_lookup_table: Pubkey,
55    }
56
57    let mut data = discriminators::INIT_USER_METADATA.to_vec();
58    Args { user_lookup_table }.serialize(&mut data).unwrap();
59
60    Instruction {
61        program_id: KLEND_PROGRAM_ID,
62        accounts: vec![
63            signer(accounts.owner),
64            signer_writable(accounts.fee_payer),
65            writable(accounts.user_metadata),
66            optional_account(&KLEND_PROGRAM_ID, accounts.referrer_user_metadata, false),
67            readonly(SYSVAR_RENT_ID),
68            readonly(SYSTEM_PROGRAM_ID),
69        ],
70        data,
71    }
72}
73
74// ---------------------------------------------------------------------------
75// withdraw_referrer_fees
76// ---------------------------------------------------------------------------
77
78pub struct WithdrawReferrerFeesAccounts {
79    pub referrer: Pubkey,
80    pub referrer_token_state: Pubkey,
81    pub reserve: Pubkey,
82    pub reserve_liquidity_mint: Pubkey,
83    pub reserve_supply_liquidity: Pubkey,
84    pub referrer_token_account: Pubkey,
85    pub lending_market: Pubkey,
86    pub lending_market_authority: Pubkey,
87    pub token_program: Pubkey,
88}
89
90pub fn withdraw_referrer_fees(accounts: WithdrawReferrerFeesAccounts) -> Instruction {
91    let data = discriminators::WITHDRAW_REFERRER_FEES.to_vec();
92
93    Instruction {
94        program_id: KLEND_PROGRAM_ID,
95        accounts: vec![
96            signer_writable(accounts.referrer),
97            writable(accounts.referrer_token_state),
98            writable(accounts.reserve),
99            readonly(accounts.reserve_liquidity_mint),
100            writable(accounts.reserve_supply_liquidity),
101            writable(accounts.referrer_token_account),
102            readonly(accounts.lending_market),
103            readonly(accounts.lending_market_authority),
104            readonly(accounts.token_program),
105        ],
106        data,
107    }
108}
109
110// ---------------------------------------------------------------------------
111// init_referrer_state_and_short_url
112// ---------------------------------------------------------------------------
113
114pub struct InitReferrerStateAndShortUrlAccounts {
115    pub referrer: Pubkey,
116    pub referrer_state: Pubkey,
117    pub referrer_short_url: Pubkey,
118    pub referrer_user_metadata: Pubkey,
119}
120
121pub fn init_referrer_state_and_short_url(
122    accounts: InitReferrerStateAndShortUrlAccounts,
123    short_url: String,
124) -> Instruction {
125    #[derive(BorshSerialize)]
126    struct Args {
127        short_url: String,
128    }
129
130    let mut data = discriminators::INIT_REFERRER_STATE_AND_SHORT_URL.to_vec();
131    Args { short_url }.serialize(&mut data).unwrap();
132
133    Instruction {
134        program_id: KLEND_PROGRAM_ID,
135        accounts: vec![
136            signer_writable(accounts.referrer),
137            writable(accounts.referrer_state),
138            writable(accounts.referrer_short_url),
139            readonly(accounts.referrer_user_metadata),
140            readonly(SYSVAR_RENT_ID),
141            readonly(SYSTEM_PROGRAM_ID),
142        ],
143        data,
144    }
145}
146
147// ---------------------------------------------------------------------------
148// delete_referrer_state_and_short_url
149// ---------------------------------------------------------------------------
150
151pub struct DeleteReferrerStateAndShortUrlAccounts {
152    pub referrer: Pubkey,
153    pub referrer_state: Pubkey,
154    pub short_url: Pubkey,
155}
156
157pub fn delete_referrer_state_and_short_url(
158    accounts: DeleteReferrerStateAndShortUrlAccounts,
159) -> Instruction {
160    let data = discriminators::DELETE_REFERRER_STATE_AND_SHORT_URL.to_vec();
161
162    Instruction {
163        program_id: KLEND_PROGRAM_ID,
164        accounts: vec![
165            signer_writable(accounts.referrer),
166            writable(accounts.referrer_state),
167            writable(accounts.short_url),
168            readonly(SYSVAR_RENT_ID),
169            readonly(SYSTEM_PROGRAM_ID),
170        ],
171        data,
172    }
173}