Skip to main content

klend_interface/helpers/
lifecycle.rs

1use solana_instruction::Instruction;
2use solana_pubkey::Pubkey;
3
4use crate::{
5    instructions::{
6        obligation::InitObligationAccounts,
7        referrer::{init_user_metadata, InitUserMetadataAccounts},
8    },
9    pda, types, KLEND_PROGRAM_ID,
10};
11
12/// Build instructions to initialize user metadata.
13///
14/// Prerequisite for creating obligations.
15///
16/// Returns: `[init_user_metadata]`
17pub fn init_user(
18    owner: Pubkey,
19    fee_payer: Pubkey,
20    user_lookup_table: Pubkey,
21    referrer_user_metadata: Option<Pubkey>,
22) -> Vec<Instruction> {
23    let (user_metadata_pda, _) = pda::user_metadata(&KLEND_PROGRAM_ID, &owner);
24
25    vec![init_user_metadata(
26        InitUserMetadataAccounts {
27            owner,
28            fee_payer,
29            user_metadata: user_metadata_pda,
30            referrer_user_metadata,
31        },
32        user_lookup_table,
33    )]
34}
35
36/// Build instructions to initialize an obligation.
37///
38/// The obligation PDA and user-metadata PDA are derived automatically.
39/// For a vanilla obligation (tag=0) use `Pubkey::default()` for seed1/seed2.
40///
41/// Returns: `[init_obligation]`
42pub fn init_obligation(
43    owner: Pubkey,
44    fee_payer: Pubkey,
45    lending_market: Pubkey,
46    tag: u8,
47    id: u8,
48    seed1: Pubkey,
49    seed2: Pubkey,
50) -> Vec<Instruction> {
51    let (obligation_pda, _) = pda::obligation(
52        &KLEND_PROGRAM_ID,
53        tag,
54        id,
55        &owner,
56        &lending_market,
57        &seed1,
58        &seed2,
59    );
60    let (user_metadata_pda, _) = pda::user_metadata(&KLEND_PROGRAM_ID, &owner);
61
62    vec![crate::instructions::obligation::init_obligation(
63        InitObligationAccounts {
64            obligation_owner: owner,
65            fee_payer,
66            obligation: obligation_pda,
67            lending_market,
68            seed1_account: seed1,
69            seed2_account: seed2,
70            owner_user_metadata: user_metadata_pda,
71        },
72        types::InitObligationArgs { tag, id },
73    )]
74}