Skip to main content

klend_interface/instructions/
orders.rs

1use borsh::BorshSerialize;
2use solana_instruction::{AccountMeta, Instruction};
3use solana_pubkey::Pubkey;
4
5use crate::{discriminators, util::*, FARMS_PROGRAM_ID, KLEND_PROGRAM_ID, SYSVAR_INSTRUCTIONS_ID};
6
7// ---------------------------------------------------------------------------
8// set_obligation_order
9// ---------------------------------------------------------------------------
10
11pub struct SetObligationOrderAccounts {
12    pub owner: Pubkey,
13    pub obligation: Pubkey,
14    pub lending_market: Pubkey,
15}
16
17pub fn set_obligation_order(
18    accounts: SetObligationOrderAccounts,
19    index: u8,
20    order: crate::types::ObligationOrder,
21) -> Instruction {
22    #[derive(BorshSerialize)]
23    struct Args {
24        index: u8,
25        order: crate::types::ObligationOrder,
26    }
27
28    let mut data = discriminators::SET_OBLIGATION_ORDER.to_vec();
29    Args { index, order }.serialize(&mut data).unwrap();
30
31    Instruction {
32        program_id: KLEND_PROGRAM_ID,
33        accounts: vec![
34            signer(accounts.owner),
35            writable(accounts.obligation),
36            readonly(accounts.lending_market),
37        ],
38        data,
39    }
40}
41
42// ---------------------------------------------------------------------------
43// set_borrow_order
44// ---------------------------------------------------------------------------
45
46pub struct SetBorrowOrderAccounts {
47    pub owner: Pubkey,
48    pub obligation: Pubkey,
49    pub lending_market: Pubkey,
50    pub reserve: Pubkey,
51    pub filled_debt_destination: Pubkey,
52    pub debt_liquidity_mint: Pubkey,
53}
54
55pub fn set_borrow_order(
56    accounts: SetBorrowOrderAccounts,
57    order_config: crate::types::BorrowOrderConfigArgs,
58    min_expected_current_remaining_debt_amount: u64,
59) -> Instruction {
60    #[derive(BorshSerialize)]
61    struct Args {
62        order_config: crate::types::BorrowOrderConfigArgs,
63        min_expected_current_remaining_debt_amount: u64,
64    }
65
66    let mut data = discriminators::SET_BORROW_ORDER.to_vec();
67    Args {
68        order_config,
69        min_expected_current_remaining_debt_amount,
70    }
71    .serialize(&mut data)
72    .unwrap();
73
74    Instruction {
75        program_id: KLEND_PROGRAM_ID,
76        accounts: vec![
77            signer(accounts.owner),
78            writable(accounts.obligation),
79            readonly(accounts.lending_market),
80            readonly(accounts.reserve),
81            readonly(accounts.filled_debt_destination),
82            readonly(accounts.debt_liquidity_mint),
83            readonly(SYSVAR_INSTRUCTIONS_ID),
84            readonly(crate::pda::event_authority(&KLEND_PROGRAM_ID).0),
85            readonly(KLEND_PROGRAM_ID),
86        ],
87        data,
88    }
89}
90
91// ---------------------------------------------------------------------------
92// fill_borrow_order
93// ---------------------------------------------------------------------------
94
95pub struct FillBorrowOrderAccounts {
96    pub payer: Pubkey,
97    pub obligation: Pubkey,
98    pub lending_market: Pubkey,
99    pub lending_market_authority: Pubkey,
100    pub borrow_reserve: Pubkey,
101    pub borrow_reserve_liquidity_mint: Pubkey,
102    pub reserve_source_liquidity: Pubkey,
103    pub borrow_reserve_liquidity_fee_receiver: Pubkey,
104    pub user_destination_liquidity: Pubkey,
105    pub referrer_token_state: Option<Pubkey>,
106    pub token_program: Pubkey,
107    // Optional farms accounts
108    pub obligation_farm_user_state: Option<Pubkey>,
109    pub reserve_farm_state: Option<Pubkey>,
110}
111
112pub fn fill_borrow_order(
113    accounts: FillBorrowOrderAccounts,
114    remaining_accounts: Vec<AccountMeta>,
115) -> Instruction {
116    let data = discriminators::FILL_BORROW_ORDER.to_vec();
117
118    let mut account_metas = vec![
119        signer(accounts.payer),
120        writable(accounts.obligation),
121        readonly(accounts.lending_market),
122        readonly(accounts.lending_market_authority),
123        writable(accounts.borrow_reserve),
124        readonly(accounts.borrow_reserve_liquidity_mint),
125        writable(accounts.reserve_source_liquidity),
126        writable(accounts.borrow_reserve_liquidity_fee_receiver),
127        writable(accounts.user_destination_liquidity),
128        optional_account(&KLEND_PROGRAM_ID, accounts.referrer_token_state, true),
129        readonly(accounts.token_program),
130        readonly(SYSVAR_INSTRUCTIONS_ID),
131        // Optional farms accounts
132        optional_account(&KLEND_PROGRAM_ID, accounts.obligation_farm_user_state, true),
133        optional_account(&KLEND_PROGRAM_ID, accounts.reserve_farm_state, true),
134        readonly(FARMS_PROGRAM_ID),
135        // event_cpi accounts
136        readonly(crate::pda::event_authority(&KLEND_PROGRAM_ID).0),
137        readonly(KLEND_PROGRAM_ID),
138    ];
139
140    account_metas.extend(remaining_accounts);
141
142    Instruction {
143        program_id: KLEND_PROGRAM_ID,
144        accounts: account_metas,
145        data,
146    }
147}