Skip to main content

klend_interface/instructions/
repay.rs

1use borsh::BorshSerialize;
2use solana_instruction::{AccountMeta, Instruction};
3use solana_pubkey::Pubkey;
4
5use crate::{
6    discriminators, util::*, FARMS_PROGRAM_ID, KLEND_PROGRAM_ID, SYSVAR_INSTRUCTIONS_ID,
7    TOKEN_PROGRAM_ID,
8};
9
10// ---------------------------------------------------------------------------
11// repay_obligation_liquidity_v2
12// ---------------------------------------------------------------------------
13
14pub struct RepayObligationLiquidityV2Accounts {
15    pub owner: Pubkey,
16    pub obligation: Pubkey,
17    pub lending_market: Pubkey,
18    pub repay_reserve: Pubkey,
19    pub reserve_liquidity_mint: Pubkey,
20    pub reserve_destination_liquidity: Pubkey,
21    pub user_source_liquidity: Pubkey,
22    pub token_program: Pubkey,
23    // V2 farms
24    pub obligation_farm_user_state: Option<Pubkey>,
25    pub reserve_farm_state: Option<Pubkey>,
26    pub lending_market_authority: Pubkey,
27}
28
29pub fn repay_obligation_liquidity_v2(
30    accounts: RepayObligationLiquidityV2Accounts,
31    liquidity_amount: u64,
32    remaining_accounts: Vec<AccountMeta>,
33) -> Instruction {
34    #[derive(BorshSerialize)]
35    struct Args {
36        liquidity_amount: u64,
37    }
38
39    let mut data = discriminators::REPAY_OBLIGATION_LIQUIDITY_V2.to_vec();
40    Args { liquidity_amount }.serialize(&mut data).unwrap();
41
42    // V1 accounts
43    let mut account_metas = vec![
44        signer(accounts.owner),
45        writable(accounts.obligation),
46        readonly(accounts.lending_market),
47        writable(accounts.repay_reserve),
48        readonly(accounts.reserve_liquidity_mint),
49        writable(accounts.reserve_destination_liquidity),
50        writable(accounts.user_source_liquidity),
51        readonly(accounts.token_program),
52        readonly(SYSVAR_INSTRUCTIONS_ID),
53    ];
54
55    // V2: farms accounts, then lending_market_authority, then farms_program
56    account_metas.push(optional_account(
57        &KLEND_PROGRAM_ID,
58        accounts.obligation_farm_user_state,
59        true,
60    ));
61    account_metas.push(optional_account(
62        &KLEND_PROGRAM_ID,
63        accounts.reserve_farm_state,
64        true,
65    ));
66    account_metas.push(readonly(accounts.lending_market_authority));
67    account_metas.push(readonly(FARMS_PROGRAM_ID));
68
69    account_metas.extend(remaining_accounts);
70
71    Instruction {
72        program_id: KLEND_PROGRAM_ID,
73        accounts: account_metas,
74        data,
75    }
76}
77
78// ---------------------------------------------------------------------------
79// repay_and_withdraw_and_redeem
80// ---------------------------------------------------------------------------
81
82pub struct RepayAndWithdrawAndRedeemAccounts {
83    // RepayObligationLiquidity
84    pub owner: Pubkey,
85    pub obligation: Pubkey,
86    pub lending_market: Pubkey,
87    pub repay_reserve: Pubkey,
88    pub reserve_liquidity_mint: Pubkey,
89    pub reserve_destination_liquidity: Pubkey,
90    pub user_source_liquidity: Pubkey,
91    pub token_program: Pubkey,
92
93    // WithdrawObligationCollateralAndRedeemReserveCollateral
94    pub lending_market_authority: Pubkey,
95    pub withdraw_reserve: Pubkey,
96    pub withdraw_reserve_liquidity_mint: Pubkey,
97    pub withdraw_reserve_source_collateral: Pubkey,
98    pub withdraw_reserve_collateral_mint: Pubkey,
99    pub withdraw_reserve_liquidity_supply: Pubkey,
100    pub user_destination_liquidity: Pubkey,
101    pub placeholder_user_destination_collateral: Option<Pubkey>,
102    pub withdraw_liquidity_token_program: Pubkey,
103
104    // Farms
105    pub collateral_obligation_farm_user_state: Option<Pubkey>,
106    pub collateral_reserve_farm_state: Option<Pubkey>,
107    pub debt_obligation_farm_user_state: Option<Pubkey>,
108    pub debt_reserve_farm_state: Option<Pubkey>,
109}
110
111pub fn repay_and_withdraw_and_redeem(
112    accounts: RepayAndWithdrawAndRedeemAccounts,
113    repay_amount: u64,
114    withdraw_collateral_amount: u64,
115    remaining_accounts: Vec<AccountMeta>,
116) -> Instruction {
117    #[derive(BorshSerialize)]
118    struct Args {
119        repay_amount: u64,
120        withdraw_collateral_amount: u64,
121    }
122
123    let mut data = discriminators::REPAY_AND_WITHDRAW_AND_REDEEM.to_vec();
124    Args {
125        repay_amount,
126        withdraw_collateral_amount,
127    }
128    .serialize(&mut data)
129    .unwrap();
130
131    // RepayObligationLiquidity accounts
132    let mut account_metas = vec![
133        signer(accounts.owner),
134        writable(accounts.obligation),
135        readonly(accounts.lending_market),
136        writable(accounts.repay_reserve),
137        readonly(accounts.reserve_liquidity_mint),
138        writable(accounts.reserve_destination_liquidity),
139        writable(accounts.user_source_liquidity),
140        readonly(accounts.token_program),
141        readonly(SYSVAR_INSTRUCTIONS_ID),
142    ];
143
144    // WithdrawObligationCollateralAndRedeemReserveCollateral accounts
145    // (owner, obligation, lending_market are duplicated in the wire format)
146    account_metas.push(signer_writable(accounts.owner));
147    account_metas.push(writable(accounts.obligation));
148    account_metas.push(readonly(accounts.lending_market));
149    account_metas.push(readonly(accounts.lending_market_authority));
150    account_metas.push(writable(accounts.withdraw_reserve));
151    account_metas.push(readonly(accounts.withdraw_reserve_liquidity_mint));
152    account_metas.push(writable(accounts.withdraw_reserve_source_collateral));
153    account_metas.push(writable(accounts.withdraw_reserve_collateral_mint));
154    account_metas.push(writable(accounts.withdraw_reserve_liquidity_supply));
155    account_metas.push(writable(accounts.user_destination_liquidity));
156    account_metas.push(optional_account(
157        &KLEND_PROGRAM_ID,
158        accounts.placeholder_user_destination_collateral,
159        false,
160    ));
161    account_metas.push(readonly(TOKEN_PROGRAM_ID));
162    account_metas.push(readonly(accounts.withdraw_liquidity_token_program));
163    account_metas.push(readonly(SYSVAR_INSTRUCTIONS_ID));
164
165    // Farms accounts
166    account_metas.push(optional_account(
167        &KLEND_PROGRAM_ID,
168        accounts.collateral_obligation_farm_user_state,
169        true,
170    ));
171    account_metas.push(optional_account(
172        &KLEND_PROGRAM_ID,
173        accounts.collateral_reserve_farm_state,
174        true,
175    ));
176    account_metas.push(optional_account(
177        &KLEND_PROGRAM_ID,
178        accounts.debt_obligation_farm_user_state,
179        true,
180    ));
181    account_metas.push(optional_account(
182        &KLEND_PROGRAM_ID,
183        accounts.debt_reserve_farm_state,
184        true,
185    ));
186    account_metas.push(readonly(FARMS_PROGRAM_ID));
187
188    account_metas.extend(remaining_accounts);
189
190    Instruction {
191        program_id: KLEND_PROGRAM_ID,
192        accounts: account_metas,
193        data,
194    }
195}