From 8328ff2c86c00d097cdd1b2a128eee9b5b93fd9e Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Sat, 8 Jun 2024 09:48:04 -0700 Subject: [PATCH] sys-lend: correctly calculate max reserve outflow --- src/bin/sys-lend.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/bin/sys-lend.rs b/src/bin/sys-lend.rs index 0b6130d..47d280c 100644 --- a/src/bin/sys-lend.rs +++ b/src/bin/sys-lend.rs @@ -1494,6 +1494,7 @@ fn mfi_deposit_or_withdraw( }) } +#[derive(Debug)] struct DepositOrWithdrawResult { instructions: Vec, required_compute_units: u32, @@ -2112,9 +2113,10 @@ fn solend_remaining_outflow_for_reserve( if reserve.rate_limiter.config.window_duration == 0 { Ok(u64::MAX) } else { - let remaining_outflow = reserve.rate_limiter.remaining_outflow(context_slot)?; - let collateral_exchange_rate = reserve.collateral_exchange_rate()?; - Ok(collateral_exchange_rate.collateral_to_liquidity(remaining_outflow.try_round_u64()?)?) + Ok(reserve + .rate_limiter + .remaining_outflow(context_slot)? + .try_floor_u64()?) } } @@ -2214,7 +2216,7 @@ fn solend_deposit_or_withdraw( amount: u64, account_data_cache: &mut AccountDataCache, ) -> Result> { - let (market_reserve_address, reserve, _context_slot) = + let (market_reserve_address, reserve, context_slot) = solend_load_reserve_for_pool(rpc_client, pool, token, account_data_cache)?; let obligation_address = solend_find_obligation_address(wallet_address, reserve.lending_market); @@ -2313,8 +2315,13 @@ fn solend_deposit_or_withdraw( .map(|collateral| collateral.deposited_amount) .unwrap_or_default(); + let remaining_outflow = + solend_remaining_outflow_for_reserve(reserve.clone(), context_slot)?; + let collateral_exchange_rate = reserve.collateral_exchange_rate()?; - collateral_exchange_rate.collateral_to_liquidity(collateral_deposited_amount)? + collateral_exchange_rate + .collateral_to_liquidity(collateral_deposited_amount)? + .min(remaining_outflow) } else { amount };