Skip to content

Commit 3b70835

Browse files
authored
Merge pull request #12 from solendprotocol/WM_add_a_test
Add testing and instruction utils for combined deposit function
2 parents 53a98a4 + 28d9b77 commit 3b70835

File tree

2 files changed

+112
-4
lines changed

2 files changed

+112
-4
lines changed

token-lending/program/src/instruction.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,54 @@ pub fn deposit_obligation_collateral(
852852
}
853853
}
854854

855+
/// Creates a 'DepositReserveLiquidityAndObligationCollateral' instruction.
856+
#[allow(clippy::too_many_arguments)]
857+
pub fn deposit_reserve_liquidity_and_obligation_collateral(
858+
program_id: Pubkey,
859+
liquidity_amount: u64,
860+
source_liquidity_pubkey: Pubkey,
861+
user_collateral_pubkey: Pubkey,
862+
reserve_pubkey: Pubkey,
863+
reserve_liquidity_supply_pubkey: Pubkey,
864+
reserve_collateral_mint_pubkey: Pubkey,
865+
lending_market_pubkey: Pubkey,
866+
destination_deposit_collateral_pubkey: Pubkey,
867+
obligation_pubkey: Pubkey,
868+
obligation_owner_pubkey: Pubkey,
869+
reserve_liquidity_pyth_oracle_pubkey: Pubkey,
870+
reserve_liquidity_switchboard_oracle_pubkey: Pubkey,
871+
user_transfer_authority_pubkey: Pubkey,
872+
) -> Instruction {
873+
let (lending_market_authority_pubkey, _bump_seed) = Pubkey::find_program_address(
874+
&[&lending_market_pubkey.to_bytes()[..PUBKEY_BYTES]],
875+
&program_id,
876+
);
877+
Instruction {
878+
program_id,
879+
accounts: vec![
880+
AccountMeta::new(source_liquidity_pubkey, false),
881+
AccountMeta::new(user_collateral_pubkey, false),
882+
AccountMeta::new(reserve_pubkey, false),
883+
AccountMeta::new(reserve_liquidity_supply_pubkey, false),
884+
AccountMeta::new(reserve_collateral_mint_pubkey, false),
885+
AccountMeta::new_readonly(lending_market_pubkey, false),
886+
AccountMeta::new_readonly(lending_market_authority_pubkey, false),
887+
AccountMeta::new(destination_deposit_collateral_pubkey, false),
888+
AccountMeta::new(obligation_pubkey, false),
889+
AccountMeta::new(obligation_owner_pubkey, true),
890+
AccountMeta::new_readonly(reserve_liquidity_pyth_oracle_pubkey, false),
891+
AccountMeta::new_readonly(reserve_liquidity_switchboard_oracle_pubkey, false),
892+
AccountMeta::new_readonly(user_transfer_authority_pubkey, true),
893+
AccountMeta::new_readonly(sysvar::clock::id(), false),
894+
AccountMeta::new_readonly(spl_token::id(), false),
895+
],
896+
data: LendingInstruction::DepositReserveLiquidityAndObligationCollateral {
897+
liquidity_amount,
898+
}
899+
.pack(),
900+
}
901+
}
902+
855903
/// Creates a 'WithdrawObligationCollateral' instruction.
856904
#[allow(clippy::too_many_arguments)]
857905
pub fn withdraw_obligation_collateral(

token-lending/program/tests/helpers/mod.rs

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ use spl_token::{
1818
};
1919
use spl_token_lending::{
2020
instruction::{
21-
borrow_obligation_liquidity, deposit_reserve_liquidity, init_lending_market,
22-
init_obligation, init_reserve, liquidate_obligation, refresh_reserve,
21+
borrow_obligation_liquidity, deposit_reserve_liquidity,
22+
deposit_reserve_liquidity_and_obligation_collateral, init_lending_market, init_obligation,
23+
init_reserve, liquidate_obligation, refresh_reserve,
2324
},
2425
math::{Decimal, Rate, TryAdd, TryMul},
2526
pyth,
@@ -206,6 +207,7 @@ pub fn add_obligation(
206207

207208
TestObligation {
208209
pubkey: obligation_pubkey,
210+
keypair: obligation_keypair,
209211
lending_market: lending_market.pubkey,
210212
owner: user_accounts_owner.pubkey(),
211213
deposits: test_deposits,
@@ -588,6 +590,62 @@ impl TestLendingMarket {
588590
assert_matches!(banks_client.process_transaction(transaction).await, Ok(()));
589591
}
590592

593+
pub async fn deposit_obligation_and_collateral(
594+
&self,
595+
banks_client: &mut BanksClient,
596+
user_accounts_owner: &Keypair,
597+
payer: &Keypair,
598+
reserve: &TestReserve,
599+
obligation: &TestObligation,
600+
obligation_keypair: &Keypair,
601+
liquidity_amount: u64,
602+
) {
603+
let user_transfer_authority = Keypair::new();
604+
let mut transaction = Transaction::new_with_payer(
605+
&[
606+
approve(
607+
&spl_token::id(),
608+
&reserve.user_liquidity_pubkey,
609+
&user_transfer_authority.pubkey(),
610+
&user_accounts_owner.pubkey(),
611+
&[],
612+
liquidity_amount,
613+
)
614+
.unwrap(),
615+
deposit_reserve_liquidity_and_obligation_collateral(
616+
spl_token_lending::id(),
617+
liquidity_amount,
618+
reserve.user_liquidity_pubkey,
619+
reserve.user_collateral_pubkey,
620+
reserve.pubkey,
621+
reserve.liquidity_supply_pubkey,
622+
reserve.collateral_mint_pubkey,
623+
reserve.pubkey,
624+
reserve.collateral_supply_pubkey,
625+
obligation.pubkey,
626+
obligation.owner,
627+
reserve.liquidity_pyth_oracle_pubkey,
628+
reserve.liquidity_switchboard_oracle_pubkey,
629+
user_transfer_authority.pubkey(),
630+
),
631+
],
632+
Some(&payer.pubkey()),
633+
);
634+
635+
let recent_blockhash = banks_client.get_recent_blockhash().await.unwrap();
636+
transaction.sign(
637+
&[
638+
payer,
639+
user_accounts_owner,
640+
&user_transfer_authority,
641+
&obligation_keypair,
642+
],
643+
recent_blockhash,
644+
);
645+
646+
assert_matches!(banks_client.process_transaction(transaction).await, Ok(()));
647+
}
648+
591649
pub async fn liquidate(
592650
&self,
593651
banks_client: &mut BanksClient,
@@ -915,6 +973,7 @@ impl TestReserve {
915973
#[derive(Debug)]
916974
pub struct TestObligation {
917975
pub pubkey: Pubkey,
976+
pub keypair: Keypair,
918977
pub lending_market: Pubkey,
919978
pub owner: Pubkey,
920979
pub deposits: Vec<TestObligationCollateral>,
@@ -932,6 +991,7 @@ impl TestObligation {
932991
let obligation_keypair = Keypair::new();
933992
let obligation = TestObligation {
934993
pubkey: obligation_keypair.pubkey(),
994+
keypair: obligation_keypair,
935995
lending_market: lending_market.pubkey,
936996
owner: user_accounts_owner.pubkey(),
937997
deposits: vec![],
@@ -943,7 +1003,7 @@ impl TestObligation {
9431003
&[
9441004
create_account(
9451005
&payer.pubkey(),
946-
&obligation_keypair.pubkey(),
1006+
&obligation.keypair.pubkey(),
9471007
rent.minimum_balance(Obligation::LEN),
9481008
Obligation::LEN as u64,
9491009
&spl_token_lending::id(),
@@ -960,7 +1020,7 @@ impl TestObligation {
9601020

9611021
let recent_blockhash = banks_client.get_recent_blockhash().await.unwrap();
9621022
transaction.sign(
963-
&vec![payer, &obligation_keypair, user_accounts_owner],
1023+
&vec![payer, &obligation.keypair, user_accounts_owner],
9641024
recent_blockhash,
9651025
);
9661026

0 commit comments

Comments
 (0)