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

add profit share fee calculation for VaultDepositor #20

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 31 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,33 @@ export class VaultDepositorAccount extends VaultsProgramAccount<
): PublicKey {
return getVaultDepositorAddressSync(programId, vault, authority);
}

/**
* Calculates the proportion 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
): number {
const accountData = this.accountSubscriber.getAccountAndSlot().data;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would use a BN to avoid random number overflow errors


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.toNumber() / depositorEquity.toNumber();

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);
Loading