Skip to content

Commit

Permalink
program: withdraw fee use last active slot(#756)
Browse files Browse the repository at this point in the history
* bigz/withdraw-fee-use-last-active-slot

* add test, set limit to 50 slots

* update CHANGELOG.md

---------

Co-authored-by: lil perp <[email protected]>
  • Loading branch information
0xbigz and crispheaney authored Dec 12, 2023
1 parent c10f8b6 commit 69bccca
Show file tree
Hide file tree
Showing 4 changed files with 36 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

### Features

- program: only consider recent last_active_slot in qualifies_for_withdraw_feen([#756](https://github.com/drift-labs/protocol-v2/pull/756))
- program: amm can use reference price offset from oracle price based on clamped inventory and persist market premiums ([#681](https://github.com/drift-labs/protocol-v2/pull/681))

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion programs/drift/src/instructions/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ pub fn handle_withdraw(
let spot_market = &mut spot_market_map.get_ref_mut(&market_index)?;
let oracle_price_data = oracle_map.get_price_data(&spot_market.oracle)?;

if user.qualifies_for_withdraw_fee(&user_stats) {
if user.qualifies_for_withdraw_fee(&user_stats, slot) {
let fee =
charge_withdraw_fee(spot_market, oracle_price_data.price, user, &mut user_stats)?;
amount = amount.safe_sub(fee.cast()?)?;
Expand Down
7 changes: 6 additions & 1 deletion programs/drift/src/state/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,12 @@ impl User {
}
}

pub fn qualifies_for_withdraw_fee(&self, user_stats: &UserStats) -> bool {
pub fn qualifies_for_withdraw_fee(&self, user_stats: &UserStats, slot: u64) -> bool {
// only qualifies for user with recent last_active_slot (~25 seconds)
if slot.saturating_sub(self.last_active_slot) >= 50 {
return false;
}

let min_total_withdraws = 10_000_000 * QUOTE_PRECISION_U64; // $10M

// if total withdraws are greater than $10M and user has paid more than %.01 of it in fees
Expand Down
32 changes: 28 additions & 4 deletions programs/drift/src/state/user/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,7 @@ mod qualifies_for_withdraw_fee {
let user = User::default();
let user_stats = UserStats::default();

let qualifies = user.qualifies_for_withdraw_fee(&user_stats);
let qualifies = user.qualifies_for_withdraw_fee(&user_stats, 0);

assert!(!qualifies);

Expand All @@ -1681,7 +1681,7 @@ mod qualifies_for_withdraw_fee {
..User::default()
};

let qualifies = user.qualifies_for_withdraw_fee(&user_stats);
let qualifies = user.qualifies_for_withdraw_fee(&user_stats, 0);

assert!(!qualifies);

Expand All @@ -1698,12 +1698,13 @@ mod qualifies_for_withdraw_fee {
..UserStats::default()
};

let qualifies = user.qualifies_for_withdraw_fee(&user_stats);
let qualifies = user.qualifies_for_withdraw_fee(&user_stats, 0);

assert!(!qualifies);

let user = User {
total_withdraws: 10_000_000 * QUOTE_PRECISION_U64,

..User::default()
};

Expand All @@ -1715,9 +1716,32 @@ mod qualifies_for_withdraw_fee {
..UserStats::default()
};

let qualifies = user.qualifies_for_withdraw_fee(&user_stats);
let qualifies = user.qualifies_for_withdraw_fee(&user_stats, 0);

assert!(qualifies);

// fee
let user = User {
total_withdraws: 13_000_000 * QUOTE_PRECISION_U64,
last_active_slot: 8900877,
..User::default()
};

let user_stats = UserStats {
fees: UserFees {
total_fee_paid: 1 * QUOTE_PRECISION_U64,
..UserFees::default()
},
..UserStats::default()
};

let qualifies = user.qualifies_for_withdraw_fee(&user_stats, user.last_active_slot + 1);

assert!(qualifies);

let qualifies = user.qualifies_for_withdraw_fee(&user_stats, user.last_active_slot + 50);

assert!(!qualifies);
}
}

Expand Down

0 comments on commit 69bccca

Please sign in to comment.