Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add testing and instruction utils for combined deposit function #12

Merged
merged 1 commit into from
Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions token-lending/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,54 @@ pub fn deposit_obligation_collateral(
}
}

/// Creates a 'DepositReserveLiquidityAndObligationCollateral' instruction.
#[allow(clippy::too_many_arguments)]
pub fn deposit_reserve_liquidity_and_obligation_collateral(
program_id: Pubkey,
liquidity_amount: u64,
source_liquidity_pubkey: Pubkey,
user_collateral_pubkey: Pubkey,
reserve_pubkey: Pubkey,
reserve_liquidity_supply_pubkey: Pubkey,
reserve_collateral_mint_pubkey: Pubkey,
lending_market_pubkey: Pubkey,
destination_deposit_collateral_pubkey: Pubkey,
obligation_pubkey: Pubkey,
obligation_owner_pubkey: Pubkey,
reserve_liquidity_pyth_oracle_pubkey: Pubkey,
reserve_liquidity_switchboard_oracle_pubkey: Pubkey,
user_transfer_authority_pubkey: Pubkey,
) -> Instruction {
let (lending_market_authority_pubkey, _bump_seed) = Pubkey::find_program_address(
&[&lending_market_pubkey.to_bytes()[..PUBKEY_BYTES]],
&program_id,
);
Instruction {
program_id,
accounts: vec![
AccountMeta::new(source_liquidity_pubkey, false),
AccountMeta::new(user_collateral_pubkey, false),
AccountMeta::new(reserve_pubkey, false),
AccountMeta::new(reserve_liquidity_supply_pubkey, false),
AccountMeta::new(reserve_collateral_mint_pubkey, false),
AccountMeta::new_readonly(lending_market_pubkey, false),
AccountMeta::new_readonly(lending_market_authority_pubkey, false),
AccountMeta::new(destination_deposit_collateral_pubkey, false),
AccountMeta::new(obligation_pubkey, false),
AccountMeta::new(obligation_owner_pubkey, true),
AccountMeta::new_readonly(reserve_liquidity_pyth_oracle_pubkey, false),
AccountMeta::new_readonly(reserve_liquidity_switchboard_oracle_pubkey, false),
AccountMeta::new_readonly(user_transfer_authority_pubkey, true),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(spl_token::id(), false),
],
data: LendingInstruction::DepositReserveLiquidityAndObligationCollateral {
liquidity_amount,
}
.pack(),
}
}

/// Creates a 'WithdrawObligationCollateral' instruction.
#[allow(clippy::too_many_arguments)]
pub fn withdraw_obligation_collateral(
Expand Down
68 changes: 64 additions & 4 deletions token-lending/program/tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ use spl_token::{
};
use spl_token_lending::{
instruction::{
borrow_obligation_liquidity, deposit_reserve_liquidity, init_lending_market,
init_obligation, init_reserve, liquidate_obligation, refresh_reserve,
borrow_obligation_liquidity, deposit_reserve_liquidity,
deposit_reserve_liquidity_and_obligation_collateral, init_lending_market, init_obligation,
init_reserve, liquidate_obligation, refresh_reserve,
},
math::{Decimal, Rate, TryAdd, TryMul},
pyth,
Expand Down Expand Up @@ -206,6 +207,7 @@ pub fn add_obligation(

TestObligation {
pubkey: obligation_pubkey,
keypair: obligation_keypair,
lending_market: lending_market.pubkey,
owner: user_accounts_owner.pubkey(),
deposits: test_deposits,
Expand Down Expand Up @@ -588,6 +590,62 @@ impl TestLendingMarket {
assert_matches!(banks_client.process_transaction(transaction).await, Ok(()));
}

pub async fn deposit_obligation_and_collateral(
&self,
banks_client: &mut BanksClient,
user_accounts_owner: &Keypair,
payer: &Keypair,
reserve: &TestReserve,
obligation: &TestObligation,
obligation_keypair: &Keypair,
liquidity_amount: u64,
) {
let user_transfer_authority = Keypair::new();
let mut transaction = Transaction::new_with_payer(
&[
approve(
&spl_token::id(),
&reserve.user_liquidity_pubkey,
&user_transfer_authority.pubkey(),
&user_accounts_owner.pubkey(),
&[],
liquidity_amount,
)
.unwrap(),
deposit_reserve_liquidity_and_obligation_collateral(
spl_token_lending::id(),
liquidity_amount,
reserve.user_liquidity_pubkey,
reserve.user_collateral_pubkey,
reserve.pubkey,
reserve.liquidity_supply_pubkey,
reserve.collateral_mint_pubkey,
reserve.pubkey,
reserve.collateral_supply_pubkey,
obligation.pubkey,
obligation.owner,
reserve.liquidity_pyth_oracle_pubkey,
reserve.liquidity_switchboard_oracle_pubkey,
user_transfer_authority.pubkey(),
),
],
Some(&payer.pubkey()),
);

let recent_blockhash = banks_client.get_recent_blockhash().await.unwrap();
transaction.sign(
&[
payer,
user_accounts_owner,
&user_transfer_authority,
&obligation_keypair,
],
recent_blockhash,
);

assert_matches!(banks_client.process_transaction(transaction).await, Ok(()));
}

pub async fn liquidate(
&self,
banks_client: &mut BanksClient,
Expand Down Expand Up @@ -915,6 +973,7 @@ impl TestReserve {
#[derive(Debug)]
pub struct TestObligation {
pub pubkey: Pubkey,
pub keypair: Keypair,
pub lending_market: Pubkey,
pub owner: Pubkey,
pub deposits: Vec<TestObligationCollateral>,
Expand All @@ -932,6 +991,7 @@ impl TestObligation {
let obligation_keypair = Keypair::new();
let obligation = TestObligation {
pubkey: obligation_keypair.pubkey(),
keypair: obligation_keypair,
lending_market: lending_market.pubkey,
owner: user_accounts_owner.pubkey(),
deposits: vec![],
Expand All @@ -943,7 +1003,7 @@ impl TestObligation {
&[
create_account(
&payer.pubkey(),
&obligation_keypair.pubkey(),
&obligation.keypair.pubkey(),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to change this because of lifetime stuff.
obligation_keypair is dead, but obligation.keypair is still alive, so we loan it to the create_account function.

Or something like that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this like you are reusing stuff between tests rather than having it sandboxed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, no I don't think this will cause it to be reused between tests. What do you mean?

rent.minimum_balance(Obligation::LEN),
Obligation::LEN as u64,
&spl_token_lending::id(),
Expand All @@ -960,7 +1020,7 @@ impl TestObligation {

let recent_blockhash = banks_client.get_recent_blockhash().await.unwrap();
transaction.sign(
&vec![payer, &obligation_keypair, user_accounts_owner],
&vec![payer, &obligation.keypair, user_accounts_owner],
recent_blockhash,
);

Expand Down