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

relax staleness requirements when withdrawing with no borrows #195

Open
wants to merge 1 commit into
base: upcoming
Choose a base branch
from
Open
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
12 changes: 9 additions & 3 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ fn process_redeem_reserve_collateral(
let clock = &Clock::get()?;
let token_program_id = next_account_info(account_info_iter)?;

_refresh_reserve_interest(program_id, reserve_info, clock)?;
Copy link
Author

Choose a reason for hiding this comment

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

_redeem_reserve_collateral(
program_id,
collateral_amount,
Expand Down Expand Up @@ -1467,6 +1468,8 @@ fn _withdraw_obligation_collateral<'a>(
}

let withdraw_reserve = Box::new(Reserve::unpack(&withdraw_reserve_info.data.borrow())?);
let mut obligation = Obligation::unpack(&obligation_info.data.borrow())?;

if withdraw_reserve_info.owner != program_id {
msg!("Withdraw reserve provided is not owned by the lending program");
return Err(LendingError::InvalidAccountOwner.into());
Expand All @@ -1483,12 +1486,11 @@ fn _withdraw_obligation_collateral<'a>(
msg!("Withdraw reserve collateral supply cannot be used as the destination collateral provided");
return Err(LendingError::InvalidAccountInput.into());
}
if withdraw_reserve.last_update.is_stale(clock.slot)? {
if withdraw_reserve.last_update.is_stale(clock.slot)? && !obligation.borrows.is_empty() {
msg!("Withdraw reserve is stale and must be refreshed in the current slot");
return Err(LendingError::ReserveStale.into());
}

let mut obligation = Obligation::unpack(&obligation_info.data.borrow())?;
if obligation_info.owner != program_id {
msg!("Obligation provided is not owned by the lending program");
return Err(LendingError::InvalidAccountOwner.into());
Expand All @@ -1505,7 +1507,7 @@ fn _withdraw_obligation_collateral<'a>(
msg!("Obligation owner provided must be a signer");
return Err(LendingError::InvalidSigner.into());
}
if obligation.last_update.is_stale(clock.slot)? {
if obligation.last_update.is_stale(clock.slot)? && !obligation.borrows.is_empty() {
msg!("Obligation is stale and must be refreshed in the current slot");
return Err(LendingError::ObligationStale.into());
}
Expand Down Expand Up @@ -2348,6 +2350,10 @@ fn process_withdraw_obligation_collateral_and_redeem_reserve_liquidity(
&accounts[12..],
)?;

// Needed in the case where the obligation has no borrows => user doesn't refresh anything
// if the obligation has borrows, then withdraw_obligation_collateral ensures that the
// obligation (and as a result, the reserves) were refreshed
_refresh_reserve_interest(program_id, reserve_info, clock)?;
_redeem_reserve_collateral(
program_id,
liquidity_amount,
Expand Down
14 changes: 8 additions & 6 deletions token-lending/program/tests/helpers/solend_program_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ impl Info<LendingMarket> {
collateral_amount: u64,
) -> Result<(), BanksClientError> {
let instructions = [
ComputeBudgetInstruction::set_compute_unit_limit(58_000),
ComputeBudgetInstruction::set_compute_unit_limit(60_000),
refresh_reserve(
solend_program::id(),
reserve.pubkey,
Expand Down Expand Up @@ -1345,14 +1345,16 @@ impl Info<LendingMarket> {
) -> Result<(), BanksClientError> {
let obligation = test.load_account::<Obligation>(obligation.pubkey).await;

let refresh_ixs = self
.build_refresh_instructions(test, &obligation, None)
.await;
test.process_transaction(&refresh_ixs, None).await.unwrap();
if !obligation.account.borrows.is_empty() {
let refresh_ixs = self
.build_refresh_instructions(test, &obligation, None)
.await;
test.process_transaction(&refresh_ixs, None).await.unwrap();
}

test.process_transaction(
&[
ComputeBudgetInstruction::set_compute_unit_limit(110_000),
ComputeBudgetInstruction::set_compute_unit_limit(120_000),
withdraw_obligation_collateral_and_redeem_reserve_collateral(
solend_program::id(),
collateral_amount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,39 @@ async fn test_withdraw_max_rate_limiter() {

assert_eq!(balance_changes, expected_balance_changes);
}

#[tokio::test]
async fn test_withdraw_no_borrows() {
let (mut test, lending_market, reserves, obligations, users, _) = custom_scenario(
&[ReserveArgs {
mint: usdc_mint::id(),
config: test_reserve_config(),
liquidity_amount: 100_000 * FRACTIONAL_TO_USDC,
price: PriceArgs {
price: 10,
conf: 0,
expo: -1,
ema_price: 10,
ema_conf: 1,
},
}],
&[ObligationArgs {
deposits: vec![(usdc_mint::id(), 100_000 * FRACTIONAL_TO_USDC)],
borrows: vec![],
}],
)
.await;

test.advance_clock_by_slots(1).await;

lending_market
.withdraw_obligation_collateral_and_redeem_reserve_collateral(
&mut test,
&reserves[0],
&obligations[0],
&users[0],
100_000 * FRACTIONAL_TO_USDC,
)
.await
.unwrap();
}
Loading