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

PoC: only apply vault fees and not profit share fees on deposits #60

Open
wants to merge 1 commit into
base: master
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
70 changes: 67 additions & 3 deletions programs/drift_vaults/src/instructions/apply_profit_share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ use crate::constraints::{
is_delegate_for_vault, is_manager_for_vault, is_user_for_vault, is_user_stats_for_vault,
is_vault_for_vault_depositor,
};
use crate::events::{VaultDepositorAction, VaultDepositorRecord, VaultDepositorV1Record};
use crate::state::{Vault, VaultProtocolProvider};
use crate::AccountMapProvider;
use crate::VaultDepositor;
use crate::{AccountMapProvider, VaultFee};
use crate::{VaultDepositor, VaultDepositorBase};

pub fn apply_profit_share<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, ApplyProfitShare<'info>>,
) -> Result<()> {
let clock = &Clock::get()?;
let now = clock.unix_timestamp;

let mut vault = ctx.accounts.vault.load_mut()?;
let mut vault_depositor = ctx.accounts.vault_depositor.load_mut()?;
Expand All @@ -36,7 +38,69 @@ pub fn apply_profit_share<'c: 'info, 'info>(
let vault_equity =
vault.calculate_equity(&user, &perp_market_map, &spot_market_map, &mut oracle_map)?;

vault_depositor.apply_profit_share(vault_equity, &mut vault, &mut vp)?;
let vault_shares_before = vault_depositor.checked_vault_shares(&vault)?;
let total_vault_shares_before = vault.total_shares;
let user_vault_shares_before = vault.user_shares;
let protocol_shares_before = vault.get_protocol_shares(&mut vp);

let VaultFee {
management_fee_payment,
management_fee_shares,
protocol_fee_payment,
protocol_fee_shares,
} = vault.apply_fee(&mut vp, vault_equity, now)?;
let (manager_profit_share, protocol_profit_share) =
vault_depositor.apply_profit_share(vault_equity, &mut vault, &mut vp)?;

let protocol_shares_after = vault.get_protocol_shares(&mut vp);

match vp {
None => {
emit!(VaultDepositorRecord {
ts: now,
vault: vault.pubkey,
depositor_authority: vault_depositor.authority,
action: VaultDepositorAction::FeePayment,
amount: 0,
spot_market_index: vault.spot_market_index,
vault_equity_before: vault_equity,
vault_shares_before,
user_vault_shares_before,
total_vault_shares_before,
vault_shares_after: vault_depositor.get_vault_shares(),
total_vault_shares_after: vault.total_shares,
user_vault_shares_after: vault.user_shares,
profit_share: manager_profit_share,
management_fee: management_fee_payment,
management_fee_shares,
});
}
Some(_) => {
emit!(VaultDepositorV1Record {
ts: now,
vault: vault.pubkey,
depositor_authority: vault_depositor.authority,
action: VaultDepositorAction::FeePayment,
amount: 0,
spot_market_index: vault.spot_market_index,
vault_equity_before: vault_equity,
vault_shares_before,
user_vault_shares_before,
total_vault_shares_before,
vault_shares_after: vault_depositor.get_vault_shares(),
total_vault_shares_after: vault.total_shares,
user_vault_shares_after: vault.user_shares,
protocol_profit_share,
protocol_fee: protocol_fee_payment,
protocol_fee_shares,
manager_profit_share,
management_fee: management_fee_payment,
management_fee_shares,
protocol_shares_before,
protocol_shares_after,
});
}
}

Ok(())
}
Expand Down
8 changes: 3 additions & 5 deletions programs/drift_vaults/src/state/vault_depositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,6 @@ impl VaultDepositor {
protocol_fee_payment,
protocol_fee_shares,
} = vault.apply_fee(vault_protocol, vault_equity, now)?;
let (manager_profit_share, protocol_profit_share) =
self.apply_profit_share(vault_equity, vault, vault_protocol)?;

let n_shares = vault_amount_to_depositor_shares(amount, vault.total_shares, vault_equity)?;

Expand Down Expand Up @@ -309,7 +307,7 @@ impl VaultDepositor {
vault_shares_after,
total_vault_shares_after: vault.total_shares,
user_vault_shares_after: vault.user_shares,
profit_share: manager_profit_share,
profit_share: 0,
management_fee: management_fee_payment,
management_fee_shares,
});
Expand All @@ -329,10 +327,10 @@ impl VaultDepositor {
vault_shares_after,
total_vault_shares_after: vault.total_shares,
user_vault_shares_after: vault.user_shares,
protocol_profit_share,
protocol_profit_share: 0,
protocol_fee: protocol_fee_payment,
protocol_fee_shares,
manager_profit_share,
manager_profit_share: 0,
management_fee: management_fee_payment,
management_fee_shares,
protocol_shares_before,
Expand Down
Loading