@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
package/src/admin.ts CHANGED
@@ -1,56 +1,34 @@
1
1
  import {
2
- ConfirmOptions,
3
- Connection,
4
2
  PublicKey,
5
3
  SYSVAR_RENT_PUBKEY,
6
4
  TransactionSignature,
7
5
  } from '@solana/web3.js';
8
6
  import {
9
7
  FeeStructure,
10
- IWallet,
11
8
  OracleGuardRails,
12
9
  OracleSource,
13
10
  OrderFillerRewardStructure,
14
11
  } from './types';
15
- import { BN, AnchorProvider } from '@project-serum/anchor';
12
+ import { BN } from '@project-serum/anchor';
16
13
  import * as anchor from '@project-serum/anchor';
17
14
  import {
18
- getClearingHouseStateAccountPublicKey,
19
15
  getClearingHouseStateAccountPublicKeyAndNonce,
20
- getOrderStateAccountPublicKeyAndNonce,
21
- } from './addresses';
16
+ getBankVaultAuthorityPublicKey,
17
+ getBankPublicKey,
18
+ getBankVaultPublicKey,
19
+ getMarketPublicKey,
20
+ } from './addresses/pda';
22
21
  import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
23
22
  import { ClearingHouse } from './clearingHouse';
24
23
  import { PEG_PRECISION } from './constants/numericConstants';
25
24
  import { calculateTargetPriceTrade } from './math/trade';
26
25
  import { calculateAmmReservesAfterSwap, getSwapDirection } from './math/amm';
27
- import {
28
- getAdmin,
29
- getWebSocketClearingHouseConfig,
30
- } from './factory/clearingHouse';
31
26
 
32
27
  export class Admin extends ClearingHouse {
33
- public static from(
34
- connection: Connection,
35
- wallet: IWallet,
36
- clearingHouseProgramId: PublicKey,
37
- opts: ConfirmOptions = AnchorProvider.defaultOptions()
38
- ): Admin {
39
- const config = getWebSocketClearingHouseConfig(
40
- connection,
41
- wallet,
42
- clearingHouseProgramId,
43
- opts
44
- );
45
- return getAdmin(config);
46
- }
47
-
48
28
  public async initialize(
49
29
  usdcMint: PublicKey,
50
30
  adminControlsPrices: boolean
51
- ): Promise<
52
- [TransactionSignature, TransactionSignature, TransactionSignature]
53
- > {
31
+ ): Promise<[TransactionSignature]> {
54
32
  const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(
55
33
  await this.getStatePublicKey()
56
34
  );
@@ -58,160 +36,107 @@ export class Admin extends ClearingHouse {
58
36
  throw new Error('Clearing house already initialized');
59
37
  }
60
38
 
61
- const [collateralVaultPublicKey, collateralVaultNonce] =
62
- await PublicKey.findProgramAddress(
63
- [Buffer.from(anchor.utils.bytes.utf8.encode('collateral_vault'))],
64
- this.program.programId
65
- );
66
-
67
- const [collateralVaultAuthority, _collateralVaultAuthorityNonce] =
68
- await PublicKey.findProgramAddress(
69
- [collateralVaultPublicKey.toBuffer()],
70
- this.program.programId
71
- );
72
-
73
- const [insuranceVaultPublicKey, insuranceVaultNonce] =
74
- await PublicKey.findProgramAddress(
75
- [Buffer.from(anchor.utils.bytes.utf8.encode('insurance_vault'))],
76
- this.program.programId
77
- );
78
-
79
- const [insuranceVaultAuthority, _insuranceVaultAuthorityNonce] =
80
- await PublicKey.findProgramAddress(
81
- [insuranceVaultPublicKey.toBuffer()],
82
- this.program.programId
83
- );
39
+ const [insuranceVaultPublicKey] = await PublicKey.findProgramAddress(
40
+ [Buffer.from(anchor.utils.bytes.utf8.encode('insurance_vault'))],
41
+ this.program.programId
42
+ );
84
43
 
85
- const markets = anchor.web3.Keypair.generate();
86
- const depositHistory = anchor.web3.Keypair.generate();
87
- const fundingRateHistory = anchor.web3.Keypair.generate();
88
- const fundingPaymentHistory = anchor.web3.Keypair.generate();
89
- const tradeHistory = anchor.web3.Keypair.generate();
90
- const liquidationHistory = anchor.web3.Keypair.generate();
91
- const curveHistory = anchor.web3.Keypair.generate();
44
+ const [insuranceVaultAuthority] = await PublicKey.findProgramAddress(
45
+ [insuranceVaultPublicKey.toBuffer()],
46
+ this.program.programId
47
+ );
92
48
 
93
- const [clearingHouseStatePublicKey, clearingHouseNonce] =
49
+ const [clearingHouseStatePublicKey] =
94
50
  await getClearingHouseStateAccountPublicKeyAndNonce(
95
51
  this.program.programId
96
52
  );
97
53
  const initializeTx = await this.program.transaction.initialize(
98
- clearingHouseNonce,
99
- collateralVaultNonce,
100
- insuranceVaultNonce,
101
54
  adminControlsPrices,
102
55
  {
103
56
  accounts: {
104
57
  admin: this.wallet.publicKey,
105
58
  state: clearingHouseStatePublicKey,
106
- collateralMint: usdcMint,
107
- collateralVault: collateralVaultPublicKey,
108
- collateralVaultAuthority: collateralVaultAuthority,
59
+ quoteAssetMint: usdcMint,
109
60
  insuranceVault: insuranceVaultPublicKey,
110
61
  insuranceVaultAuthority: insuranceVaultAuthority,
111
- markets: markets.publicKey,
112
62
  rent: SYSVAR_RENT_PUBKEY,
113
63
  systemProgram: anchor.web3.SystemProgram.programId,
114
64
  tokenProgram: TOKEN_PROGRAM_ID,
115
65
  },
116
- instructions: [
117
- await this.program.account.markets.createInstruction(markets),
118
- ],
119
66
  }
120
67
  );
121
68
 
122
- const initializeTxSig = await this.txSender.send(
69
+ const { txSig: initializeTxSig } = await this.txSender.send(
123
70
  initializeTx,
124
- [markets],
71
+ [],
125
72
  this.opts
126
73
  );
127
74
 
128
- const initializeHistoryTx =
129
- await this.program.transaction.initializeHistory({
130
- accounts: {
131
- admin: this.wallet.publicKey,
132
- state: clearingHouseStatePublicKey,
133
- depositHistory: depositHistory.publicKey,
134
- fundingRateHistory: fundingRateHistory.publicKey,
135
- fundingPaymentHistory: fundingPaymentHistory.publicKey,
136
- tradeHistory: tradeHistory.publicKey,
137
- liquidationHistory: liquidationHistory.publicKey,
138
- curveHistory: curveHistory.publicKey,
139
- rent: SYSVAR_RENT_PUBKEY,
140
- systemProgram: anchor.web3.SystemProgram.programId,
141
- },
142
- instructions: [
143
- await this.program.account.fundingRateHistory.createInstruction(
144
- fundingRateHistory
145
- ),
146
- await this.program.account.fundingPaymentHistory.createInstruction(
147
- fundingPaymentHistory
148
- ),
149
- await this.program.account.tradeHistory.createInstruction(
150
- tradeHistory
151
- ),
152
- await this.program.account.liquidationHistory.createInstruction(
153
- liquidationHistory
154
- ),
155
- await this.program.account.depositHistory.createInstruction(
156
- depositHistory
157
- ),
158
- await this.program.account.extendedCurveHistory.createInstruction(
159
- curveHistory
160
- ),
161
- ],
162
- });
163
-
164
- const initializeHistoryTxSig = await this.txSender.send(
165
- initializeHistoryTx,
166
- [
167
- depositHistory,
168
- fundingPaymentHistory,
169
- tradeHistory,
170
- liquidationHistory,
171
- fundingRateHistory,
172
- curveHistory,
173
- ],
174
- this.opts
175
- );
75
+ return [initializeTxSig];
76
+ }
176
77
 
177
- const initializeOrderStateTxSig = await this.initializeOrderState();
78
+ public async initializeBank(
79
+ mint: PublicKey,
80
+ optimalUtilization: BN,
81
+ optimalRate: BN,
82
+ maxRate: BN,
83
+ oracle: PublicKey,
84
+ oracleSource: OracleSource,
85
+ initialAssetWeight: BN,
86
+ maintenanceAssetWeight: BN,
87
+ initialLiabilityWeight: BN,
88
+ maintenanceLiabilityWeight: BN
89
+ ): Promise<TransactionSignature> {
90
+ const bankIndex = this.getStateAccount().numberOfBanks;
91
+ const bank = await getBankPublicKey(this.program.programId, bankIndex);
178
92
 
179
- return [initializeTxSig, initializeHistoryTxSig, initializeOrderStateTxSig];
180
- }
93
+ const bankVault = await getBankVaultPublicKey(
94
+ this.program.programId,
95
+ bankIndex
96
+ );
181
97
 
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);
98
+ const bankVaultAuthority = await getBankVaultAuthorityPublicKey(
99
+ this.program.programId,
100
+ bankIndex
101
+ );
188
102
 
189
- const initializeOrderStateTx =
190
- await this.program.transaction.initializeOrderState(orderStateNonce, {
103
+ const initializeTx = await this.program.transaction.initializeBank(
104
+ optimalUtilization,
105
+ optimalRate,
106
+ maxRate,
107
+ oracleSource,
108
+ initialAssetWeight,
109
+ maintenanceAssetWeight,
110
+ initialLiabilityWeight,
111
+ maintenanceLiabilityWeight,
112
+ {
191
113
  accounts: {
192
114
  admin: this.wallet.publicKey,
193
- state: clearingHouseStatePublicKey,
194
- orderHistory: orderHistory.publicKey,
195
- orderState: orderStatePublicKey,
115
+ state: await this.getStatePublicKey(),
116
+ bank,
117
+ bankVault,
118
+ bankVaultAuthority,
119
+ bankMint: mint,
120
+ oracle,
196
121
  rent: SYSVAR_RENT_PUBKEY,
197
122
  systemProgram: anchor.web3.SystemProgram.programId,
123
+ tokenProgram: TOKEN_PROGRAM_ID,
198
124
  },
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
125
+ }
210
126
  );
127
+
128
+ const { txSig } = await this.txSender.send(initializeTx, [], this.opts);
129
+
130
+ await this.accountSubscriber.addBank(bankIndex);
131
+ await this.accountSubscriber.addOracle({
132
+ source: oracleSource,
133
+ publicKey: oracle,
134
+ });
135
+
136
+ return txSig;
211
137
  }
212
138
 
213
139
  public async initializeMarket(
214
- marketIndex: BN,
215
140
  priceOracle: PublicKey,
216
141
  baseAssetReserve: BN,
217
142
  quoteAssetReserve: BN,
@@ -222,12 +147,12 @@ export class Admin extends ClearingHouse {
222
147
  marginRatioPartial = 625,
223
148
  marginRatioMaintenance = 500
224
149
  ): Promise<TransactionSignature> {
225
- if (this.getMarketsAccount().markets[marketIndex.toNumber()].initialized) {
226
- throw Error(`MarketIndex ${marketIndex.toNumber()} already initialized`);
227
- }
150
+ const marketPublicKey = await getMarketPublicKey(
151
+ this.program.programId,
152
+ this.getStateAccount().numberOfMarkets
153
+ );
228
154
 
229
155
  const initializeMarketTx = await this.program.transaction.initializeMarket(
230
- marketIndex,
231
156
  baseAssetReserve,
232
157
  quoteAssetReserve,
233
158
  periodicity,
@@ -241,11 +166,27 @@ export class Admin extends ClearingHouse {
241
166
  state: await this.getStatePublicKey(),
242
167
  admin: this.wallet.publicKey,
243
168
  oracle: priceOracle,
244
- markets: this.getStateAccount().markets,
169
+ market: marketPublicKey,
170
+ rent: SYSVAR_RENT_PUBKEY,
171
+ systemProgram: anchor.web3.SystemProgram.programId,
245
172
  },
246
173
  }
247
174
  );
248
- return await this.txSender.send(initializeMarketTx, [], this.opts);
175
+ const { txSig } = await this.txSender.send(
176
+ initializeMarketTx,
177
+ [],
178
+ this.opts
179
+ );
180
+
181
+ await this.accountSubscriber.addMarket(
182
+ this.getStateAccount().numberOfMarkets
183
+ );
184
+ await this.accountSubscriber.addOracle({
185
+ source: oracleSource,
186
+ publicKey: priceOracle,
187
+ });
188
+
189
+ return txSig;
249
190
  }
250
191
 
251
192
  public async moveAmmPrice(
@@ -253,16 +194,19 @@ export class Admin extends ClearingHouse {
253
194
  quoteAssetReserve: BN,
254
195
  marketIndex: BN
255
196
  ): Promise<TransactionSignature> {
256
- const state = this.getStateAccount();
197
+ const marketPublicKey = await getMarketPublicKey(
198
+ this.program.programId,
199
+ marketIndex
200
+ );
201
+
257
202
  return await this.program.rpc.moveAmmPrice(
258
203
  baseAssetReserve,
259
204
  quoteAssetReserve,
260
- marketIndex,
261
205
  {
262
206
  accounts: {
263
207
  state: await this.getStatePublicKey(),
264
208
  admin: this.wallet.publicKey,
265
- markets: state.markets,
209
+ market: marketPublicKey,
266
210
  },
267
211
  }
268
212
  );
@@ -272,51 +216,28 @@ export class Admin extends ClearingHouse {
272
216
  sqrtK: BN,
273
217
  marketIndex: BN
274
218
  ): Promise<TransactionSignature> {
275
- const state = this.getStateAccount();
276
- const markets = this.getMarketsAccount();
277
- const marketData = markets.markets[marketIndex.toNumber()];
278
- const ammData = marketData.amm;
279
-
280
- return await this.program.rpc.updateK(sqrtK, marketIndex, {
219
+ return await this.program.rpc.updateK(sqrtK, {
281
220
  accounts: {
282
221
  state: await this.getStatePublicKey(),
283
222
  admin: this.wallet.publicKey,
284
- markets: state.markets,
285
- curveHistory: state.extendedCurveHistory,
286
- oracle: ammData.oracle,
223
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
224
+ oracle: this.getMarketAccount(marketIndex).amm.oracle,
287
225
  },
288
226
  });
289
227
  }
290
228
 
291
- public async updateCurveHistory(): Promise<TransactionSignature> {
292
- const extendedCurveHistory = anchor.web3.Keypair.generate();
293
-
294
- const state = this.getStateAccount();
295
- return await this.program.rpc.updateCurveHistory({
296
- accounts: {
297
- state: await this.getStatePublicKey(),
298
- admin: this.wallet.publicKey,
299
- curveHistory: state.curveHistory,
300
- extendedCurveHistory: extendedCurveHistory.publicKey,
301
- },
302
- instructions: [
303
- await this.program.account.extendedCurveHistory.createInstruction(
304
- extendedCurveHistory
305
- ),
306
- ],
307
- signers: [extendedCurveHistory],
308
- });
309
- }
310
-
311
229
  public async moveAmmToPrice(
312
230
  marketIndex: BN,
313
231
  targetPrice: BN
314
232
  ): Promise<TransactionSignature> {
315
- const market = this.getMarket(marketIndex);
233
+ const market = this.getMarketAccount(marketIndex);
316
234
 
317
235
  const [direction, tradeSize, _] = calculateTargetPriceTrade(
318
236
  market,
319
- targetPrice
237
+ targetPrice,
238
+ new BN(1000),
239
+ 'quote',
240
+ undefined //todo
320
241
  );
321
242
 
322
243
  const [newQuoteAssetAmount, newBaseAssetAmount] =
@@ -327,16 +248,19 @@ export class Admin extends ClearingHouse {
327
248
  getSwapDirection('quote', direction)
328
249
  );
329
250
 
330
- const state = this.getStateAccount();
251
+ const marketPublicKey = await getMarketPublicKey(
252
+ this.program.programId,
253
+ marketIndex
254
+ );
255
+
331
256
  return await this.program.rpc.moveAmmPrice(
332
257
  newBaseAssetAmount,
333
258
  newQuoteAssetAmount,
334
- marketIndex,
335
259
  {
336
260
  accounts: {
337
261
  state: await this.getStatePublicKey(),
338
262
  admin: this.wallet.publicKey,
339
- markets: state.markets,
263
+ market: marketPublicKey,
340
264
  },
341
265
  }
342
266
  );
@@ -346,18 +270,18 @@ export class Admin extends ClearingHouse {
346
270
  newPeg: BN,
347
271
  marketIndex: BN
348
272
  ): Promise<TransactionSignature> {
349
- const state = this.getStateAccount();
350
- const markets = this.getMarketsAccount();
351
- const marketData = markets.markets[marketIndex.toNumber()];
352
- const ammData = marketData.amm;
273
+ const marketPublicKey = await getMarketPublicKey(
274
+ this.program.programId,
275
+ marketIndex
276
+ );
277
+ const ammData = this.getMarketAccount(marketIndex).amm;
353
278
 
354
- return await this.program.rpc.repegAmmCurve(newPeg, marketIndex, {
279
+ return await this.program.rpc.repegAmmCurve(newPeg, {
355
280
  accounts: {
356
281
  state: await this.getStatePublicKey(),
357
282
  admin: this.wallet.publicKey,
358
283
  oracle: ammData.oracle,
359
- markets: state.markets,
360
- curveHistory: state.extendedCurveHistory,
284
+ market: marketPublicKey,
361
285
  },
362
286
  });
363
287
  }
@@ -365,18 +289,18 @@ export class Admin extends ClearingHouse {
365
289
  public async updateAmmOracleTwap(
366
290
  marketIndex: BN
367
291
  ): Promise<TransactionSignature> {
368
- const state = this.getStateAccount();
369
- const markets = this.getMarketsAccount();
370
- const marketData = markets.markets[marketIndex.toNumber()];
371
- const ammData = marketData.amm;
292
+ const ammData = this.getMarketAccount(marketIndex).amm;
293
+ const marketPublicKey = await getMarketPublicKey(
294
+ this.program.programId,
295
+ marketIndex
296
+ );
372
297
 
373
- return await this.program.rpc.updateAmmOracleTwap(marketIndex, {
298
+ return await this.program.rpc.updateAmmOracleTwap({
374
299
  accounts: {
375
300
  state: await this.getStatePublicKey(),
376
301
  admin: this.wallet.publicKey,
377
302
  oracle: ammData.oracle,
378
- markets: state.markets,
379
- curveHistory: state.extendedCurveHistory,
303
+ market: marketPublicKey,
380
304
  },
381
305
  });
382
306
  }
@@ -384,18 +308,18 @@ export class Admin extends ClearingHouse {
384
308
  public async resetAmmOracleTwap(
385
309
  marketIndex: BN
386
310
  ): Promise<TransactionSignature> {
387
- const state = this.getStateAccount();
388
- const markets = this.getMarketsAccount();
389
- const marketData = markets.markets[marketIndex.toNumber()];
390
- const ammData = marketData.amm;
311
+ const ammData = this.getMarketAccount(marketIndex).amm;
312
+ const marketPublicKey = await getMarketPublicKey(
313
+ this.program.programId,
314
+ marketIndex
315
+ );
391
316
 
392
- return await this.program.rpc.resetAmmOracleTwap(marketIndex, {
317
+ return await this.program.rpc.resetAmmOracleTwap({
393
318
  accounts: {
394
319
  state: await this.getStatePublicKey(),
395
320
  admin: this.wallet.publicKey,
396
321
  oracle: ammData.oracle,
397
- markets: state.markets,
398
- curveHistory: state.extendedCurveHistory,
322
+ market: marketPublicKey,
399
323
  },
400
324
  });
401
325
  }
@@ -422,14 +346,19 @@ export class Admin extends ClearingHouse {
422
346
  amount: BN,
423
347
  recipient: PublicKey
424
348
  ): Promise<TransactionSignature> {
425
- const state = await this.getStateAccount();
426
- return await this.program.rpc.withdrawFees(marketIndex, amount, {
349
+ const marketPublicKey = await getMarketPublicKey(
350
+ this.program.programId,
351
+ marketIndex
352
+ );
353
+ const bank = this.getQuoteAssetBankAccount();
354
+ return await this.program.rpc.withdrawFees(amount, {
427
355
  accounts: {
428
356
  admin: this.wallet.publicKey,
429
357
  state: await this.getStatePublicKey(),
430
- markets: state.markets,
431
- collateralVault: state.collateralVault,
432
- collateralVaultAuthority: state.collateralVaultAuthority,
358
+ market: marketPublicKey,
359
+ bank: bank.pubkey,
360
+ bankVault: bank.vault,
361
+ bankVaultAuthority: bank.vaultAuthority,
433
362
  recipient: recipient,
434
363
  tokenProgram: TOKEN_PROGRAM_ID,
435
364
  },
@@ -441,21 +370,17 @@ export class Admin extends ClearingHouse {
441
370
  amount: BN
442
371
  ): Promise<TransactionSignature> {
443
372
  const state = await this.getStateAccount();
444
- return await this.program.rpc.withdrawFromInsuranceVaultToMarket(
445
- marketIndex,
446
- amount,
447
- {
448
- accounts: {
449
- admin: this.wallet.publicKey,
450
- state: await this.getStatePublicKey(),
451
- markets: state.markets,
452
- insuranceVault: state.insuranceVault,
453
- insuranceVaultAuthority: state.insuranceVaultAuthority,
454
- collateralVault: state.collateralVault,
455
- tokenProgram: TOKEN_PROGRAM_ID,
456
- },
457
- }
458
- );
373
+ return await this.program.rpc.withdrawFromInsuranceVaultToMarket(amount, {
374
+ accounts: {
375
+ admin: this.wallet.publicKey,
376
+ state: await this.getStatePublicKey(),
377
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
378
+ insuranceVault: state.insuranceVault,
379
+ insuranceVaultAuthority: state.insuranceVaultAuthority,
380
+ bankVault: this.getQuoteAssetBankAccount().vault,
381
+ tokenProgram: TOKEN_PROGRAM_ID,
382
+ },
383
+ });
459
384
  }
460
385
 
461
386
  public async updateAdmin(admin: PublicKey): Promise<TransactionSignature> {
@@ -467,6 +392,25 @@ export class Admin extends ClearingHouse {
467
392
  });
468
393
  }
469
394
 
395
+ public async updateCurveUpdateIntensity(
396
+ marketIndex: BN,
397
+ curveUpdateIntensity: number
398
+ ): Promise<TransactionSignature> {
399
+ // assert(curveUpdateIntensity >= 0 && curveUpdateIntensity <= 100);
400
+ // assert(Number.isInteger(curveUpdateIntensity));
401
+
402
+ return await this.program.rpc.updateCurveUpdateIntensity(
403
+ curveUpdateIntensity,
404
+ {
405
+ accounts: {
406
+ admin: this.wallet.publicKey,
407
+ state: await this.getStatePublicKey(),
408
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
409
+ },
410
+ }
411
+ );
412
+ }
413
+
470
414
  public async updateMarginRatio(
471
415
  marketIndex: BN,
472
416
  marginRatioInitial: number,
@@ -474,7 +418,6 @@ export class Admin extends ClearingHouse {
474
418
  marginRatioMaintenance: number
475
419
  ): Promise<TransactionSignature> {
476
420
  return await this.program.rpc.updateMarginRatio(
477
- marketIndex,
478
421
  marginRatioInitial,
479
422
  marginRatioPartial,
480
423
  marginRatioMaintenance,
@@ -482,7 +425,7 @@ export class Admin extends ClearingHouse {
482
425
  accounts: {
483
426
  admin: this.wallet.publicKey,
484
427
  state: await this.getStatePublicKey(),
485
- markets: this.getStateAccount().markets,
428
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
486
429
  },
487
430
  }
488
431
  );
@@ -492,17 +435,13 @@ export class Admin extends ClearingHouse {
492
435
  marketIndex: BN,
493
436
  baseSpread: number
494
437
  ): Promise<TransactionSignature> {
495
- return await this.program.rpc.updateMarketBaseSpread(
496
- marketIndex,
497
- baseSpread,
498
- {
499
- accounts: {
500
- admin: this.wallet.publicKey,
501
- state: await this.getStatePublicKey(),
502
- markets: this.getStateAccount().markets,
503
- },
504
- }
505
- );
438
+ return await this.program.rpc.updateMarketBaseSpread(baseSpread, {
439
+ accounts: {
440
+ admin: this.wallet.publicKey,
441
+ state: await this.getStatePublicKey(),
442
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
443
+ },
444
+ });
506
445
  }
507
446
 
508
447
  public async updatePartialLiquidationClosePercentage(
@@ -590,7 +529,6 @@ export class Admin extends ClearingHouse {
590
529
  accounts: {
591
530
  admin: this.wallet.publicKey,
592
531
  state: await this.getStatePublicKey(),
593
- orderState: await this.getOrderStatePublicKey(),
594
532
  },
595
533
  }
596
534
  );
@@ -621,52 +559,42 @@ export class Admin extends ClearingHouse {
621
559
  oracle: PublicKey,
622
560
  oracleSource: OracleSource
623
561
  ): Promise<TransactionSignature> {
624
- const state = this.getStateAccount();
625
- return await this.program.rpc.updateMarketOracle(
626
- marketIndex,
627
- oracle,
628
- oracleSource,
629
- {
630
- accounts: {
631
- admin: this.wallet.publicKey,
632
- state: await this.getStatePublicKey(),
633
- markets: state.markets,
634
- },
635
- }
636
- );
562
+ return await this.program.rpc.updateMarketOracle(oracle, oracleSource, {
563
+ accounts: {
564
+ admin: this.wallet.publicKey,
565
+ state: await this.getStatePublicKey(),
566
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
567
+ },
568
+ });
637
569
  }
638
570
 
639
571
  public async updateMarketMinimumQuoteAssetTradeSize(
640
572
  marketIndex: BN,
641
573
  minimumTradeSize: BN
642
574
  ): Promise<TransactionSignature> {
643
- const state = this.getStateAccount();
644
575
  return await this.program.rpc.updateMarketMinimumQuoteAssetTradeSize(
645
- marketIndex,
646
576
  minimumTradeSize,
647
577
  {
648
578
  accounts: {
649
579
  admin: this.wallet.publicKey,
650
580
  state: await this.getStatePublicKey(),
651
- markets: state.markets,
581
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
652
582
  },
653
583
  }
654
584
  );
655
585
  }
656
586
 
657
- public async updateMarketMinimumBaseAssetTradeSize(
587
+ public async updateMarketBaseAssetAmountStepSize(
658
588
  marketIndex: BN,
659
- minimumTradeSize: BN
589
+ stepSize: BN
660
590
  ): Promise<TransactionSignature> {
661
- const state = this.getStateAccount();
662
- return await this.program.rpc.updateMarketMinimumBaseAssetTradeSize(
663
- marketIndex,
664
- minimumTradeSize,
591
+ return await this.program.rpc.updateMarketBaseAssetAmountStepSize(
592
+ stepSize,
665
593
  {
666
594
  accounts: {
667
595
  admin: this.wallet.publicKey,
668
596
  state: await this.getStatePublicKey(),
669
- markets: state.markets,
597
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
670
598
  },
671
599
  }
672
600
  );
@@ -733,4 +661,18 @@ export class Admin extends ClearingHouse {
733
661
  },
734
662
  });
735
663
  }
664
+
665
+ public async updateOrderAuctionTime(
666
+ time: BN | number
667
+ ): Promise<TransactionSignature> {
668
+ return await this.program.rpc.updateOrderAuctionTime(
669
+ typeof time === 'number' ? time : time.toNumber,
670
+ {
671
+ accounts: {
672
+ admin: this.wallet.publicKey,
673
+ state: await this.getStatePublicKey(),
674
+ },
675
+ }
676
+ );
677
+ }
736
678
  }