@drift-labs/sdk 0.1.36-master.7 → 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 +6 -6
  2. package/lib/accounts/bulkAccountLoader.js +81 -95
  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 -17
  26. package/lib/admin.js +366 -558
  27. package/lib/clearingHouse.d.ts +84 -109
  28. package/lib/clearingHouse.js +778 -899
  29. package/lib/clearingHouseConfig.d.ts +34 -0
  30. package/lib/clearingHouseConfig.js +2 -0
  31. package/lib/clearingHouseUser.d.ts +19 -22
  32. package/lib/clearingHouseUser.js +155 -141
  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 +2213 -2951
  65. package/lib/index.d.ts +12 -4
  66. package/lib/index.js +12 -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/orders.d.ts +5 -0
  80. package/lib/math/orders.js +31 -1
  81. package/lib/math/position.d.ts +7 -5
  82. package/lib/math/position.js +27 -27
  83. package/lib/math/repeg.d.ts +22 -0
  84. package/lib/math/repeg.js +128 -0
  85. package/lib/math/trade.d.ts +5 -4
  86. package/lib/math/trade.js +29 -21
  87. package/lib/mockUSDCFaucet.js +87 -116
  88. package/lib/oracles/oracleClientCache.d.ts +8 -0
  89. package/lib/oracles/oracleClientCache.js +19 -0
  90. package/lib/oracles/pythClient.d.ts +3 -5
  91. package/lib/oracles/pythClient.js +12 -31
  92. package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
  93. package/lib/oracles/quoteAssetOracleClient.js +21 -0
  94. package/lib/oracles/switchboardClient.d.ts +3 -5
  95. package/lib/oracles/switchboardClient.js +29 -50
  96. package/lib/oracles/types.d.ts +6 -1
  97. package/lib/orders.d.ts +6 -6
  98. package/lib/orders.js +10 -9
  99. package/lib/slot/SlotSubscriber.d.ts +12 -0
  100. package/lib/slot/SlotSubscriber.js +23 -0
  101. package/lib/tx/retryTxSender.d.ts +2 -2
  102. package/lib/tx/retryTxSender.js +108 -123
  103. package/lib/tx/types.d.ts +5 -1
  104. package/lib/tx/utils.d.ts +1 -1
  105. package/lib/tx/utils.js +11 -2
  106. package/lib/types.d.ts +105 -109
  107. package/lib/types.js +13 -1
  108. package/lib/userName.d.ts +4 -0
  109. package/lib/userName.js +20 -0
  110. package/lib/util/computeUnits.js +10 -21
  111. package/lib/util/tps.js +11 -22
  112. package/lib/wallet.js +7 -20
  113. package/package.json +10 -3
  114. package/src/accounts/bulkAccountLoader.ts +21 -15
  115. package/src/accounts/bulkUserSubscription.ts +1 -45
  116. package/src/accounts/fetch.ts +33 -0
  117. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
  118. package/src/accounts/pollingOracleSubscriber.ts +16 -8
  119. package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
  120. package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
  121. package/src/accounts/types.ts +41 -70
  122. package/src/accounts/webSocketAccountSubscriber.ts +33 -16
  123. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
  124. package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
  125. package/src/addresses/marketAddresses.ts +18 -0
  126. package/src/addresses/pda.ts +118 -0
  127. package/src/admin.ts +205 -346
  128. package/src/clearingHouse.ts +918 -961
  129. package/src/clearingHouseConfig.ts +37 -0
  130. package/src/clearingHouseUser.ts +275 -185
  131. package/src/clearingHouseUserConfig.ts +18 -0
  132. package/src/config.ts +54 -1
  133. package/src/constants/banks.ts +43 -0
  134. package/src/constants/markets.ts +16 -207
  135. package/src/constants/numericConstants.ts +33 -5
  136. package/src/events/eventList.ts +94 -0
  137. package/src/events/eventSubscriber.ts +194 -0
  138. package/src/events/fetchLogs.ts +80 -0
  139. package/src/events/pollingLogProvider.ts +79 -0
  140. package/src/events/sort.ts +65 -0
  141. package/src/events/txEventCache.ts +74 -0
  142. package/src/events/types.ts +98 -0
  143. package/src/events/webSocketLogProvider.ts +38 -0
  144. package/src/examples/makeTradeExample.ts +20 -11
  145. package/src/factory/bigNum.ts +507 -0
  146. package/src/factory/oracleClient.ts +7 -4
  147. package/src/idl/clearing_house.json +2213 -2951
  148. package/src/index.ts +12 -4
  149. package/src/math/amm.ts +229 -245
  150. package/src/math/auction.ts +39 -0
  151. package/src/math/bankBalance.ts +112 -0
  152. package/src/math/conversion.ts +1 -11
  153. package/src/math/funding.ts +12 -9
  154. package/src/math/market.ts +37 -30
  155. package/src/math/orders.ts +38 -0
  156. package/src/math/position.ts +48 -35
  157. package/src/math/repeg.ts +175 -0
  158. package/src/math/trade.ts +45 -35
  159. package/src/math/utils.js +27 -0
  160. package/src/math/utils.js.map +1 -0
  161. package/src/oracles/oracleClientCache.ts +20 -0
  162. package/src/oracles/pythClient.ts +4 -11
  163. package/src/oracles/quoteAssetOracleClient.ts +25 -0
  164. package/src/oracles/switchboardClient.ts +11 -24
  165. package/src/oracles/types.ts +7 -1
  166. package/src/orders.ts +35 -20
  167. package/src/slot/SlotSubscriber.ts +32 -0
  168. package/src/tx/retryTxSender.ts +6 -4
  169. package/src/tx/types.ts +6 -1
  170. package/src/tx/utils.ts +22 -3
  171. package/src/types.ts +108 -122
  172. package/src/userName.ts +20 -0
  173. package/src/util/computeUnits.js +17 -0
  174. package/src/util/computeUnits.js.map +1 -0
  175. package/tests/bn/test.ts +255 -0
  176. package/tsconfig.json +12 -12
  177. package/lib/addresses.d.ts +0 -10
  178. package/lib/addresses.js +0 -93
  179. package/lib/constants/accounts.d.ts +0 -15
  180. package/lib/constants/accounts.js +0 -18
  181. package/lib/factory/clearingHouse.d.ts +0 -35
  182. package/lib/factory/clearingHouse.js +0 -81
  183. package/lib/factory/clearingHouseUser.d.ts +0 -19
  184. package/lib/factory/clearingHouseUser.js +0 -34
  185. package/lib/math/insuranceFund.d.ts +0 -15
  186. package/lib/math/insuranceFund.js +0 -33
  187. package/lib/settlement.d.ts +0 -4
  188. package/lib/settlement.js +0 -10
  189. package/lib/tx/defaultTxSender.d.ts +0 -8
  190. package/lib/tx/defaultTxSender.js +0 -12
  191. package/src/addresses.ts +0 -82
  192. package/src/constants/accounts.ts +0 -26
  193. package/src/factory/clearingHouse.ts +0 -173
  194. package/src/factory/clearingHouseUser.ts +0 -73
  195. package/src/math/insuranceFund.ts +0 -29
  196. package/src/settlement.ts +0 -9
  197. package/src/tx/defaultTxSender.ts +0 -24
package/src/admin.ts CHANGED
@@ -1,58 +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
- getSettlementStatePublicKey,
22
- getUserAccountPublicKey,
23
- } from './addresses';
16
+ getBankVaultAuthorityPublicKey,
17
+ getBankPublicKey,
18
+ getBankVaultPublicKey,
19
+ getMarketPublicKey,
20
+ } from './addresses/pda';
24
21
  import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
25
22
  import { ClearingHouse } from './clearingHouse';
26
23
  import { PEG_PRECISION } from './constants/numericConstants';
27
24
  import { calculateTargetPriceTrade } from './math/trade';
28
25
  import { calculateAmmReservesAfterSwap, getSwapDirection } from './math/amm';
29
- import {
30
- getAdmin,
31
- getWebSocketClearingHouseConfig,
32
- } from './factory/clearingHouse';
33
26
 
34
27
  export class Admin extends ClearingHouse {
35
- public static from(
36
- connection: Connection,
37
- wallet: IWallet,
38
- clearingHouseProgramId: PublicKey,
39
- opts: ConfirmOptions = AnchorProvider.defaultOptions()
40
- ): Admin {
41
- const config = getWebSocketClearingHouseConfig(
42
- connection,
43
- wallet,
44
- clearingHouseProgramId,
45
- opts
46
- );
47
- return getAdmin(config);
48
- }
49
-
50
28
  public async initialize(
51
29
  usdcMint: PublicKey,
52
30
  adminControlsPrices: boolean
53
- ): Promise<
54
- [TransactionSignature, TransactionSignature, TransactionSignature]
55
- > {
31
+ ): Promise<[TransactionSignature]> {
56
32
  const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(
57
33
  await this.getStatePublicKey()
58
34
  );
@@ -60,160 +36,107 @@ export class Admin extends ClearingHouse {
60
36
  throw new Error('Clearing house already initialized');
61
37
  }
62
38
 
63
- const [collateralVaultPublicKey, collateralVaultNonce] =
64
- await PublicKey.findProgramAddress(
65
- [Buffer.from(anchor.utils.bytes.utf8.encode('collateral_vault'))],
66
- this.program.programId
67
- );
68
-
69
- const [collateralVaultAuthority, _collateralVaultAuthorityNonce] =
70
- await PublicKey.findProgramAddress(
71
- [collateralVaultPublicKey.toBuffer()],
72
- this.program.programId
73
- );
74
-
75
- const [insuranceVaultPublicKey, insuranceVaultNonce] =
76
- await PublicKey.findProgramAddress(
77
- [Buffer.from(anchor.utils.bytes.utf8.encode('insurance_vault'))],
78
- this.program.programId
79
- );
80
-
81
- const [insuranceVaultAuthority, _insuranceVaultAuthorityNonce] =
82
- await PublicKey.findProgramAddress(
83
- [insuranceVaultPublicKey.toBuffer()],
84
- this.program.programId
85
- );
39
+ const [insuranceVaultPublicKey] = await PublicKey.findProgramAddress(
40
+ [Buffer.from(anchor.utils.bytes.utf8.encode('insurance_vault'))],
41
+ this.program.programId
42
+ );
86
43
 
87
- const markets = anchor.web3.Keypair.generate();
88
- const depositHistory = anchor.web3.Keypair.generate();
89
- const fundingRateHistory = anchor.web3.Keypair.generate();
90
- const fundingPaymentHistory = anchor.web3.Keypair.generate();
91
- const tradeHistory = anchor.web3.Keypair.generate();
92
- const liquidationHistory = anchor.web3.Keypair.generate();
93
- const curveHistory = anchor.web3.Keypair.generate();
44
+ const [insuranceVaultAuthority] = await PublicKey.findProgramAddress(
45
+ [insuranceVaultPublicKey.toBuffer()],
46
+ this.program.programId
47
+ );
94
48
 
95
- const [clearingHouseStatePublicKey, clearingHouseNonce] =
49
+ const [clearingHouseStatePublicKey] =
96
50
  await getClearingHouseStateAccountPublicKeyAndNonce(
97
51
  this.program.programId
98
52
  );
99
53
  const initializeTx = await this.program.transaction.initialize(
100
- clearingHouseNonce,
101
- collateralVaultNonce,
102
- insuranceVaultNonce,
103
54
  adminControlsPrices,
104
55
  {
105
56
  accounts: {
106
57
  admin: this.wallet.publicKey,
107
58
  state: clearingHouseStatePublicKey,
108
- collateralMint: usdcMint,
109
- collateralVault: collateralVaultPublicKey,
110
- collateralVaultAuthority: collateralVaultAuthority,
59
+ quoteAssetMint: usdcMint,
111
60
  insuranceVault: insuranceVaultPublicKey,
112
61
  insuranceVaultAuthority: insuranceVaultAuthority,
113
- markets: markets.publicKey,
114
62
  rent: SYSVAR_RENT_PUBKEY,
115
63
  systemProgram: anchor.web3.SystemProgram.programId,
116
64
  tokenProgram: TOKEN_PROGRAM_ID,
117
65
  },
118
- instructions: [
119
- await this.program.account.markets.createInstruction(markets),
120
- ],
121
66
  }
122
67
  );
123
68
 
124
- const initializeTxSig = await this.txSender.send(
69
+ const { txSig: initializeTxSig } = await this.txSender.send(
125
70
  initializeTx,
126
- [markets],
71
+ [],
127
72
  this.opts
128
73
  );
129
74
 
130
- const initializeHistoryTx =
131
- await this.program.transaction.initializeHistory({
132
- accounts: {
133
- admin: this.wallet.publicKey,
134
- state: clearingHouseStatePublicKey,
135
- depositHistory: depositHistory.publicKey,
136
- fundingRateHistory: fundingRateHistory.publicKey,
137
- fundingPaymentHistory: fundingPaymentHistory.publicKey,
138
- tradeHistory: tradeHistory.publicKey,
139
- liquidationHistory: liquidationHistory.publicKey,
140
- curveHistory: curveHistory.publicKey,
141
- rent: SYSVAR_RENT_PUBKEY,
142
- systemProgram: anchor.web3.SystemProgram.programId,
143
- },
144
- instructions: [
145
- await this.program.account.fundingRateHistory.createInstruction(
146
- fundingRateHistory
147
- ),
148
- await this.program.account.fundingPaymentHistory.createInstruction(
149
- fundingPaymentHistory
150
- ),
151
- await this.program.account.tradeHistory.createInstruction(
152
- tradeHistory
153
- ),
154
- await this.program.account.liquidationHistory.createInstruction(
155
- liquidationHistory
156
- ),
157
- await this.program.account.depositHistory.createInstruction(
158
- depositHistory
159
- ),
160
- await this.program.account.extendedCurveHistory.createInstruction(
161
- curveHistory
162
- ),
163
- ],
164
- });
165
-
166
- const initializeHistoryTxSig = await this.txSender.send(
167
- initializeHistoryTx,
168
- [
169
- depositHistory,
170
- fundingPaymentHistory,
171
- tradeHistory,
172
- liquidationHistory,
173
- fundingRateHistory,
174
- curveHistory,
175
- ],
176
- this.opts
177
- );
75
+ return [initializeTxSig];
76
+ }
178
77
 
179
- 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);
180
92
 
181
- return [initializeTxSig, initializeHistoryTxSig, initializeOrderStateTxSig];
182
- }
93
+ const bankVault = await getBankVaultPublicKey(
94
+ this.program.programId,
95
+ bankIndex
96
+ );
183
97
 
184
- public async initializeOrderState(): Promise<TransactionSignature> {
185
- const orderHistory = anchor.web3.Keypair.generate();
186
- const [orderStatePublicKey, orderStateNonce] =
187
- await getOrderStateAccountPublicKeyAndNonce(this.program.programId);
188
- const clearingHouseStatePublicKey =
189
- await getClearingHouseStateAccountPublicKey(this.program.programId);
98
+ const bankVaultAuthority = await getBankVaultAuthorityPublicKey(
99
+ this.program.programId,
100
+ bankIndex
101
+ );
190
102
 
191
- const initializeOrderStateTx =
192
- 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
+ {
193
113
  accounts: {
194
114
  admin: this.wallet.publicKey,
195
- state: clearingHouseStatePublicKey,
196
- orderHistory: orderHistory.publicKey,
197
- orderState: orderStatePublicKey,
115
+ state: await this.getStatePublicKey(),
116
+ bank,
117
+ bankVault,
118
+ bankVaultAuthority,
119
+ bankMint: mint,
120
+ oracle,
198
121
  rent: SYSVAR_RENT_PUBKEY,
199
122
  systemProgram: anchor.web3.SystemProgram.programId,
123
+ tokenProgram: TOKEN_PROGRAM_ID,
200
124
  },
201
- instructions: [
202
- await this.program.account.orderHistory.createInstruction(
203
- orderHistory
204
- ),
205
- ],
206
- });
207
-
208
- return await this.txSender.send(
209
- initializeOrderStateTx,
210
- [orderHistory],
211
- this.opts
125
+ }
212
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;
213
137
  }
214
138
 
215
139
  public async initializeMarket(
216
- marketIndex: BN,
217
140
  priceOracle: PublicKey,
218
141
  baseAssetReserve: BN,
219
142
  quoteAssetReserve: BN,
@@ -224,12 +147,12 @@ export class Admin extends ClearingHouse {
224
147
  marginRatioPartial = 625,
225
148
  marginRatioMaintenance = 500
226
149
  ): Promise<TransactionSignature> {
227
- if (this.getMarketsAccount().markets[marketIndex.toNumber()].initialized) {
228
- throw Error(`MarketIndex ${marketIndex.toNumber()} already initialized`);
229
- }
150
+ const marketPublicKey = await getMarketPublicKey(
151
+ this.program.programId,
152
+ this.getStateAccount().numberOfMarkets
153
+ );
230
154
 
231
155
  const initializeMarketTx = await this.program.transaction.initializeMarket(
232
- marketIndex,
233
156
  baseAssetReserve,
234
157
  quoteAssetReserve,
235
158
  periodicity,
@@ -243,11 +166,27 @@ export class Admin extends ClearingHouse {
243
166
  state: await this.getStatePublicKey(),
244
167
  admin: this.wallet.publicKey,
245
168
  oracle: priceOracle,
246
- markets: this.getStateAccount().markets,
169
+ market: marketPublicKey,
170
+ rent: SYSVAR_RENT_PUBKEY,
171
+ systemProgram: anchor.web3.SystemProgram.programId,
247
172
  },
248
173
  }
249
174
  );
250
- 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;
251
190
  }
252
191
 
253
192
  public async moveAmmPrice(
@@ -255,16 +194,19 @@ export class Admin extends ClearingHouse {
255
194
  quoteAssetReserve: BN,
256
195
  marketIndex: BN
257
196
  ): Promise<TransactionSignature> {
258
- const state = this.getStateAccount();
197
+ const marketPublicKey = await getMarketPublicKey(
198
+ this.program.programId,
199
+ marketIndex
200
+ );
201
+
259
202
  return await this.program.rpc.moveAmmPrice(
260
203
  baseAssetReserve,
261
204
  quoteAssetReserve,
262
- marketIndex,
263
205
  {
264
206
  accounts: {
265
207
  state: await this.getStatePublicKey(),
266
208
  admin: this.wallet.publicKey,
267
- markets: state.markets,
209
+ market: marketPublicKey,
268
210
  },
269
211
  }
270
212
  );
@@ -274,39 +216,13 @@ export class Admin extends ClearingHouse {
274
216
  sqrtK: BN,
275
217
  marketIndex: BN
276
218
  ): Promise<TransactionSignature> {
277
- const state = this.getStateAccount();
278
- const markets = this.getMarketsAccount();
279
- const marketData = markets.markets[marketIndex.toNumber()];
280
- const ammData = marketData.amm;
281
-
282
- return await this.program.rpc.updateK(sqrtK, marketIndex, {
283
- accounts: {
284
- state: await this.getStatePublicKey(),
285
- admin: this.wallet.publicKey,
286
- markets: state.markets,
287
- curveHistory: state.extendedCurveHistory,
288
- oracle: ammData.oracle,
289
- },
290
- });
291
- }
292
-
293
- public async updateCurveHistory(): Promise<TransactionSignature> {
294
- const extendedCurveHistory = anchor.web3.Keypair.generate();
295
-
296
- const state = this.getStateAccount();
297
- return await this.program.rpc.updateCurveHistory({
219
+ return await this.program.rpc.updateK(sqrtK, {
298
220
  accounts: {
299
221
  state: await this.getStatePublicKey(),
300
222
  admin: this.wallet.publicKey,
301
- curveHistory: state.curveHistory,
302
- extendedCurveHistory: extendedCurveHistory.publicKey,
223
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
224
+ oracle: this.getMarketAccount(marketIndex).amm.oracle,
303
225
  },
304
- instructions: [
305
- await this.program.account.extendedCurveHistory.createInstruction(
306
- extendedCurveHistory
307
- ),
308
- ],
309
- signers: [extendedCurveHistory],
310
226
  });
311
227
  }
312
228
 
@@ -314,11 +230,14 @@ export class Admin extends ClearingHouse {
314
230
  marketIndex: BN,
315
231
  targetPrice: BN
316
232
  ): Promise<TransactionSignature> {
317
- const market = this.getMarket(marketIndex);
233
+ const market = this.getMarketAccount(marketIndex);
318
234
 
319
235
  const [direction, tradeSize, _] = calculateTargetPriceTrade(
320
236
  market,
321
- targetPrice
237
+ targetPrice,
238
+ new BN(1000),
239
+ 'quote',
240
+ undefined //todo
322
241
  );
323
242
 
324
243
  const [newQuoteAssetAmount, newBaseAssetAmount] =
@@ -329,16 +248,19 @@ export class Admin extends ClearingHouse {
329
248
  getSwapDirection('quote', direction)
330
249
  );
331
250
 
332
- const state = this.getStateAccount();
251
+ const marketPublicKey = await getMarketPublicKey(
252
+ this.program.programId,
253
+ marketIndex
254
+ );
255
+
333
256
  return await this.program.rpc.moveAmmPrice(
334
257
  newBaseAssetAmount,
335
258
  newQuoteAssetAmount,
336
- marketIndex,
337
259
  {
338
260
  accounts: {
339
261
  state: await this.getStatePublicKey(),
340
262
  admin: this.wallet.publicKey,
341
- markets: state.markets,
263
+ market: marketPublicKey,
342
264
  },
343
265
  }
344
266
  );
@@ -348,18 +270,18 @@ export class Admin extends ClearingHouse {
348
270
  newPeg: BN,
349
271
  marketIndex: BN
350
272
  ): Promise<TransactionSignature> {
351
- const state = this.getStateAccount();
352
- const markets = this.getMarketsAccount();
353
- const marketData = markets.markets[marketIndex.toNumber()];
354
- const ammData = marketData.amm;
273
+ const marketPublicKey = await getMarketPublicKey(
274
+ this.program.programId,
275
+ marketIndex
276
+ );
277
+ const ammData = this.getMarketAccount(marketIndex).amm;
355
278
 
356
- return await this.program.rpc.repegAmmCurve(newPeg, marketIndex, {
279
+ return await this.program.rpc.repegAmmCurve(newPeg, {
357
280
  accounts: {
358
281
  state: await this.getStatePublicKey(),
359
282
  admin: this.wallet.publicKey,
360
283
  oracle: ammData.oracle,
361
- markets: state.markets,
362
- curveHistory: state.extendedCurveHistory,
284
+ market: marketPublicKey,
363
285
  },
364
286
  });
365
287
  }
@@ -367,18 +289,18 @@ export class Admin extends ClearingHouse {
367
289
  public async updateAmmOracleTwap(
368
290
  marketIndex: BN
369
291
  ): Promise<TransactionSignature> {
370
- const state = this.getStateAccount();
371
- const markets = this.getMarketsAccount();
372
- const marketData = markets.markets[marketIndex.toNumber()];
373
- const ammData = marketData.amm;
292
+ const ammData = this.getMarketAccount(marketIndex).amm;
293
+ const marketPublicKey = await getMarketPublicKey(
294
+ this.program.programId,
295
+ marketIndex
296
+ );
374
297
 
375
- return await this.program.rpc.updateAmmOracleTwap(marketIndex, {
298
+ return await this.program.rpc.updateAmmOracleTwap({
376
299
  accounts: {
377
300
  state: await this.getStatePublicKey(),
378
301
  admin: this.wallet.publicKey,
379
302
  oracle: ammData.oracle,
380
- markets: state.markets,
381
- curveHistory: state.extendedCurveHistory,
303
+ market: marketPublicKey,
382
304
  },
383
305
  });
384
306
  }
@@ -386,18 +308,18 @@ export class Admin extends ClearingHouse {
386
308
  public async resetAmmOracleTwap(
387
309
  marketIndex: BN
388
310
  ): Promise<TransactionSignature> {
389
- const state = this.getStateAccount();
390
- const markets = this.getMarketsAccount();
391
- const marketData = markets.markets[marketIndex.toNumber()];
392
- const ammData = marketData.amm;
311
+ const ammData = this.getMarketAccount(marketIndex).amm;
312
+ const marketPublicKey = await getMarketPublicKey(
313
+ this.program.programId,
314
+ marketIndex
315
+ );
393
316
 
394
- return await this.program.rpc.resetAmmOracleTwap(marketIndex, {
317
+ return await this.program.rpc.resetAmmOracleTwap({
395
318
  accounts: {
396
319
  state: await this.getStatePublicKey(),
397
320
  admin: this.wallet.publicKey,
398
321
  oracle: ammData.oracle,
399
- markets: state.markets,
400
- curveHistory: state.extendedCurveHistory,
322
+ market: marketPublicKey,
401
323
  },
402
324
  });
403
325
  }
@@ -424,14 +346,19 @@ export class Admin extends ClearingHouse {
424
346
  amount: BN,
425
347
  recipient: PublicKey
426
348
  ): Promise<TransactionSignature> {
427
- const state = await this.getStateAccount();
428
- 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, {
429
355
  accounts: {
430
356
  admin: this.wallet.publicKey,
431
357
  state: await this.getStatePublicKey(),
432
- markets: state.markets,
433
- collateralVault: state.collateralVault,
434
- collateralVaultAuthority: state.collateralVaultAuthority,
358
+ market: marketPublicKey,
359
+ bank: bank.pubkey,
360
+ bankVault: bank.vault,
361
+ bankVaultAuthority: bank.vaultAuthority,
435
362
  recipient: recipient,
436
363
  tokenProgram: TOKEN_PROGRAM_ID,
437
364
  },
@@ -443,21 +370,17 @@ export class Admin extends ClearingHouse {
443
370
  amount: BN
444
371
  ): Promise<TransactionSignature> {
445
372
  const state = await this.getStateAccount();
446
- return await this.program.rpc.withdrawFromInsuranceVaultToMarket(
447
- marketIndex,
448
- amount,
449
- {
450
- accounts: {
451
- admin: this.wallet.publicKey,
452
- state: await this.getStatePublicKey(),
453
- markets: state.markets,
454
- insuranceVault: state.insuranceVault,
455
- insuranceVaultAuthority: state.insuranceVaultAuthority,
456
- collateralVault: state.collateralVault,
457
- tokenProgram: TOKEN_PROGRAM_ID,
458
- },
459
- }
460
- );
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
+ });
461
384
  }
462
385
 
463
386
  public async updateAdmin(admin: PublicKey): Promise<TransactionSignature> {
@@ -469,6 +392,25 @@ export class Admin extends ClearingHouse {
469
392
  });
470
393
  }
471
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
+
472
414
  public async updateMarginRatio(
473
415
  marketIndex: BN,
474
416
  marginRatioInitial: number,
@@ -476,7 +418,6 @@ export class Admin extends ClearingHouse {
476
418
  marginRatioMaintenance: number
477
419
  ): Promise<TransactionSignature> {
478
420
  return await this.program.rpc.updateMarginRatio(
479
- marketIndex,
480
421
  marginRatioInitial,
481
422
  marginRatioPartial,
482
423
  marginRatioMaintenance,
@@ -484,7 +425,7 @@ export class Admin extends ClearingHouse {
484
425
  accounts: {
485
426
  admin: this.wallet.publicKey,
486
427
  state: await this.getStatePublicKey(),
487
- markets: this.getStateAccount().markets,
428
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
488
429
  },
489
430
  }
490
431
  );
@@ -494,17 +435,13 @@ export class Admin extends ClearingHouse {
494
435
  marketIndex: BN,
495
436
  baseSpread: number
496
437
  ): Promise<TransactionSignature> {
497
- return await this.program.rpc.updateMarketBaseSpread(
498
- marketIndex,
499
- baseSpread,
500
- {
501
- accounts: {
502
- admin: this.wallet.publicKey,
503
- state: await this.getStatePublicKey(),
504
- markets: this.getStateAccount().markets,
505
- },
506
- }
507
- );
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
+ });
508
445
  }
509
446
 
510
447
  public async updatePartialLiquidationClosePercentage(
@@ -592,7 +529,6 @@ export class Admin extends ClearingHouse {
592
529
  accounts: {
593
530
  admin: this.wallet.publicKey,
594
531
  state: await this.getStatePublicKey(),
595
- orderState: await this.getOrderStatePublicKey(),
596
532
  },
597
533
  }
598
534
  );
@@ -623,52 +559,42 @@ export class Admin extends ClearingHouse {
623
559
  oracle: PublicKey,
624
560
  oracleSource: OracleSource
625
561
  ): Promise<TransactionSignature> {
626
- const state = this.getStateAccount();
627
- return await this.program.rpc.updateMarketOracle(
628
- marketIndex,
629
- oracle,
630
- oracleSource,
631
- {
632
- accounts: {
633
- admin: this.wallet.publicKey,
634
- state: await this.getStatePublicKey(),
635
- markets: state.markets,
636
- },
637
- }
638
- );
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
+ });
639
569
  }
640
570
 
641
571
  public async updateMarketMinimumQuoteAssetTradeSize(
642
572
  marketIndex: BN,
643
573
  minimumTradeSize: BN
644
574
  ): Promise<TransactionSignature> {
645
- const state = this.getStateAccount();
646
575
  return await this.program.rpc.updateMarketMinimumQuoteAssetTradeSize(
647
- marketIndex,
648
576
  minimumTradeSize,
649
577
  {
650
578
  accounts: {
651
579
  admin: this.wallet.publicKey,
652
580
  state: await this.getStatePublicKey(),
653
- markets: state.markets,
581
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
654
582
  },
655
583
  }
656
584
  );
657
585
  }
658
586
 
659
- public async updateMarketMinimumBaseAssetTradeSize(
587
+ public async updateMarketBaseAssetAmountStepSize(
660
588
  marketIndex: BN,
661
- minimumTradeSize: BN
589
+ stepSize: BN
662
590
  ): Promise<TransactionSignature> {
663
- const state = this.getStateAccount();
664
- return await this.program.rpc.updateMarketMinimumBaseAssetTradeSize(
665
- marketIndex,
666
- minimumTradeSize,
591
+ return await this.program.rpc.updateMarketBaseAssetAmountStepSize(
592
+ stepSize,
667
593
  {
668
594
  accounts: {
669
595
  admin: this.wallet.publicKey,
670
596
  state: await this.getStatePublicKey(),
671
- markets: state.markets,
597
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
672
598
  },
673
599
  }
674
600
  );
@@ -736,84 +662,17 @@ export class Admin extends ClearingHouse {
736
662
  });
737
663
  }
738
664
 
739
- public async transferFromInsuranceVaultToCollateralVault(): Promise<TransactionSignature> {
740
- const state = await this.getStateAccount();
741
- return await this.program.rpc.transferFromInsuranceVaultToCollateralVault({
742
- accounts: {
743
- admin: this.wallet.publicKey,
744
- state: await this.getStatePublicKey(),
745
- insuranceVault: state.insuranceVault,
746
- insuranceVaultAuthority: state.insuranceVaultAuthority,
747
- collateralVault: state.collateralVault,
748
- tokenProgram: TOKEN_PROGRAM_ID,
749
- },
750
- });
751
- }
752
-
753
- public async adminUpdateUserForgoSettlement(
754
- authority: PublicKey
755
- ): Promise<TransactionSignature> {
756
- const user = await getUserAccountPublicKey(
757
- this.program.programId,
758
- authority
759
- );
760
-
761
- return await this.program.rpc.adminUpdateUserForgoSettlement({
762
- accounts: {
763
- admin: this.wallet.publicKey,
764
- state: await this.getStatePublicKey(),
765
- user: user,
766
- },
767
- });
768
- }
769
-
770
- public async initializeSettlementState(): Promise<TransactionSignature> {
771
- const settlementState = await getSettlementStatePublicKey(
772
- this.program.programId
773
- );
774
-
775
- const settlementSize = await this.getTotalSettlementSize();
776
-
777
- return await this.program.rpc.initializeSettlementState(settlementSize, {
778
- accounts: {
779
- admin: this.wallet.publicKey,
780
- state: await this.getStatePublicKey(),
781
- settlementState: settlementState,
782
- rent: SYSVAR_RENT_PUBKEY,
783
- systemProgram: anchor.web3.SystemProgram.programId,
784
- collateralVault: (await this.getStateAccount()).collateralVault,
785
- },
786
- });
787
- }
788
-
789
- public async updateSettlementState(): Promise<TransactionSignature> {
790
- const settlementState = await getSettlementStatePublicKey(
791
- this.program.programId
792
- );
793
-
794
- return await this.program.rpc.updateSettlementState({
795
- accounts: {
796
- admin: this.wallet.publicKey,
797
- state: await this.getStatePublicKey(),
798
- settlementState: settlementState,
799
- collateralVault: (await this.getStateAccount()).collateralVault,
800
- },
801
- });
802
- }
803
-
804
- public async updateSettlementStateEnabled(
805
- enabled: boolean
665
+ public async updateOrderAuctionTime(
666
+ time: BN | number
806
667
  ): Promise<TransactionSignature> {
807
- const settlementState = await getSettlementStatePublicKey(
808
- this.program.programId
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
+ }
809
676
  );
810
-
811
- return await this.program.rpc.updateSettlementStateEnabled(enabled, {
812
- accounts: {
813
- admin: this.wallet.publicKey,
814
- state: await this.getStatePublicKey(),
815
- settlementState: settlementState,
816
- },
817
- });
818
677
  }
819
678
  }