@drift-labs/sdk 0.1.19-master.2 → 0.1.21-master.4

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 (143) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +31 -0
  2. package/lib/accounts/bulkAccountLoader.js +177 -0
  3. package/lib/accounts/bulkUserSubscription.d.ts +7 -0
  4. package/lib/accounts/bulkUserSubscription.js +28 -0
  5. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +49 -0
  6. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +228 -0
  7. package/lib/accounts/pollingTokenAccountSubscriber.d.ts +25 -0
  8. package/lib/accounts/pollingTokenAccountSubscriber.js +79 -0
  9. package/lib/accounts/pollingUserAccountSubscriber.d.ts +32 -0
  10. package/lib/accounts/pollingUserAccountSubscriber.js +136 -0
  11. package/lib/accounts/types.d.ts +38 -3
  12. package/lib/accounts/utils.d.ts +1 -0
  13. package/lib/accounts/utils.js +7 -0
  14. package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -3
  15. package/lib/accounts/webSocketAccountSubscriber.js +43 -12
  16. package/lib/accounts/{defaultClearingHouseAccountSubscriber.d.ts → webSocketClearingHouseAccountSubscriber.d.ts} +8 -3
  17. package/lib/accounts/{defaultClearingHouseAccountSubscriber.js → webSocketClearingHouseAccountSubscriber.js} +30 -4
  18. package/lib/accounts/{defaultUserAccountSubscriber.d.ts → webSocketUserAccountSubscriber.d.ts} +6 -3
  19. package/lib/accounts/{defaultUserAccountSubscriber.js → webSocketUserAccountSubscriber.js} +16 -4
  20. package/lib/addresses.d.ts +4 -1
  21. package/lib/addresses.js +28 -1
  22. package/lib/admin.d.ts +10 -4
  23. package/lib/admin.js +54 -17
  24. package/lib/assert/assert.d.ts +0 -1
  25. package/lib/clearingHouse.d.ts +39 -4
  26. package/lib/clearingHouse.js +334 -23
  27. package/lib/clearingHouseUser.d.ts +27 -6
  28. package/lib/clearingHouseUser.js +125 -45
  29. package/lib/config.d.ts +0 -1
  30. package/lib/constants/markets.d.ts +2 -2
  31. package/lib/constants/markets.js +28 -15
  32. package/lib/constants/numericConstants.d.ts +4 -2
  33. package/lib/constants/numericConstants.js +16 -17
  34. package/lib/examples/makeTradeExample.d.ts +0 -1
  35. package/lib/examples/makeTradeExample.js +6 -6
  36. package/lib/factory/clearingHouse.d.ts +25 -0
  37. package/lib/factory/clearingHouse.js +64 -0
  38. package/lib/factory/clearingHouseUser.d.ts +19 -0
  39. package/lib/factory/clearingHouseUser.js +34 -0
  40. package/lib/idl/clearing_house.json +1066 -39
  41. package/lib/index.d.ts +11 -3
  42. package/lib/index.js +12 -2
  43. package/lib/math/amm.d.ts +3 -1
  44. package/lib/math/amm.js +128 -15
  45. package/lib/math/conversion.d.ts +1 -2
  46. package/lib/math/conversion.js +1 -1
  47. package/lib/math/funding.d.ts +0 -1
  48. package/lib/math/funding.js +1 -1
  49. package/lib/math/insuranceFund.d.ts +2 -2
  50. package/lib/math/insuranceFund.js +3 -6
  51. package/lib/math/market.d.ts +2 -2
  52. package/lib/math/market.js +12 -2
  53. package/lib/math/orders.d.ts +3 -0
  54. package/lib/math/orders.js +32 -0
  55. package/lib/math/position.d.ts +6 -3
  56. package/lib/math/position.js +21 -10
  57. package/lib/math/trade.d.ts +0 -1
  58. package/lib/math/trade.js +16 -16
  59. package/lib/math/utils.d.ts +2 -2
  60. package/lib/math/utils.js +3 -3
  61. package/lib/mockUSDCFaucet.d.ts +2 -2
  62. package/lib/orderParams.d.ts +7 -0
  63. package/lib/orderParams.js +108 -0
  64. package/lib/orders.d.ts +6 -0
  65. package/lib/orders.js +136 -0
  66. package/lib/pythClient.d.ts +0 -1
  67. package/lib/pythClient.js +1 -1
  68. package/lib/token/index.d.ts +3 -0
  69. package/lib/token/index.js +38 -0
  70. package/lib/tx/defaultTxSender.d.ts +0 -1
  71. package/lib/tx/types.d.ts +0 -1
  72. package/lib/tx/utils.d.ts +0 -1
  73. package/lib/types.d.ts +147 -3
  74. package/lib/types.js +36 -1
  75. package/lib/util/computeUnits.d.ts +0 -1
  76. package/lib/util/tps.d.ts +0 -1
  77. package/lib/wallet.d.ts +0 -1
  78. package/package.json +11 -3
  79. package/src/accounts/bulkAccountLoader.ts +215 -0
  80. package/src/accounts/bulkUserSubscription.ts +28 -0
  81. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +326 -0
  82. package/src/accounts/pollingTokenAccountSubscriber.ts +99 -0
  83. package/src/accounts/pollingUserAccountSubscriber.ts +194 -0
  84. package/src/accounts/types.ts +48 -1
  85. package/src/accounts/utils.ts +3 -0
  86. package/src/accounts/webSocketAccountSubscriber.ts +67 -17
  87. package/src/accounts/{defaultClearingHouseAccountSubscriber.ts → webSocketClearingHouseAccountSubscriber.ts} +51 -1
  88. package/src/accounts/{defaultUserAccountSubscriber.ts → webSocketUserAccountSubscriber.ts} +33 -3
  89. package/src/addresses.ts +37 -0
  90. package/src/admin.ts +92 -24
  91. package/src/clearingHouse.ts +455 -22
  92. package/src/clearingHouseUser.ts +155 -18
  93. package/src/constants/markets.ts +17 -1
  94. package/src/constants/numericConstants.ts +3 -1
  95. package/src/examples/makeTradeExample.ts +4 -1
  96. package/src/factory/clearingHouse.ts +125 -0
  97. package/src/factory/clearingHouseUser.ts +73 -0
  98. package/src/idl/clearing_house.json +1066 -39
  99. package/src/index.ts +11 -2
  100. package/src/math/amm.ts +169 -14
  101. package/src/math/conversion.ts +1 -1
  102. package/src/math/insuranceFund.ts +1 -1
  103. package/src/math/market.ts +28 -2
  104. package/src/math/orders.ts +44 -0
  105. package/src/math/position.ts +24 -4
  106. package/src/math/utils.ts +1 -1
  107. package/src/mockUSDCFaucet.ts +1 -1
  108. package/src/orderParams.ts +151 -0
  109. package/src/orders.ts +236 -0
  110. package/src/token/index.ts +37 -0
  111. package/src/types.ts +130 -2
  112. package/tsconfig.json +0 -1
  113. package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +0 -1
  114. package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +0 -1
  115. package/lib/accounts/types.d.ts.map +0 -1
  116. package/lib/accounts/webSocketAccountSubscriber.d.ts.map +0 -1
  117. package/lib/addresses.d.ts.map +0 -1
  118. package/lib/admin.d.ts.map +0 -1
  119. package/lib/assert/assert.d.ts.map +0 -1
  120. package/lib/clearingHouse.d.ts.map +0 -1
  121. package/lib/clearingHouseUser.d.ts.map +0 -1
  122. package/lib/config.d.ts.map +0 -1
  123. package/lib/constants/markets.d.ts.map +0 -1
  124. package/lib/constants/numericConstants.d.ts.map +0 -1
  125. package/lib/examples/makeTradeExample.d.ts.map +0 -1
  126. package/lib/index.d.ts.map +0 -1
  127. package/lib/math/amm.d.ts.map +0 -1
  128. package/lib/math/conversion.d.ts.map +0 -1
  129. package/lib/math/funding.d.ts.map +0 -1
  130. package/lib/math/insuranceFund.d.ts.map +0 -1
  131. package/lib/math/market.d.ts.map +0 -1
  132. package/lib/math/position.d.ts.map +0 -1
  133. package/lib/math/trade.d.ts.map +0 -1
  134. package/lib/math/utils.d.ts.map +0 -1
  135. package/lib/mockUSDCFaucet.d.ts.map +0 -1
  136. package/lib/pythClient.d.ts.map +0 -1
  137. package/lib/tx/defaultTxSender.d.ts.map +0 -1
  138. package/lib/tx/types.d.ts.map +0 -1
  139. package/lib/tx/utils.d.ts.map +0 -1
  140. package/lib/types.d.ts.map +0 -1
  141. package/lib/util/computeUnits.d.ts.map +0 -1
  142. package/lib/util/tps.d.ts.map +0 -1
  143. package/lib/wallet.d.ts.map +0 -1
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.PollingUserAccountSubscriber = void 0;
13
+ const types_1 = require("./types");
14
+ const events_1 = require("events");
15
+ const addresses_1 = require("../addresses");
16
+ const utils_1 = require("./utils");
17
+ class PollingUserAccountSubscriber {
18
+ constructor(program, authority, accountLoader) {
19
+ this.accountsToPoll = new Map();
20
+ this.type = 'polling';
21
+ this.isSubscribed = false;
22
+ this.program = program;
23
+ this.authority = authority;
24
+ this.accountLoader = accountLoader;
25
+ this.eventEmitter = new events_1.EventEmitter();
26
+ }
27
+ subscribe() {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ if (this.isSubscribed) {
30
+ return true;
31
+ }
32
+ yield this.addToAccountLoader();
33
+ yield this.fetchIfUnloaded();
34
+ this.eventEmitter.emit('update');
35
+ this.isSubscribed = true;
36
+ return true;
37
+ });
38
+ }
39
+ addToAccountLoader() {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ if (this.accountsToPoll.size > 0) {
42
+ return;
43
+ }
44
+ const userPublicKey = yield addresses_1.getUserAccountPublicKey(this.program.programId, this.authority);
45
+ const userAccount = (yield this.program.account.user.fetch(userPublicKey));
46
+ this.accountsToPoll.set(userPublicKey.toString(), {
47
+ key: 'user',
48
+ publicKey: userPublicKey,
49
+ eventType: 'userAccountData',
50
+ });
51
+ this.accountsToPoll.set(userAccount.positions.toString(), {
52
+ key: 'userPositions',
53
+ publicKey: userAccount.positions,
54
+ eventType: 'userPositionsData',
55
+ });
56
+ const userOrdersPublicKey = yield addresses_1.getUserOrdersAccountPublicKey(this.program.programId, userPublicKey);
57
+ const userOrdersExist = (yield this.program.provider.connection.getParsedAccountInfo(userOrdersPublicKey)).value !== null;
58
+ if (userOrdersExist) {
59
+ this.accountsToPoll.set(userOrdersPublicKey.toString(), {
60
+ key: 'userOrders',
61
+ publicKey: userOrdersPublicKey,
62
+ eventType: 'userOrdersData',
63
+ });
64
+ }
65
+ for (const [_, accountToPoll] of this.accountsToPoll) {
66
+ accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, (buffer) => {
67
+ const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
68
+ this[accountToPoll.key] = account;
69
+ // @ts-ignore
70
+ this.eventEmitter.emit(accountToPoll.eventType, account);
71
+ this.eventEmitter.emit('update');
72
+ });
73
+ }
74
+ this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
75
+ this.eventEmitter.emit('error', error);
76
+ });
77
+ });
78
+ }
79
+ fetchIfUnloaded() {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ let shouldFetch = false;
82
+ for (const [_, accountToPoll] of this.accountsToPoll) {
83
+ if (this[accountToPoll.key] === undefined) {
84
+ shouldFetch = true;
85
+ break;
86
+ }
87
+ }
88
+ if (shouldFetch) {
89
+ yield this.fetch();
90
+ }
91
+ });
92
+ }
93
+ fetch() {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ yield this.accountLoader.load();
96
+ for (const [_, accountToPoll] of this.accountsToPoll) {
97
+ const buffer = this.accountLoader.getAccountData(accountToPoll.publicKey);
98
+ if (buffer) {
99
+ this[accountToPoll.key] = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
100
+ }
101
+ }
102
+ });
103
+ }
104
+ unsubscribe() {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ if (!this.isSubscribed) {
107
+ return;
108
+ }
109
+ for (const [_, accountToPoll] of this.accountsToPoll) {
110
+ this.accountLoader.removeAccount(accountToPoll.publicKey, accountToPoll.callbackId);
111
+ }
112
+ this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
113
+ this.errorCallbackId = undefined;
114
+ this.accountsToPoll.clear();
115
+ this.isSubscribed = false;
116
+ });
117
+ }
118
+ assertIsSubscribed() {
119
+ if (!this.isSubscribed) {
120
+ throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
121
+ }
122
+ }
123
+ getUserAccount() {
124
+ this.assertIsSubscribed();
125
+ return this.user;
126
+ }
127
+ getUserPositionsAccount() {
128
+ this.assertIsSubscribed();
129
+ return this.userPositions;
130
+ }
131
+ getUserOrdersAccount() {
132
+ this.assertIsSubscribed();
133
+ return this.userOrders;
134
+ }
135
+ }
136
+ exports.PollingUserAccountSubscriber = PollingUserAccountSubscriber;
@@ -1,7 +1,10 @@
1
1
  /// <reference types="node" />
2
- import { DepositHistoryAccount, ExtendedCurveHistoryAccount, FundingPaymentHistoryAccount, FundingRateHistoryAccount, LiquidationHistoryAccount, MarketsAccount, StateAccount, TradeHistoryAccount, UserAccount, UserPositionsAccount } from '../types';
2
+ import { DepositHistoryAccount, ExtendedCurveHistoryAccount, FundingPaymentHistoryAccount, FundingRateHistoryAccount, LiquidationHistoryAccount, MarketsAccount, OrderHistoryAccount, OrderStateAccount, StateAccount, TradeHistoryAccount, UserAccount, UserOrdersAccount, UserPositionsAccount } from '../types';
3
3
  import StrictEventEmitter from 'strict-event-emitter-types';
4
4
  import { EventEmitter } from 'events';
5
+ import { PublicKey } from '@solana/web3.js';
6
+ import { AccountInfo } from '@solana/spl-token';
7
+ import { ClearingHouseConfigType, ClearingHouseUserConfigType } from '..';
5
8
  export interface AccountSubscriber<T> {
6
9
  data?: T;
7
10
  subscribe(onChange: (data: T) => void): Promise<void>;
@@ -20,9 +23,12 @@ export interface ClearingHouseAccountEvents {
20
23
  liquidationHistoryAccountUpdate: (payload: LiquidationHistoryAccount) => void;
21
24
  depositHistoryAccountUpdate: (payload: DepositHistoryAccount) => void;
22
25
  curveHistoryAccountUpdate: (payload: ExtendedCurveHistoryAccount) => void;
26
+ orderHistoryAccountUpdate: (payload: OrderHistoryAccount) => void;
27
+ orderStateAccountUpdate: (payload: OrderStateAccount) => void;
23
28
  update: void;
29
+ error: (e: Error) => void;
24
30
  }
25
- export declare type ClearingHouseAccountTypes = 'tradeHistoryAccount' | 'depositHistoryAccount' | 'fundingPaymentHistoryAccount' | 'fundingRateHistoryAccount' | 'curveHistoryAccount' | 'liquidationHistoryAccount';
31
+ export declare type ClearingHouseAccountTypes = 'tradeHistoryAccount' | 'depositHistoryAccount' | 'fundingPaymentHistoryAccount' | 'fundingRateHistoryAccount' | 'curveHistoryAccount' | 'liquidationHistoryAccount' | 'orderHistoryAccount';
26
32
  export interface ClearingHouseAccountSubscriber {
27
33
  eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
28
34
  isSubscribed: boolean;
@@ -38,11 +44,16 @@ export interface ClearingHouseAccountSubscriber {
38
44
  getFundingRateHistoryAccount(): FundingRateHistoryAccount;
39
45
  getCurveHistoryAccount(): ExtendedCurveHistoryAccount;
40
46
  getLiquidationHistoryAccount(): LiquidationHistoryAccount;
47
+ getOrderStateAccount(): OrderStateAccount;
48
+ getOrderHistoryAccount(): OrderHistoryAccount;
49
+ type: ClearingHouseConfigType;
41
50
  }
42
51
  export interface UserAccountEvents {
43
52
  userAccountData: (payload: UserAccount) => void;
44
53
  userPositionsData: (payload: UserPositionsAccount) => void;
54
+ userOrdersData: (payload: UserOrdersAccount) => void;
45
55
  update: void;
56
+ error: (e: Error) => void;
46
57
  }
47
58
  export interface UserAccountSubscriber {
48
59
  eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
@@ -52,5 +63,29 @@ export interface UserAccountSubscriber {
52
63
  unsubscribe(): Promise<void>;
53
64
  getUserAccount(): UserAccount;
54
65
  getUserPositionsAccount(): UserPositionsAccount;
66
+ getUserOrdersAccount(): UserOrdersAccount;
67
+ type: ClearingHouseUserConfigType;
55
68
  }
56
- //# sourceMappingURL=types.d.ts.map
69
+ export interface TokenAccountEvents {
70
+ tokenAccountUpdate: (payload: AccountInfo) => void;
71
+ update: void;
72
+ error: (e: Error) => void;
73
+ }
74
+ export interface TokenAccountSubscriber {
75
+ eventEmitter: StrictEventEmitter<EventEmitter, TokenAccountEvents>;
76
+ isSubscribed: boolean;
77
+ subscribe(): Promise<boolean>;
78
+ fetch(): Promise<void>;
79
+ unsubscribe(): Promise<void>;
80
+ getTokenAccount(): AccountInfo;
81
+ }
82
+ export declare type AccountToPoll = {
83
+ key: string;
84
+ publicKey: PublicKey;
85
+ eventType: string;
86
+ callbackId?: string;
87
+ };
88
+ export declare type AccountData = {
89
+ slot: number;
90
+ buffer: Buffer | undefined;
91
+ };
@@ -0,0 +1 @@
1
+ export declare function capitalize(value: string): string;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.capitalize = void 0;
4
+ function capitalize(value) {
5
+ return value[0].toUpperCase() + value.slice(1);
6
+ }
7
+ exports.capitalize = capitalize;
@@ -1,15 +1,18 @@
1
- import { AccountSubscriber } from './types';
1
+ /// <reference types="node" />
2
+ import { AccountData, AccountSubscriber } from './types';
2
3
  import { Program } from '@project-serum/anchor';
3
- import { PublicKey } from '@solana/web3.js';
4
+ import { AccountInfo, Context, PublicKey } from '@solana/web3.js';
4
5
  export declare class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
5
6
  data?: T;
7
+ accountData?: AccountData;
6
8
  accountName: string;
7
9
  program: Program;
8
10
  accountPublicKey: PublicKey;
9
11
  onChange: (data: T) => void;
12
+ listenerId?: number;
10
13
  constructor(accountName: string, program: Program, accountPublicKey: PublicKey);
11
14
  subscribe(onChange: (data: T) => void): Promise<void>;
12
15
  fetch(): Promise<void>;
16
+ handleRpcResponse(context: Context, accountInfo?: AccountInfo<Buffer>): void;
13
17
  unsubscribe(): Promise<void>;
14
18
  }
15
- //# sourceMappingURL=webSocketAccountSubscriber.d.ts.map
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.WebSocketAccountSubscriber = void 0;
13
+ const utils_1 = require("./utils");
13
14
  class WebSocketAccountSubscriber {
14
15
  constructor(accountName, program, accountPublicKey) {
15
16
  this.accountName = accountName;
@@ -18,28 +19,58 @@ class WebSocketAccountSubscriber {
18
19
  }
19
20
  subscribe(onChange) {
20
21
  return __awaiter(this, void 0, void 0, function* () {
22
+ if (this.listenerId) {
23
+ return;
24
+ }
21
25
  this.onChange = onChange;
22
26
  yield this.fetch();
23
- this.program.account[this.accountName]
24
- .subscribe(this.accountPublicKey, this.program.provider.opts.commitment)
25
- .on('change', (data) => __awaiter(this, void 0, void 0, function* () {
26
- this.data = data;
27
- this.onChange(data);
28
- }));
27
+ this.listenerId = this.program.provider.connection.onAccountChange(this.accountPublicKey, (accountInfo, context) => {
28
+ this.handleRpcResponse(context, accountInfo);
29
+ }, this.program.provider.opts.commitment);
29
30
  });
30
31
  }
31
32
  fetch() {
32
33
  return __awaiter(this, void 0, void 0, function* () {
33
- const newData = (yield this.program.account[this.accountName].fetch(this.accountPublicKey));
34
- // if data has changed trigger update
35
- if (JSON.stringify(newData) !== JSON.stringify(this.data)) {
36
- this.data = newData;
34
+ const rpcResponse = yield this.program.provider.connection.getAccountInfoAndContext(this.accountPublicKey, this.program.provider.opts.commitment);
35
+ this.handleRpcResponse(rpcResponse.context, rpcResponse === null || rpcResponse === void 0 ? void 0 : rpcResponse.value);
36
+ });
37
+ }
38
+ handleRpcResponse(context, accountInfo) {
39
+ const newSlot = context.slot;
40
+ let newBuffer = undefined;
41
+ if (accountInfo) {
42
+ newBuffer = accountInfo.data;
43
+ }
44
+ if (!this.accountData) {
45
+ this.accountData = {
46
+ buffer: newBuffer,
47
+ slot: newSlot,
48
+ };
49
+ if (newBuffer) {
50
+ this.data = this.program.account[this.accountName].coder.accounts.decode(utils_1.capitalize(this.accountName), newBuffer);
37
51
  this.onChange(this.data);
38
52
  }
39
- });
53
+ return;
54
+ }
55
+ if (newSlot <= this.accountData.slot) {
56
+ return;
57
+ }
58
+ const oldBuffer = this.accountData.buffer;
59
+ if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
60
+ this.accountData = {
61
+ buffer: newBuffer,
62
+ slot: newSlot,
63
+ };
64
+ this.data = this.program.account[this.accountName].coder.accounts.decode(utils_1.capitalize(this.accountName), newBuffer);
65
+ this.onChange(this.data);
66
+ }
40
67
  }
41
68
  unsubscribe() {
42
- return this.program.account[this.accountName].unsubscribe(this.accountPublicKey);
69
+ if (this.listenerId) {
70
+ const promise = this.program.provider.connection.removeAccountChangeListener(this.listenerId);
71
+ this.listenerId = undefined;
72
+ return promise;
73
+ }
43
74
  }
44
75
  }
45
76
  exports.WebSocketAccountSubscriber = WebSocketAccountSubscriber;
@@ -1,11 +1,12 @@
1
1
  /// <reference types="node" />
2
2
  import { ClearingHouseAccountSubscriber, ClearingHouseAccountEvents, ClearingHouseAccountTypes } from './types';
3
3
  import { AccountSubscriber } from './types';
4
- import { DepositHistoryAccount, ExtendedCurveHistoryAccount, FundingPaymentHistoryAccount, FundingRateHistoryAccount, LiquidationHistoryAccount, MarketsAccount, StateAccount, TradeHistoryAccount } from '../types';
4
+ import { DepositHistoryAccount, ExtendedCurveHistoryAccount, FundingPaymentHistoryAccount, FundingRateHistoryAccount, LiquidationHistoryAccount, MarketsAccount, OrderHistoryAccount, OrderStateAccount, StateAccount, TradeHistoryAccount } from '../types';
5
5
  import { Program } from '@project-serum/anchor';
6
6
  import StrictEventEmitter from 'strict-event-emitter-types';
7
7
  import { EventEmitter } from 'events';
8
- export declare class DefaultClearingHouseAccountSubscriber implements ClearingHouseAccountSubscriber {
8
+ import { ClearingHouseConfigType } from '../factory/clearingHouse';
9
+ export declare class WebSocketClearingHouseAccountSubscriber implements ClearingHouseAccountSubscriber {
9
10
  isSubscribed: boolean;
10
11
  program: Program;
11
12
  eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
@@ -17,7 +18,10 @@ export declare class DefaultClearingHouseAccountSubscriber implements ClearingHo
17
18
  fundingRateHistoryAccountSubscriber?: AccountSubscriber<FundingRateHistoryAccount>;
18
19
  curveHistoryAccountSubscriber?: AccountSubscriber<ExtendedCurveHistoryAccount>;
19
20
  liquidationHistoryAccountSubscriber?: AccountSubscriber<LiquidationHistoryAccount>;
21
+ orderStateAccountSubscriber?: AccountSubscriber<OrderStateAccount>;
22
+ orderHistoryAccountSubscriber?: AccountSubscriber<OrderHistoryAccount>;
20
23
  optionalExtraSubscriptions: ClearingHouseAccountTypes[];
24
+ type: ClearingHouseConfigType;
21
25
  private isSubscribing;
22
26
  private subscriptionPromise;
23
27
  private subscriptionPromiseResolver;
@@ -35,5 +39,6 @@ export declare class DefaultClearingHouseAccountSubscriber implements ClearingHo
35
39
  getFundingRateHistoryAccount(): FundingRateHistoryAccount;
36
40
  getCurveHistoryAccount(): ExtendedCurveHistoryAccount;
37
41
  getLiquidationHistoryAccount(): LiquidationHistoryAccount;
42
+ getOrderHistoryAccount(): OrderHistoryAccount;
43
+ getOrderStateAccount(): OrderStateAccount;
38
44
  }
39
- //# sourceMappingURL=defaultClearingHouseAccountSubscriber.d.ts.map
@@ -9,14 +9,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DefaultClearingHouseAccountSubscriber = void 0;
12
+ exports.WebSocketClearingHouseAccountSubscriber = void 0;
13
13
  const types_1 = require("./types");
14
14
  const events_1 = require("events");
15
15
  const addresses_1 = require("../addresses");
16
16
  const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
17
- class DefaultClearingHouseAccountSubscriber {
17
+ class WebSocketClearingHouseAccountSubscriber {
18
18
  constructor(program) {
19
19
  this.optionalExtraSubscriptions = [];
20
+ this.type = 'websocket';
20
21
  this.isSubscribing = false;
21
22
  this.isSubscribed = false;
22
23
  this.program = program;
@@ -34,7 +35,7 @@ class DefaultClearingHouseAccountSubscriber {
34
35
  this.subscriptionPromise = new Promise((res) => {
35
36
  this.subscriptionPromiseResolver = res;
36
37
  });
37
- const statePublicKey = yield (0, addresses_1.getClearingHouseStateAccountPublicKey)(this.program.programId);
38
+ const statePublicKey = yield addresses_1.getClearingHouseStateAccountPublicKey(this.program.programId);
38
39
  // create and activate main state account subscription
39
40
  this.stateAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('state', this.program, statePublicKey);
40
41
  yield this.stateAccountSubscriber.subscribe((data) => {
@@ -47,6 +48,12 @@ class DefaultClearingHouseAccountSubscriber {
47
48
  this.eventEmitter.emit('marketsAccountUpdate', data);
48
49
  this.eventEmitter.emit('update');
49
50
  });
51
+ this.orderStateAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('orderState', this.program, state.orderState);
52
+ yield this.orderStateAccountSubscriber.subscribe((data) => {
53
+ this.eventEmitter.emit('orderStateAccountUpdate', data);
54
+ this.eventEmitter.emit('update');
55
+ });
56
+ const orderState = this.orderStateAccountSubscriber.data;
50
57
  // create subscribers for other state accounts
51
58
  this.tradeHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('tradeHistory', this.program, state.tradeHistory);
52
59
  this.depositHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('depositHistory', this.program, state.depositHistory);
@@ -55,6 +62,7 @@ class DefaultClearingHouseAccountSubscriber {
55
62
  this.fundingRateHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('fundingRateHistory', this.program, state.fundingRateHistory);
56
63
  this.liquidationHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('liquidationHistory', this.program, state.liquidationHistory);
57
64
  this.curveHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('extendedCurveHistory', this.program, state.extendedCurveHistory);
65
+ this.orderHistoryAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('orderHistory', this.program, orderState.orderHistory);
58
66
  const extraSusbcribersToUse = [];
59
67
  if (optionalSubscriptions === null || optionalSubscriptions === void 0 ? void 0 : optionalSubscriptions.includes('tradeHistoryAccount'))
60
68
  extraSusbcribersToUse.push({
@@ -86,6 +94,11 @@ class DefaultClearingHouseAccountSubscriber {
86
94
  subscriber: this.curveHistoryAccountSubscriber,
87
95
  eventType: 'curveHistoryAccountUpdate',
88
96
  });
97
+ if (optionalSubscriptions === null || optionalSubscriptions === void 0 ? void 0 : optionalSubscriptions.includes('orderHistoryAccount'))
98
+ extraSusbcribersToUse.push({
99
+ subscriber: this.orderHistoryAccountSubscriber,
100
+ eventType: 'orderHistoryAccountUpdate',
101
+ });
89
102
  this.optionalExtraSubscriptions = optionalSubscriptions !== null && optionalSubscriptions !== void 0 ? optionalSubscriptions : [];
90
103
  // await all subcriptions in parallel to boost performance
91
104
  //// the state account subscription above can't happen in here, because some of these susbcriptions are dependent on clearing house state being available
@@ -124,6 +137,7 @@ class DefaultClearingHouseAccountSubscriber {
124
137
  }
125
138
  yield this.stateAccountSubscriber.unsubscribe();
126
139
  yield this.marketsAccountSubscriber.unsubscribe();
140
+ yield this.orderStateAccountSubscriber.unsubscribe();
127
141
  if (this.optionalExtraSubscriptions.includes('tradeHistoryAccount')) {
128
142
  yield this.tradeHistoryAccountSubscriber.unsubscribe();
129
143
  }
@@ -142,6 +156,9 @@ class DefaultClearingHouseAccountSubscriber {
142
156
  if (this.optionalExtraSubscriptions.includes('liquidationHistoryAccount')) {
143
157
  yield this.liquidationHistoryAccountSubscriber.unsubscribe();
144
158
  }
159
+ if (this.optionalExtraSubscriptions.includes('orderHistoryAccount')) {
160
+ yield this.orderHistoryAccountSubscriber.unsubscribe();
161
+ }
145
162
  this.isSubscribed = false;
146
163
  });
147
164
  }
@@ -196,5 +213,14 @@ class DefaultClearingHouseAccountSubscriber {
196
213
  this.assertOptionalIsSubscribed('liquidationHistoryAccount');
197
214
  return this.liquidationHistoryAccountSubscriber.data;
198
215
  }
216
+ getOrderHistoryAccount() {
217
+ this.assertIsSubscribed();
218
+ this.assertOptionalIsSubscribed('orderHistoryAccount');
219
+ return this.orderHistoryAccountSubscriber.data;
220
+ }
221
+ getOrderStateAccount() {
222
+ this.assertIsSubscribed();
223
+ return this.orderStateAccountSubscriber.data;
224
+ }
199
225
  }
200
- exports.DefaultClearingHouseAccountSubscriber = DefaultClearingHouseAccountSubscriber;
226
+ exports.WebSocketClearingHouseAccountSubscriber = WebSocketClearingHouseAccountSubscriber;
@@ -4,14 +4,17 @@ import { Program } from '@project-serum/anchor';
4
4
  import StrictEventEmitter from 'strict-event-emitter-types';
5
5
  import { EventEmitter } from 'events';
6
6
  import { PublicKey } from '@solana/web3.js';
7
- import { UserAccount, UserPositionsAccount } from '../types';
8
- export declare class DefaultUserAccountSubscriber implements UserAccountSubscriber {
7
+ import { UserAccount, UserOrdersAccount, UserPositionsAccount } from '../types';
8
+ import { ClearingHouseConfigType } from '../factory/clearingHouse';
9
+ export declare class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
9
10
  isSubscribed: boolean;
10
11
  program: Program;
11
12
  eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
12
13
  authority: PublicKey;
13
14
  userDataAccountSubscriber: AccountSubscriber<UserAccount>;
14
15
  userPositionsAccountSubscriber: AccountSubscriber<UserPositionsAccount>;
16
+ userOrdersAccountSubscriber: AccountSubscriber<UserOrdersAccount>;
17
+ type: ClearingHouseConfigType;
15
18
  constructor(program: Program, authority: PublicKey);
16
19
  subscribe(): Promise<boolean>;
17
20
  fetch(): Promise<void>;
@@ -19,5 +22,5 @@ export declare class DefaultUserAccountSubscriber implements UserAccountSubscrib
19
22
  assertIsSubscribed(): void;
20
23
  getUserAccount(): UserAccount;
21
24
  getUserPositionsAccount(): UserPositionsAccount;
25
+ getUserOrdersAccount(): UserOrdersAccount;
22
26
  }
23
- //# sourceMappingURL=defaultUserAccountSubscriber.d.ts.map
@@ -9,13 +9,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DefaultUserAccountSubscriber = void 0;
12
+ exports.WebSocketUserAccountSubscriber = void 0;
13
13
  const types_1 = require("./types");
14
14
  const events_1 = require("events");
15
15
  const addresses_1 = require("../addresses");
16
16
  const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
17
- class DefaultUserAccountSubscriber {
17
+ class WebSocketUserAccountSubscriber {
18
18
  constructor(program, authority) {
19
+ this.type = 'websocket';
19
20
  this.isSubscribed = false;
20
21
  this.program = program;
21
22
  this.authority = authority;
@@ -26,7 +27,7 @@ class DefaultUserAccountSubscriber {
26
27
  if (this.isSubscribed) {
27
28
  return true;
28
29
  }
29
- const userPublicKey = yield (0, addresses_1.getUserAccountPublicKey)(this.program.programId, this.authority);
30
+ const userPublicKey = yield addresses_1.getUserAccountPublicKey(this.program.programId, this.authority);
30
31
  this.userDataAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('user', this.program, userPublicKey);
31
32
  yield this.userDataAccountSubscriber.subscribe((data) => {
32
33
  this.eventEmitter.emit('userAccountData', data);
@@ -38,6 +39,12 @@ class DefaultUserAccountSubscriber {
38
39
  this.eventEmitter.emit('userPositionsData', data);
39
40
  this.eventEmitter.emit('update');
40
41
  });
42
+ const userOrdersPublicKey = yield addresses_1.getUserOrdersAccountPublicKey(this.program.programId, userPublicKey);
43
+ this.userOrdersAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('userOrders', this.program, userOrdersPublicKey);
44
+ yield this.userOrdersAccountSubscriber.subscribe((data) => {
45
+ this.eventEmitter.emit('userOrdersData', data);
46
+ this.eventEmitter.emit('update');
47
+ });
41
48
  this.eventEmitter.emit('update');
42
49
  this.isSubscribed = true;
43
50
  return true;
@@ -48,6 +55,7 @@ class DefaultUserAccountSubscriber {
48
55
  yield Promise.all([
49
56
  this.userDataAccountSubscriber.fetch(),
50
57
  this.userPositionsAccountSubscriber.fetch(),
58
+ this.userOrdersAccountSubscriber.fetch(),
51
59
  ]);
52
60
  });
53
61
  }
@@ -59,6 +67,7 @@ class DefaultUserAccountSubscriber {
59
67
  yield Promise.all([
60
68
  this.userDataAccountSubscriber.unsubscribe(),
61
69
  this.userPositionsAccountSubscriber.unsubscribe(),
70
+ yield this.userOrdersAccountSubscriber.unsubscribe(),
62
71
  ]);
63
72
  this.isSubscribed = false;
64
73
  });
@@ -76,5 +85,8 @@ class DefaultUserAccountSubscriber {
76
85
  this.assertIsSubscribed();
77
86
  return this.userPositionsAccountSubscriber.data;
78
87
  }
88
+ getUserOrdersAccount() {
89
+ return this.userOrdersAccountSubscriber.data;
90
+ }
79
91
  }
80
- exports.DefaultUserAccountSubscriber = DefaultUserAccountSubscriber;
92
+ exports.WebSocketUserAccountSubscriber = WebSocketUserAccountSubscriber;
@@ -1,6 +1,9 @@
1
1
  import { PublicKey } from '@solana/web3.js';
2
2
  export declare function getClearingHouseStateAccountPublicKeyAndNonce(programId: PublicKey): Promise<[PublicKey, number]>;
3
+ export declare function getOrderStateAccountPublicKey(programId: PublicKey): Promise<PublicKey>;
4
+ export declare function getOrderStateAccountPublicKeyAndNonce(programId: PublicKey): Promise<[PublicKey, number]>;
3
5
  export declare function getClearingHouseStateAccountPublicKey(programId: PublicKey): Promise<PublicKey>;
4
6
  export declare function getUserAccountPublicKeyAndNonce(programId: PublicKey, authority: PublicKey): Promise<[PublicKey, number]>;
5
7
  export declare function getUserAccountPublicKey(programId: PublicKey, authority: PublicKey): Promise<PublicKey>;
6
- //# sourceMappingURL=addresses.d.ts.map
8
+ export declare function getUserOrdersAccountPublicKeyAndNonce(programId: PublicKey, userAccount: PublicKey): Promise<[PublicKey, number]>;
9
+ export declare function getUserOrdersAccountPublicKey(programId: PublicKey, userAccount: PublicKey): Promise<PublicKey>;
package/lib/addresses.js CHANGED
@@ -28,7 +28,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
28
28
  });
29
29
  };
30
30
  Object.defineProperty(exports, "__esModule", { value: true });
31
- exports.getUserAccountPublicKey = exports.getUserAccountPublicKeyAndNonce = exports.getClearingHouseStateAccountPublicKey = exports.getClearingHouseStateAccountPublicKeyAndNonce = void 0;
31
+ exports.getUserOrdersAccountPublicKey = exports.getUserOrdersAccountPublicKeyAndNonce = exports.getUserAccountPublicKey = exports.getUserAccountPublicKeyAndNonce = exports.getClearingHouseStateAccountPublicKey = exports.getOrderStateAccountPublicKeyAndNonce = exports.getOrderStateAccountPublicKey = exports.getClearingHouseStateAccountPublicKeyAndNonce = void 0;
32
32
  const anchor = __importStar(require("@project-serum/anchor"));
33
33
  function getClearingHouseStateAccountPublicKeyAndNonce(programId) {
34
34
  return __awaiter(this, void 0, void 0, function* () {
@@ -36,6 +36,18 @@ function getClearingHouseStateAccountPublicKeyAndNonce(programId) {
36
36
  });
37
37
  }
38
38
  exports.getClearingHouseStateAccountPublicKeyAndNonce = getClearingHouseStateAccountPublicKeyAndNonce;
39
+ function getOrderStateAccountPublicKey(programId) {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ return (yield getOrderStateAccountPublicKeyAndNonce(programId))[0];
42
+ });
43
+ }
44
+ exports.getOrderStateAccountPublicKey = getOrderStateAccountPublicKey;
45
+ function getOrderStateAccountPublicKeyAndNonce(programId) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('order_state'))], programId);
48
+ });
49
+ }
50
+ exports.getOrderStateAccountPublicKeyAndNonce = getOrderStateAccountPublicKeyAndNonce;
39
51
  function getClearingHouseStateAccountPublicKey(programId) {
40
52
  return __awaiter(this, void 0, void 0, function* () {
41
53
  return (yield getClearingHouseStateAccountPublicKeyAndNonce(programId))[0];
@@ -54,3 +66,18 @@ function getUserAccountPublicKey(programId, authority) {
54
66
  });
55
67
  }
56
68
  exports.getUserAccountPublicKey = getUserAccountPublicKey;
69
+ function getUserOrdersAccountPublicKeyAndNonce(programId, userAccount) {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ return anchor.web3.PublicKey.findProgramAddress([
72
+ Buffer.from(anchor.utils.bytes.utf8.encode('user_orders')),
73
+ userAccount.toBuffer(),
74
+ ], programId);
75
+ });
76
+ }
77
+ exports.getUserOrdersAccountPublicKeyAndNonce = getUserOrdersAccountPublicKeyAndNonce;
78
+ function getUserOrdersAccountPublicKey(programId, userAccount) {
79
+ return __awaiter(this, void 0, void 0, function* () {
80
+ return (yield getUserOrdersAccountPublicKeyAndNonce(programId, userAccount))[0];
81
+ });
82
+ }
83
+ exports.getUserOrdersAccountPublicKey = getUserOrdersAccountPublicKey;
package/lib/admin.d.ts CHANGED
@@ -1,11 +1,16 @@
1
1
  /// <reference types="bn.js" />
2
2
  import { ConfirmOptions, Connection, PublicKey, TransactionSignature } from '@solana/web3.js';
3
- import { FeeStructure, IWallet, OracleGuardRails, OracleSource } from './types';
3
+ import { FeeStructure, IWallet, OracleGuardRails, OracleSource, OrderFillerRewardStructure } from './types';
4
4
  import { BN } from '@project-serum/anchor';
5
5
  import { ClearingHouse } from './clearingHouse';
6
6
  export declare class Admin extends ClearingHouse {
7
7
  static from(connection: Connection, wallet: IWallet, clearingHouseProgramId: PublicKey, opts?: ConfirmOptions): Admin;
8
- initialize(usdcMint: PublicKey, adminControlsPrices: boolean): Promise<[TransactionSignature, TransactionSignature]>;
8
+ initialize(usdcMint: PublicKey, adminControlsPrices: boolean): Promise<[
9
+ TransactionSignature,
10
+ TransactionSignature,
11
+ TransactionSignature
12
+ ]>;
13
+ initializeOrderState(): Promise<TransactionSignature>;
9
14
  initializeMarket(marketIndex: BN, priceOracle: PublicKey, baseAssetReserve: BN, quoteAssetReserve: BN, periodicity: BN, pegMultiplier?: BN): Promise<TransactionSignature>;
10
15
  moveAmmPrice(baseAssetReserve: BN, quoteAssetReserve: BN, marketIndex: BN): Promise<TransactionSignature>;
11
16
  updateK(sqrtK: BN, marketIndex: BN): Promise<TransactionSignature>;
@@ -24,10 +29,12 @@ export declare class Admin extends ClearingHouse {
24
29
  updateFullLiquidationPenaltyPercentage(numerator: BN, denominator: BN): Promise<TransactionSignature>;
25
30
  updatePartialLiquidationShareDenominator(denominator: BN): Promise<TransactionSignature>;
26
31
  updateFullLiquidationShareDenominator(denominator: BN): Promise<TransactionSignature>;
32
+ updateOrderFillerRewardStructure(orderFillerRewardStructure: OrderFillerRewardStructure): Promise<TransactionSignature>;
27
33
  updateFee(fees: FeeStructure): Promise<TransactionSignature>;
28
34
  updateOracleGuardRails(oracleGuardRails: OracleGuardRails): Promise<TransactionSignature>;
29
35
  updateMarketOracle(marketIndex: BN, oracle: PublicKey, oracleSource: OracleSource): Promise<TransactionSignature>;
30
- updateMarketMinimumTradeSize(marketIndex: BN, minimumTradeSize: BN): Promise<TransactionSignature>;
36
+ updateMarketMinimumQuoteAssetTradeSize(marketIndex: BN, minimumTradeSize: BN): Promise<TransactionSignature>;
37
+ updateMarketMinimumBaseAssetTradeSize(marketIndex: BN, minimumTradeSize: BN): Promise<TransactionSignature>;
31
38
  updateWhitelistMint(whitelistMint?: PublicKey): Promise<TransactionSignature>;
32
39
  updateDiscountMint(discountMint: PublicKey): Promise<TransactionSignature>;
33
40
  updateMaxDeposit(maxDeposit: BN): Promise<TransactionSignature>;
@@ -35,4 +42,3 @@ export declare class Admin extends ClearingHouse {
35
42
  updateExchangePaused(exchangePaused: boolean): Promise<TransactionSignature>;
36
43
  disableAdminControlsPrices(): Promise<TransactionSignature>;
37
44
  }
38
- //# sourceMappingURL=admin.d.ts.map