@drift-labs/vaults-sdk 0.1.462 → 0.1.464

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@ import { Program } from '@coral-xyz/anchor';
2
2
  import { BulkAccountLoader, BN } from '@drift-labs/sdk';
3
3
  import { PublicKey } from '@solana/web3.js';
4
4
  import { DriftVaults } from '../types/drift_vaults';
5
- import { Vault, VaultAccountEvents } from '../types/types';
5
+ import { Vault, VaultAccountEvents, VaultProtocol } from '../types/types';
6
6
  import { VaultsProgramAccount } from './vaultsProgramAccount';
7
7
  export declare class VaultAccount extends VaultsProgramAccount<Vault, VaultAccountEvents> {
8
8
  constructor(program: Program<DriftVaults>, vaultPubkey: PublicKey, accountLoader: BulkAccountLoader, accountSubscriptionType?: 'polling' | 'websocket');
@@ -11,4 +11,9 @@ export declare class VaultAccount extends VaultsProgramAccount<Vault, VaultAccou
11
11
  totalShares: BN;
12
12
  managementFeeShares: BN;
13
13
  };
14
+ calcSharesAfterManagementAndProtocolFee(vaultEquity: BN, vaultProtocol: VaultProtocol): {
15
+ totalShares: BN;
16
+ managementFeeShares: BN;
17
+ protocolFeeShares: BN;
18
+ };
14
19
  }
@@ -48,5 +48,120 @@ class VaultAccount extends vaultsProgramAccount_1.VaultsProgramAccount {
48
48
  const managementFeeShares = newTotalShares.sub(accountData.totalShares);
49
49
  return { totalShares: newTotalShares, managementFeeShares };
50
50
  }
51
+ calcSharesAfterManagementAndProtocolFee(vaultEquity, vaultProtocol) {
52
+ const accountData = this.accountSubscriber.getAccountAndSlot().data;
53
+ if (!accountData.vaultProtocol) {
54
+ throw new Error('VaultProtocol does not exist for vault');
55
+ }
56
+ const depositorsEquity = accountData.userShares
57
+ .mul(vaultEquity)
58
+ .div(accountData.totalShares);
59
+ const now = new sdk_1.BN(Date.now() / 1000);
60
+ const sinceLast = now.sub(accountData.lastFeeUpdateTs);
61
+ if (!accountData.managementFee.eq(sdk_1.ZERO) &&
62
+ !vaultProtocol.protocolFee.eq(sdk_1.ZERO) &&
63
+ depositorsEquity.gt(sdk_1.ZERO)) {
64
+ const totalFee = accountData.managementFee.add(vaultProtocol.protocolFee);
65
+ const totalFeePayment = depositorsEquity
66
+ .mul(totalFee)
67
+ .div(sdk_1.PERCENTAGE_PRECISION)
68
+ .mul(sinceLast)
69
+ .div(sdk_1.ONE_YEAR);
70
+ const managementFeePayment = depositorsEquity
71
+ .mul(accountData.managementFee)
72
+ .div(sdk_1.PERCENTAGE_PRECISION)
73
+ .mul(sinceLast)
74
+ .div(sdk_1.ONE_YEAR);
75
+ const protocolFeePayment = sdk_1.BN.min(totalFeePayment, depositorsEquity.sub(new sdk_1.BN(1)))
76
+ .mul(vaultProtocol.protocolFee)
77
+ .div(totalFee);
78
+ const newTotalSharesFactor = depositorsEquity
79
+ .mul(sdk_1.PERCENTAGE_PRECISION)
80
+ .div(depositorsEquity.sub(managementFeePayment).sub(protocolFeePayment));
81
+ const newTotalShares = sdk_1.BN.max(accountData.totalShares
82
+ .mul(newTotalSharesFactor)
83
+ .div(sdk_1.PERCENTAGE_PRECISION), accountData.userShares);
84
+ if ((managementFeePayment.eq(sdk_1.ZERO) && protocolFeePayment.eq(sdk_1.ZERO)) ||
85
+ accountData.totalShares.eq(newTotalShares)) {
86
+ return {
87
+ totalShares: accountData.totalShares,
88
+ managementFeeShares: sdk_1.ZERO,
89
+ protocolFeeShares: sdk_1.ZERO,
90
+ };
91
+ }
92
+ const managementFeeShares = newTotalShares.sub(accountData.totalShares);
93
+ const protocolFeeShares = newTotalShares.sub(accountData.totalShares);
94
+ return {
95
+ totalShares: newTotalShares,
96
+ managementFeeShares,
97
+ protocolFeeShares,
98
+ };
99
+ }
100
+ else if (accountData.managementFee.eq(sdk_1.ZERO) &&
101
+ !vaultProtocol.protocolFee.eq(sdk_1.ZERO) &&
102
+ depositorsEquity.gt(sdk_1.ZERO)) {
103
+ const protocolFeePayment = depositorsEquity
104
+ .mul(vaultProtocol.protocolFee)
105
+ .div(sdk_1.PERCENTAGE_PRECISION)
106
+ .mul(sinceLast)
107
+ .div(sdk_1.ONE_YEAR);
108
+ const newTotalSharesFactor = depositorsEquity
109
+ .mul(sdk_1.PERCENTAGE_PRECISION)
110
+ .div(depositorsEquity.sub(protocolFeePayment));
111
+ const newTotalShares = sdk_1.BN.max(accountData.totalShares
112
+ .mul(newTotalSharesFactor)
113
+ .div(sdk_1.PERCENTAGE_PRECISION), accountData.userShares);
114
+ if (protocolFeePayment.eq(sdk_1.ZERO) ||
115
+ accountData.totalShares.eq(newTotalShares)) {
116
+ return {
117
+ totalShares: accountData.totalShares,
118
+ managementFeeShares: sdk_1.ZERO,
119
+ protocolFeeShares: sdk_1.ZERO,
120
+ };
121
+ }
122
+ const protocolFeeShares = newTotalShares.sub(accountData.totalShares);
123
+ return {
124
+ totalShares: newTotalShares,
125
+ managementFeeShares: sdk_1.ZERO,
126
+ protocolFeeShares,
127
+ };
128
+ }
129
+ else if (!accountData.managementFee.eq(sdk_1.ZERO) &&
130
+ vaultProtocol.protocolFee.eq(sdk_1.ZERO) &&
131
+ depositorsEquity.gt(sdk_1.ZERO)) {
132
+ const managementFeePayment = sdk_1.BN.min(depositorsEquity
133
+ .mul(accountData.managementFee)
134
+ .div(sdk_1.PERCENTAGE_PRECISION)
135
+ .mul(sinceLast)
136
+ .div(sdk_1.ONE_YEAR), depositorsEquity.sub(sdk_1.ONE));
137
+ const newTotalSharesFactor = depositorsEquity
138
+ .mul(sdk_1.PERCENTAGE_PRECISION)
139
+ .div(depositorsEquity.sub(managementFeePayment));
140
+ const newTotalShares = sdk_1.BN.max(accountData.totalShares
141
+ .mul(newTotalSharesFactor)
142
+ .div(sdk_1.PERCENTAGE_PRECISION), accountData.userShares);
143
+ if (managementFeePayment.eq(sdk_1.ZERO) ||
144
+ accountData.totalShares.eq(newTotalShares)) {
145
+ return {
146
+ totalShares: accountData.totalShares,
147
+ managementFeeShares: sdk_1.ZERO,
148
+ protocolFeeShares: sdk_1.ZERO,
149
+ };
150
+ }
151
+ const managementFeeShares = newTotalShares.sub(accountData.totalShares);
152
+ return {
153
+ totalShares: newTotalShares,
154
+ managementFeeShares,
155
+ protocolFeeShares: sdk_1.ZERO,
156
+ };
157
+ }
158
+ else {
159
+ return {
160
+ totalShares: accountData.totalShares,
161
+ managementFeeShares: sdk_1.ZERO,
162
+ protocolFeeShares: sdk_1.ZERO,
163
+ };
164
+ }
165
+ }
51
166
  }
52
167
  exports.VaultAccount = VaultAccount;
@@ -2,3 +2,4 @@ import { PublicKey } from '@solana/web3.js';
2
2
  export declare function getVaultAddressSync(programId: PublicKey, encodedName: number[]): PublicKey;
3
3
  export declare function getVaultDepositorAddressSync(programId: PublicKey, vault: PublicKey, authority: PublicKey): PublicKey;
4
4
  export declare function getTokenVaultAddressSync(programId: PublicKey, vault: PublicKey): PublicKey;
5
+ export declare function getVaultProtocolAddressSync(programId: PublicKey, vault: PublicKey): PublicKey;
package/lib/addresses.js CHANGED
@@ -26,6 +26,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.getVaultAddressSync = getVaultAddressSync;
27
27
  exports.getVaultDepositorAddressSync = getVaultDepositorAddressSync;
28
28
  exports.getTokenVaultAddressSync = getTokenVaultAddressSync;
29
+ exports.getVaultProtocolAddressSync = getVaultProtocolAddressSync;
29
30
  const web3_js_1 = require("@solana/web3.js");
30
31
  const anchor = __importStar(require("@coral-xyz/anchor"));
31
32
  function getVaultAddressSync(programId, encodedName) {
@@ -47,3 +48,9 @@ function getTokenVaultAddressSync(programId, vault) {
47
48
  vault.toBuffer(),
48
49
  ], programId)[0];
49
50
  }
51
+ function getVaultProtocolAddressSync(programId, vault) {
52
+ return web3_js_1.PublicKey.findProgramAddressSync([
53
+ Buffer.from(anchor.utils.bytes.utf8.encode('vault_protocol')),
54
+ vault.toBuffer(),
55
+ ], programId)[0];
56
+ }
@@ -1,5 +1,5 @@
1
1
  import { BN } from '@drift-labs/sdk';
2
- import { Vault, VaultDepositor } from '../types/types';
2
+ import { Vault, VaultDepositor, VaultProtocol } from '../types/types';
3
3
  /**
4
4
  * Calculates the unrealized profitShare for a vaultDepositor
5
5
  * @param vaultDepositor
@@ -11,4 +11,13 @@ export declare function calculateApplyProfitShare(vaultDepositor: VaultDepositor
11
11
  profitShareAmount: BN;
12
12
  profitShareShares: BN;
13
13
  };
14
- export declare function calculateProfitShare(vaultDepositor: VaultDepositor, totalAmount: BN, vault: Vault): BN;
14
+ export declare function calculateProfitShare(vaultDepositor: VaultDepositor, totalAmount: BN, vault: Vault, vaultProtocol?: VaultProtocol): BN;
15
+ /**
16
+ * Calculates the equity across deposits and realized profit for a vaultDepositor
17
+ * @param vaultDepositor vault depositor account
18
+ * @param vaultEquity total vault equity
19
+ * @param vault vault account
20
+ * @param vaultProtocol if vault account has "vaultProtocol" then this is needed
21
+ * @returns
22
+ */
23
+ export declare function calculateRealizedVaultDepositorEquity(vaultDepositor: VaultDepositor, vaultEquity: BN, vault: Vault, vaultProtocol?: VaultProtocol): BN;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.calculateApplyProfitShare = calculateApplyProfitShare;
4
4
  exports.calculateProfitShare = calculateProfitShare;
5
+ exports.calculateRealizedVaultDepositorEquity = calculateRealizedVaultDepositorEquity;
5
6
  const sdk_1 = require("@drift-labs/sdk");
6
7
  /**
7
8
  * Calculates the unrealized profitShare for a vaultDepositor
@@ -19,13 +20,30 @@ function calculateApplyProfitShare(vaultDepositor, vaultEquity, vault) {
19
20
  profitShareShares,
20
21
  };
21
22
  }
22
- function calculateProfitShare(vaultDepositor, totalAmount, vault) {
23
+ function calculateProfitShare(vaultDepositor, totalAmount, vault, vaultProtocol) {
23
24
  const profit = totalAmount.sub(vaultDepositor.netDeposits.add(vaultDepositor.cumulativeProfitShareAmount));
25
+ let profitShare = vault.profitShare;
26
+ if (vaultProtocol) {
27
+ profitShare += vaultProtocol.protocolProfitShare;
28
+ }
24
29
  if (profit.gt(sdk_1.ZERO)) {
25
30
  const profitShareAmount = profit
26
- .mul(new sdk_1.BN(vault.profitShare))
31
+ .mul(new sdk_1.BN(profitShare))
27
32
  .div(sdk_1.PERCENTAGE_PRECISION);
28
33
  return profitShareAmount;
29
34
  }
30
35
  return sdk_1.ZERO;
31
36
  }
37
+ /**
38
+ * Calculates the equity across deposits and realized profit for a vaultDepositor
39
+ * @param vaultDepositor vault depositor account
40
+ * @param vaultEquity total vault equity
41
+ * @param vault vault account
42
+ * @param vaultProtocol if vault account has "vaultProtocol" then this is needed
43
+ * @returns
44
+ */
45
+ function calculateRealizedVaultDepositorEquity(vaultDepositor, vaultEquity, vault, vaultProtocol) {
46
+ const vdAmount = (0, sdk_1.unstakeSharesToAmount)(vaultDepositor.vaultShares, vault.totalShares, vaultEquity);
47
+ const profitShareAmount = calculateProfitShare(vaultDepositor, vdAmount, vault, vaultProtocol);
48
+ return vdAmount.sub(profitShareAmount);
49
+ }