@velocity-exchange/vaults-sdk 0.0.1 → 0.0.2

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.
Files changed (66) hide show
  1. package/lib/accountSubscribers/index.d.ts +3 -0
  2. package/lib/accountSubscribers/index.d.ts.map +1 -0
  3. package/lib/accountSubscribers/index.js +18 -0
  4. package/lib/accountSubscribers/pollingVaultDepositorSubscriber.d.ts +8 -0
  5. package/lib/accountSubscribers/pollingVaultDepositorSubscriber.d.ts.map +1 -0
  6. package/lib/accountSubscribers/pollingVaultDepositorSubscriber.js +46 -0
  7. package/lib/accountSubscribers/pollingVaultSubscriber.d.ts +8 -0
  8. package/lib/accountSubscribers/pollingVaultSubscriber.d.ts.map +1 -0
  9. package/lib/accountSubscribers/pollingVaultSubscriber.js +46 -0
  10. package/lib/accountSubscribers/pollingVaultsProgramAccountSubscriber.d.ts +28 -0
  11. package/lib/accountSubscribers/pollingVaultsProgramAccountSubscriber.d.ts.map +1 -0
  12. package/lib/accountSubscribers/pollingVaultsProgramAccountSubscriber.js +71 -0
  13. package/lib/accounts/index.d.ts +3 -0
  14. package/lib/accounts/index.d.ts.map +1 -0
  15. package/lib/accounts/index.js +18 -0
  16. package/lib/accounts/vaultAccount.d.ts +27 -0
  17. package/lib/accounts/vaultAccount.d.ts.map +1 -0
  18. package/lib/accounts/vaultAccount.js +174 -0
  19. package/lib/accounts/vaultDepositorAccount.d.ts +18 -0
  20. package/lib/accounts/vaultDepositorAccount.d.ts.map +1 -0
  21. package/lib/accounts/vaultDepositorAccount.js +47 -0
  22. package/lib/accounts/vaultsProgramAccount.d.ts +13 -0
  23. package/lib/accounts/vaultsProgramAccount.d.ts.map +1 -0
  24. package/lib/accounts/vaultsProgramAccount.js +25 -0
  25. package/lib/addresses.d.ts +10 -0
  26. package/lib/addresses.d.ts.map +1 -0
  27. package/lib/addresses.js +87 -0
  28. package/lib/constants/index.d.ts +8 -0
  29. package/lib/constants/index.d.ts.map +1 -0
  30. package/lib/constants/index.js +12 -0
  31. package/lib/idl/drift_vaults.json +5698 -0
  32. package/lib/index.d.ts +12 -0
  33. package/lib/index.d.ts.map +1 -0
  34. package/lib/index.js +27 -0
  35. package/lib/math/index.d.ts +3 -0
  36. package/lib/math/index.d.ts.map +1 -0
  37. package/lib/math/index.js +18 -0
  38. package/lib/math/vault.d.ts +17 -0
  39. package/lib/math/vault.d.ts.map +1 -0
  40. package/lib/math/vault.js +56 -0
  41. package/lib/math/vaultDepositor.d.ts +24 -0
  42. package/lib/math/vaultDepositor.d.ts.map +1 -0
  43. package/lib/math/vaultDepositor.js +49 -0
  44. package/lib/name.d.ts +4 -0
  45. package/lib/name.d.ts.map +1 -0
  46. package/lib/name.js +19 -0
  47. package/lib/parsers/index.d.ts +2 -0
  48. package/lib/parsers/index.d.ts.map +1 -0
  49. package/lib/parsers/index.js +17 -0
  50. package/lib/parsers/logParser.d.ts +15 -0
  51. package/lib/parsers/logParser.d.ts.map +1 -0
  52. package/lib/parsers/logParser.js +21 -0
  53. package/lib/types/drift_vaults.d.ts +6212 -0
  54. package/lib/types/drift_vaults.d.ts.map +1 -0
  55. package/lib/types/drift_vaults.js +2 -0
  56. package/lib/types/types.d.ts +283 -0
  57. package/lib/types/types.d.ts.map +1 -0
  58. package/lib/types/types.js +48 -0
  59. package/lib/utils.d.ts +10 -0
  60. package/lib/utils.d.ts.map +1 -0
  61. package/lib/utils.js +69 -0
  62. package/lib/vaultClient.d.ts +446 -0
  63. package/lib/vaultClient.d.ts.map +1 -0
  64. package/lib/vaultClient.js +1914 -0
  65. package/package.json +1 -1
  66. package/velocity-exchange-vaults-sdk-0.0.1.tgz +0 -0
@@ -0,0 +1,3 @@
1
+ export * from './pollingVaultDepositorSubscriber';
2
+ export * from './pollingVaultSubscriber';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/accountSubscribers/index.ts"],"names":[],"mappings":"AAAA,cAAc,mCAAmC,CAAC;AAClD,cAAc,0BAA0B,CAAC"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./pollingVaultDepositorSubscriber"), exports);
18
+ __exportStar(require("./pollingVaultSubscriber"), exports);
@@ -0,0 +1,8 @@
1
+ import { VaultDepositor, VaultDepositorAccountEvents, VaultDepositorAccountSubscriber } from '../types/types';
2
+ import { PollingVaultsProgramAccountSubscriber } from './pollingVaultsProgramAccountSubscriber';
3
+ export declare class PollingVaultDepositorSubscriber extends PollingVaultsProgramAccountSubscriber<VaultDepositor, VaultDepositorAccountEvents> implements VaultDepositorAccountSubscriber {
4
+ addToAccountLoader(): Promise<void>;
5
+ fetch(): Promise<void>;
6
+ updateData(vaultDepositorAcc: VaultDepositor, slot: number): void;
7
+ }
8
+ //# sourceMappingURL=pollingVaultDepositorSubscriber.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pollingVaultDepositorSubscriber.d.ts","sourceRoot":"","sources":["../../src/accountSubscribers/pollingVaultDepositorSubscriber.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,cAAc,EACd,2BAA2B,EAC3B,+BAA+B,EAC/B,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,qCAAqC,EAAE,MAAM,yCAAyC,CAAC;AAEhG,qBAAa,+BACZ,SAAQ,qCAAqC,CAC5C,cAAc,EACd,2BAA2B,CAE5B,YAAW,+BAA+B;IAEpC,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;IAiCnC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAc5B,UAAU,CAAC,iBAAiB,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;CAOjE"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PollingVaultDepositorSubscriber = void 0;
4
+ const pollingVaultsProgramAccountSubscriber_1 = require("./pollingVaultsProgramAccountSubscriber");
5
+ class PollingVaultDepositorSubscriber extends pollingVaultsProgramAccountSubscriber_1.PollingVaultsProgramAccountSubscriber {
6
+ async addToAccountLoader() {
7
+ if (this.callbackId) {
8
+ console.log('Account for vault depositor already added to account loader');
9
+ return;
10
+ }
11
+ this.callbackId = await this.accountLoader.addAccount(this.pubkey, (buffer, slot) => {
12
+ if (!buffer)
13
+ return;
14
+ if (this.account && this.account.slot > slot) {
15
+ return;
16
+ }
17
+ const account = this.program.account.vaultDepositor.coder.accounts.decode('vaultDepositor', buffer);
18
+ this.account = { data: account, slot };
19
+ this._eventEmitter.emit('vaultDepositorUpdate', account);
20
+ this._eventEmitter.emit('update');
21
+ });
22
+ this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
23
+ this._eventEmitter.emit('error', error);
24
+ });
25
+ }
26
+ async fetch() {
27
+ var _a, _b;
28
+ await this.accountLoader.load();
29
+ const bufferAndSlot = this.accountLoader.getBufferAndSlot(this.pubkey);
30
+ if (!bufferAndSlot)
31
+ return;
32
+ const currentSlot = (_b = (_a = this.account) === null || _a === void 0 ? void 0 : _a.slot) !== null && _b !== void 0 ? _b : 0;
33
+ if (bufferAndSlot.buffer && bufferAndSlot.slot > currentSlot) {
34
+ const account = this.program.account.vaultDepositor.coder.accounts.decode('vaultDepositor', bufferAndSlot.buffer);
35
+ this.account = { data: account, slot: bufferAndSlot.slot };
36
+ }
37
+ }
38
+ updateData(vaultDepositorAcc, slot) {
39
+ if (!this.account || this.account.slot < slot) {
40
+ this.account = { data: vaultDepositorAcc, slot };
41
+ this._eventEmitter.emit('vaultDepositorUpdate', vaultDepositorAcc);
42
+ this._eventEmitter.emit('update');
43
+ }
44
+ }
45
+ }
46
+ exports.PollingVaultDepositorSubscriber = PollingVaultDepositorSubscriber;
@@ -0,0 +1,8 @@
1
+ import { Vault, VaultAccountEvents, VaultAccountSubscriber } from '../types/types';
2
+ import { PollingVaultsProgramAccountSubscriber } from './pollingVaultsProgramAccountSubscriber';
3
+ export declare class PollingVaultSubscriber extends PollingVaultsProgramAccountSubscriber<Vault, VaultAccountEvents> implements VaultAccountSubscriber {
4
+ addToAccountLoader(): Promise<void>;
5
+ fetch(): Promise<void>;
6
+ updateData(vaultAcc: Vault, slot: number): void;
7
+ }
8
+ //# sourceMappingURL=pollingVaultSubscriber.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pollingVaultSubscriber.d.ts","sourceRoot":"","sources":["../../src/accountSubscribers/pollingVaultSubscriber.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,EACL,kBAAkB,EAClB,sBAAsB,EACtB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,qCAAqC,EAAE,MAAM,yCAAyC,CAAC;AAEhG,qBAAa,sBACZ,SAAQ,qCAAqC,CAAC,KAAK,EAAE,kBAAkB,CACvE,YAAW,sBAAsB;IAE3B,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;IA8BnC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAc5B,UAAU,CAAC,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;CAO/C"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PollingVaultSubscriber = void 0;
4
+ const pollingVaultsProgramAccountSubscriber_1 = require("./pollingVaultsProgramAccountSubscriber");
5
+ class PollingVaultSubscriber extends pollingVaultsProgramAccountSubscriber_1.PollingVaultsProgramAccountSubscriber {
6
+ async addToAccountLoader() {
7
+ if (this.callbackId) {
8
+ console.log('Account for vault already added to account loader');
9
+ return;
10
+ }
11
+ this.callbackId = await this.accountLoader.addAccount(this.pubkey, (buffer, slot) => {
12
+ if (!buffer)
13
+ return;
14
+ if (this.account && this.account.slot > slot) {
15
+ return;
16
+ }
17
+ const account = this.program.account.vault.coder.accounts.decode('vault', buffer);
18
+ this.account = { data: account, slot };
19
+ this._eventEmitter.emit('vaultUpdate', account);
20
+ this._eventEmitter.emit('update');
21
+ });
22
+ this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
23
+ this._eventEmitter.emit('error', error);
24
+ });
25
+ }
26
+ async fetch() {
27
+ var _a, _b;
28
+ await this.accountLoader.load();
29
+ const bufferAndSlot = this.accountLoader.getBufferAndSlot(this.pubkey);
30
+ if (!bufferAndSlot)
31
+ return;
32
+ const currentSlot = (_b = (_a = this.account) === null || _a === void 0 ? void 0 : _a.slot) !== null && _b !== void 0 ? _b : 0;
33
+ if (bufferAndSlot.buffer && bufferAndSlot.slot > currentSlot) {
34
+ const account = this.program.account.vault.coder.accounts.decode('vault', bufferAndSlot.buffer);
35
+ this.account = { data: account, slot: bufferAndSlot.slot };
36
+ }
37
+ }
38
+ updateData(vaultAcc, slot) {
39
+ if (!this.account || this.account.slot < slot) {
40
+ this.account = { data: vaultAcc, slot };
41
+ this._eventEmitter.emit('vaultUpdate', vaultAcc);
42
+ this._eventEmitter.emit('update');
43
+ }
44
+ }
45
+ }
46
+ exports.PollingVaultSubscriber = PollingVaultSubscriber;
@@ -0,0 +1,28 @@
1
+ import { BulkAccountLoader, DataAndSlot, PublicKey } from '@velocity-exchange/sdk';
2
+ import { Program } from '@coral-xyz/anchor';
3
+ import StrictEventEmitter from 'strict-event-emitter-types';
4
+ import { EventEmitter } from 'events';
5
+ import { DriftVaults } from '../types/drift_vaults';
6
+ import { VaultsProgramAccountBaseEvents, VaultsProgramAccountSubscriber } from '../types/types';
7
+ export declare abstract class PollingVaultsProgramAccountSubscriber<Account, AccountEvents extends VaultsProgramAccountBaseEvents> implements VaultsProgramAccountSubscriber<Account, AccountEvents> {
8
+ protected program: Program<DriftVaults>;
9
+ protected _isSubscribed: boolean;
10
+ protected pubkey: PublicKey;
11
+ protected account?: DataAndSlot<Account>;
12
+ protected _eventEmitter: StrictEventEmitter<EventEmitter, AccountEvents>;
13
+ protected accountLoader: BulkAccountLoader;
14
+ protected callbackId: string | null;
15
+ protected errorCallbackId: string | null;
16
+ constructor(program: Program<DriftVaults>, accountPubkey: PublicKey, accountLoader: BulkAccountLoader);
17
+ get isSubscribed(): boolean;
18
+ get eventEmitter(): StrictEventEmitter<EventEmitter, AccountEvents>;
19
+ subscribe(): Promise<boolean>;
20
+ unsubscribe(): Promise<void>;
21
+ fetchIfUnloaded(): Promise<void>;
22
+ assertIsSubscribed(): void;
23
+ getAccountAndSlot(): DataAndSlot<Account>;
24
+ abstract addToAccountLoader(): Promise<void>;
25
+ abstract fetch(): Promise<void>;
26
+ abstract updateData(account: Account, slot: number): void;
27
+ }
28
+ //# sourceMappingURL=pollingVaultsProgramAccountSubscriber.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pollingVaultsProgramAccountSubscriber.d.ts","sourceRoot":"","sources":["../../src/accountSubscribers/pollingVaultsProgramAccountSubscriber.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,iBAAiB,EACjB,WAAW,EAEX,SAAS,EACT,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,kBAAkB,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EACN,8BAA8B,EAC9B,8BAA8B,EAC9B,MAAM,gBAAgB,CAAC;AAExB,8BAAsB,qCAAqC,CAC1D,OAAO,EACP,aAAa,SAAS,8BAA8B,CACnD,YAAW,8BAA8B,CAAC,OAAO,EAAE,aAAa,CAAC;IAElE,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;IACxC,SAAS,CAAC,aAAa,EAAE,OAAO,CAAC;IACjC,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC;IAC5B,SAAS,CAAC,OAAO,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;IAEzC,SAAS,CAAC,aAAa,EAAE,kBAAkB,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC;IACzE,SAAS,CAAC,aAAa,EAAE,iBAAiB,CAAC;IAC3C,SAAS,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAQ;IAC3C,SAAS,CAAC,eAAe,EAAE,MAAM,GAAG,IAAI,CAAQ;gBAG/C,OAAO,EAAE,OAAO,CAAC,WAAW,CAAC,EAC7B,aAAa,EAAE,SAAS,EACxB,aAAa,EAAE,iBAAiB;IAUjC,IAAI,YAAY,IAAI,OAAO,CAE1B;IAED,IAAI,YAAY,IAAI,kBAAkB,CAAC,YAAY,EAAE,aAAa,CAAC,CAElE;IAEK,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAuB7B,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAc5B,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAMtC,kBAAkB,IAAI,IAAI;IAQ1B,iBAAiB,IAAI,WAAW,CAAC,OAAO,CAAC;IAQzC,QAAQ,CAAC,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;IAC5C,QAAQ,CAAC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAC/B,QAAQ,CAAC,UAAU,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;CACzD"}
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PollingVaultsProgramAccountSubscriber = void 0;
4
+ const sdk_1 = require("@velocity-exchange/sdk");
5
+ const events_1 = require("events");
6
+ class PollingVaultsProgramAccountSubscriber {
7
+ constructor(program, accountPubkey, accountLoader) {
8
+ this.callbackId = null;
9
+ this.errorCallbackId = null;
10
+ this.accountLoader = accountLoader;
11
+ this._isSubscribed = false;
12
+ this.pubkey = accountPubkey;
13
+ this.program = program;
14
+ // @ts-ignore
15
+ this._eventEmitter = new events_1.EventEmitter();
16
+ }
17
+ get isSubscribed() {
18
+ return this._isSubscribed;
19
+ }
20
+ get eventEmitter() {
21
+ return this._eventEmitter;
22
+ }
23
+ async subscribe() {
24
+ if (this._isSubscribed) {
25
+ return true;
26
+ }
27
+ try {
28
+ await this.addToAccountLoader();
29
+ await this.fetchIfUnloaded();
30
+ if (this.account) {
31
+ // @ts-ignore
32
+ this._eventEmitter.emit('update');
33
+ }
34
+ this._isSubscribed = true;
35
+ return true;
36
+ }
37
+ catch (err) {
38
+ console.error(err);
39
+ this._isSubscribed = false;
40
+ return false;
41
+ }
42
+ }
43
+ async unsubscribe() {
44
+ if (!this._isSubscribed) {
45
+ return;
46
+ }
47
+ this.accountLoader.removeAccount(this.pubkey, this.callbackId);
48
+ this.callbackId = null;
49
+ this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
50
+ this.errorCallbackId = null;
51
+ this._isSubscribed = false;
52
+ }
53
+ async fetchIfUnloaded() {
54
+ if (this.account === undefined) {
55
+ await this.fetch();
56
+ }
57
+ }
58
+ assertIsSubscribed() {
59
+ if (!this._isSubscribed) {
60
+ throw new sdk_1.NotSubscribedError('You must call `subscribe` before using this function');
61
+ }
62
+ }
63
+ getAccountAndSlot() {
64
+ this.assertIsSubscribed();
65
+ if (!this.account) {
66
+ throw new Error('Account not loaded');
67
+ }
68
+ return this.account;
69
+ }
70
+ }
71
+ exports.PollingVaultsProgramAccountSubscriber = PollingVaultsProgramAccountSubscriber;
@@ -0,0 +1,3 @@
1
+ export * from './vaultDepositorAccount';
2
+ export * from './vaultAccount';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/accounts/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./vaultDepositorAccount"), exports);
18
+ __exportStar(require("./vaultAccount"), exports);
@@ -0,0 +1,27 @@
1
+ import { Program } from '@coral-xyz/anchor';
2
+ import { BulkAccountLoader, BN } from '@velocity-exchange/sdk';
3
+ import { PublicKey } from '@solana/web3.js';
4
+ import { DriftVaults } from '../types/drift_vaults';
5
+ import { Vault, VaultAccountEvents, VaultProtocol } from '../types/types';
6
+ import { VaultsProgramAccount } from './vaultsProgramAccount';
7
+ export declare class VaultAccount extends VaultsProgramAccount<Vault, VaultAccountEvents> {
8
+ constructor(program: Program<DriftVaults>, vaultPubkey: PublicKey, accountLoader: BulkAccountLoader, accountSubscriptionType?: 'polling' | 'websocket');
9
+ static getAddressSync(programId: PublicKey, vaultName: string): PublicKey;
10
+ /**
11
+ * Calculates the new total shares and management fee shares after a management fee is applied.
12
+ * Only applies to deposits.
13
+ * Management fee is applied to a depositor's existing equity, and the total shares are updated (increased) accordingly.
14
+ * @param vaultEquity - The equity of the vault.
15
+ * @returns An object containing the new total shares and management fee shares.
16
+ */
17
+ calcSharesAfterManagementFee(vaultEquity: BN): {
18
+ totalShares: BN;
19
+ managementFeeShares: BN;
20
+ };
21
+ calcSharesAfterManagementAndProtocolFee(vaultEquity: BN, vaultProtocol: VaultProtocol): {
22
+ totalShares: BN;
23
+ managementFeeShares: BN;
24
+ protocolFeeShares: BN;
25
+ };
26
+ }
27
+ //# sourceMappingURL=vaultAccount.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vaultAccount.d.ts","sourceRoot":"","sources":["../../src/accounts/vaultAccount.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EACN,iBAAiB,EAKjB,EAAE,EACF,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,KAAK,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE1E,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAI9D,qBAAa,YAAa,SAAQ,oBAAoB,CACrD,KAAK,EACL,kBAAkB,CAClB;gBAEC,OAAO,EAAE,OAAO,CAAC,WAAW,CAAC,EAC7B,WAAW,EAAE,SAAS,EACtB,aAAa,EAAE,iBAAiB,EAChC,uBAAuB,GAAE,SAAS,GAAG,WAAuB;IAe7D,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,GAAG,SAAS;IAIzE;;;;;;OAMG;IACH,4BAA4B,CAAC,WAAW,EAAE,EAAE,GAAG;QAC9C,WAAW,EAAE,EAAE,CAAC;QAChB,mBAAmB,EAAE,EAAE,CAAC;KACxB;IAuCD,uCAAuC,CACtC,WAAW,EAAE,EAAE,EACf,aAAa,EAAE,aAAa,GAC1B;QACF,WAAW,EAAE,EAAE,CAAC;QAChB,mBAAmB,EAAE,EAAE,CAAC;QACxB,iBAAiB,EAAE,EAAE,CAAC;KACtB;CA0JD"}
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VaultAccount = void 0;
4
+ const sdk_1 = require("@velocity-exchange/sdk");
5
+ const accountSubscribers_1 = require("../accountSubscribers");
6
+ const vaultsProgramAccount_1 = require("./vaultsProgramAccount");
7
+ const addresses_1 = require("../addresses");
8
+ const name_1 = require("../name");
9
+ class VaultAccount extends vaultsProgramAccount_1.VaultsProgramAccount {
10
+ constructor(program, vaultPubkey, accountLoader, accountSubscriptionType = 'polling') {
11
+ super();
12
+ if (accountSubscriptionType === 'polling') {
13
+ this.accountSubscriber = new accountSubscribers_1.PollingVaultSubscriber(program, vaultPubkey, accountLoader);
14
+ }
15
+ else {
16
+ throw new Error('Websocket subscription not yet implemented');
17
+ }
18
+ }
19
+ static getAddressSync(programId, vaultName) {
20
+ return (0, addresses_1.getVaultAddressSync)(programId, (0, name_1.encodeName)(vaultName));
21
+ }
22
+ /**
23
+ * Calculates the new total shares and management fee shares after a management fee is applied.
24
+ * Only applies to deposits.
25
+ * Management fee is applied to a depositor's existing equity, and the total shares are updated (increased) accordingly.
26
+ * @param vaultEquity - The equity of the vault.
27
+ * @returns An object containing the new total shares and management fee shares.
28
+ */
29
+ calcSharesAfterManagementFee(vaultEquity) {
30
+ const accountData = this.accountSubscriber.getAccountAndSlot().data;
31
+ const depositorsEquity = accountData.userShares
32
+ .mul(vaultEquity)
33
+ .div(accountData.totalShares);
34
+ if (accountData.managementFee.eq(sdk_1.ZERO) || depositorsEquity.lte(sdk_1.ZERO)) {
35
+ return {
36
+ totalShares: accountData.totalShares,
37
+ managementFeeShares: sdk_1.ZERO,
38
+ };
39
+ }
40
+ const now = new sdk_1.BN(Date.now() / 1000);
41
+ const sinceLast = now.sub(accountData.lastFeeUpdateTs);
42
+ let managementFeeAmount = depositorsEquity
43
+ .mul(accountData.managementFee)
44
+ .div(sdk_1.PERCENTAGE_PRECISION)
45
+ .mul(sinceLast)
46
+ .div(sdk_1.ONE_YEAR);
47
+ managementFeeAmount = sdk_1.BN.min(managementFeeAmount, depositorsEquity.sub(sdk_1.ONE));
48
+ const newTotalSharesFactor = depositorsEquity
49
+ .mul(sdk_1.PERCENTAGE_PRECISION)
50
+ .div(depositorsEquity.sub(managementFeeAmount));
51
+ let newTotalShares = accountData.totalShares
52
+ .mul(newTotalSharesFactor)
53
+ .div(sdk_1.PERCENTAGE_PRECISION);
54
+ newTotalShares = sdk_1.BN.max(newTotalShares, accountData.userShares);
55
+ const managementFeeShares = newTotalShares.sub(accountData.totalShares);
56
+ return { totalShares: newTotalShares, managementFeeShares };
57
+ }
58
+ calcSharesAfterManagementAndProtocolFee(vaultEquity, vaultProtocol) {
59
+ const accountData = this.accountSubscriber.getAccountAndSlot().data;
60
+ if (!accountData.vaultProtocol) {
61
+ throw new Error('VaultProtocol does not exist for vault');
62
+ }
63
+ const depositorsEquity = accountData.userShares
64
+ .mul(vaultEquity)
65
+ .div(accountData.totalShares);
66
+ const now = new sdk_1.BN(Date.now() / 1000);
67
+ const sinceLast = now.sub(accountData.lastFeeUpdateTs);
68
+ if (!accountData.managementFee.eq(sdk_1.ZERO) &&
69
+ !vaultProtocol.protocolFee.eq(sdk_1.ZERO) &&
70
+ depositorsEquity.gt(sdk_1.ZERO)) {
71
+ const totalFee = accountData.managementFee.add(vaultProtocol.protocolFee);
72
+ const totalFeePayment = depositorsEquity
73
+ .mul(totalFee)
74
+ .div(sdk_1.PERCENTAGE_PRECISION)
75
+ .mul(sinceLast)
76
+ .div(sdk_1.ONE_YEAR);
77
+ const managementFeePayment = depositorsEquity
78
+ .mul(accountData.managementFee)
79
+ .div(sdk_1.PERCENTAGE_PRECISION)
80
+ .mul(sinceLast)
81
+ .div(sdk_1.ONE_YEAR);
82
+ const protocolFeePayment = sdk_1.BN.min(totalFeePayment, depositorsEquity.sub(new sdk_1.BN(1)))
83
+ .mul(vaultProtocol.protocolFee)
84
+ .div(totalFee);
85
+ const newTotalSharesFactor = depositorsEquity
86
+ .mul(sdk_1.PERCENTAGE_PRECISION)
87
+ .div(depositorsEquity.sub(managementFeePayment).sub(protocolFeePayment));
88
+ const newTotalShares = sdk_1.BN.max(accountData.totalShares
89
+ .mul(newTotalSharesFactor)
90
+ .div(sdk_1.PERCENTAGE_PRECISION), accountData.userShares);
91
+ if ((managementFeePayment.eq(sdk_1.ZERO) && protocolFeePayment.eq(sdk_1.ZERO)) ||
92
+ accountData.totalShares.eq(newTotalShares)) {
93
+ return {
94
+ totalShares: accountData.totalShares,
95
+ managementFeeShares: sdk_1.ZERO,
96
+ protocolFeeShares: sdk_1.ZERO,
97
+ };
98
+ }
99
+ const managementFeeShares = newTotalShares.sub(accountData.totalShares);
100
+ const protocolFeeShares = newTotalShares.sub(accountData.totalShares);
101
+ return {
102
+ totalShares: newTotalShares,
103
+ managementFeeShares,
104
+ protocolFeeShares,
105
+ };
106
+ }
107
+ else if (accountData.managementFee.eq(sdk_1.ZERO) &&
108
+ !vaultProtocol.protocolFee.eq(sdk_1.ZERO) &&
109
+ depositorsEquity.gt(sdk_1.ZERO)) {
110
+ const protocolFeePayment = depositorsEquity
111
+ .mul(vaultProtocol.protocolFee)
112
+ .div(sdk_1.PERCENTAGE_PRECISION)
113
+ .mul(sinceLast)
114
+ .div(sdk_1.ONE_YEAR);
115
+ const newTotalSharesFactor = depositorsEquity
116
+ .mul(sdk_1.PERCENTAGE_PRECISION)
117
+ .div(depositorsEquity.sub(protocolFeePayment));
118
+ const newTotalShares = sdk_1.BN.max(accountData.totalShares
119
+ .mul(newTotalSharesFactor)
120
+ .div(sdk_1.PERCENTAGE_PRECISION), accountData.userShares);
121
+ if (protocolFeePayment.eq(sdk_1.ZERO) ||
122
+ accountData.totalShares.eq(newTotalShares)) {
123
+ return {
124
+ totalShares: accountData.totalShares,
125
+ managementFeeShares: sdk_1.ZERO,
126
+ protocolFeeShares: sdk_1.ZERO,
127
+ };
128
+ }
129
+ const protocolFeeShares = newTotalShares.sub(accountData.totalShares);
130
+ return {
131
+ totalShares: newTotalShares,
132
+ managementFeeShares: sdk_1.ZERO,
133
+ protocolFeeShares,
134
+ };
135
+ }
136
+ else if (!accountData.managementFee.eq(sdk_1.ZERO) &&
137
+ vaultProtocol.protocolFee.eq(sdk_1.ZERO) &&
138
+ depositorsEquity.gt(sdk_1.ZERO)) {
139
+ const managementFeePayment = sdk_1.BN.min(depositorsEquity
140
+ .mul(accountData.managementFee)
141
+ .div(sdk_1.PERCENTAGE_PRECISION)
142
+ .mul(sinceLast)
143
+ .div(sdk_1.ONE_YEAR), depositorsEquity.sub(sdk_1.ONE));
144
+ const newTotalSharesFactor = depositorsEquity
145
+ .mul(sdk_1.PERCENTAGE_PRECISION)
146
+ .div(depositorsEquity.sub(managementFeePayment));
147
+ const newTotalShares = sdk_1.BN.max(accountData.totalShares
148
+ .mul(newTotalSharesFactor)
149
+ .div(sdk_1.PERCENTAGE_PRECISION), accountData.userShares);
150
+ if (managementFeePayment.eq(sdk_1.ZERO) ||
151
+ accountData.totalShares.eq(newTotalShares)) {
152
+ return {
153
+ totalShares: accountData.totalShares,
154
+ managementFeeShares: sdk_1.ZERO,
155
+ protocolFeeShares: sdk_1.ZERO,
156
+ };
157
+ }
158
+ const managementFeeShares = newTotalShares.sub(accountData.totalShares);
159
+ return {
160
+ totalShares: newTotalShares,
161
+ managementFeeShares,
162
+ protocolFeeShares: sdk_1.ZERO,
163
+ };
164
+ }
165
+ else {
166
+ return {
167
+ totalShares: accountData.totalShares,
168
+ managementFeeShares: sdk_1.ZERO,
169
+ protocolFeeShares: sdk_1.ZERO,
170
+ };
171
+ }
172
+ }
173
+ }
174
+ exports.VaultAccount = VaultAccount;
@@ -0,0 +1,18 @@
1
+ import { Program } from '@coral-xyz/anchor';
2
+ import { BN, BulkAccountLoader } from '@velocity-exchange/sdk';
3
+ import { PublicKey } from '@solana/web3.js';
4
+ import { DriftVaults } from '../types/drift_vaults';
5
+ import { VaultDepositor, VaultDepositorAccountEvents } from '../types/types';
6
+ import { VaultsProgramAccount } from './vaultsProgramAccount';
7
+ export declare class VaultDepositorAccount extends VaultsProgramAccount<VaultDepositor, VaultDepositorAccountEvents> {
8
+ constructor(program: Program<DriftVaults>, vaultDepositorPubkey: PublicKey, accountLoader: BulkAccountLoader, accountSubscriptionType?: 'polling' | 'websocket');
9
+ static getAddressSync(programId: PublicKey, vault: PublicKey, authority: PublicKey): PublicKey;
10
+ /**
11
+ * Calculates the percentage of a depositor's equity that will be paid as profit share fees.
12
+ *
13
+ * @param vaultProfitShare Vault's profit share fee
14
+ * @param depositorEquity Vault depositor's equity amount
15
+ */
16
+ calcProfitShareFeesPct(vaultProfitShare: BN, depositorEquity: BN): BN;
17
+ }
18
+ //# sourceMappingURL=vaultDepositorAccount.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vaultDepositorAccount.d.ts","sourceRoot":"","sources":["../../src/accounts/vaultDepositorAccount.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EACN,EAAE,EACF,iBAAiB,EAGjB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,2BAA2B,EAAE,MAAM,gBAAgB,CAAC;AAE7E,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAG9D,qBAAa,qBAAsB,SAAQ,oBAAoB,CAC9D,cAAc,EACd,2BAA2B,CAC3B;gBAEC,OAAO,EAAE,OAAO,CAAC,WAAW,CAAC,EAC7B,oBAAoB,EAAE,SAAS,EAC/B,aAAa,EAAE,iBAAiB,EAChC,uBAAuB,GAAE,SAAS,GAAG,WAAuB;IAe7D,MAAM,CAAC,cAAc,CACpB,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,SAAS,EAChB,SAAS,EAAE,SAAS,GAClB,SAAS;IAIZ;;;;;OAKG;IACH,sBAAsB,CAAC,gBAAgB,EAAE,EAAE,EAAE,eAAe,EAAE,EAAE,GAAG,EAAE;CAyBrE"}
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VaultDepositorAccount = void 0;
4
+ const sdk_1 = require("@velocity-exchange/sdk");
5
+ const accountSubscribers_1 = require("../accountSubscribers");
6
+ const vaultsProgramAccount_1 = require("./vaultsProgramAccount");
7
+ const addresses_1 = require("../addresses");
8
+ class VaultDepositorAccount extends vaultsProgramAccount_1.VaultsProgramAccount {
9
+ constructor(program, vaultDepositorPubkey, accountLoader, accountSubscriptionType = 'polling') {
10
+ super();
11
+ if (accountSubscriptionType === 'polling') {
12
+ this.accountSubscriber = new accountSubscribers_1.PollingVaultDepositorSubscriber(program, vaultDepositorPubkey, accountLoader);
13
+ }
14
+ else {
15
+ throw new Error('Websocket subscription not yet implemented');
16
+ }
17
+ }
18
+ static getAddressSync(programId, vault, authority) {
19
+ return (0, addresses_1.getVaultDepositorAddressSync)(programId, vault, authority);
20
+ }
21
+ /**
22
+ * Calculates the percentage of a depositor's equity that will be paid as profit share fees.
23
+ *
24
+ * @param vaultProfitShare Vault's profit share fee
25
+ * @param depositorEquity Vault depositor's equity amount
26
+ */
27
+ calcProfitShareFeesPct(vaultProfitShare, depositorEquity) {
28
+ const accountData = this.accountSubscriber.getAccountAndSlot().data;
29
+ const profit = depositorEquity
30
+ .sub(accountData.netDeposits)
31
+ .sub(accountData.cumulativeProfitShareAmount);
32
+ if (profit.lte(new sdk_1.BN(0)) || depositorEquity.isZero()) {
33
+ return sdk_1.ZERO;
34
+ }
35
+ const profitShareAmount = profit
36
+ .mul(vaultProfitShare)
37
+ .div(sdk_1.PERCENTAGE_PRECISION);
38
+ if (depositorEquity.eq(new sdk_1.BN(0))) {
39
+ return sdk_1.ZERO;
40
+ }
41
+ const profitShareProportion = profitShareAmount
42
+ .mul(sdk_1.PERCENTAGE_PRECISION)
43
+ .div(depositorEquity);
44
+ return profitShareProportion;
45
+ }
46
+ }
47
+ exports.VaultDepositorAccount = VaultDepositorAccount;
@@ -0,0 +1,13 @@
1
+ import EventEmitter from 'events';
2
+ import StrictEventEmitter from 'strict-event-emitter-types';
3
+ import { VaultsProgramAccountBaseEvents, VaultsProgramAccountSubscriber } from '../types/types';
4
+ export declare abstract class VaultsProgramAccount<Account, AccountEvents extends VaultsProgramAccountBaseEvents> {
5
+ accountSubscriber: VaultsProgramAccountSubscriber<Account, AccountEvents>;
6
+ get isSubscribed(): boolean;
7
+ get eventEmitter(): StrictEventEmitter<EventEmitter, AccountEvents>;
8
+ subscribe(): Promise<boolean>;
9
+ unsubscribe(): Promise<void>;
10
+ getData(): Account;
11
+ updateData(newData: Account, slot: number): Promise<void>;
12
+ }
13
+ //# sourceMappingURL=vaultsProgramAccount.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vaultsProgramAccount.d.ts","sourceRoot":"","sources":["../../src/accounts/vaultsProgramAccount.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAClC,OAAO,kBAAkB,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EACN,8BAA8B,EAC9B,8BAA8B,EAC9B,MAAM,gBAAgB,CAAC;AAExB,8BAAsB,oBAAoB,CACzC,OAAO,EACP,aAAa,SAAS,8BAA8B;IAGpD,iBAAiB,EAAE,8BAA8B,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;IAE1E,IAAI,YAAY,IAAI,OAAO,CAE1B;IAED,IAAI,YAAY,IAAI,kBAAkB,CAAC,YAAY,EAAE,aAAa,CAAC,CAElE;IAEK,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAI7B,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAIlC,OAAO,IAAI,OAAO;IAIZ,UAAU,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAG/D"}
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VaultsProgramAccount = void 0;
4
+ class VaultsProgramAccount {
5
+ get isSubscribed() {
6
+ return this.accountSubscriber.isSubscribed;
7
+ }
8
+ get eventEmitter() {
9
+ return this.accountSubscriber.eventEmitter;
10
+ }
11
+ async subscribe() {
12
+ return await this.accountSubscriber.subscribe();
13
+ }
14
+ async unsubscribe() {
15
+ return await this.accountSubscriber.unsubscribe();
16
+ }
17
+ getData() {
18
+ var _a;
19
+ return (_a = this.accountSubscriber.getAccountAndSlot()) === null || _a === void 0 ? void 0 : _a.data;
20
+ }
21
+ async updateData(newData, slot) {
22
+ return await this.accountSubscriber.updateData(newData, slot);
23
+ }
24
+ }
25
+ exports.VaultsProgramAccount = VaultsProgramAccount;
@@ -0,0 +1,10 @@
1
+ import { PublicKey } from '@solana/web3.js';
2
+ export declare function getVaultAddressSync(programId: PublicKey, encodedName: number[]): PublicKey;
3
+ export declare function getVaultDepositorAddressSync(programId: PublicKey, vault: PublicKey, authority: PublicKey): PublicKey;
4
+ export declare function getTokenVaultAddressSync(programId: PublicKey, vault: PublicKey): PublicKey;
5
+ export declare function getInsuranceFundTokenVaultAddressSync(programId: PublicKey, vault: PublicKey, marketIndex: number): PublicKey;
6
+ export declare function getVaultProtocolAddressSync(programId: PublicKey, vault: PublicKey): PublicKey;
7
+ export declare function getTokenizedVaultAddressSync(programId: PublicKey, vault: PublicKey, sharesBase: number): PublicKey;
8
+ export declare function getTokenizedVaultMintAddressSync(programId: PublicKey, vault: PublicKey, sharesBase: number): PublicKey;
9
+ export declare function getFeeUpdateAddressSync(programId: PublicKey, vault: PublicKey): PublicKey;
10
+ //# sourceMappingURL=addresses.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addresses.d.ts","sourceRoot":"","sources":["../src/addresses.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,wBAAgB,mBAAmB,CAClC,SAAS,EAAE,SAAS,EACpB,WAAW,EAAE,MAAM,EAAE,GACnB,SAAS,CAQX;AAED,wBAAgB,4BAA4B,CAC3C,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,SAAS,EAChB,SAAS,EAAE,SAAS,GAClB,SAAS,CASX;AAED,wBAAgB,wBAAwB,CACvC,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,SAAS,GACd,SAAS,CAQX;AAED,wBAAgB,qCAAqC,CACpD,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,SAAS,EAChB,WAAW,EAAE,MAAM,GACjB,SAAS,CASX;AAED,wBAAgB,2BAA2B,CAC1C,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,SAAS,GACd,SAAS,CAQX;AAED,wBAAgB,4BAA4B,CAC3C,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,SAAS,EAChB,UAAU,EAAE,MAAM,GAChB,SAAS,CASX;AAED,wBAAgB,gCAAgC,CAC/C,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,SAAS,EAChB,UAAU,EAAE,MAAM,GAChB,SAAS,CASX;AAED,wBAAgB,uBAAuB,CACtC,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,SAAS,GACd,SAAS,CAQX"}