Skip to content

Commit

Permalink
writing compute unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
0xripleys committed Nov 17, 2023
1 parent 92a9438 commit 9c9cbd3
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
81 changes: 81 additions & 0 deletions token-lending/program/tests/attributed_borrows.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}
18 changes: 14 additions & 4 deletions token-lending/program/tests/helpers/solend_program_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ impl User {
}
}

#[derive(Debug, Clone)]
pub struct PriceArgs {
pub price: i64,
pub conf: u64,
Expand Down Expand Up @@ -997,7 +998,7 @@ impl Info<LendingMarket> {
.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,
Expand Down Expand Up @@ -1665,6 +1666,7 @@ pub async fn scenario_1(
)
}

#[derive(Debug, Clone)]
pub struct ReserveArgs {
pub mint: Pubkey,
pub config: ReserveConfig,
Expand All @@ -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::<Vec<_>>();

let lending_market_owner =
Expand Down

0 comments on commit 9c9cbd3

Please sign in to comment.