From 3fa3bc19b4e6b50efcdef13554e4b788fbc962ec Mon Sep 17 00:00:00 2001 From: Chester Sim Date: Fri, 4 Aug 2023 23:52:51 +0800 Subject: [PATCH] add profit share fee calculation --- ts/sdk/src/accounts/vaultDepositorAccount.ts | 33 ++++++++++++++++++-- ts/sdk/src/constants/index.ts | 2 +- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/ts/sdk/src/accounts/vaultDepositorAccount.ts b/ts/sdk/src/accounts/vaultDepositorAccount.ts index c4bf4551..d83b9887 100644 --- a/ts/sdk/src/accounts/vaultDepositorAccount.ts +++ b/ts/sdk/src/accounts/vaultDepositorAccount.ts @@ -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'; @@ -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; + + 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; + } } diff --git a/ts/sdk/src/constants/index.ts b/ts/sdk/src/constants/index.ts index ba964452..feefd5b5 100644 --- a/ts/sdk/src/constants/index.ts +++ b/ts/sdk/src/constants/index.ts @@ -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);