@drift-labs/vaults-sdk 0.1.113 → 0.1.114
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.
- package/bun.lockb +0 -0
- package/cli/commands/applyProfitShare.ts +1 -1
- package/lib/math/index.d.ts +1 -0
- package/lib/math/index.js +1 -0
- package/lib/math/vault.d.ts +16 -0
- package/lib/math/vault.js +57 -0
- package/lib/vaultClient.d.ts +5 -3
- package/lib/vaultClient.js +16 -7
- package/package.json +1 -1
- package/src/math/index.ts +1 -0
- package/src/math/vault.ts +71 -0
- package/src/vaultClient.ts +22 -3
package/bun.lockb
CHANGED
|
Binary file
|
|
@@ -22,7 +22,7 @@ export const applyProfitShare = async (program: Command, cmdOpts: OptionValues)
|
|
|
22
22
|
const vdToRealizeProfit = await driftVault.getAllVaultDepositorsWithNoWithdrawRequest(vaultAddress);
|
|
23
23
|
console.log(`Applying profit share for ${vdToRealizeProfit.length} depositors...`);
|
|
24
24
|
|
|
25
|
-
const chunkSize =
|
|
25
|
+
const chunkSize = 6;
|
|
26
26
|
const ixChunks: Array<Array<TransactionInstruction>> = [];
|
|
27
27
|
for (let i = 0; i < vdToRealizeProfit.length; i += chunkSize) {
|
|
28
28
|
const chunk = vdToRealizeProfit.slice(i, i + chunkSize);
|
package/lib/math/index.d.ts
CHANGED
package/lib/math/index.js
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { BN, BigNum } from '@drift-labs/sdk';
|
|
2
|
+
/**
|
|
3
|
+
* https://en.wikipedia.org/wiki/Modified_Dietz_method
|
|
4
|
+
* @param currentVaultEquityBaseValue
|
|
5
|
+
* @param vaultDeposits
|
|
6
|
+
* @returns weighted APY and cumulative returns calculated using the Modified Dietz method
|
|
7
|
+
*/
|
|
8
|
+
export declare const calcModifiedDietz: (currentVaultEquityBaseValue: BigNum, precisionExp: BN, vaultDeposits: {
|
|
9
|
+
ts: string;
|
|
10
|
+
marketIndex: number;
|
|
11
|
+
amount: string;
|
|
12
|
+
direction: 'deposit' | 'withdraw';
|
|
13
|
+
}[]) => {
|
|
14
|
+
apy: number;
|
|
15
|
+
returns: number;
|
|
16
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calcModifiedDietz = void 0;
|
|
4
|
+
const sdk_1 = require("@drift-labs/sdk");
|
|
5
|
+
const DEFAULT_MODIFIED_DIETZ_RESULT = {
|
|
6
|
+
apy: 0,
|
|
7
|
+
returns: 0,
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* https://en.wikipedia.org/wiki/Modified_Dietz_method
|
|
11
|
+
* @param currentVaultEquityBaseValue
|
|
12
|
+
* @param vaultDeposits
|
|
13
|
+
* @returns weighted APY and cumulative returns calculated using the Modified Dietz method
|
|
14
|
+
*/
|
|
15
|
+
const calcModifiedDietz = (currentVaultEquityBaseValue, precisionExp, vaultDeposits) => {
|
|
16
|
+
if (vaultDeposits.length === 0) {
|
|
17
|
+
return DEFAULT_MODIFIED_DIETZ_RESULT;
|
|
18
|
+
}
|
|
19
|
+
const startingMarketValue = 0;
|
|
20
|
+
const endingMarkeValue = currentVaultEquityBaseValue.toNum();
|
|
21
|
+
const firstDepositTs = parseInt(vaultDeposits[vaultDeposits.length - 1].ts);
|
|
22
|
+
const lastDepositTs = parseInt(vaultDeposits[0].ts);
|
|
23
|
+
const nowTs = Date.now() / 1000;
|
|
24
|
+
if (nowTs < firstDepositTs) {
|
|
25
|
+
console.error('nowTs < firstDepositTs');
|
|
26
|
+
return DEFAULT_MODIFIED_DIETZ_RESULT;
|
|
27
|
+
}
|
|
28
|
+
if (lastDepositTs < firstDepositTs) {
|
|
29
|
+
console.error('lastDepositTs < firstDepositTs');
|
|
30
|
+
return DEFAULT_MODIFIED_DIETZ_RESULT;
|
|
31
|
+
}
|
|
32
|
+
const totalDuration = nowTs - firstDepositTs;
|
|
33
|
+
let totalNetFlow = 0;
|
|
34
|
+
let weightedNetFlow = 0;
|
|
35
|
+
vaultDeposits.forEach((deposit) => {
|
|
36
|
+
let depositAmount = sdk_1.BigNum.from(deposit.amount, precisionExp).toNum();
|
|
37
|
+
if (deposit.direction === 'withdraw') {
|
|
38
|
+
depositAmount *= -1;
|
|
39
|
+
}
|
|
40
|
+
totalNetFlow += depositAmount;
|
|
41
|
+
const depositAge = parseInt(deposit.ts) - firstDepositTs;
|
|
42
|
+
const depositWeight = (totalDuration - depositAge) / totalDuration;
|
|
43
|
+
if (depositWeight < 0) {
|
|
44
|
+
console.error('depositWeight < 0');
|
|
45
|
+
return -1;
|
|
46
|
+
}
|
|
47
|
+
weightedNetFlow += depositWeight * depositAmount;
|
|
48
|
+
}, 0);
|
|
49
|
+
const modifiedDietzReturns = (endingMarkeValue - startingMarketValue - totalNetFlow) /
|
|
50
|
+
(startingMarketValue + weightedNetFlow);
|
|
51
|
+
if (modifiedDietzReturns < 0)
|
|
52
|
+
return DEFAULT_MODIFIED_DIETZ_RESULT;
|
|
53
|
+
const annualized = Math.pow(1 + modifiedDietzReturns, (86400 * 365) / totalDuration) - 1;
|
|
54
|
+
const positiveApy = Math.max(annualized, 0);
|
|
55
|
+
return { apy: positiveApy, returns: modifiedDietzReturns };
|
|
56
|
+
};
|
|
57
|
+
exports.calcModifiedDietz = calcModifiedDietz;
|
package/lib/vaultClient.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
/// <reference types="@coral-xyz/anchor/node_modules/@solana/web3.js" />
|
|
3
3
|
/// <reference types="@pythnetwork/client/node_modules/@solana/web3.js" />
|
|
4
|
-
import { BN, DriftClient, User } from '@drift-labs/sdk';
|
|
4
|
+
import { BN, DriftClient, User, UserMap } from '@drift-labs/sdk';
|
|
5
5
|
import { Program, ProgramAccount } from '@coral-xyz/anchor';
|
|
6
6
|
import { DriftVaults } from './types/drift_vaults';
|
|
7
7
|
import { CompetitionsClient } from '@drift-labs/competitions-sdk';
|
|
8
8
|
import { PublicKey, SetComputeUnitLimitParams, TransactionInstruction, TransactionSignature } from '@solana/web3.js';
|
|
9
9
|
import { Vault, VaultDepositor, WithdrawUnit } from './types/types';
|
|
10
|
+
import { UserMapConfig } from '@drift-labs/sdk/lib/userMap/userMapConfig';
|
|
10
11
|
export declare class VaultClient {
|
|
11
12
|
driftClient: DriftClient;
|
|
12
13
|
program: Program<DriftVaults>;
|
|
@@ -14,11 +15,12 @@ export declare class VaultClient {
|
|
|
14
15
|
/**
|
|
15
16
|
* Cache map of drift user accounts of vaults.
|
|
16
17
|
*/
|
|
17
|
-
readonly vaultUsers:
|
|
18
|
-
constructor({ driftClient, program, cliMode, }: {
|
|
18
|
+
readonly vaultUsers: UserMap;
|
|
19
|
+
constructor({ driftClient, program, cliMode, userMapConfig, }: {
|
|
19
20
|
driftClient: DriftClient;
|
|
20
21
|
program: Program<DriftVaults>;
|
|
21
22
|
cliMode?: boolean;
|
|
23
|
+
userMapConfig?: UserMapConfig;
|
|
22
24
|
});
|
|
23
25
|
getVault(vault: PublicKey): Promise<Vault>;
|
|
24
26
|
getVaultDepositor(vaultDepositor: PublicKey): Promise<any>;
|
package/lib/vaultClient.js
CHANGED
|
@@ -9,14 +9,23 @@ const web3_js_1 = require("@solana/web3.js");
|
|
|
9
9
|
const spl_token_1 = require("@solana/spl-token");
|
|
10
10
|
const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
|
|
11
11
|
class VaultClient {
|
|
12
|
-
constructor({ driftClient, program, cliMode, }) {
|
|
13
|
-
/**
|
|
14
|
-
* Cache map of drift user accounts of vaults.
|
|
15
|
-
*/
|
|
16
|
-
this.vaultUsers = new Map();
|
|
12
|
+
constructor({ driftClient, program, cliMode, userMapConfig, }) {
|
|
17
13
|
this.driftClient = driftClient;
|
|
18
14
|
this.program = program;
|
|
19
15
|
this.cliMode = !!cliMode;
|
|
16
|
+
if (!userMapConfig) {
|
|
17
|
+
this.vaultUsers = new sdk_1.UserMap({
|
|
18
|
+
driftClient: driftClient,
|
|
19
|
+
subscriptionConfig: {
|
|
20
|
+
type: 'polling',
|
|
21
|
+
frequency: 1000,
|
|
22
|
+
commitment: 'processed',
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
this.vaultUsers = new sdk_1.UserMap(userMapConfig);
|
|
28
|
+
}
|
|
20
29
|
}
|
|
21
30
|
async getVault(vault) {
|
|
22
31
|
// @ts-ignore
|
|
@@ -79,8 +88,7 @@ class VaultClient {
|
|
|
79
88
|
driftClient: this.driftClient,
|
|
80
89
|
userAccountPublicKey: vaultDriftUserAccountPubKey,
|
|
81
90
|
});
|
|
82
|
-
await
|
|
83
|
-
this.vaultUsers.set(vaultDriftUserAccountPubKey.toBase58(), vaultUser);
|
|
91
|
+
await this.vaultUsers.addPubkey(vaultDriftUserAccountPubKey);
|
|
84
92
|
}
|
|
85
93
|
if (!(vaultUser === null || vaultUser === void 0 ? void 0 : vaultUser.isSubscribed)) {
|
|
86
94
|
await vaultUser.subscribe();
|
|
@@ -666,6 +674,7 @@ class VaultClient {
|
|
|
666
674
|
tx.add(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit(computeUnitParams || {
|
|
667
675
|
units: 400000,
|
|
668
676
|
}));
|
|
677
|
+
tx.add(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 1000000 }));
|
|
669
678
|
tx.add(...ixs);
|
|
670
679
|
const { txSig } = await this.driftClient.sendTransaction(tx, [], this.driftClient.opts);
|
|
671
680
|
return txSig;
|
package/package.json
CHANGED
package/src/math/index.ts
CHANGED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { BN, BigNum } from '@drift-labs/sdk';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_MODIFIED_DIETZ_RESULT = {
|
|
4
|
+
apy: 0,
|
|
5
|
+
returns: 0,
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* https://en.wikipedia.org/wiki/Modified_Dietz_method
|
|
10
|
+
* @param currentVaultEquityBaseValue
|
|
11
|
+
* @param vaultDeposits
|
|
12
|
+
* @returns weighted APY and cumulative returns calculated using the Modified Dietz method
|
|
13
|
+
*/
|
|
14
|
+
export const calcModifiedDietz = (
|
|
15
|
+
currentVaultEquityBaseValue: BigNum,
|
|
16
|
+
precisionExp: BN,
|
|
17
|
+
vaultDeposits: {
|
|
18
|
+
ts: string;
|
|
19
|
+
marketIndex: number;
|
|
20
|
+
amount: string;
|
|
21
|
+
direction: 'deposit' | 'withdraw';
|
|
22
|
+
}[]
|
|
23
|
+
): { apy: number; returns: number } => {
|
|
24
|
+
if (vaultDeposits.length === 0) {
|
|
25
|
+
return DEFAULT_MODIFIED_DIETZ_RESULT;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const startingMarketValue = 0;
|
|
29
|
+
const endingMarkeValue = currentVaultEquityBaseValue.toNum();
|
|
30
|
+
const firstDepositTs = parseInt(vaultDeposits[vaultDeposits.length - 1].ts);
|
|
31
|
+
const lastDepositTs = parseInt(vaultDeposits[0].ts);
|
|
32
|
+
const nowTs = Date.now() / 1000;
|
|
33
|
+
if (nowTs < firstDepositTs) {
|
|
34
|
+
console.error('nowTs < firstDepositTs');
|
|
35
|
+
return DEFAULT_MODIFIED_DIETZ_RESULT;
|
|
36
|
+
}
|
|
37
|
+
if (lastDepositTs < firstDepositTs) {
|
|
38
|
+
console.error('lastDepositTs < firstDepositTs');
|
|
39
|
+
return DEFAULT_MODIFIED_DIETZ_RESULT;
|
|
40
|
+
}
|
|
41
|
+
const totalDuration = nowTs - firstDepositTs;
|
|
42
|
+
|
|
43
|
+
let totalNetFlow = 0;
|
|
44
|
+
let weightedNetFlow = 0;
|
|
45
|
+
vaultDeposits.forEach((deposit) => {
|
|
46
|
+
let depositAmount = BigNum.from(deposit.amount, precisionExp).toNum();
|
|
47
|
+
if (deposit.direction === 'withdraw') {
|
|
48
|
+
depositAmount *= -1;
|
|
49
|
+
}
|
|
50
|
+
totalNetFlow += depositAmount;
|
|
51
|
+
const depositAge = parseInt(deposit.ts) - firstDepositTs;
|
|
52
|
+
const depositWeight = (totalDuration - depositAge) / totalDuration;
|
|
53
|
+
if (depositWeight < 0) {
|
|
54
|
+
console.error('depositWeight < 0');
|
|
55
|
+
return -1;
|
|
56
|
+
}
|
|
57
|
+
weightedNetFlow += depositWeight * depositAmount;
|
|
58
|
+
}, 0);
|
|
59
|
+
|
|
60
|
+
const modifiedDietzReturns =
|
|
61
|
+
(endingMarkeValue - startingMarketValue - totalNetFlow) /
|
|
62
|
+
(startingMarketValue + weightedNetFlow);
|
|
63
|
+
|
|
64
|
+
if (modifiedDietzReturns < 0) return DEFAULT_MODIFIED_DIETZ_RESULT;
|
|
65
|
+
|
|
66
|
+
const annualized =
|
|
67
|
+
Math.pow(1 + modifiedDietzReturns, (86400 * 365) / totalDuration) - 1;
|
|
68
|
+
|
|
69
|
+
const positiveApy = Math.max(annualized, 0);
|
|
70
|
+
return { apy: positiveApy, returns: modifiedDietzReturns };
|
|
71
|
+
};
|
package/src/vaultClient.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
getUserAccountPublicKeySync,
|
|
8
8
|
getUserStatsAccountPublicKey,
|
|
9
9
|
User,
|
|
10
|
+
UserMap,
|
|
10
11
|
} from '@drift-labs/sdk';
|
|
11
12
|
import { BorshAccountsCoder, Program, ProgramAccount } from '@coral-xyz/anchor';
|
|
12
13
|
import { DriftVaults } from './types/drift_vaults';
|
|
@@ -36,6 +37,7 @@ import {
|
|
|
36
37
|
} from '@solana/spl-token';
|
|
37
38
|
import { Vault, VaultDepositor, WithdrawUnit } from './types/types';
|
|
38
39
|
import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes';
|
|
40
|
+
import { UserMapConfig } from '@drift-labs/sdk/lib/userMap/userMapConfig';
|
|
39
41
|
|
|
40
42
|
export class VaultClient {
|
|
41
43
|
driftClient: DriftClient;
|
|
@@ -45,20 +47,35 @@ export class VaultClient {
|
|
|
45
47
|
/**
|
|
46
48
|
* Cache map of drift user accounts of vaults.
|
|
47
49
|
*/
|
|
48
|
-
readonly vaultUsers:
|
|
50
|
+
readonly vaultUsers: UserMap;
|
|
49
51
|
|
|
50
52
|
constructor({
|
|
51
53
|
driftClient,
|
|
52
54
|
program,
|
|
53
55
|
cliMode,
|
|
56
|
+
userMapConfig,
|
|
54
57
|
}: {
|
|
55
58
|
driftClient: DriftClient;
|
|
56
59
|
program: Program<DriftVaults>;
|
|
57
60
|
cliMode?: boolean;
|
|
61
|
+
userMapConfig?: UserMapConfig;
|
|
58
62
|
}) {
|
|
59
63
|
this.driftClient = driftClient;
|
|
60
64
|
this.program = program;
|
|
61
65
|
this.cliMode = !!cliMode;
|
|
66
|
+
|
|
67
|
+
if (!userMapConfig) {
|
|
68
|
+
this.vaultUsers = new UserMap({
|
|
69
|
+
driftClient: driftClient,
|
|
70
|
+
subscriptionConfig: {
|
|
71
|
+
type: 'polling',
|
|
72
|
+
frequency: 1000,
|
|
73
|
+
commitment: 'processed',
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
} else {
|
|
77
|
+
this.vaultUsers = new UserMap(userMapConfig);
|
|
78
|
+
}
|
|
62
79
|
}
|
|
63
80
|
|
|
64
81
|
public async getVault(vault: PublicKey): Promise<Vault> {
|
|
@@ -139,8 +156,7 @@ export class VaultClient {
|
|
|
139
156
|
driftClient: this.driftClient,
|
|
140
157
|
userAccountPublicKey: vaultDriftUserAccountPubKey,
|
|
141
158
|
});
|
|
142
|
-
await
|
|
143
|
-
this.vaultUsers.set(vaultDriftUserAccountPubKey.toBase58(), vaultUser);
|
|
159
|
+
await this.vaultUsers.addPubkey(vaultDriftUserAccountPubKey);
|
|
144
160
|
}
|
|
145
161
|
|
|
146
162
|
if (!vaultUser?.isSubscribed) {
|
|
@@ -1022,6 +1038,9 @@ export class VaultClient {
|
|
|
1022
1038
|
}
|
|
1023
1039
|
)
|
|
1024
1040
|
);
|
|
1041
|
+
tx.add(
|
|
1042
|
+
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 1_000_000 })
|
|
1043
|
+
);
|
|
1025
1044
|
tx.add(...ixs);
|
|
1026
1045
|
const { txSig } = await this.driftClient.sendTransaction(
|
|
1027
1046
|
tx,
|