Skip to main content

klend_interface/instructions/
compound.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// deposit_and_withdraw
12// ---------------------------------------------------------------------------
13
14pub struct DepositAndWithdrawAccounts {
15    // DepositReserveLiquidityAndObligationCollateral accounts
16    pub owner: Pubkey,
17    pub obligation: Pubkey,
18    pub lending_market: Pubkey,
19    pub lending_market_authority: Pubkey,
20    pub reserve: Pubkey,
21    pub reserve_liquidity_mint: Pubkey,
22    pub reserve_liquidity_supply: Pubkey,
23    pub reserve_collateral_mint: Pubkey,
24    pub reserve_destination_deposit_collateral: Pubkey,
25    pub user_source_liquidity: Pubkey,
26    pub placeholder_user_destination_collateral: Option<Pubkey>,
27    pub liquidity_token_program: Pubkey,
28
29    // WithdrawObligationCollateralAndRedeemReserveCollateral accounts
30    pub withdraw_owner: Pubkey,
31    pub withdraw_obligation: Pubkey,
32    pub withdraw_lending_market: Pubkey,
33    pub withdraw_lending_market_authority: Pubkey,
34    pub withdraw_reserve: Pubkey,
35    pub withdraw_reserve_liquidity_mint: Pubkey,
36    pub withdraw_reserve_source_collateral: Pubkey,
37    pub withdraw_reserve_collateral_mint: Pubkey,
38    pub withdraw_reserve_liquidity_supply: Pubkey,
39    pub withdraw_user_destination_liquidity: Pubkey,
40    pub withdraw_placeholder_user_destination_collateral: Option<Pubkey>,
41    pub withdraw_liquidity_token_program: Pubkey,
42
43    // Farms accounts
44    pub deposit_obligation_farm_user_state: Option<Pubkey>,
45    pub deposit_reserve_farm_state: Option<Pubkey>,
46    pub withdraw_obligation_farm_user_state: Option<Pubkey>,
47    pub withdraw_reserve_farm_state: Option<Pubkey>,
48}
49
50pub fn deposit_and_withdraw(
51    accounts: DepositAndWithdrawAccounts,
52    liquidity_amount: u64,
53    withdraw_collateral_amount: u64,
54    remaining_accounts: Vec<AccountMeta>,
55) -> Instruction {
56    #[derive(BorshSerialize)]
57    struct Args {
58        liquidity_amount: u64,
59        withdraw_collateral_amount: u64,
60    }
61
62    let args = Args {
63        liquidity_amount,
64        withdraw_collateral_amount,
65    };
66    let mut data = discriminators::DEPOSIT_AND_WITHDRAW.to_vec();
67    args.serialize(&mut data).unwrap();
68
69    let mut account_metas = vec![
70        // DepositReserveLiquidityAndObligationCollateral
71        signer_writable(accounts.owner),
72        writable(accounts.obligation),
73        readonly(accounts.lending_market),
74        readonly(accounts.lending_market_authority),
75        writable(accounts.reserve),
76        readonly(accounts.reserve_liquidity_mint),
77        writable(accounts.reserve_liquidity_supply),
78        writable(accounts.reserve_collateral_mint),
79        writable(accounts.reserve_destination_deposit_collateral),
80        writable(accounts.user_source_liquidity),
81        optional_account(
82            &KLEND_PROGRAM_ID,
83            accounts.placeholder_user_destination_collateral,
84            false,
85        ),
86        readonly(TOKEN_PROGRAM_ID),
87        readonly(accounts.liquidity_token_program),
88        readonly(SYSVAR_INSTRUCTIONS_ID),
89        // WithdrawObligationCollateralAndRedeemReserveCollateral
90        signer_writable(accounts.withdraw_owner),
91        writable(accounts.withdraw_obligation),
92        readonly(accounts.withdraw_lending_market),
93        readonly(accounts.withdraw_lending_market_authority),
94        writable(accounts.withdraw_reserve),
95        readonly(accounts.withdraw_reserve_liquidity_mint),
96        writable(accounts.withdraw_reserve_source_collateral),
97        writable(accounts.withdraw_reserve_collateral_mint),
98        writable(accounts.withdraw_reserve_liquidity_supply),
99        writable(accounts.withdraw_user_destination_liquidity),
100        optional_account(
101            &KLEND_PROGRAM_ID,
102            accounts.withdraw_placeholder_user_destination_collateral,
103            false,
104        ),
105        readonly(TOKEN_PROGRAM_ID),
106        readonly(accounts.withdraw_liquidity_token_program),
107        readonly(SYSVAR_INSTRUCTIONS_ID),
108        // Farms
109        optional_account(
110            &KLEND_PROGRAM_ID,
111            accounts.deposit_obligation_farm_user_state,
112            true,
113        ),
114        optional_account(&KLEND_PROGRAM_ID, accounts.deposit_reserve_farm_state, true),
115        optional_account(
116            &KLEND_PROGRAM_ID,
117            accounts.withdraw_obligation_farm_user_state,
118            true,
119        ),
120        optional_account(
121            &KLEND_PROGRAM_ID,
122            accounts.withdraw_reserve_farm_state,
123            true,
124        ),
125        readonly(FARMS_PROGRAM_ID),
126    ];
127
128    account_metas.extend(remaining_accounts);
129
130    Instruction {
131        program_id: KLEND_PROGRAM_ID,
132        accounts: account_metas,
133        data,
134    }
135}