@velocity-exchange/vaults-sdk 0.0.1
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/.env.example +3 -0
- package/README.md +152 -0
- package/cli/cli.ts +751 -0
- package/cli/commands/adminDeleteFeeUpdate.ts +73 -0
- package/cli/commands/adminInitFeeUpdate.ts +73 -0
- package/cli/commands/adminUpdateVaultClass.ts +49 -0
- package/cli/commands/applyProfitShare.ts +139 -0
- package/cli/commands/decodeLogs.ts +98 -0
- package/cli/commands/deposit.ts +98 -0
- package/cli/commands/deriveVaultAddress.ts +14 -0
- package/cli/commands/forceWithdraw.ts +56 -0
- package/cli/commands/forceWithdrawAll.ts +142 -0
- package/cli/commands/index.ts +28 -0
- package/cli/commands/initVault.ts +227 -0
- package/cli/commands/initVaultDepositor.ts +42 -0
- package/cli/commands/listDepositorsForVault.ts +32 -0
- package/cli/commands/managerApplyProfitShare.ts +32 -0
- package/cli/commands/managerBorrow.ts +77 -0
- package/cli/commands/managerCancelWithdraw.ts +30 -0
- package/cli/commands/managerDeposit.ts +45 -0
- package/cli/commands/managerRepay.ts +94 -0
- package/cli/commands/managerRequestWithdraw.ts +86 -0
- package/cli/commands/managerUpdateBorrow.ts +56 -0
- package/cli/commands/managerUpdateFees.ts +156 -0
- package/cli/commands/managerUpdateMarginTradingEnabled.ts +32 -0
- package/cli/commands/managerUpdatePoolId.ts +36 -0
- package/cli/commands/managerUpdateVault.ts +210 -0
- package/cli/commands/managerUpdateVaultDelegate.ts +43 -0
- package/cli/commands/managerUpdateVaultManager.ts +77 -0
- package/cli/commands/managerWithdraw.ts +30 -0
- package/cli/commands/requestWithdraw.ts +58 -0
- package/cli/commands/vaultDeposit.ts +42 -0
- package/cli/commands/vaultInvariantChecks.ts +407 -0
- package/cli/commands/vaultWithdraw.ts +42 -0
- package/cli/commands/viewVault.ts +50 -0
- package/cli/commands/viewVaultDepositor.ts +36 -0
- package/cli/commands/withdraw.ts +40 -0
- package/cli/ledgerWallet.test.ts +49 -0
- package/cli/ledgerWallet.ts +111 -0
- package/cli/utils.ts +389 -0
- package/package.json +48 -0
- package/src/accountSubscribers/index.ts +2 -0
- package/src/accountSubscribers/pollingVaultDepositorSubscriber.ts +69 -0
- package/src/accountSubscribers/pollingVaultSubscriber.ts +63 -0
- package/src/accountSubscribers/pollingVaultsProgramAccountSubscriber.ts +114 -0
- package/src/accounts/index.ts +2 -0
- package/src/accounts/vaultAccount.ts +255 -0
- package/src/accounts/vaultDepositorAccount.ts +77 -0
- package/src/accounts/vaultsProgramAccount.ts +38 -0
- package/src/addresses.ts +114 -0
- package/src/constants/index.ts +15 -0
- package/src/idl/drift_vaults.json +5698 -0
- package/src/index.ts +11 -0
- package/src/math/index.ts +2 -0
- package/src/math/vault.ts +71 -0
- package/src/math/vaultDepositor.ts +90 -0
- package/src/name.ts +18 -0
- package/src/parsers/index.ts +1 -0
- package/src/parsers/logParser.ts +28 -0
- package/src/types/drift_vaults.ts +6211 -0
- package/src/types/types.ts +336 -0
- package/src/utils.ts +74 -0
- package/src/vaultClient.ts +3666 -0
- package/tsconfig.json +24 -0
- package/velocity-exchange-vaults-sdk-0.0.1.tgz +0 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './addresses';
|
|
2
|
+
export * from './name';
|
|
3
|
+
export * from './vaultClient';
|
|
4
|
+
export * from './accounts';
|
|
5
|
+
export * from './accountSubscribers';
|
|
6
|
+
export * from './utils';
|
|
7
|
+
export * from './types/types';
|
|
8
|
+
export * from './constants';
|
|
9
|
+
export * from './parsers';
|
|
10
|
+
export * from './types/drift_vaults';
|
|
11
|
+
export * from './math';
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { BN, BigNum } from '@velocity-exchange/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
|
+
startingMarketValue = 0
|
|
24
|
+
): { apy: number; returns: number } => {
|
|
25
|
+
if (vaultDeposits.length === 0) {
|
|
26
|
+
return DEFAULT_MODIFIED_DIETZ_RESULT;
|
|
27
|
+
}
|
|
28
|
+
|
|
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
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BN,
|
|
3
|
+
PERCENTAGE_PRECISION,
|
|
4
|
+
ZERO,
|
|
5
|
+
unstakeSharesToAmount as depositSharesToVaultAmount,
|
|
6
|
+
stakeAmountToShares as vaultAmountToDepositorShares,
|
|
7
|
+
} from '@velocity-exchange/sdk';
|
|
8
|
+
import { Vault, VaultDepositor, VaultProtocol } from '../types/types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Calculates the unrealized profitShare for a vaultDepositor
|
|
12
|
+
* @param vaultDepositor
|
|
13
|
+
* @param vaultEquity
|
|
14
|
+
* @param vault
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
export function calculateApplyProfitShare(
|
|
18
|
+
vaultDepositor: VaultDepositor,
|
|
19
|
+
vaultEquity: BN,
|
|
20
|
+
vault: Vault
|
|
21
|
+
): {
|
|
22
|
+
profitShareAmount: BN;
|
|
23
|
+
profitShareShares: BN;
|
|
24
|
+
} {
|
|
25
|
+
const amount = depositSharesToVaultAmount(
|
|
26
|
+
vaultDepositor.vaultShares,
|
|
27
|
+
vault.totalShares,
|
|
28
|
+
vaultEquity
|
|
29
|
+
);
|
|
30
|
+
const profitShareAmount = calculateProfitShare(vaultDepositor, amount, vault);
|
|
31
|
+
const profitShareShares = vaultAmountToDepositorShares(
|
|
32
|
+
profitShareAmount,
|
|
33
|
+
vault.totalShares,
|
|
34
|
+
vaultEquity
|
|
35
|
+
);
|
|
36
|
+
return {
|
|
37
|
+
profitShareAmount,
|
|
38
|
+
profitShareShares,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function calculateProfitShare(
|
|
43
|
+
vaultDepositor: VaultDepositor,
|
|
44
|
+
totalAmount: BN,
|
|
45
|
+
vault: Vault,
|
|
46
|
+
vaultProtocol?: VaultProtocol
|
|
47
|
+
) {
|
|
48
|
+
const profit = totalAmount.sub(
|
|
49
|
+
vaultDepositor.netDeposits.add(vaultDepositor.cumulativeProfitShareAmount)
|
|
50
|
+
);
|
|
51
|
+
let profitShare = vault.profitShare;
|
|
52
|
+
if (vaultProtocol) {
|
|
53
|
+
profitShare += vaultProtocol.protocolProfitShare;
|
|
54
|
+
}
|
|
55
|
+
if (profit.gt(ZERO)) {
|
|
56
|
+
const profitShareAmount = profit
|
|
57
|
+
.mul(new BN(profitShare))
|
|
58
|
+
.div(PERCENTAGE_PRECISION);
|
|
59
|
+
return profitShareAmount;
|
|
60
|
+
}
|
|
61
|
+
return ZERO;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Calculates the equity across deposits and realized profit for a vaultDepositor
|
|
66
|
+
* @param vaultDepositor vault depositor account
|
|
67
|
+
* @param vaultEquity total vault equity
|
|
68
|
+
* @param vault vault account
|
|
69
|
+
* @param vaultProtocol if vault account has "vaultProtocol" then this is needed
|
|
70
|
+
* @returns
|
|
71
|
+
*/
|
|
72
|
+
export function calculateRealizedVaultDepositorEquity(
|
|
73
|
+
vaultDepositor: VaultDepositor,
|
|
74
|
+
vaultEquity: BN,
|
|
75
|
+
vault: Vault,
|
|
76
|
+
vaultProtocol?: VaultProtocol
|
|
77
|
+
): BN {
|
|
78
|
+
const vdAmount = depositSharesToVaultAmount(
|
|
79
|
+
vaultDepositor.vaultShares,
|
|
80
|
+
vault.totalShares,
|
|
81
|
+
vaultEquity
|
|
82
|
+
);
|
|
83
|
+
const profitShareAmount = calculateProfitShare(
|
|
84
|
+
vaultDepositor,
|
|
85
|
+
vdAmount,
|
|
86
|
+
vault,
|
|
87
|
+
vaultProtocol
|
|
88
|
+
);
|
|
89
|
+
return vdAmount.sub(profitShareAmount);
|
|
90
|
+
}
|
package/src/name.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const MAX_NAME_LENGTH = 32;
|
|
2
|
+
|
|
3
|
+
export function encodeName(name: string): number[] {
|
|
4
|
+
if (name.length > MAX_NAME_LENGTH) {
|
|
5
|
+
throw Error(`Name (${name}) longer than 32 characters`);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const buffer = Buffer.alloc(32);
|
|
9
|
+
buffer.fill(name);
|
|
10
|
+
buffer.fill(' ', name.length);
|
|
11
|
+
|
|
12
|
+
return Array(...buffer);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function decodeName(bytes: number[]): string {
|
|
16
|
+
const buffer = Buffer.from(bytes);
|
|
17
|
+
return buffer.toString('utf8').trim();
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './logParser';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Program } from '@coral-xyz/anchor';
|
|
2
|
+
import { TransactionSignature } from '@solana/web3.js';
|
|
3
|
+
import { WrappedEvents } from '../types/types';
|
|
4
|
+
|
|
5
|
+
type Log = { txSig: TransactionSignature; slot: number; logs: string[] };
|
|
6
|
+
|
|
7
|
+
export class LogParser {
|
|
8
|
+
constructor(private program: Program) {}
|
|
9
|
+
|
|
10
|
+
public parseEventsFromLogs(event: Log): WrappedEvents {
|
|
11
|
+
const records: WrappedEvents = [];
|
|
12
|
+
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
const eventGenerator = this.program._events._eventParser.parseLogs(
|
|
15
|
+
event.logs,
|
|
16
|
+
false
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
for (const eventLog of eventGenerator) {
|
|
20
|
+
eventLog.data.txSig = event.txSig;
|
|
21
|
+
eventLog.data.slot = event.slot;
|
|
22
|
+
eventLog.data.eventType = eventLog.name;
|
|
23
|
+
records.push(eventLog.data);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return records;
|
|
27
|
+
}
|
|
28
|
+
}
|