Skip to content

Commit

Permalink
Merge pull request #20 from drift-labs/chester/fees-calculation
Browse files Browse the repository at this point in the history
add profit share fee calculation for VaultDepositor
  • Loading branch information
ChesterSim authored Aug 7, 2023
2 parents 9f6233f + b1d80c0 commit de3cfe5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
31 changes: 29 additions & 2 deletions ts/sdk/src/accounts/vaultDepositorAccount.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Program } from '@coral-xyz/anchor';
import { BulkAccountLoader } from '@drift-labs/sdk';
import { BN, Program } from '@coral-xyz/anchor';
import { BulkAccountLoader, PERCENTAGE_PRECISION, ZERO } from '@drift-labs/sdk';
import { PublicKey } from '@solana/web3.js';
import { DriftVaults } from '../types/drift_vaults';
import { VaultDepositor, VaultDepositorAccountEvents } from '../types/types';
Expand Down Expand Up @@ -37,4 +37,31 @@ export class VaultDepositorAccount extends VaultsProgramAccount<
): PublicKey {
return getVaultDepositorAddressSync(programId, vault, authority);
}

/**
* Calculates the percentage of a depositor's funds that will be paid as profit share fees.
*
* @param vaultProfitShare Vault's profit share fee
* @param depositorEquity Vault depositor's net deposit value
*/
calcProfitShareFeesProportion(vaultProfitShare: BN, depositorEquity: BN): BN {
const accountData = this.accountSubscriber.getAccountAndSlot().data;

const profit = depositorEquity
.sub(accountData.netDeposits)
.sub(accountData.cumulativeProfitShareAmount);

if (profit.lte(new BN(0))) {
return ZERO;
}

const profitShareAmount = profit
.mul(vaultProfitShare)
.div(PERCENTAGE_PRECISION);
const profitShareProportion = profitShareAmount
.mul(PERCENTAGE_PRECISION)
.div(depositorEquity);

return profitShareProportion;
}
}
2 changes: 1 addition & 1 deletion ts/sdk/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { BN } from '@drift-labs/sdk';

export const VAULT_SHARES_PRECISION = new BN(6);
export const VAULT_SHARES_PRECISION_EXP = new BN(6);

0 comments on commit de3cfe5

Please sign in to comment.