From 9c9cbd31e9604c95122d95e42448fb9e125bc0a5 Mon Sep 17 00:00:00 2001 From: 0xripleys <0xripleys@solend.fi> Date: Fri, 17 Nov 2023 10:20:43 -0500 Subject: [PATCH] writing compute unit test --- .../program/tests/attributed_borrows.rs | 81 +++++++++++++++++++ .../tests/helpers/solend_program_test.rs | 18 ++++- 2 files changed, 95 insertions(+), 4 deletions(-) diff --git a/token-lending/program/tests/attributed_borrows.rs b/token-lending/program/tests/attributed_borrows.rs index ab7cc531059..adf87770fa4 100644 --- a/token-lending/program/tests/attributed_borrows.rs +++ b/token-lending/program/tests/attributed_borrows.rs @@ -1,6 +1,7 @@ #![cfg(feature = "test-bpf")] use crate::solend_program_test::custom_scenario; +use crate::solend_program_test::User; use solana_sdk::instruction::InstructionError; use solana_sdk::transaction::TransactionError; use solend_program::math::TryAdd; @@ -282,3 +283,83 @@ async fn test_calculations() { Decimal::from(30u64) ); } + +#[tokio::test] +async fn benchmark() { + // setup + let reserve_arg = ReserveArgs { + mint: usdc_mint::id(), + config: ReserveConfig { + loan_to_value_ratio: 80, + liquidation_threshold: 81, + max_liquidation_threshold: 82, + fees: ReserveFees { + host_fee_percentage: 0, + ..ReserveFees::default() + }, + optimal_borrow_rate: 0, + max_borrow_rate: 0, + ..test_reserve_config() + }, + liquidity_amount: 100 * FRACTIONAL_TO_USDC, + price: PriceArgs { + price: 10, + conf: 0, + expo: -1, + ema_price: 10, + ema_conf: 1, + }, + }; + + let reserve_args = vec![reserve_arg; 9]; + + let obligation_args = ObligationArgs { + deposits: vec![], + borrows: vec![], + }; + + let (mut test, lending_market, reserves, obligations, mut users, lending_market_owner) = + custom_scenario(&reserve_args, &[obligation_args]).await; + + let user = User::new_with_balances( + &mut test, + &[(&usdc_mint::id(), 100_000 * FRACTIONAL_TO_USDC)], + ) + .await; + + user.transfer( + &usdc_mint::id(), + users[0].get_account(&usdc_mint::id()).unwrap(), + 100_000 * FRACTIONAL_TO_USDC, + &mut test, + ) + .await; + + for reserve in &reserves { + users[0].create_token_account(&reserve.account.collateral.mint_pubkey, &mut test) + .await; + + lending_market + .deposit_reserve_liquidity_and_obligation_collateral( + &mut test, + reserve, + &obligations[0], + &users[0], + 10 * FRACTIONAL_TO_USDC, + ) + .await + .unwrap(); + } + + lending_market + .borrow_obligation_liquidity( + &mut test, + &reserves[0], + &obligations[0], + &users[0], + None, + FRACTIONAL_TO_USDC, + ) + .await + .unwrap(); +} diff --git a/token-lending/program/tests/helpers/solend_program_test.rs b/token-lending/program/tests/helpers/solend_program_test.rs index 27a00523380..754528a6589 100644 --- a/token-lending/program/tests/helpers/solend_program_test.rs +++ b/token-lending/program/tests/helpers/solend_program_test.rs @@ -664,6 +664,7 @@ impl User { } } +#[derive(Debug, Clone)] pub struct PriceArgs { pub price: i64, pub conf: u64, @@ -997,7 +998,7 @@ impl Info { .await; test.process_transaction(&refresh_ixs, None).await.unwrap(); - let mut instructions = vec![ComputeBudgetInstruction::set_compute_unit_limit(80_000)]; + let mut instructions = vec![ComputeBudgetInstruction::set_compute_unit_limit(1_000_000)]; instructions.push(borrow_obligation_liquidity( solend_program::id(), liquidity_amount, @@ -1665,6 +1666,7 @@ pub async fn scenario_1( ) } +#[derive(Debug, Clone)] pub struct ReserveArgs { pub mint: Pubkey, pub config: ReserveConfig, @@ -1689,9 +1691,17 @@ pub async fn custom_scenario( User, ) { let mut test = SolendProgramTest::start_new().await; - let mints_and_liquidity_amounts = reserve_args - .iter() - .map(|reserve_arg| (&reserve_arg.mint, reserve_arg.liquidity_amount)) + let mut mints_and_liquidity_amounts = HashMap::new(); + for arg in reserve_args { + mints_and_liquidity_amounts + .entry(&arg.mint) + .and_modify(|e| *e += arg.liquidity_amount) + .or_insert(arg.liquidity_amount); + } + + let mints_and_liquidity_amounts = mints_and_liquidity_amounts + .into_iter() + .map(|(mint, liquidity_amount)| (mint, liquidity_amount)) .collect::>(); let lending_market_owner =