Skip to content

Commit

Permalink
program: rm withdraw fee (#1334)
Browse files Browse the repository at this point in the history
* program: rm withdraw fee

* CHANGELOG

* cargo fmt --
  • Loading branch information
crispheaney authored Nov 21, 2024
1 parent a3196bb commit 951b06a
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 184 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- program: rm withdraw fee ([#1334](https://github.com/drift-labs/protocol-v2/pull/1334))

### Fixes

- program: skip validate_post_only_order if amm paused ([#1202](https://github.com/drift-labs/protocol-v2/pull/1202))
Expand Down
31 changes: 0 additions & 31 deletions programs/drift/src/controller/spot_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,34 +206,3 @@ pub fn transfer_spot_position_deposit(

Ok(())
}

pub fn charge_withdraw_fee(
spot_market: &mut SpotMarket,
oracle_price: i64,
user: &mut User,
user_stats: &mut UserStats,
) -> DriftResult<u128> {
let fee_quote = QUOTE_PRECISION / 2000;
let fee = fee_quote
.safe_mul(spot_market.get_precision().cast()?)?
.safe_div(oracle_price.unsigned_abs().cast()?)?;

user.update_cumulative_spot_fees(-fee.cast()?)?;
user_stats.increment_total_fees(fee.cast()?)?;

msg!("Charging withdraw fee of {}", fee);

update_revenue_pool_balances(fee, &SpotBalanceType::Deposit, spot_market)?;

let position_index = user.force_get_spot_position_index(spot_market.market_index)?;
update_spot_balances_and_cumulative_deposits(
fee,
&SpotBalanceType::Borrow,
spot_market,
&mut user.spot_positions[position_index],
false,
Some(0), // to make fee show in cumulative deposits
)?;

Ok(fee)
}
51 changes: 0 additions & 51 deletions programs/drift/src/controller/spot_position/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,54 +139,3 @@ mod update_spot_position_balance {
.is_err());
}
}

mod charge_withdraw_fee {
use crate::controller::spot_position::charge_withdraw_fee;
use crate::math::constants::SPOT_BALANCE_PRECISION_U64;
use crate::math::spot_balance::get_token_amount;
use crate::state::spot_market::{SpotBalanceType, SpotMarket};
use crate::state::user::{SpotPosition, User, UserStats};
use crate::test_utils::get_spot_positions;
use crate::QUOTE_PRECISION_I64;

#[test]
fn deposit() {
let mut user = User {
spot_positions: get_spot_positions(SpotPosition {
market_index: 0,
scaled_balance: SPOT_BALANCE_PRECISION_U64,
cumulative_deposits: QUOTE_PRECISION_I64,
..SpotPosition::default()
}),
..User::default()
};
let mut user_stats = UserStats::default();
let mut spot_market = SpotMarket::default_quote_market();

let oracle_price = QUOTE_PRECISION_I64;

charge_withdraw_fee(&mut spot_market, oracle_price, &mut user, &mut user_stats).unwrap();

let token_amount = user
.get_spot_position(0)
.unwrap()
.get_token_amount(&spot_market)
.unwrap();

let cumulative_deposits = user.get_spot_position(0).unwrap().cumulative_deposits;

assert_eq!(token_amount, 999500);
assert_eq!(cumulative_deposits, QUOTE_PRECISION_I64);
assert_eq!(user_stats.fees.total_fee_paid, 500);
assert_eq!(user.cumulative_spot_fees, -500);

let revenue_pool_amount = get_token_amount(
spot_market.revenue_pool.scaled_balance,
&spot_market,
&SpotBalanceType::Deposit,
)
.unwrap();

assert_eq!(revenue_pool_amount, 500);
}
}
8 changes: 1 addition & 7 deletions programs/drift/src/instructions/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::controller::orders::{cancel_orders, ModifyOrderId};
use crate::controller::position::PositionDirection;
use crate::controller::spot_balance::update_revenue_pool_balances;
use crate::controller::spot_position::{
charge_withdraw_fee, update_spot_balances_and_cumulative_deposits,
update_spot_balances_and_cumulative_deposits,
update_spot_balances_and_cumulative_deposits_with_limits,
};
use crate::error::ErrorCode;
Expand Down Expand Up @@ -568,12 +568,6 @@ pub fn handle_withdraw<'c: 'info, 'info>(
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, slot) {
let fee =
charge_withdraw_fee(spot_market, oracle_price_data.price, user, &mut user_stats)?;
amount = amount.safe_sub(fee.cast()?)?;
}

user.increment_total_withdraws(
amount,
oracle_price_data.price,
Expand Down
13 changes: 0 additions & 13 deletions programs/drift/src/state/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,19 +390,6 @@ impl User {
}
}

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
self.total_withdraws >= min_total_withdraws
&& self.total_withdraws / user_stats.fees.total_fee_paid.max(1) > 10_000
}

pub fn update_reduce_only_status(&mut self, reduce_only: bool) -> DriftResult {
if reduce_only {
self.add_user_status(UserStatus::ReduceOnly);
Expand Down
82 changes: 0 additions & 82 deletions programs/drift/src/state/user/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1663,88 +1663,6 @@ mod open_orders {
}
}

mod qualifies_for_withdraw_fee {
use crate::state::user::{User, UserFees, UserStats};
use crate::QUOTE_PRECISION_U64;

#[test]
fn test() {
let user = User::default();
let user_stats = UserStats::default();

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

assert!(!qualifies);

let user = User {
total_withdraws: 9_999_999 * QUOTE_PRECISION_U64,
..User::default()
};

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()
};

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

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()
};

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

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: 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);
}
}

mod update_user_status {
use crate::state::user::{User, UserStatus};

Expand Down

0 comments on commit 951b06a

Please sign in to comment.