@drift-labs/sdk 0.1.36-master.5 → 0.2.0-master.0

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 (197) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +7 -6
  2. package/lib/accounts/bulkAccountLoader.js +83 -93
  3. package/lib/accounts/bulkUserSubscription.js +13 -57
  4. package/lib/accounts/fetch.d.ts +4 -0
  5. package/lib/accounts/fetch.js +18 -0
  6. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +34 -38
  7. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +224 -224
  8. package/lib/accounts/pollingOracleSubscriber.d.ts +3 -3
  9. package/lib/accounts/pollingOracleSubscriber.js +37 -49
  10. package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
  11. package/lib/accounts/pollingTokenAccountSubscriber.js +35 -50
  12. package/lib/accounts/pollingUserAccountSubscriber.d.ts +7 -13
  13. package/lib/accounts/pollingUserAccountSubscriber.js +71 -134
  14. package/lib/accounts/types.d.ts +34 -41
  15. package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -4
  16. package/lib/accounts/webSocketAccountSubscriber.js +39 -35
  17. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +33 -28
  18. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +153 -185
  19. package/lib/accounts/webSocketUserAccountSubscriber.d.ts +5 -11
  20. package/lib/accounts/webSocketUserAccountSubscriber.js +22 -67
  21. package/lib/addresses/marketAddresses.d.ts +4 -0
  22. package/lib/addresses/marketAddresses.js +15 -0
  23. package/lib/addresses/pda.d.ts +12 -0
  24. package/lib/addresses/pda.js +83 -0
  25. package/lib/admin.d.ts +8 -12
  26. package/lib/admin.js +366 -490
  27. package/lib/clearingHouse.d.ts +84 -103
  28. package/lib/clearingHouse.js +779 -810
  29. package/lib/clearingHouseConfig.d.ts +34 -0
  30. package/lib/clearingHouseConfig.js +2 -0
  31. package/lib/clearingHouseUser.d.ts +19 -18
  32. package/lib/clearingHouseUser.js +157 -115
  33. package/lib/clearingHouseUserConfig.d.ts +14 -0
  34. package/lib/clearingHouseUserConfig.js +2 -0
  35. package/lib/config.d.ts +12 -0
  36. package/lib/config.js +35 -4
  37. package/lib/constants/banks.d.ts +16 -0
  38. package/lib/constants/banks.js +34 -0
  39. package/lib/constants/markets.d.ts +8 -3
  40. package/lib/constants/markets.js +13 -206
  41. package/lib/constants/numericConstants.d.ts +16 -0
  42. package/lib/constants/numericConstants.js +22 -6
  43. package/lib/events/eventList.d.ts +22 -0
  44. package/lib/events/eventList.js +77 -0
  45. package/lib/events/eventSubscriber.d.ts +34 -0
  46. package/lib/events/eventSubscriber.js +126 -0
  47. package/lib/events/fetchLogs.d.ts +13 -0
  48. package/lib/events/fetchLogs.js +39 -0
  49. package/lib/events/pollingLogProvider.d.ts +15 -0
  50. package/lib/events/pollingLogProvider.js +53 -0
  51. package/lib/events/sort.d.ts +2 -0
  52. package/lib/events/sort.js +44 -0
  53. package/lib/events/txEventCache.d.ts +24 -0
  54. package/lib/events/txEventCache.js +71 -0
  55. package/lib/events/types.d.ts +49 -0
  56. package/lib/events/types.js +20 -0
  57. package/lib/events/webSocketLogProvider.d.ts +12 -0
  58. package/lib/events/webSocketLogProvider.js +30 -0
  59. package/lib/examples/makeTradeExample.js +26 -27
  60. package/lib/factory/bigNum.d.ts +112 -0
  61. package/lib/factory/bigNum.js +356 -0
  62. package/lib/factory/oracleClient.d.ts +1 -2
  63. package/lib/factory/oracleClient.js +6 -2
  64. package/lib/idl/clearing_house.json +1739 -2287
  65. package/lib/index.d.ts +13 -4
  66. package/lib/index.js +13 -4
  67. package/lib/math/amm.d.ts +19 -29
  68. package/lib/math/amm.js +129 -179
  69. package/lib/math/auction.d.ts +5 -0
  70. package/lib/math/auction.js +39 -0
  71. package/lib/math/bankBalance.d.ts +9 -0
  72. package/lib/math/bankBalance.js +75 -0
  73. package/lib/math/conversion.d.ts +0 -1
  74. package/lib/math/conversion.js +1 -5
  75. package/lib/math/funding.d.ts +6 -6
  76. package/lib/math/funding.js +158 -175
  77. package/lib/math/market.d.ts +6 -6
  78. package/lib/math/market.js +10 -9
  79. package/lib/math/oracles.d.ts +3 -0
  80. package/lib/math/oracles.js +26 -0
  81. package/lib/math/orders.d.ts +5 -0
  82. package/lib/math/orders.js +31 -1
  83. package/lib/math/position.d.ts +7 -4
  84. package/lib/math/position.js +27 -9
  85. package/lib/math/repeg.d.ts +22 -0
  86. package/lib/math/repeg.js +128 -0
  87. package/lib/math/trade.d.ts +13 -8
  88. package/lib/math/trade.js +85 -22
  89. package/lib/mockUSDCFaucet.js +87 -116
  90. package/lib/oracles/oracleClientCache.d.ts +8 -0
  91. package/lib/oracles/oracleClientCache.js +19 -0
  92. package/lib/oracles/pythClient.d.ts +3 -5
  93. package/lib/oracles/pythClient.js +12 -30
  94. package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
  95. package/lib/oracles/quoteAssetOracleClient.js +21 -0
  96. package/lib/oracles/switchboardClient.d.ts +3 -5
  97. package/lib/oracles/switchboardClient.js +29 -47
  98. package/lib/oracles/types.d.ts +7 -1
  99. package/lib/orders.d.ts +6 -6
  100. package/lib/orders.js +10 -9
  101. package/lib/slot/SlotSubscriber.d.ts +12 -0
  102. package/lib/slot/SlotSubscriber.js +23 -0
  103. package/lib/tx/retryTxSender.d.ts +2 -2
  104. package/lib/tx/retryTxSender.js +108 -123
  105. package/lib/tx/types.d.ts +5 -1
  106. package/lib/tx/utils.d.ts +1 -1
  107. package/lib/tx/utils.js +11 -2
  108. package/lib/types.d.ts +105 -98
  109. package/lib/types.js +13 -1
  110. package/lib/userName.d.ts +4 -0
  111. package/lib/userName.js +20 -0
  112. package/lib/util/computeUnits.js +10 -21
  113. package/lib/util/tps.js +11 -22
  114. package/lib/wallet.js +7 -20
  115. package/package.json +10 -3
  116. package/src/accounts/bulkAccountLoader.ts +26 -15
  117. package/src/accounts/bulkUserSubscription.ts +1 -45
  118. package/src/accounts/fetch.ts +33 -0
  119. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
  120. package/src/accounts/pollingOracleSubscriber.ts +16 -8
  121. package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
  122. package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
  123. package/src/accounts/types.ts +41 -70
  124. package/src/accounts/webSocketAccountSubscriber.ts +33 -16
  125. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
  126. package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
  127. package/src/addresses/marketAddresses.ts +18 -0
  128. package/src/addresses/pda.ts +118 -0
  129. package/src/admin.ts +209 -267
  130. package/src/clearingHouse.ts +921 -829
  131. package/src/clearingHouseConfig.ts +37 -0
  132. package/src/clearingHouseUser.ts +280 -127
  133. package/src/clearingHouseUserConfig.ts +18 -0
  134. package/src/config.ts +54 -1
  135. package/src/constants/banks.ts +43 -0
  136. package/src/constants/markets.ts +16 -207
  137. package/src/constants/numericConstants.ts +33 -5
  138. package/src/events/eventList.ts +94 -0
  139. package/src/events/eventSubscriber.ts +194 -0
  140. package/src/events/fetchLogs.ts +80 -0
  141. package/src/events/pollingLogProvider.ts +79 -0
  142. package/src/events/sort.ts +65 -0
  143. package/src/events/txEventCache.ts +74 -0
  144. package/src/events/types.ts +98 -0
  145. package/src/events/webSocketLogProvider.ts +38 -0
  146. package/src/examples/makeTradeExample.ts +20 -11
  147. package/src/factory/bigNum.ts +507 -0
  148. package/src/factory/oracleClient.ts +7 -4
  149. package/src/idl/clearing_house.json +1739 -2287
  150. package/src/index.ts +13 -4
  151. package/src/math/amm.ts +229 -245
  152. package/src/math/auction.ts +39 -0
  153. package/src/math/bankBalance.ts +112 -0
  154. package/src/math/conversion.ts +1 -11
  155. package/src/math/funding.ts +12 -9
  156. package/src/math/market.ts +37 -30
  157. package/src/math/oracles.ts +36 -0
  158. package/src/math/orders.ts +38 -0
  159. package/src/math/position.ts +48 -13
  160. package/src/math/repeg.ts +175 -0
  161. package/src/math/trade.ts +114 -36
  162. package/src/math/utils.js +27 -0
  163. package/src/math/utils.js.map +1 -0
  164. package/src/oracles/oracleClientCache.ts +20 -0
  165. package/src/oracles/pythClient.ts +5 -11
  166. package/src/oracles/quoteAssetOracleClient.ts +25 -0
  167. package/src/oracles/switchboardClient.ts +16 -24
  168. package/src/oracles/types.ts +8 -1
  169. package/src/orders.ts +35 -20
  170. package/src/slot/SlotSubscriber.ts +32 -0
  171. package/src/tx/retryTxSender.ts +6 -4
  172. package/src/tx/types.ts +6 -1
  173. package/src/tx/utils.ts +22 -3
  174. package/src/types.ts +108 -110
  175. package/src/userName.ts +20 -0
  176. package/src/util/computeUnits.js +17 -0
  177. package/src/util/computeUnits.js.map +1 -0
  178. package/tests/bn/test.ts +255 -0
  179. package/tsconfig.json +12 -12
  180. package/lib/addresses.d.ts +0 -9
  181. package/lib/addresses.js +0 -87
  182. package/lib/constants/accounts.d.ts +0 -15
  183. package/lib/constants/accounts.js +0 -18
  184. package/lib/factory/clearingHouse.d.ts +0 -35
  185. package/lib/factory/clearingHouse.js +0 -81
  186. package/lib/factory/clearingHouseUser.d.ts +0 -19
  187. package/lib/factory/clearingHouseUser.js +0 -34
  188. package/lib/math/insuranceFund.d.ts +0 -15
  189. package/lib/math/insuranceFund.js +0 -33
  190. package/lib/tx/defaultTxSender.d.ts +0 -8
  191. package/lib/tx/defaultTxSender.js +0 -12
  192. package/src/addresses.ts +0 -71
  193. package/src/constants/accounts.ts +0 -26
  194. package/src/factory/clearingHouse.ts +0 -173
  195. package/src/factory/clearingHouseUser.ts +0 -73
  196. package/src/math/insuranceFund.ts +0 -29
  197. package/src/tx/defaultTxSender.ts +0 -24
@@ -1,4 +1,5 @@
1
1
  import {
2
+ DataAndSlot,
2
3
  NotSubscribedError,
3
4
  TokenAccountEvents,
4
5
  TokenAccountSubscriber,
@@ -21,7 +22,7 @@ export class PollingTokenAccountSubscriber implements TokenAccountSubscriber {
21
22
  callbackId?: string;
22
23
  errorCallbackId?: string;
23
24
 
24
- tokenAccount?: AccountInfo;
25
+ tokenAccountAndSlot?: DataAndSlot<AccountInfo>;
25
26
 
26
27
  public constructor(publicKey: PublicKey, accountLoader: BulkAccountLoader) {
27
28
  this.isSubscribed = false;
@@ -59,9 +60,9 @@ export class PollingTokenAccountSubscriber implements TokenAccountSubscriber {
59
60
 
60
61
  this.callbackId = this.accountLoader.addAccount(
61
62
  this.publicKey,
62
- (buffer) => {
63
+ (buffer, slot: number) => {
63
64
  const tokenAccount = parseTokenAccount(buffer);
64
- this.tokenAccount = tokenAccount;
65
+ this.tokenAccountAndSlot = { data: tokenAccount, slot };
65
66
  // @ts-ignore
66
67
  this.eventEmitter.emit('tokenAccountUpdate', tokenAccount);
67
68
  this.eventEmitter.emit('update');
@@ -75,8 +76,10 @@ export class PollingTokenAccountSubscriber implements TokenAccountSubscriber {
75
76
 
76
77
  async fetch(): Promise<void> {
77
78
  await this.accountLoader.load();
78
- const buffer = this.accountLoader.getAccountData(this.publicKey);
79
- this.tokenAccount = parseTokenAccount(buffer);
79
+ const { buffer, slot } = this.accountLoader.getBufferAndSlot(
80
+ this.publicKey
81
+ );
82
+ this.tokenAccountAndSlot = { data: parseTokenAccount(buffer), slot };
80
83
  }
81
84
 
82
85
  async unsubscribe(): Promise<void> {
@@ -101,12 +104,12 @@ export class PollingTokenAccountSubscriber implements TokenAccountSubscriber {
101
104
  }
102
105
  }
103
106
 
104
- public getTokenAccount(): AccountInfo {
107
+ public getTokenAccountAndSlot(): DataAndSlot<AccountInfo> {
105
108
  this.assertIsSubscribed();
106
- return this.tokenAccount;
109
+ return this.tokenAccountAndSlot;
107
110
  }
108
111
 
109
112
  didSubscriptionSucceed(): boolean {
110
- return !!this.tokenAccount;
113
+ return !!this.tokenAccountAndSlot;
111
114
  }
112
115
  }
@@ -1,49 +1,40 @@
1
1
  import {
2
+ DataAndSlot,
2
3
  AccountToPoll,
3
4
  NotSubscribedError,
4
5
  UserAccountEvents,
5
6
  UserAccountSubscriber,
6
- UserPublicKeys,
7
7
  } from './types';
8
8
  import { Program } from '@project-serum/anchor';
9
9
  import StrictEventEmitter from 'strict-event-emitter-types';
10
10
  import { EventEmitter } from 'events';
11
11
  import { PublicKey } from '@solana/web3.js';
12
- import {
13
- getUserAccountPublicKey,
14
- getUserOrdersAccountPublicKey,
15
- } from '../addresses';
16
- import { UserAccount, UserOrdersAccount, UserPositionsAccount } from '../types';
12
+ import { UserAccount } from '../types';
17
13
  import { BulkAccountLoader } from './bulkAccountLoader';
18
14
  import { capitalize } from './utils';
19
- import { ClearingHouseConfigType } from '../factory/clearingHouse';
20
15
 
21
16
  export class PollingUserAccountSubscriber implements UserAccountSubscriber {
22
17
  isSubscribed: boolean;
23
18
  program: Program;
24
19
  eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
25
- authority: PublicKey;
20
+ userAccountPublicKey: PublicKey;
26
21
 
27
22
  accountLoader: BulkAccountLoader;
28
23
  accountsToPoll = new Map<string, AccountToPoll>();
29
24
  errorCallbackId?: string;
30
25
 
31
- user?: UserAccount;
32
- userPositions?: UserPositionsAccount;
33
- userOrders?: UserOrdersAccount;
34
-
35
- type: ClearingHouseConfigType = 'polling';
26
+ user?: DataAndSlot<UserAccount>;
36
27
 
37
28
  public constructor(
38
29
  program: Program,
39
- authority: PublicKey,
30
+ userAccountPublicKey: PublicKey,
40
31
  accountLoader: BulkAccountLoader
41
32
  ) {
42
33
  this.isSubscribed = false;
43
34
  this.program = program;
44
- this.authority = authority;
45
35
  this.accountLoader = accountLoader;
46
36
  this.eventEmitter = new EventEmitter();
37
+ this.userAccountPublicKey = userAccountPublicKey;
47
38
  }
48
39
 
49
40
  async subscribe(): Promise<boolean> {
@@ -69,69 +60,21 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
69
60
  return subscriptionSucceeded;
70
61
  }
71
62
 
72
- async addToAccountLoader(userPublicKeys?: UserPublicKeys): Promise<void> {
63
+ async addToAccountLoader(): Promise<void> {
73
64
  if (this.accountsToPoll.size > 0) {
74
65
  return;
75
66
  }
76
67
 
77
- if (!userPublicKeys) {
78
- const userPublicKey = await getUserAccountPublicKey(
79
- this.program.programId,
80
- this.authority
81
- );
82
-
83
- const userAccount = (await this.program.account.user.fetch(
84
- userPublicKey
85
- )) as UserAccount;
86
-
87
- this.accountsToPoll.set(userPublicKey.toString(), {
88
- key: 'user',
89
- publicKey: userPublicKey,
90
- eventType: 'userAccountData',
91
- });
92
-
93
- this.accountsToPoll.set(userAccount.positions.toString(), {
94
- key: 'userPositions',
95
- publicKey: userAccount.positions,
96
- eventType: 'userPositionsData',
97
- });
98
-
99
- const userOrdersPublicKey = await getUserOrdersAccountPublicKey(
100
- this.program.programId,
101
- userPublicKey
102
- );
103
-
104
- this.accountsToPoll.set(userOrdersPublicKey.toString(), {
105
- key: 'userOrders',
106
- publicKey: userOrdersPublicKey,
107
- eventType: 'userOrdersData',
108
- });
109
- } else {
110
- this.accountsToPoll.set(userPublicKeys.user.toString(), {
111
- key: 'user',
112
- publicKey: userPublicKeys.user,
113
- eventType: 'userAccountData',
114
- });
115
-
116
- this.accountsToPoll.set(userPublicKeys.userPositions.toString(), {
117
- key: 'userPositions',
118
- publicKey: userPublicKeys.userPositions,
119
- eventType: 'userPositionsData',
120
- });
121
-
122
- if (userPublicKeys.userOrders) {
123
- this.accountsToPoll.set(userPublicKeys.userOrders.toString(), {
124
- key: 'userOrders',
125
- publicKey: userPublicKeys.userOrders,
126
- eventType: 'userOrdersData',
127
- });
128
- }
129
- }
68
+ this.accountsToPoll.set(this.userAccountPublicKey.toString(), {
69
+ key: 'user',
70
+ publicKey: this.userAccountPublicKey,
71
+ eventType: 'userAccountUpdate',
72
+ });
130
73
 
131
74
  for (const [_, accountToPoll] of this.accountsToPoll) {
132
75
  accountToPoll.callbackId = this.accountLoader.addAccount(
133
76
  accountToPoll.publicKey,
134
- (buffer) => {
77
+ (buffer, slot) => {
135
78
  if (!buffer) {
136
79
  return;
137
80
  }
@@ -139,7 +82,7 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
139
82
  const account = this.program.account[
140
83
  accountToPoll.key
141
84
  ].coder.accounts.decode(capitalize(accountToPoll.key), buffer);
142
- this[accountToPoll.key] = account;
85
+ this[accountToPoll.key] = { data: account, slot };
143
86
  // @ts-ignore
144
87
  this.eventEmitter.emit(accountToPoll.eventType, account);
145
88
  this.eventEmitter.emit('update');
@@ -169,11 +112,14 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
169
112
  async fetch(): Promise<void> {
170
113
  await this.accountLoader.load();
171
114
  for (const [_, accountToPoll] of this.accountsToPoll) {
172
- const buffer = this.accountLoader.getAccountData(accountToPoll.publicKey);
115
+ const { buffer, slot } = this.accountLoader.getBufferAndSlot(
116
+ accountToPoll.publicKey
117
+ );
173
118
  if (buffer) {
174
- this[accountToPoll.key] = this.program.account[
119
+ const account = this.program.account[
175
120
  accountToPoll.key
176
121
  ].coder.accounts.decode(capitalize(accountToPoll.key), buffer);
122
+ this[accountToPoll.key] = { data: account, slot };
177
123
  }
178
124
  }
179
125
  }
@@ -181,8 +127,7 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
181
127
  didSubscriptionSucceed(): boolean {
182
128
  let success = true;
183
129
  for (const [_, accountToPoll] of this.accountsToPoll) {
184
- // userOrders may not exist
185
- if (accountToPoll.key !== 'userOrders' && !this[accountToPoll.key]) {
130
+ if (!this[accountToPoll.key]) {
186
131
  success = false;
187
132
  break;
188
133
  }
@@ -218,18 +163,8 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
218
163
  }
219
164
  }
220
165
 
221
- public getUserAccount(): UserAccount {
166
+ public getUserAccountAndSlot(): DataAndSlot<UserAccount> {
222
167
  this.assertIsSubscribed();
223
168
  return this.user;
224
169
  }
225
-
226
- public getUserPositionsAccount(): UserPositionsAccount {
227
- this.assertIsSubscribed();
228
- return this.userPositions;
229
- }
230
-
231
- public getUserOrdersAccount(): UserOrdersAccount {
232
- this.assertIsSubscribed();
233
- return this.userOrders;
234
- }
235
170
  }
@@ -1,30 +1,19 @@
1
1
  import {
2
- DepositHistoryAccount,
3
- ExtendedCurveHistoryAccount,
4
- FundingPaymentHistoryAccount,
5
- FundingRateHistoryAccount,
6
- LiquidationHistoryAccount,
7
- MarketsAccount,
8
- OrderHistoryAccount,
9
- OrderStateAccount,
2
+ BankAccount,
3
+ MarketAccount,
4
+ OracleSource,
10
5
  StateAccount,
11
- TradeHistoryAccount,
12
6
  UserAccount,
13
- UserOrdersAccount,
14
- UserPositionsAccount,
15
7
  } from '../types';
16
8
  import StrictEventEmitter from 'strict-event-emitter-types';
17
9
  import { EventEmitter } from 'events';
18
10
  import { PublicKey } from '@solana/web3.js';
19
11
  import { AccountInfo } from '@solana/spl-token';
20
- import {
21
- ClearingHouseConfigType,
22
- ClearingHouseUserConfigType,
23
- OraclePriceData,
24
- } from '..';
12
+ import { OracleInfo, OraclePriceData } from '..';
13
+ import { BN } from '@project-serum/anchor';
25
14
 
26
15
  export interface AccountSubscriber<T> {
27
- data?: T;
16
+ dataAndSlot?: DataAndSlot<T>;
28
17
  subscribe(onChange: (data: T) => void): Promise<void>;
29
18
  fetch(): Promise<void>;
30
19
  unsubscribe(): Promise<void>;
@@ -36,66 +25,39 @@ export class NotSubscribedError extends Error {
36
25
 
37
26
  export interface ClearingHouseAccountEvents {
38
27
  stateAccountUpdate: (payload: StateAccount) => void;
39
- marketsAccountUpdate: (payload: MarketsAccount) => void;
40
- fundingPaymentHistoryAccountUpdate: (
41
- payload: FundingPaymentHistoryAccount
42
- ) => void;
43
- fundingRateHistoryAccountUpdate: (payload: FundingRateHistoryAccount) => void;
44
- tradeHistoryAccountUpdate: (payload: TradeHistoryAccount) => void;
45
- liquidationHistoryAccountUpdate: (payload: LiquidationHistoryAccount) => void;
46
- depositHistoryAccountUpdate: (payload: DepositHistoryAccount) => void;
47
- curveHistoryAccountUpdate: (payload: ExtendedCurveHistoryAccount) => void;
48
- orderHistoryAccountUpdate: (payload: OrderHistoryAccount) => void;
49
- orderStateAccountUpdate: (payload: OrderStateAccount) => void;
28
+ marketAccountUpdate: (payload: MarketAccount) => void;
29
+ bankAccountUpdate: (payload: BankAccount) => void;
30
+ oraclePriceUpdate: (publicKey: PublicKey, data: OraclePriceData) => void;
31
+ userAccountUpdate: (payload: UserAccount) => void;
50
32
  update: void;
51
33
  error: (e: Error) => void;
52
34
  }
53
35
 
54
- export type ClearingHouseAccountTypes =
55
- | 'tradeHistoryAccount'
56
- | 'depositHistoryAccount'
57
- | 'fundingPaymentHistoryAccount'
58
- | 'fundingRateHistoryAccount'
59
- | 'curveHistoryAccount'
60
- | 'liquidationHistoryAccount'
61
- | 'orderHistoryAccount';
62
-
63
36
  export interface ClearingHouseAccountSubscriber {
64
37
  eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
65
38
  isSubscribed: boolean;
66
39
 
67
- optionalExtraSubscriptions: ClearingHouseAccountTypes[];
68
-
69
- subscribe(
70
- optionalSubscriptions?: ClearingHouseAccountTypes[]
71
- ): Promise<boolean>;
40
+ subscribe(): Promise<boolean>;
72
41
  fetch(): Promise<void>;
73
42
  unsubscribe(): Promise<void>;
74
43
 
75
- getStateAccount(): StateAccount;
76
- getMarketsAccount(): MarketsAccount;
77
- getTradeHistoryAccount(): TradeHistoryAccount;
78
- getDepositHistoryAccount(): DepositHistoryAccount;
79
- getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount;
80
- getFundingRateHistoryAccount(): FundingRateHistoryAccount;
81
- getCurveHistoryAccount(): ExtendedCurveHistoryAccount;
82
- getLiquidationHistoryAccount(): LiquidationHistoryAccount;
83
- getOrderStateAccount(): OrderStateAccount;
84
- getOrderHistoryAccount(): OrderHistoryAccount;
85
-
86
- type: ClearingHouseConfigType;
44
+ addMarket(marketIndex: BN): Promise<boolean>;
45
+ addBank(bankIndex: BN): Promise<boolean>;
46
+ addOracle(oracleInfo: OracleInfo): Promise<boolean>;
47
+
48
+ getStateAccountAndSlot(): DataAndSlot<StateAccount>;
49
+ getMarketAccountAndSlot(
50
+ marketIndex: BN
51
+ ): DataAndSlot<MarketAccount> | undefined;
52
+ getMarketAccountsAndSlots(): DataAndSlot<MarketAccount>[];
53
+ getBankAccountAndSlot(bankIndex: BN): DataAndSlot<BankAccount> | undefined;
54
+ getOraclePriceDataAndSlot(
55
+ oraclePublicKey: PublicKey
56
+ ): DataAndSlot<OraclePriceData> | undefined;
87
57
  }
88
58
 
89
- export type UserPublicKeys = {
90
- user: PublicKey;
91
- userPositions: PublicKey;
92
- userOrders: PublicKey | undefined;
93
- };
94
-
95
59
  export interface UserAccountEvents {
96
- userAccountData: (payload: UserAccount) => void;
97
- userPositionsData: (payload: UserPositionsAccount) => void;
98
- userOrdersData: (payload: UserOrdersAccount) => void;
60
+ userAccountUpdate: (payload: UserAccount) => void;
99
61
  update: void;
100
62
  error: (e: Error) => void;
101
63
  }
@@ -108,10 +70,7 @@ export interface UserAccountSubscriber {
108
70
  fetch(): Promise<void>;
109
71
  unsubscribe(): Promise<void>;
110
72
 
111
- getUserAccount(): UserAccount;
112
- getUserPositionsAccount(): UserPositionsAccount;
113
- getUserOrdersAccount(): UserOrdersAccount;
114
- type: ClearingHouseUserConfigType;
73
+ getUserAccountAndSlot(): DataAndSlot<UserAccount>;
115
74
  }
116
75
 
117
76
  export interface TokenAccountEvents {
@@ -128,7 +87,7 @@ export interface TokenAccountSubscriber {
128
87
  fetch(): Promise<void>;
129
88
  unsubscribe(): Promise<void>;
130
89
 
131
- getTokenAccount(): AccountInfo;
90
+ getTokenAccountAndSlot(): DataAndSlot<AccountInfo>;
132
91
  }
133
92
 
134
93
  export interface OracleEvents {
@@ -145,7 +104,7 @@ export interface OracleSubscriber {
145
104
  fetch(): Promise<void>;
146
105
  unsubscribe(): Promise<void>;
147
106
 
148
- getOraclePriceData(): OraclePriceData;
107
+ getOraclePriceData(): DataAndSlot<OraclePriceData>;
149
108
  }
150
109
 
151
110
  export type AccountToPoll = {
@@ -153,9 +112,21 @@ export type AccountToPoll = {
153
112
  publicKey: PublicKey;
154
113
  eventType: string;
155
114
  callbackId?: string;
115
+ mapKey?: number;
156
116
  };
157
117
 
158
- export type AccountData = {
118
+ export type OraclesToPoll = {
119
+ publicKey: PublicKey;
120
+ source: OracleSource;
121
+ callbackId?: string;
122
+ };
123
+
124
+ export type BufferAndSlot = {
159
125
  slot: number;
160
126
  buffer: Buffer | undefined;
161
127
  };
128
+
129
+ export type DataAndSlot<T> = {
130
+ data: T;
131
+ slot: number;
132
+ };
@@ -1,26 +1,29 @@
1
- import { AccountData, AccountSubscriber } from './types';
1
+ import { DataAndSlot, BufferAndSlot, AccountSubscriber } from './types';
2
2
  import { AnchorProvider, Program } from '@project-serum/anchor';
3
3
  import { AccountInfo, Context, PublicKey } from '@solana/web3.js';
4
4
  import { capitalize } from './utils';
5
5
  import * as Buffer from 'buffer';
6
6
 
7
7
  export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
8
- data?: T;
9
- accountData?: AccountData;
8
+ dataAndSlot?: DataAndSlot<T>;
9
+ bufferAndSlot?: BufferAndSlot;
10
10
  accountName: string;
11
11
  program: Program;
12
12
  accountPublicKey: PublicKey;
13
+ decodeBufferFn: (buffer: Buffer) => T;
13
14
  onChange: (data: T) => void;
14
15
  listenerId?: number;
15
16
 
16
17
  public constructor(
17
18
  accountName: string,
18
19
  program: Program,
19
- accountPublicKey: PublicKey
20
+ accountPublicKey: PublicKey,
21
+ decodeBuffer?: (buffer: Buffer) => T
20
22
  ) {
21
23
  this.accountName = accountName;
22
24
  this.program = program;
23
25
  this.accountPublicKey = accountPublicKey;
26
+ this.decodeBufferFn = decodeBuffer;
24
27
  }
25
28
 
26
29
  async subscribe(onChange: (data: T) => void): Promise<void> {
@@ -56,35 +59,49 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
56
59
  newBuffer = accountInfo.data;
57
60
  }
58
61
 
59
- if (!this.accountData) {
60
- this.accountData = {
62
+ if (!this.bufferAndSlot) {
63
+ this.bufferAndSlot = {
61
64
  buffer: newBuffer,
62
65
  slot: newSlot,
63
66
  };
64
67
  if (newBuffer) {
65
- this.data = this.program.account[
66
- this.accountName
67
- ].coder.accounts.decode(capitalize(this.accountName), newBuffer);
68
- this.onChange(this.data);
68
+ const account = this.decodeBuffer(newBuffer);
69
+ this.dataAndSlot = {
70
+ data: account,
71
+ slot: newSlot,
72
+ };
73
+ this.onChange(account);
69
74
  }
70
75
  return;
71
76
  }
72
77
 
73
- if (newSlot <= this.accountData.slot) {
78
+ if (newSlot <= this.bufferAndSlot.slot) {
74
79
  return;
75
80
  }
76
81
 
77
- const oldBuffer = this.accountData.buffer;
82
+ const oldBuffer = this.bufferAndSlot.buffer;
78
83
  if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
79
- this.accountData = {
84
+ this.bufferAndSlot = {
80
85
  buffer: newBuffer,
81
86
  slot: newSlot,
82
87
  };
83
- this.data = this.program.account[this.accountName].coder.accounts.decode(
88
+ const account = this.decodeBuffer(newBuffer);
89
+ this.dataAndSlot = {
90
+ data: account,
91
+ slot: newSlot,
92
+ };
93
+ this.onChange(account);
94
+ }
95
+ }
96
+
97
+ decodeBuffer(buffer: Buffer): T {
98
+ if (this.decodeBufferFn) {
99
+ return this.decodeBufferFn(buffer);
100
+ } else {
101
+ return this.program.account[this.accountName].coder.accounts.decode(
84
102
  capitalize(this.accountName),
85
- newBuffer
103
+ buffer
86
104
  );
87
- this.onChange(this.data);
88
105
  }
89
106
  }
90
107