Skip to content

Commit

Permalink
bigz/improve-withdraw-borrow-guard (#354)
Browse files Browse the repository at this point in the history
* bigz/improve-withdraw-borrow-guard

* wip: potential better borrow thres (todo: sdk to match)

* align sdk borrow threshold

* spot_withdraw.rs: check both deposit/borrow constraints for resulting borrow

* update changelog.md
  • Loading branch information
0xbigz committed Feb 20, 2023
1 parent 20c0b31 commit 064dc1a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Features
- program: improve conditions for withdraw/borrow guard ([#354](https://github.com/drift-labs/protocol-v2/pull/354))

### Fixes

Expand Down
6 changes: 5 additions & 1 deletion programs/drift/src/controller/spot_balance/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ fn test_check_withdraw_limits() {
deposit_balance: 2 * SPOT_BALANCE_PRECISION,
borrow_balance: SPOT_BALANCE_PRECISION,
liquidator_fee: LIQUIDATION_FEE_PRECISION / 1000,
deposit_token_twap: 26_000_000_000_u64,
status: MarketStatus::Active,

..SpotMarket::default()
Expand Down Expand Up @@ -458,7 +459,10 @@ fn test_check_withdraw_limits() {
assert!(valid_withdraw);

let valid_withdraw =
check_withdraw_limits(&sol_spot_market, Some(&user), Some(QUOTE_PRECISION)).unwrap();
check_withdraw_limits(&sol_spot_market, Some(&user), Some(10_000_000_000)).unwrap();
assert!(!valid_withdraw);

let valid_withdraw = check_withdraw_limits(&sol_spot_market, None, None).unwrap();
assert!(!valid_withdraw);
}

Expand Down
18 changes: 14 additions & 4 deletions programs/drift/src/math/spot_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::math::casting::Cast;
use crate::math::safe_math::SafeMath;

use crate::math::spot_balance::get_token_amount;
use crate::state::spot_market::{SpotBalanceType, SpotMarket};
use crate::state::spot_market::{SpotBalance, SpotBalanceType, SpotMarket};
use crate::state::user::User;
use crate::validate;

Expand All @@ -26,7 +26,7 @@ pub fn calculate_max_borrow_token_amount(
) -> DriftResult<u128> {
let max_borrow_token = withdraw_guard_threshold.max(
(deposit_token_amount / 6)
.max(borrow_token_twap.safe_add(borrow_token_twap / 5)?)
.max(borrow_token_twap.safe_add(deposit_token_amount / 10)?)
.min(deposit_token_amount.safe_sub(deposit_token_amount / 5)?),
); // between ~15-80% utilization with friction on twap

Expand Down Expand Up @@ -105,8 +105,18 @@ pub fn check_withdraw_limits(
spot_market.withdraw_guard_threshold.cast()?,
)?;

let valid_global_withdrawal =
deposit_token_amount >= min_deposit_token && borrow_token_amount <= max_borrow_token;
// for resulting deposit or ZERO, check if deposits above minimum
// for resulting borrow, check both deposit and borrow constraints
let valid_global_withdrawal = if let Some(user) = user {
let spot_position_index = user.get_spot_position_index(spot_market.market_index)?;
if user.spot_positions[spot_position_index].balance_type() == &SpotBalanceType::Borrow {
borrow_token_amount <= max_borrow_token && deposit_token_amount >= min_deposit_token
} else {
deposit_token_amount >= min_deposit_token
}
} else {
deposit_token_amount >= min_deposit_token && borrow_token_amount <= max_borrow_token
};

let valid_withdrawal = if !valid_global_withdrawal {
msg!(
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/math/spotBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export function calculateWithdrawLimit(
BN.min(
BN.max(
marketDepositTokenAmount.div(new BN(6)),
borrowTokenTwapLive.add(borrowTokenTwapLive.div(new BN(5)))
borrowTokenTwapLive.add(marketDepositTokenAmount.div(new BN(10)))
),
marketDepositTokenAmount.sub(marketDepositTokenAmount.div(new BN(5)))
)
Expand Down

0 comments on commit 064dc1a

Please sign in to comment.