@drift-labs/sdk 0.1.18-master.8 → 0.1.18-orders.3

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 (134) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +32 -0
  2. package/lib/accounts/bulkAccountLoader.js +156 -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 +133 -0
  11. package/lib/accounts/types.d.ts +34 -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 +0 -1
  15. package/lib/accounts/{defaultClearingHouseAccountSubscriber.d.ts → webSocketClearingHouseAccountSubscriber.d.ts} +8 -3
  16. package/lib/accounts/{defaultClearingHouseAccountSubscriber.js → webSocketClearingHouseAccountSubscriber.js} +30 -4
  17. package/lib/accounts/{defaultUserAccountSubscriber.d.ts → webSocketUserAccountSubscriber.d.ts} +6 -3
  18. package/lib/accounts/{defaultUserAccountSubscriber.js → webSocketUserAccountSubscriber.js} +16 -4
  19. package/lib/addresses.d.ts +4 -1
  20. package/lib/addresses.js +28 -1
  21. package/lib/admin.d.ts +11 -4
  22. package/lib/admin.js +71 -17
  23. package/lib/assert/assert.d.ts +0 -1
  24. package/lib/clearingHouse.d.ts +35 -4
  25. package/lib/clearingHouse.js +294 -23
  26. package/lib/clearingHouseUser.d.ts +19 -18
  27. package/lib/clearingHouseUser.js +129 -101
  28. package/lib/config.d.ts +0 -1
  29. package/lib/config.js +1 -1
  30. package/lib/constants/markets.d.ts +0 -1
  31. package/lib/constants/numericConstants.d.ts +0 -1
  32. package/lib/examples/makeTradeExample.d.ts +0 -1
  33. package/lib/examples/makeTradeExample.js +6 -6
  34. package/lib/factory/clearingHouse.d.ts +25 -0
  35. package/lib/factory/clearingHouse.js +64 -0
  36. package/lib/factory/clearingHouseUser.d.ts +19 -0
  37. package/lib/factory/clearingHouseUser.js +34 -0
  38. package/lib/idl/clearing_house.json +1077 -54
  39. package/lib/index.d.ts +11 -3
  40. package/lib/index.js +12 -2
  41. package/lib/math/amm.d.ts +1 -1
  42. package/lib/math/amm.js +38 -15
  43. package/lib/math/conversion.d.ts +0 -1
  44. package/lib/math/conversion.js +1 -1
  45. package/lib/math/funding.d.ts +0 -1
  46. package/lib/math/funding.js +1 -1
  47. package/lib/math/insuranceFund.d.ts +0 -1
  48. package/lib/math/market.d.ts +2 -2
  49. package/lib/math/market.js +12 -2
  50. package/lib/math/orders.d.ts +3 -0
  51. package/lib/math/orders.js +30 -0
  52. package/lib/math/position.d.ts +4 -2
  53. package/lib/math/position.js +19 -5
  54. package/lib/math/trade.d.ts +0 -1
  55. package/lib/math/trade.js +16 -16
  56. package/lib/math/utils.d.ts +2 -3
  57. package/lib/math/utils.js +6 -3
  58. package/lib/mockUSDCFaucet.d.ts +0 -1
  59. package/lib/orderParams.d.ts +7 -0
  60. package/lib/orderParams.js +108 -0
  61. package/lib/orders.d.ts +5 -0
  62. package/lib/orders.js +136 -0
  63. package/lib/pythClient.d.ts +0 -1
  64. package/lib/pythClient.js +1 -1
  65. package/lib/token/index.d.ts +3 -0
  66. package/lib/token/index.js +38 -0
  67. package/lib/tx/defaultTxSender.d.ts +0 -1
  68. package/lib/tx/types.d.ts +0 -1
  69. package/lib/tx/utils.d.ts +0 -1
  70. package/lib/types.d.ts +144 -2
  71. package/lib/types.js +36 -1
  72. package/lib/util/computeUnits.d.ts +0 -1
  73. package/lib/util/tps.d.ts +0 -1
  74. package/lib/wallet.d.ts +0 -1
  75. package/package.json +3 -3
  76. package/src/accounts/bulkAccountLoader.ts +195 -0
  77. package/src/accounts/bulkUserSubscription.ts +28 -0
  78. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +326 -0
  79. package/src/accounts/pollingTokenAccountSubscriber.ts +99 -0
  80. package/src/accounts/pollingUserAccountSubscriber.ts +186 -0
  81. package/src/accounts/types.ts +43 -1
  82. package/src/accounts/utils.ts +3 -0
  83. package/src/accounts/{defaultClearingHouseAccountSubscriber.ts → webSocketClearingHouseAccountSubscriber.ts} +51 -1
  84. package/src/accounts/{defaultUserAccountSubscriber.ts → webSocketUserAccountSubscriber.ts} +33 -3
  85. package/src/addresses.ts +37 -0
  86. package/src/admin.ts +111 -24
  87. package/src/clearingHouse.ts +399 -22
  88. package/src/clearingHouseUser.ts +179 -107
  89. package/src/config.ts +1 -1
  90. package/src/factory/clearingHouse.ts +125 -0
  91. package/src/factory/clearingHouseUser.ts +73 -0
  92. package/src/idl/clearing_house.json +1077 -54
  93. package/src/index.ts +11 -2
  94. package/src/math/amm.ts +47 -14
  95. package/src/math/market.ts +28 -2
  96. package/src/math/orders.ts +39 -0
  97. package/src/math/position.ts +23 -3
  98. package/src/math/utils.ts +1 -1
  99. package/src/orderParams.ts +151 -0
  100. package/src/orders.ts +230 -0
  101. package/src/token/index.ts +37 -0
  102. package/src/types.ts +128 -1
  103. package/tsconfig.json +0 -1
  104. package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +0 -1
  105. package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +0 -1
  106. package/lib/accounts/types.d.ts.map +0 -1
  107. package/lib/accounts/webSocketAccountSubscriber.d.ts.map +0 -1
  108. package/lib/addresses.d.ts.map +0 -1
  109. package/lib/admin.d.ts.map +0 -1
  110. package/lib/assert/assert.d.ts.map +0 -1
  111. package/lib/clearingHouse.d.ts.map +0 -1
  112. package/lib/clearingHouseUser.d.ts.map +0 -1
  113. package/lib/config.d.ts.map +0 -1
  114. package/lib/constants/markets.d.ts.map +0 -1
  115. package/lib/constants/numericConstants.d.ts.map +0 -1
  116. package/lib/examples/makeTradeExample.d.ts.map +0 -1
  117. package/lib/index.d.ts.map +0 -1
  118. package/lib/math/amm.d.ts.map +0 -1
  119. package/lib/math/conversion.d.ts.map +0 -1
  120. package/lib/math/funding.d.ts.map +0 -1
  121. package/lib/math/insuranceFund.d.ts.map +0 -1
  122. package/lib/math/market.d.ts.map +0 -1
  123. package/lib/math/position.d.ts.map +0 -1
  124. package/lib/math/trade.d.ts.map +0 -1
  125. package/lib/math/utils.d.ts.map +0 -1
  126. package/lib/mockUSDCFaucet.d.ts.map +0 -1
  127. package/lib/pythClient.d.ts.map +0 -1
  128. package/lib/tx/defaultTxSender.d.ts.map +0 -1
  129. package/lib/tx/types.d.ts.map +0 -1
  130. package/lib/tx/utils.d.ts.map +0 -1
  131. package/lib/types.d.ts.map +0 -1
  132. package/lib/util/computeUnits.d.ts.map +0 -1
  133. package/lib/util/tps.d.ts.map +0 -1
  134. package/lib/wallet.d.ts.map +0 -1
@@ -5,13 +5,19 @@ import {
5
5
  FundingRateHistoryAccount,
6
6
  LiquidationHistoryAccount,
7
7
  MarketsAccount,
8
+ OrderHistoryAccount,
9
+ OrderStateAccount,
8
10
  StateAccount,
9
11
  TradeHistoryAccount,
10
12
  UserAccount,
13
+ UserOrdersAccount,
11
14
  UserPositionsAccount,
12
15
  } from '../types';
13
16
  import StrictEventEmitter from 'strict-event-emitter-types';
14
17
  import { EventEmitter } from 'events';
18
+ import { PublicKey } from '@solana/web3.js';
19
+ import { AccountInfo } from '@solana/spl-token';
20
+ import { ClearingHouseConfigType, ClearingHouseUserConfigType } from '..';
15
21
 
16
22
  export interface AccountSubscriber<T> {
17
23
  data?: T;
@@ -35,7 +41,10 @@ export interface ClearingHouseAccountEvents {
35
41
  liquidationHistoryAccountUpdate: (payload: LiquidationHistoryAccount) => void;
36
42
  depositHistoryAccountUpdate: (payload: DepositHistoryAccount) => void;
37
43
  curveHistoryAccountUpdate: (payload: ExtendedCurveHistoryAccount) => void;
44
+ orderHistoryAccountUpdate: (payload: OrderHistoryAccount) => void;
45
+ orderStateAccountUpdate: (payload: OrderStateAccount) => void;
38
46
  update: void;
47
+ error: (e: Error) => void;
39
48
  }
40
49
 
41
50
  export type ClearingHouseAccountTypes =
@@ -44,7 +53,8 @@ export type ClearingHouseAccountTypes =
44
53
  | 'fundingPaymentHistoryAccount'
45
54
  | 'fundingRateHistoryAccount'
46
55
  | 'curveHistoryAccount'
47
- | 'liquidationHistoryAccount';
56
+ | 'liquidationHistoryAccount'
57
+ | 'orderHistoryAccount';
48
58
 
49
59
  export interface ClearingHouseAccountSubscriber {
50
60
  eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
@@ -66,12 +76,18 @@ export interface ClearingHouseAccountSubscriber {
66
76
  getFundingRateHistoryAccount(): FundingRateHistoryAccount;
67
77
  getCurveHistoryAccount(): ExtendedCurveHistoryAccount;
68
78
  getLiquidationHistoryAccount(): LiquidationHistoryAccount;
79
+ getOrderStateAccount(): OrderStateAccount;
80
+ getOrderHistoryAccount(): OrderHistoryAccount;
81
+
82
+ type: ClearingHouseConfigType;
69
83
  }
70
84
 
71
85
  export interface UserAccountEvents {
72
86
  userAccountData: (payload: UserAccount) => void;
73
87
  userPositionsData: (payload: UserPositionsAccount) => void;
88
+ userOrdersData: (payload: UserOrdersAccount) => void;
74
89
  update: void;
90
+ error: (e: Error) => void;
75
91
  }
76
92
 
77
93
  export interface UserAccountSubscriber {
@@ -84,4 +100,30 @@ export interface UserAccountSubscriber {
84
100
 
85
101
  getUserAccount(): UserAccount;
86
102
  getUserPositionsAccount(): UserPositionsAccount;
103
+ getUserOrdersAccount(): UserOrdersAccount;
104
+ type: ClearingHouseUserConfigType;
105
+ }
106
+
107
+ export interface TokenAccountEvents {
108
+ tokenAccountUpdate: (payload: AccountInfo) => void;
109
+ update: void;
110
+ error: (e: Error) => void;
87
111
  }
112
+
113
+ export interface TokenAccountSubscriber {
114
+ eventEmitter: StrictEventEmitter<EventEmitter, TokenAccountEvents>;
115
+ isSubscribed: boolean;
116
+
117
+ subscribe(): Promise<boolean>;
118
+ fetch(): Promise<void>;
119
+ unsubscribe(): Promise<void>;
120
+
121
+ getTokenAccount(): AccountInfo;
122
+ }
123
+
124
+ export type AccountToPoll = {
125
+ key: string;
126
+ publicKey: PublicKey;
127
+ eventType: string;
128
+ callbackId?: string;
129
+ };
@@ -0,0 +1,3 @@
1
+ export function capitalize(value: string): string {
2
+ return value[0].toUpperCase() + value.slice(1);
3
+ }
@@ -11,6 +11,8 @@ import {
11
11
  FundingRateHistoryAccount,
12
12
  LiquidationHistoryAccount,
13
13
  MarketsAccount,
14
+ OrderHistoryAccount,
15
+ OrderStateAccount,
14
16
  StateAccount,
15
17
  TradeHistoryAccount,
16
18
  } from '../types';
@@ -19,8 +21,9 @@ import StrictEventEmitter from 'strict-event-emitter-types';
19
21
  import { EventEmitter } from 'events';
20
22
  import { getClearingHouseStateAccountPublicKey } from '../addresses';
21
23
  import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
24
+ import { ClearingHouseConfigType } from '../factory/clearingHouse';
22
25
 
23
- export class DefaultClearingHouseAccountSubscriber
26
+ export class WebSocketClearingHouseAccountSubscriber
24
27
  implements ClearingHouseAccountSubscriber
25
28
  {
26
29
  isSubscribed: boolean;
@@ -34,9 +37,13 @@ export class DefaultClearingHouseAccountSubscriber
34
37
  fundingRateHistoryAccountSubscriber?: AccountSubscriber<FundingRateHistoryAccount>;
35
38
  curveHistoryAccountSubscriber?: AccountSubscriber<ExtendedCurveHistoryAccount>;
36
39
  liquidationHistoryAccountSubscriber?: AccountSubscriber<LiquidationHistoryAccount>;
40
+ orderStateAccountSubscriber?: AccountSubscriber<OrderStateAccount>;
41
+ orderHistoryAccountSubscriber?: AccountSubscriber<OrderHistoryAccount>;
37
42
 
38
43
  optionalExtraSubscriptions: ClearingHouseAccountTypes[] = [];
39
44
 
45
+ type: ClearingHouseConfigType = 'websocket';
46
+
40
47
  private isSubscribing = false;
41
48
  private subscriptionPromise: Promise<boolean>;
42
49
  private subscriptionPromiseResolver: (val: boolean) => void;
@@ -92,6 +99,21 @@ export class DefaultClearingHouseAccountSubscriber
92
99
  this.eventEmitter.emit('update');
93
100
  });
94
101
 
102
+ this.orderStateAccountSubscriber = new WebSocketAccountSubscriber(
103
+ 'orderState',
104
+ this.program,
105
+ state.orderState
106
+ );
107
+
108
+ await this.orderStateAccountSubscriber.subscribe(
109
+ (data: OrderStateAccount) => {
110
+ this.eventEmitter.emit('orderStateAccountUpdate', data);
111
+ this.eventEmitter.emit('update');
112
+ }
113
+ );
114
+
115
+ const orderState = this.orderStateAccountSubscriber.data;
116
+
95
117
  // create subscribers for other state accounts
96
118
 
97
119
  this.tradeHistoryAccountSubscriber = new WebSocketAccountSubscriber(
@@ -131,6 +153,12 @@ export class DefaultClearingHouseAccountSubscriber
131
153
  state.extendedCurveHistory
132
154
  );
133
155
 
156
+ this.orderHistoryAccountSubscriber = new WebSocketAccountSubscriber(
157
+ 'orderHistory',
158
+ this.program,
159
+ orderState.orderHistory
160
+ );
161
+
134
162
  const extraSusbcribersToUse: {
135
163
  subscriber: AccountSubscriber<any>;
136
164
  eventType: keyof ClearingHouseAccountEvents;
@@ -172,6 +200,12 @@ export class DefaultClearingHouseAccountSubscriber
172
200
  eventType: 'curveHistoryAccountUpdate',
173
201
  });
174
202
 
203
+ if (optionalSubscriptions?.includes('orderHistoryAccount'))
204
+ extraSusbcribersToUse.push({
205
+ subscriber: this.orderHistoryAccountSubscriber,
206
+ eventType: 'orderHistoryAccountUpdate',
207
+ });
208
+
175
209
  this.optionalExtraSubscriptions = optionalSubscriptions ?? [];
176
210
 
177
211
  // await all subcriptions in parallel to boost performance
@@ -219,6 +253,7 @@ export class DefaultClearingHouseAccountSubscriber
219
253
 
220
254
  await this.stateAccountSubscriber.unsubscribe();
221
255
  await this.marketsAccountSubscriber.unsubscribe();
256
+ await this.orderStateAccountSubscriber.unsubscribe();
222
257
 
223
258
  if (this.optionalExtraSubscriptions.includes('tradeHistoryAccount')) {
224
259
  await this.tradeHistoryAccountSubscriber.unsubscribe();
@@ -246,6 +281,10 @@ export class DefaultClearingHouseAccountSubscriber
246
281
  await this.liquidationHistoryAccountSubscriber.unsubscribe();
247
282
  }
248
283
 
284
+ if (this.optionalExtraSubscriptions.includes('orderHistoryAccount')) {
285
+ await this.orderHistoryAccountSubscriber.unsubscribe();
286
+ }
287
+
249
288
  this.isSubscribed = false;
250
289
  }
251
290
 
@@ -318,4 +357,15 @@ export class DefaultClearingHouseAccountSubscriber
318
357
  this.assertOptionalIsSubscribed('liquidationHistoryAccount');
319
358
  return this.liquidationHistoryAccountSubscriber.data;
320
359
  }
360
+
361
+ public getOrderHistoryAccount(): OrderHistoryAccount {
362
+ this.assertIsSubscribed();
363
+ this.assertOptionalIsSubscribed('orderHistoryAccount');
364
+ return this.orderHistoryAccountSubscriber.data;
365
+ }
366
+
367
+ public getOrderStateAccount(): OrderStateAccount {
368
+ this.assertIsSubscribed();
369
+ return this.orderStateAccountSubscriber.data;
370
+ }
321
371
  }
@@ -8,11 +8,15 @@ import { Program } from '@project-serum/anchor';
8
8
  import StrictEventEmitter from 'strict-event-emitter-types';
9
9
  import { EventEmitter } from 'events';
10
10
  import { PublicKey } from '@solana/web3.js';
11
- import { getUserAccountPublicKey } from '../addresses';
11
+ import {
12
+ getUserAccountPublicKey,
13
+ getUserOrdersAccountPublicKey,
14
+ } from '../addresses';
12
15
  import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
13
- import { UserAccount, UserPositionsAccount } from '../types';
16
+ import { UserAccount, UserOrdersAccount, UserPositionsAccount } from '../types';
17
+ import { ClearingHouseConfigType } from '../factory/clearingHouse';
14
18
 
15
- export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
19
+ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
16
20
  isSubscribed: boolean;
17
21
  program: Program;
18
22
  eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
@@ -20,6 +24,9 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
20
24
 
21
25
  userDataAccountSubscriber: AccountSubscriber<UserAccount>;
22
26
  userPositionsAccountSubscriber: AccountSubscriber<UserPositionsAccount>;
27
+ userOrdersAccountSubscriber: AccountSubscriber<UserOrdersAccount>;
28
+
29
+ type: ClearingHouseConfigType = 'websocket';
23
30
 
24
31
  public constructor(program: Program, authority: PublicKey) {
25
32
  this.isSubscribed = false;
@@ -61,6 +68,23 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
61
68
  }
62
69
  );
63
70
 
71
+ const userOrdersPublicKey = await getUserOrdersAccountPublicKey(
72
+ this.program.programId,
73
+ userPublicKey
74
+ );
75
+
76
+ this.userOrdersAccountSubscriber = new WebSocketAccountSubscriber(
77
+ 'userOrders',
78
+ this.program,
79
+ userOrdersPublicKey
80
+ );
81
+ await this.userOrdersAccountSubscriber.subscribe(
82
+ (data: UserOrdersAccount) => {
83
+ this.eventEmitter.emit('userOrdersData', data);
84
+ this.eventEmitter.emit('update');
85
+ }
86
+ );
87
+
64
88
  this.eventEmitter.emit('update');
65
89
  this.isSubscribed = true;
66
90
  return true;
@@ -70,6 +94,7 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
70
94
  await Promise.all([
71
95
  this.userDataAccountSubscriber.fetch(),
72
96
  this.userPositionsAccountSubscriber.fetch(),
97
+ this.userOrdersAccountSubscriber.fetch(),
73
98
  ]);
74
99
  }
75
100
 
@@ -81,6 +106,7 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
81
106
  await Promise.all([
82
107
  this.userDataAccountSubscriber.unsubscribe(),
83
108
  this.userPositionsAccountSubscriber.unsubscribe(),
109
+ await this.userOrdersAccountSubscriber.unsubscribe(),
84
110
  ]);
85
111
 
86
112
  this.isSubscribed = false;
@@ -103,4 +129,8 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
103
129
  this.assertIsSubscribed();
104
130
  return this.userPositionsAccountSubscriber.data;
105
131
  }
132
+
133
+ public getUserOrdersAccount(): UserOrdersAccount {
134
+ return this.userOrdersAccountSubscriber.data;
135
+ }
106
136
  }
package/src/addresses.ts CHANGED
@@ -10,6 +10,21 @@ export async function getClearingHouseStateAccountPublicKeyAndNonce(
10
10
  );
11
11
  }
12
12
 
13
+ export async function getOrderStateAccountPublicKey(
14
+ programId: PublicKey
15
+ ): Promise<PublicKey> {
16
+ return (await getOrderStateAccountPublicKeyAndNonce(programId))[0];
17
+ }
18
+
19
+ export async function getOrderStateAccountPublicKeyAndNonce(
20
+ programId: PublicKey
21
+ ): Promise<[PublicKey, number]> {
22
+ return anchor.web3.PublicKey.findProgramAddress(
23
+ [Buffer.from(anchor.utils.bytes.utf8.encode('order_state'))],
24
+ programId
25
+ );
26
+ }
27
+
13
28
  export async function getClearingHouseStateAccountPublicKey(
14
29
  programId: PublicKey
15
30
  ): Promise<PublicKey> {
@@ -32,3 +47,25 @@ export async function getUserAccountPublicKey(
32
47
  ): Promise<PublicKey> {
33
48
  return (await getUserAccountPublicKeyAndNonce(programId, authority))[0];
34
49
  }
50
+
51
+ export async function getUserOrdersAccountPublicKeyAndNonce(
52
+ programId: PublicKey,
53
+ userAccount: PublicKey
54
+ ): Promise<[PublicKey, number]> {
55
+ return anchor.web3.PublicKey.findProgramAddress(
56
+ [
57
+ Buffer.from(anchor.utils.bytes.utf8.encode('user_orders')),
58
+ userAccount.toBuffer(),
59
+ ],
60
+ programId
61
+ );
62
+ }
63
+
64
+ export async function getUserOrdersAccountPublicKey(
65
+ programId: PublicKey,
66
+ userAccount: PublicKey
67
+ ): Promise<PublicKey> {
68
+ return (
69
+ await getUserOrdersAccountPublicKeyAndNonce(programId, userAccount)
70
+ )[0];
71
+ }
package/src/admin.ts CHANGED
@@ -5,18 +5,29 @@ import {
5
5
  SYSVAR_RENT_PUBKEY,
6
6
  TransactionSignature,
7
7
  } from '@solana/web3.js';
8
- import { FeeStructure, IWallet, OracleGuardRails, OracleSource } from './types';
9
- import { BN, Idl, Program, Provider } from '@project-serum/anchor';
8
+ import {
9
+ FeeStructure,
10
+ IWallet,
11
+ OracleGuardRails,
12
+ OracleSource,
13
+ OrderFillerRewardStructure,
14
+ } from './types';
15
+ import { BN, Provider } from '@project-serum/anchor';
10
16
  import * as anchor from '@project-serum/anchor';
11
- import { getClearingHouseStateAccountPublicKeyAndNonce } from './addresses';
17
+ import {
18
+ getClearingHouseStateAccountPublicKey,
19
+ getClearingHouseStateAccountPublicKeyAndNonce,
20
+ getOrderStateAccountPublicKeyAndNonce,
21
+ } from './addresses';
12
22
  import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
13
23
  import { ClearingHouse } from './clearingHouse';
14
24
  import { PEG_PRECISION } from './constants/numericConstants';
15
- import clearingHouseIDL from './idl/clearing_house.json';
16
- import { DefaultClearingHouseAccountSubscriber } from './accounts/defaultClearingHouseAccountSubscriber';
17
- import { DefaultTxSender } from './tx/defaultTxSender';
18
25
  import { calculateTargetPriceTrade } from './math/trade';
19
26
  import { calculateAmmReservesAfterSwap, getSwapDirection } from './math/amm';
27
+ import {
28
+ getAdmin,
29
+ getWebSocketClearingHouseConfig,
30
+ } from './factory/clearingHouse';
20
31
 
21
32
  export class Admin extends ClearingHouse {
22
33
  public static from(
@@ -25,30 +36,21 @@ export class Admin extends ClearingHouse {
25
36
  clearingHouseProgramId: PublicKey,
26
37
  opts: ConfirmOptions = Provider.defaultOptions()
27
38
  ): Admin {
28
- const provider = new Provider(connection, wallet, opts);
29
- const program = new Program(
30
- clearingHouseIDL as Idl,
31
- clearingHouseProgramId,
32
- provider
33
- );
34
- const accountSubscriber = new DefaultClearingHouseAccountSubscriber(
35
- program
36
- );
37
- const txSender = new DefaultTxSender(provider);
38
- return new Admin(
39
+ const config = getWebSocketClearingHouseConfig(
39
40
  connection,
40
41
  wallet,
41
- program,
42
- accountSubscriber,
43
- txSender,
42
+ clearingHouseProgramId,
44
43
  opts
45
44
  );
45
+ return getAdmin(config);
46
46
  }
47
47
 
48
48
  public async initialize(
49
49
  usdcMint: PublicKey,
50
50
  adminControlsPrices: boolean
51
- ): Promise<[TransactionSignature, TransactionSignature]> {
51
+ ): Promise<
52
+ [TransactionSignature, TransactionSignature, TransactionSignature]
53
+ > {
52
54
  const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(
53
55
  await this.getStatePublicKey()
54
56
  );
@@ -172,7 +174,40 @@ export class Admin extends ClearingHouse {
172
174
  this.opts
173
175
  );
174
176
 
175
- return [initializeTxSig, initializeHistoryTxSig];
177
+ const initializeOrderStateTxSig = await this.initializeOrderState();
178
+
179
+ return [initializeTxSig, initializeHistoryTxSig, initializeOrderStateTxSig];
180
+ }
181
+
182
+ public async initializeOrderState(): Promise<TransactionSignature> {
183
+ const orderHistory = anchor.web3.Keypair.generate();
184
+ const [orderStatePublicKey, orderStateNonce] =
185
+ await getOrderStateAccountPublicKeyAndNonce(this.program.programId);
186
+ const clearingHouseStatePublicKey =
187
+ await getClearingHouseStateAccountPublicKey(this.program.programId);
188
+
189
+ const initializeOrderStateTx =
190
+ await this.program.transaction.initializeOrderState(orderStateNonce, {
191
+ accounts: {
192
+ admin: this.wallet.publicKey,
193
+ state: clearingHouseStatePublicKey,
194
+ orderHistory: orderHistory.publicKey,
195
+ orderState: orderStatePublicKey,
196
+ rent: SYSVAR_RENT_PUBKEY,
197
+ systemProgram: anchor.web3.SystemProgram.programId,
198
+ },
199
+ instructions: [
200
+ await this.program.account.orderHistory.createInstruction(
201
+ orderHistory
202
+ ),
203
+ ],
204
+ });
205
+
206
+ return await this.txSender.send(
207
+ initializeOrderStateTx,
208
+ [orderHistory],
209
+ this.opts
210
+ );
176
211
  }
177
212
 
178
213
  public async initializeMarket(
@@ -338,6 +373,25 @@ export class Admin extends ClearingHouse {
338
373
  });
339
374
  }
340
375
 
376
+ public async resetAmmOracleTwap(
377
+ marketIndex: BN
378
+ ): Promise<TransactionSignature> {
379
+ const state = this.getStateAccount();
380
+ const markets = this.getMarketsAccount();
381
+ const marketData = markets.markets[marketIndex.toNumber()];
382
+ const ammData = marketData.amm;
383
+
384
+ return await this.program.rpc.resetAmmOracleTwap(marketIndex, {
385
+ accounts: {
386
+ state: await this.getStatePublicKey(),
387
+ admin: this.wallet.publicKey,
388
+ oracle: ammData.oracle,
389
+ markets: state.markets,
390
+ curveHistory: state.extendedCurveHistory,
391
+ },
392
+ });
393
+ }
394
+
341
395
  public async withdrawFromInsuranceVault(
342
396
  amount: BN,
343
397
  recipient: PublicKey
@@ -499,6 +553,21 @@ export class Admin extends ClearingHouse {
499
553
  );
500
554
  }
501
555
 
556
+ public async updateOrderFillerRewardStructure(
557
+ orderFillerRewardStructure: OrderFillerRewardStructure
558
+ ): Promise<TransactionSignature> {
559
+ return await this.program.rpc.updateOrderFillerRewardStructure(
560
+ orderFillerRewardStructure,
561
+ {
562
+ accounts: {
563
+ admin: this.wallet.publicKey,
564
+ state: await this.getStatePublicKey(),
565
+ orderState: await this.getOrderStatePublicKey(),
566
+ },
567
+ }
568
+ );
569
+ }
570
+
502
571
  public async updateFee(fees: FeeStructure): Promise<TransactionSignature> {
503
572
  return await this.program.rpc.updateFee(fees, {
504
573
  accounts: {
@@ -539,12 +608,30 @@ export class Admin extends ClearingHouse {
539
608
  );
540
609
  }
541
610
 
542
- public async updateMarketMinimumTradeSize(
611
+ public async updateMarketMinimumQuoteAssetTradeSize(
612
+ marketIndex: BN,
613
+ minimumTradeSize: BN
614
+ ): Promise<TransactionSignature> {
615
+ const state = this.getStateAccount();
616
+ return await this.program.rpc.updateMarketMinimumQuoteAssetTradeSize(
617
+ marketIndex,
618
+ minimumTradeSize,
619
+ {
620
+ accounts: {
621
+ admin: this.wallet.publicKey,
622
+ state: await this.getStatePublicKey(),
623
+ markets: state.markets,
624
+ },
625
+ }
626
+ );
627
+ }
628
+
629
+ public async updateMarketMinimumBaseAssetTradeSize(
543
630
  marketIndex: BN,
544
631
  minimumTradeSize: BN
545
632
  ): Promise<TransactionSignature> {
546
633
  const state = this.getStateAccount();
547
- return await this.program.rpc.updateMarketMinimumTradeSize(
634
+ return await this.program.rpc.updateMarketMinimumBaseAssetTradeSize(
548
635
  marketIndex,
549
636
  minimumTradeSize,
550
637
  {