Skip to main content

klend_interface/instructions/
obligation.rs

1use borsh::BorshSerialize;
2use solana_instruction::{AccountMeta, Instruction};
3use solana_pubkey::Pubkey;
4
5use crate::{
6    discriminators, types::UpdateObligationConfigMode, util::*, FARMS_PROGRAM_ID, KLEND_PROGRAM_ID,
7    SYSTEM_PROGRAM_ID, SYSVAR_INSTRUCTIONS_ID, SYSVAR_RENT_ID,
8};
9
10// ---------------------------------------------------------------------------
11// init_obligation
12// ---------------------------------------------------------------------------
13
14pub struct InitObligationAccounts {
15    pub obligation_owner: Pubkey,
16    pub fee_payer: Pubkey,
17    pub obligation: Pubkey,
18    pub lending_market: Pubkey,
19    pub seed1_account: Pubkey,
20    pub seed2_account: Pubkey,
21    pub owner_user_metadata: Pubkey,
22}
23
24pub fn init_obligation(
25    accounts: InitObligationAccounts,
26    args: crate::types::InitObligationArgs,
27) -> Instruction {
28    let mut data = discriminators::INIT_OBLIGATION.to_vec();
29    args.serialize(&mut data).unwrap();
30
31    Instruction {
32        program_id: KLEND_PROGRAM_ID,
33        accounts: vec![
34            signer(accounts.obligation_owner),
35            signer_writable(accounts.fee_payer),
36            writable(accounts.obligation),
37            readonly(accounts.lending_market),
38            readonly(accounts.seed1_account),
39            readonly(accounts.seed2_account),
40            readonly(accounts.owner_user_metadata),
41            readonly(SYSVAR_RENT_ID),
42            readonly(SYSTEM_PROGRAM_ID),
43        ],
44        data,
45    }
46}
47
48// ---------------------------------------------------------------------------
49// init_obligation_farms_for_reserve
50// ---------------------------------------------------------------------------
51
52pub struct InitObligationFarmsForReserveAccounts {
53    pub payer: Pubkey,
54    pub owner: Pubkey,
55    pub obligation: Pubkey,
56    pub lending_market_authority: Pubkey,
57    pub reserve: Pubkey,
58    pub reserve_farm_state: Pubkey,
59    pub obligation_farm: Pubkey,
60    pub lending_market: Pubkey,
61}
62
63pub fn init_obligation_farms_for_reserve(
64    accounts: InitObligationFarmsForReserveAccounts,
65    mode: u8,
66) -> Instruction {
67    #[derive(BorshSerialize)]
68    struct Args {
69        mode: u8,
70    }
71
72    let mut data = discriminators::INIT_OBLIGATION_FARMS_FOR_RESERVE.to_vec();
73    Args { mode }.serialize(&mut data).unwrap();
74
75    Instruction {
76        program_id: KLEND_PROGRAM_ID,
77        accounts: vec![
78            signer_writable(accounts.payer),
79            readonly(accounts.owner),
80            writable(accounts.obligation),
81            readonly(accounts.lending_market_authority),
82            writable(accounts.reserve),
83            writable(accounts.reserve_farm_state),
84            writable(accounts.obligation_farm),
85            readonly(accounts.lending_market),
86            readonly(FARMS_PROGRAM_ID),
87            readonly(SYSVAR_RENT_ID),
88            readonly(SYSTEM_PROGRAM_ID),
89        ],
90        data,
91    }
92}
93
94// ---------------------------------------------------------------------------
95// refresh_obligation_farms_for_reserve
96// ---------------------------------------------------------------------------
97
98pub struct RefreshObligationFarmsForReserveAccounts {
99    pub crank: Pubkey,
100    pub obligation: Pubkey,
101    pub lending_market_authority: Pubkey,
102    pub reserve: Pubkey,
103    pub reserve_farm_state: Pubkey,
104    pub obligation_farm_user_state: Pubkey,
105    pub lending_market: Pubkey,
106}
107
108pub fn refresh_obligation_farms_for_reserve(
109    accounts: RefreshObligationFarmsForReserveAccounts,
110    mode: u8,
111) -> Instruction {
112    #[derive(BorshSerialize)]
113    struct Args {
114        mode: u8,
115    }
116
117    let mut data = discriminators::REFRESH_OBLIGATION_FARMS_FOR_RESERVE.to_vec();
118    Args { mode }.serialize(&mut data).unwrap();
119
120    Instruction {
121        program_id: KLEND_PROGRAM_ID,
122        accounts: vec![
123            signer(accounts.crank),
124            readonly(accounts.obligation),
125            readonly(accounts.lending_market_authority),
126            readonly(accounts.reserve),
127            writable(accounts.reserve_farm_state),
128            writable(accounts.obligation_farm_user_state),
129            readonly(accounts.lending_market),
130            readonly(FARMS_PROGRAM_ID),
131            readonly(SYSVAR_RENT_ID),
132            readonly(SYSTEM_PROGRAM_ID),
133        ],
134        data,
135    }
136}
137
138// ---------------------------------------------------------------------------
139// request_elevation_group
140// ---------------------------------------------------------------------------
141
142pub struct RequestElevationGroupAccounts {
143    pub owner: Pubkey,
144    pub obligation: Pubkey,
145    pub lending_market: Pubkey,
146}
147
148pub fn request_elevation_group(
149    accounts: RequestElevationGroupAccounts,
150    elevation_group: u8,
151    remaining_accounts: Vec<AccountMeta>,
152) -> Instruction {
153    #[derive(BorshSerialize)]
154    struct Args {
155        elevation_group: u8,
156    }
157
158    let mut data = discriminators::REQUEST_ELEVATION_GROUP.to_vec();
159    Args { elevation_group }.serialize(&mut data).unwrap();
160
161    let mut account_metas = vec![
162        signer(accounts.owner),
163        writable(accounts.obligation),
164        readonly(accounts.lending_market),
165    ];
166    account_metas.extend(remaining_accounts);
167
168    Instruction {
169        program_id: KLEND_PROGRAM_ID,
170        accounts: account_metas,
171        data,
172    }
173}
174
175// ---------------------------------------------------------------------------
176// update_obligation_config
177// ---------------------------------------------------------------------------
178
179pub struct UpdateObligationConfigAccounts {
180    pub owner: Pubkey,
181    pub obligation: Pubkey,
182    pub borrow_reserve: Option<Pubkey>,
183    pub deposit_reserve: Option<Pubkey>,
184    pub lending_market: Pubkey,
185}
186
187pub fn update_obligation_config(
188    accounts: UpdateObligationConfigAccounts,
189    mode: UpdateObligationConfigMode,
190    value: Vec<u8>,
191) -> Instruction {
192    #[derive(BorshSerialize)]
193    struct Args {
194        mode: UpdateObligationConfigMode,
195        value: Vec<u8>,
196    }
197
198    let mut data = discriminators::UPDATE_OBLIGATION_CONFIG.to_vec();
199    Args { mode, value }.serialize(&mut data).unwrap();
200
201    Instruction {
202        program_id: KLEND_PROGRAM_ID,
203        accounts: vec![
204            signer(accounts.owner),
205            writable(accounts.obligation),
206            optional_account(&KLEND_PROGRAM_ID, accounts.borrow_reserve, false),
207            optional_account(&KLEND_PROGRAM_ID, accounts.deposit_reserve, false),
208            readonly(accounts.lending_market),
209        ],
210        data,
211    }
212}
213
214// ---------------------------------------------------------------------------
215// initiate_obligation_ownership_transfer
216// ---------------------------------------------------------------------------
217
218pub struct InitiateObligationOwnershipTransferAccounts {
219    pub owner: Pubkey,
220    pub obligation: Pubkey,
221}
222
223pub fn initiate_obligation_ownership_transfer(
224    accounts: InitiateObligationOwnershipTransferAccounts,
225    new_owner: Pubkey,
226) -> Instruction {
227    #[derive(BorshSerialize)]
228    struct Args {
229        new_owner: Pubkey,
230    }
231
232    let mut data = discriminators::INITIATE_OBLIGATION_OWNERSHIP_TRANSFER.to_vec();
233    Args { new_owner }.serialize(&mut data).unwrap();
234
235    Instruction {
236        program_id: KLEND_PROGRAM_ID,
237        accounts: vec![
238            signer(accounts.owner),
239            writable(accounts.obligation),
240            readonly(SYSVAR_INSTRUCTIONS_ID),
241        ],
242        data,
243    }
244}
245
246// ---------------------------------------------------------------------------
247// approve_obligation_ownership_transfer
248// ---------------------------------------------------------------------------
249
250pub struct ApproveObligationOwnershipTransferAccounts {
251    pub global_admin: Pubkey,
252    pub global_config: Pubkey,
253    pub obligation: Pubkey,
254    pub pending_owner: Pubkey,
255}
256
257pub fn approve_obligation_ownership_transfer(
258    accounts: ApproveObligationOwnershipTransferAccounts,
259) -> Instruction {
260    Instruction {
261        program_id: KLEND_PROGRAM_ID,
262        accounts: vec![
263            signer(accounts.global_admin),
264            readonly(accounts.global_config),
265            writable(accounts.obligation),
266            readonly(accounts.pending_owner),
267        ],
268        data: discriminators::APPROVE_OBLIGATION_OWNERSHIP_TRANSFER.to_vec(),
269    }
270}
271
272// ---------------------------------------------------------------------------
273// accept_obligation_ownership
274// ---------------------------------------------------------------------------
275
276pub struct AcceptObligationOwnershipAccounts {
277    pub pending_owner: Pubkey,
278    pub obligation: Pubkey,
279}
280
281pub fn accept_obligation_ownership(accounts: AcceptObligationOwnershipAccounts) -> Instruction {
282    Instruction {
283        program_id: KLEND_PROGRAM_ID,
284        accounts: vec![
285            signer(accounts.pending_owner),
286            writable(accounts.obligation),
287            readonly(SYSVAR_INSTRUCTIONS_ID),
288        ],
289        data: discriminators::ACCEPT_OBLIGATION_OWNERSHIP.to_vec(),
290    }
291}
292
293// ---------------------------------------------------------------------------
294// abort_obligation_ownership_transfer
295// ---------------------------------------------------------------------------
296
297pub struct AbortObligationOwnershipTransferAccounts {
298    pub owner: Pubkey,
299    pub obligation: Pubkey,
300}
301
302pub fn abort_obligation_ownership_transfer(
303    accounts: AbortObligationOwnershipTransferAccounts,
304) -> Instruction {
305    Instruction {
306        program_id: KLEND_PROGRAM_ID,
307        accounts: vec![
308            signer(accounts.owner),
309            writable(accounts.obligation),
310            readonly(SYSVAR_INSTRUCTIONS_ID),
311        ],
312        data: discriminators::ABORT_OBLIGATION_OWNERSHIP_TRANSFER.to_vec(),
313    }
314}