Skip to content

Commit

Permalink
fixes #62
Browse files Browse the repository at this point in the history
  • Loading branch information
wphan committed Jan 10, 2025
1 parent eb1d03e commit 5d99b87
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 9 deletions.
4 changes: 3 additions & 1 deletion programs/drift_vaults/src/state/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ pub trait VaultDepositorBase {
let profit_share_amount =
manager_profit_share_amount.safe_add(protocol_profit_share_amount)?;

let net_profit = profit_u128.safe_sub(profit_share_amount)?;

self.set_cumulative_profit_share_amount(
self.get_cumulative_profit_share_amount()
.safe_add(profit_u128.cast()?)?,
.safe_add(net_profit.cast()?)?,
);

self.set_profit_share_fee_paid(
Expand Down
8 changes: 4 additions & 4 deletions programs/drift_vaults/src/state/vault_depositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,8 +1132,8 @@ mod vault_v1_tests {
// total shares outside of user is now 100M + 7.5M = 107.5M
assert_eq!(vault.total_shares, 107_500_000);
assert_eq!(withdraw_amount, equity_minus_fee);
// $100 worth of profit that has been realized (this is not total fees paid)
assert_eq!(vd.cumulative_profit_share_amount, 100_000_000);
// $85 = 100 - 10% - 5%, worth of profit that has been realized (this is not total fees paid)
assert_eq!(vd.cumulative_profit_share_amount, 85_000_000);
println!("vault shares: {}", vd.checked_vault_shares(&vault).unwrap());
println!("shares base: {}", vd.vault_shares_base);
println!("user shares: {}", vault.user_shares);
Expand Down Expand Up @@ -1246,8 +1246,8 @@ mod vault_v1_tests {
// total shares outside of user is now 100M + 5M = 105M
assert_eq!(vault.total_shares, 105_000_000);
assert_eq!(withdraw_amount, equity_minus_fee);
// $100 worth of profit that has been realized (this is not total fees paid)
assert_eq!(vd.cumulative_profit_share_amount, 100_000_000);
// $90 = $100 - 10% worth of profit that has been realized (this is not total fees paid)
assert_eq!(vd.cumulative_profit_share_amount, 90_000_000);
println!("vault shares: {}", vd.checked_vault_shares(&vault).unwrap());
println!("shares base: {}", vd.vault_shares_base);
println!("user shares: {}", vault.user_shares);
Expand Down
98 changes: 94 additions & 4 deletions programs/drift_vaults/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ mod vault_fcn {
use crate::withdraw_request::WithdrawRequest;
use crate::{Vault, VaultDepositor, WithdrawUnit};
use anchor_lang::prelude::Pubkey;
use drift::math::constants::{ONE_YEAR, QUOTE_PRECISION_U64};
use drift::math::constants::{
ONE_YEAR, QUOTE_PRECISION, QUOTE_PRECISION_I64, QUOTE_PRECISION_U64,
};
use drift::math::insurance::if_shares_to_vault_amount as depositor_shares_to_vault_amount;

#[test]
Expand Down Expand Up @@ -498,7 +500,7 @@ mod vault_fcn {
assert_eq!(cnt, 4); // 4 days
assert_eq!(
vd.cumulative_profit_share_amount,
(1000 * QUOTE_PRECISION_U64) as i64
(850 * QUOTE_PRECISION_U64) as i64 // 1000 - 15% profit share
);
assert_eq!(vd.net_deposits, (2000 * QUOTE_PRECISION_U64) as i64);

Expand Down Expand Up @@ -747,6 +749,94 @@ mod vault_fcn {
);
assert!(!finishing_liquidation);
}

#[test]
fn test_apply_profit_share_on_net_hwm() {
let mut now = 123456789;
let mut vault = Vault::default();
let mut vp = None;
vault.management_fee = 0;
vault.profit_share = 100_000; // 10%
vault.last_fee_update_ts = now;

let mut vault_equity: u64 = 0;

let deposit_amount = 2000 * QUOTE_PRECISION_U64;
let vd =
&mut VaultDepositor::new(Pubkey::default(), Pubkey::default(), Pubkey::default(), now);
vd.deposit(deposit_amount, vault_equity, &mut vault, &mut vp, now)
.unwrap(); // new user deposits $2000
vault_equity += deposit_amount;

let depositor_amount_before = depositor_shares_to_vault_amount(
vd.checked_vault_shares(&vault).unwrap(),
vault.total_shares,
vault_equity,
)
.unwrap();
assert_eq!(depositor_amount_before, vault_equity);

// vault up 10% (user +$200 gross, +$180 net)
now += 60 * 60 * 24; // 1 day later
vault_equity = 2200 * QUOTE_PRECISION_U64;

vd.apply_profit_share(vault_equity, &mut vault, &mut vp)
.unwrap();
vault.apply_fee(&mut vp, vault_equity, now).unwrap();

let depositor_amount_in_profit = depositor_shares_to_vault_amount(
vd.checked_vault_shares(&vault).unwrap(),
vault.total_shares,
vault_equity,
)
.unwrap();
assert_eq!(depositor_amount_in_profit, 2180 * QUOTE_PRECISION_U64);
assert_eq!(vd.cumulative_profit_share_amount, 180 * QUOTE_PRECISION_I64);
assert_eq!(vd.profit_share_fee_paid, 20 * QUOTE_PRECISION_U64);
assert_eq!(vault.total_shares, 2000 * QUOTE_PRECISION);
assert_eq!(vd.checked_vault_shares(&vault).unwrap(), 1981_818_182);

// vault drawdown 10%
now += 60 * 60 * 24; // 1 day later
vault_equity = 1980 * QUOTE_PRECISION_U64;

vd.apply_profit_share(vault_equity, &mut vault, &mut vp)
.unwrap();
vault.apply_fee(&mut vp, vault_equity, now).unwrap();

let depositor_amount_in_drawdown = depositor_shares_to_vault_amount(
vd.checked_vault_shares(&vault).unwrap(),
vault.total_shares,
vault_equity,
)
.unwrap();
assert_eq!(depositor_amount_in_drawdown, 1962 * QUOTE_PRECISION_U64);
assert_eq!(vd.cumulative_profit_share_amount, 180 * QUOTE_PRECISION_I64);
assert_eq!(vd.profit_share_fee_paid, 20 * QUOTE_PRECISION_U64);
assert_eq!(vault.total_shares, 2000 * QUOTE_PRECISION);
assert_eq!(vd.checked_vault_shares(&vault).unwrap(), 1981_818_182);

// in profit again (above net hwm (2180), below gross hwm (2200))
// vd equity = 2210*1982/2000 = 2190
now += 60 * 60 * 24; // 1 day later
vault_equity = 2210 * QUOTE_PRECISION_U64;

vd.apply_profit_share(vault_equity, &mut vault, &mut vp)
.unwrap();
vault.apply_fee(&mut vp, vault_equity, now).unwrap();

let depositor_amount_in_profit = depositor_shares_to_vault_amount(
vd.checked_vault_shares(&vault).unwrap(),
vault.total_shares,
vault_equity,
)
.unwrap();
assert_eq!(depositor_amount_in_profit, 2188_918_182);
assert_eq!(vd.cumulative_profit_share_amount, 188_918_182);
assert_eq!(vd.profit_share_fee_paid, 20_990_909);
assert_eq!(vault.total_shares, 2000 * QUOTE_PRECISION);
assert_eq!(vd.checked_vault_shares(&vault).unwrap(), 1980_921_432);
}
}

#[cfg(test)]
Expand Down Expand Up @@ -1511,7 +1601,7 @@ mod vault_v1_fcn {
assert_eq!(cnt, 4); // 4 days
assert_eq!(
vd.cumulative_profit_share_amount,
(1000 * QUOTE_PRECISION_U64) as i64
(850 * QUOTE_PRECISION_U64) as i64 // $850 = $1000 - 15%
);
assert_eq!(vd.net_deposits, (2000 * QUOTE_PRECISION_U64) as i64);

Expand Down Expand Up @@ -1619,7 +1709,7 @@ mod vault_v1_fcn {
assert_eq!(cnt, 4); // 4 days
assert_eq!(
vd.cumulative_profit_share_amount,
(1000 * QUOTE_PRECISION_U64) as i64
(850 * QUOTE_PRECISION_U64) as i64 // $850 = $1000 - 15%
);
assert_eq!(vd.net_deposits, (2000 * QUOTE_PRECISION_U64) as i64);

Expand Down

0 comments on commit 5d99b87

Please sign in to comment.