@drift-labs/sdk 0.2.0-master.3 → 0.2.0-master.30

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 (208) hide show
  1. package/README.md +13 -13
  2. package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
  3. package/lib/accounts/bulkUserStatsSubscription.js +21 -0
  4. package/lib/accounts/bulkUserSubscription.js +0 -1
  5. package/lib/accounts/fetch.d.ts +2 -1
  6. package/lib/accounts/fetch.js +9 -1
  7. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +16 -16
  8. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +31 -28
  9. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
  10. package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
  11. package/lib/accounts/types.d.ts +22 -11
  12. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +17 -17
  13. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +37 -35
  14. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  15. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
  16. package/lib/addresses/marketAddresses.d.ts +1 -3
  17. package/lib/addresses/pda.d.ts +10 -4
  18. package/lib/addresses/pda.js +51 -14
  19. package/lib/admin.d.ts +34 -22
  20. package/lib/admin.js +182 -73
  21. package/lib/clearingHouse.d.ts +120 -42
  22. package/lib/clearingHouse.js +1505 -254
  23. package/lib/clearingHouseConfig.d.ts +4 -4
  24. package/lib/clearingHouseUser.d.ts +50 -38
  25. package/lib/clearingHouseUser.js +410 -190
  26. package/lib/clearingHouseUserStats.d.ts +18 -0
  27. package/lib/clearingHouseUserStats.js +49 -0
  28. package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
  29. package/lib/clearingHouseUserStatsConfig.js +2 -0
  30. package/lib/config.d.ts +7 -9
  31. package/lib/config.js +21 -21
  32. package/lib/constants/numericConstants.d.ts +18 -12
  33. package/lib/constants/numericConstants.js +28 -21
  34. package/lib/constants/perpMarkets.d.ts +18 -0
  35. package/lib/constants/{markets.js → perpMarkets.js} +7 -7
  36. package/lib/constants/spotMarkets.d.ts +19 -0
  37. package/lib/constants/spotMarkets.js +53 -0
  38. package/lib/dlob/DLOB.d.ts +73 -0
  39. package/lib/dlob/DLOB.js +553 -0
  40. package/lib/dlob/DLOBNode.d.ts +52 -0
  41. package/lib/dlob/DLOBNode.js +82 -0
  42. package/lib/dlob/NodeList.d.ts +26 -0
  43. package/lib/dlob/NodeList.js +138 -0
  44. package/lib/events/eventList.js +3 -0
  45. package/lib/events/eventSubscriber.d.ts +4 -2
  46. package/lib/events/eventSubscriber.js +16 -9
  47. package/lib/events/fetchLogs.d.ts +10 -1
  48. package/lib/events/fetchLogs.js +27 -7
  49. package/lib/events/pollingLogProvider.d.ts +2 -1
  50. package/lib/events/pollingLogProvider.js +6 -2
  51. package/lib/events/sort.js +8 -11
  52. package/lib/events/types.d.ts +7 -2
  53. package/lib/events/types.js +5 -0
  54. package/lib/examples/makeTradeExample.js +20 -8
  55. package/lib/factory/bigNum.d.ts +1 -0
  56. package/lib/factory/bigNum.js +34 -10
  57. package/lib/idl/clearing_house.json +4313 -1433
  58. package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  59. package/lib/index.d.ts +21 -6
  60. package/lib/index.js +25 -6
  61. package/lib/math/amm.d.ts +8 -5
  62. package/lib/math/amm.js +68 -46
  63. package/lib/math/auction.js +4 -1
  64. package/lib/math/conversion.js +1 -1
  65. package/lib/math/funding.d.ts +6 -6
  66. package/lib/math/funding.js +11 -10
  67. package/lib/math/insurance.d.ts +4 -0
  68. package/lib/math/insurance.js +27 -0
  69. package/lib/math/margin.d.ts +11 -0
  70. package/lib/math/margin.js +77 -0
  71. package/lib/math/market.d.ts +14 -9
  72. package/lib/math/market.js +69 -7
  73. package/lib/math/oracles.d.ts +4 -0
  74. package/lib/math/oracles.js +36 -8
  75. package/lib/math/orders.d.ts +6 -2
  76. package/lib/math/orders.js +78 -3
  77. package/lib/math/position.d.ts +21 -13
  78. package/lib/math/position.js +76 -36
  79. package/lib/math/repeg.js +14 -5
  80. package/lib/math/spotBalance.d.ts +22 -0
  81. package/lib/math/spotBalance.js +193 -0
  82. package/lib/math/spotMarket.d.ts +4 -0
  83. package/lib/math/spotMarket.js +8 -0
  84. package/lib/math/spotPosition.d.ts +6 -0
  85. package/lib/math/spotPosition.js +23 -0
  86. package/lib/math/trade.d.ts +10 -10
  87. package/lib/math/trade.js +22 -29
  88. package/lib/oracles/pythClient.js +1 -1
  89. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  90. package/lib/oracles/switchboardClient.js +1 -1
  91. package/lib/orderParams.d.ts +14 -5
  92. package/lib/orderParams.js +12 -92
  93. package/lib/serum/serumSubscriber.d.ts +23 -0
  94. package/lib/serum/serumSubscriber.js +41 -0
  95. package/lib/serum/types.d.ts +11 -0
  96. package/lib/serum/types.js +2 -0
  97. package/lib/slot/SlotSubscriber.d.ts +7 -0
  98. package/lib/slot/SlotSubscriber.js +3 -0
  99. package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
  100. package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
  101. package/lib/tx/retryTxSender.d.ts +1 -1
  102. package/lib/tx/retryTxSender.js +13 -4
  103. package/lib/tx/types.d.ts +1 -1
  104. package/lib/tx/utils.js +1 -1
  105. package/lib/types.d.ts +474 -123
  106. package/lib/types.js +99 -5
  107. package/lib/userMap/userMap.d.ts +25 -0
  108. package/lib/userMap/userMap.js +73 -0
  109. package/lib/userMap/userStatsMap.d.ts +19 -0
  110. package/lib/userMap/userStatsMap.js +68 -0
  111. package/lib/util/computeUnits.js +1 -1
  112. package/lib/util/getTokenAddress.d.ts +2 -0
  113. package/lib/util/getTokenAddress.js +9 -0
  114. package/package.json +9 -5
  115. package/src/accounts/bulkUserStatsSubscription.ts +33 -0
  116. package/src/accounts/bulkUserSubscription.ts +0 -1
  117. package/src/accounts/fetch.ts +27 -2
  118. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +46 -42
  119. package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
  120. package/src/accounts/types.ts +31 -11
  121. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +64 -59
  122. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  123. package/src/addresses/marketAddresses.ts +1 -2
  124. package/src/addresses/pda.ts +88 -14
  125. package/src/admin.ts +333 -128
  126. package/src/assert/assert.js +9 -0
  127. package/src/clearingHouse.ts +2464 -458
  128. package/src/clearingHouseConfig.ts +4 -3
  129. package/src/clearingHouseUser.ts +747 -291
  130. package/src/clearingHouseUserStats.ts +75 -0
  131. package/src/clearingHouseUserStatsConfig.ts +18 -0
  132. package/src/config.ts +30 -31
  133. package/src/constants/numericConstants.ts +41 -25
  134. package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
  135. package/src/constants/spotMarkets.ts +72 -0
  136. package/src/dlob/DLOB.ts +868 -0
  137. package/src/dlob/DLOBNode.ts +162 -0
  138. package/src/dlob/NodeList.ts +185 -0
  139. package/src/events/eventList.js +77 -0
  140. package/src/events/eventList.ts +3 -0
  141. package/src/events/eventSubscriber.ts +20 -12
  142. package/src/events/fetchLogs.ts +35 -8
  143. package/src/events/pollingLogProvider.ts +10 -2
  144. package/src/events/sort.ts +11 -15
  145. package/src/events/types.ts +16 -1
  146. package/src/examples/makeTradeExample.js +157 -0
  147. package/src/examples/makeTradeExample.ts +32 -14
  148. package/src/factory/bigNum.ts +42 -13
  149. package/src/idl/clearing_house.json +4313 -1433
  150. package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  151. package/src/index.ts +21 -6
  152. package/src/math/amm.ts +136 -66
  153. package/src/math/auction.ts +5 -1
  154. package/src/math/conversion.ts +2 -2
  155. package/src/math/funding.ts +20 -18
  156. package/src/math/insurance.ts +35 -0
  157. package/src/math/margin.ts +127 -0
  158. package/src/math/market.ts +138 -12
  159. package/src/math/oracles.ts +63 -9
  160. package/src/math/orders.ts +138 -4
  161. package/src/math/position.ts +119 -58
  162. package/src/math/repeg.ts +16 -6
  163. package/src/math/spotBalance.ts +316 -0
  164. package/src/math/spotMarket.ts +9 -0
  165. package/src/math/spotPosition.ts +47 -0
  166. package/src/math/trade.ts +43 -49
  167. package/src/oracles/pythClient.ts +2 -2
  168. package/src/oracles/quoteAssetOracleClient.ts +2 -2
  169. package/src/oracles/switchboardClient.ts +2 -2
  170. package/src/orderParams.ts +24 -137
  171. package/src/serum/serumSubscriber.ts +80 -0
  172. package/src/serum/types.ts +13 -0
  173. package/src/slot/SlotSubscriber.ts +11 -1
  174. package/src/token/index.js +38 -0
  175. package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
  176. package/src/tx/retryTxSender.ts +16 -5
  177. package/src/tx/types.js +2 -0
  178. package/src/tx/types.ts +2 -1
  179. package/src/tx/utils.js +17 -0
  180. package/src/tx/utils.ts +1 -1
  181. package/src/types.ts +477 -125
  182. package/src/userMap/userMap.ts +100 -0
  183. package/src/userMap/userStatsMap.ts +110 -0
  184. package/src/util/computeUnits.js +21 -11
  185. package/src/util/computeUnits.ts +1 -1
  186. package/src/util/getTokenAddress.js +9 -0
  187. package/src/util/getTokenAddress.ts +18 -0
  188. package/src/util/promiseTimeout.js +14 -0
  189. package/src/util/tps.js +27 -0
  190. package/tests/bn/test.ts +12 -3
  191. package/tests/dlob/helpers.ts +374 -0
  192. package/tests/dlob/test.ts +2865 -0
  193. package/lib/constants/banks.d.ts +0 -16
  194. package/lib/constants/banks.js +0 -34
  195. package/lib/constants/markets.d.ts +0 -19
  196. package/lib/math/bankBalance.d.ts +0 -9
  197. package/lib/math/bankBalance.js +0 -75
  198. package/lib/math/state.d.ts +0 -8
  199. package/lib/math/state.js +0 -15
  200. package/lib/orders.d.ts +0 -8
  201. package/lib/orders.js +0 -134
  202. package/src/constants/banks.ts +0 -43
  203. package/src/math/bankBalance.ts +0 -112
  204. package/src/math/state.ts +0 -14
  205. package/src/math/utils.js +0 -27
  206. package/src/math/utils.js.map +0 -1
  207. package/src/orders.ts +0 -244
  208. package/src/util/computeUnits.js.map +0 -1
package/src/admin.ts CHANGED
@@ -3,24 +3,22 @@ import {
3
3
  SYSVAR_RENT_PUBKEY,
4
4
  TransactionSignature,
5
5
  } from '@solana/web3.js';
6
- import {
7
- FeeStructure,
8
- OracleGuardRails,
9
- OracleSource,
10
- OrderFillerRewardStructure,
11
- } from './types';
6
+ import { FeeStructure, OracleGuardRails, OracleSource } from './types';
12
7
  import { BN } from '@project-serum/anchor';
13
8
  import * as anchor from '@project-serum/anchor';
14
9
  import {
15
10
  getClearingHouseStateAccountPublicKeyAndNonce,
16
- getBankVaultAuthorityPublicKey,
17
- getBankPublicKey,
18
- getBankVaultPublicKey,
11
+ getSpotMarketPublicKey,
12
+ getSpotMarketVaultPublicKey,
19
13
  getMarketPublicKey,
14
+ getInsuranceFundVaultPublicKey,
15
+ getSerumOpenOrdersPublicKey,
16
+ getSerumFulfillmentConfigPublicKey,
20
17
  } from './addresses/pda';
18
+ import { squareRootBN } from './math/utils';
21
19
  import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
22
20
  import { ClearingHouse } from './clearingHouse';
23
- import { PEG_PRECISION } from './constants/numericConstants';
21
+ import { PEG_PRECISION, ZERO } from './constants/numericConstants';
24
22
  import { calculateTargetPriceTrade } from './math/trade';
25
23
  import { calculateAmmReservesAfterSwap, getSwapDirection } from './math/amm';
26
24
 
@@ -36,20 +34,11 @@ export class Admin extends ClearingHouse {
36
34
  throw new Error('Clearing house already initialized');
37
35
  }
38
36
 
39
- const [insuranceVaultPublicKey] = await PublicKey.findProgramAddress(
40
- [Buffer.from(anchor.utils.bytes.utf8.encode('insurance_vault'))],
41
- this.program.programId
42
- );
43
-
44
- const [insuranceVaultAuthority] = await PublicKey.findProgramAddress(
45
- [insuranceVaultPublicKey.toBuffer()],
46
- this.program.programId
47
- );
48
-
49
37
  const [clearingHouseStatePublicKey] =
50
38
  await getClearingHouseStateAccountPublicKeyAndNonce(
51
39
  this.program.programId
52
40
  );
41
+
53
42
  const initializeTx = await this.program.transaction.initialize(
54
43
  adminControlsPrices,
55
44
  {
@@ -57,9 +46,8 @@ export class Admin extends ClearingHouse {
57
46
  admin: this.wallet.publicKey,
58
47
  state: clearingHouseStatePublicKey,
59
48
  quoteAssetMint: usdcMint,
60
- insuranceVault: insuranceVaultPublicKey,
61
- insuranceVaultAuthority: insuranceVaultAuthority,
62
49
  rent: SYSVAR_RENT_PUBKEY,
50
+ clearingHouseSigner: this.getSignerPublicKey(),
63
51
  systemProgram: anchor.web3.SystemProgram.programId,
64
52
  tokenProgram: TOKEN_PROGRAM_ID,
65
53
  },
@@ -75,7 +63,7 @@ export class Admin extends ClearingHouse {
75
63
  return [initializeTxSig];
76
64
  }
77
65
 
78
- public async initializeBank(
66
+ public async initializeSpotMarket(
79
67
  mint: PublicKey,
80
68
  optimalUtilization: BN,
81
69
  optimalRate: BN,
@@ -85,22 +73,27 @@ export class Admin extends ClearingHouse {
85
73
  initialAssetWeight: BN,
86
74
  maintenanceAssetWeight: BN,
87
75
  initialLiabilityWeight: BN,
88
- maintenanceLiabilityWeight: BN
76
+ maintenanceLiabilityWeight: BN,
77
+ imfFactor = new BN(0),
78
+ liquidationFee = ZERO
89
79
  ): Promise<TransactionSignature> {
90
- const bankIndex = this.getStateAccount().numberOfBanks;
91
- const bank = await getBankPublicKey(this.program.programId, bankIndex);
80
+ const spotMarketIndex = this.getStateAccount().numberOfSpotMarkets;
81
+ const spotMarket = await getSpotMarketPublicKey(
82
+ this.program.programId,
83
+ spotMarketIndex
84
+ );
92
85
 
93
- const bankVault = await getBankVaultPublicKey(
86
+ const spotMarketVault = await getSpotMarketVaultPublicKey(
94
87
  this.program.programId,
95
- bankIndex
88
+ spotMarketIndex
96
89
  );
97
90
 
98
- const bankVaultAuthority = await getBankVaultAuthorityPublicKey(
91
+ const insuranceFundVault = await getInsuranceFundVaultPublicKey(
99
92
  this.program.programId,
100
- bankIndex
93
+ spotMarketIndex
101
94
  );
102
95
 
103
- const initializeTx = await this.program.transaction.initializeBank(
96
+ const initializeTx = await this.program.transaction.initializeSpotMarket(
104
97
  optimalUtilization,
105
98
  optimalRate,
106
99
  maxRate,
@@ -109,14 +102,17 @@ export class Admin extends ClearingHouse {
109
102
  maintenanceAssetWeight,
110
103
  initialLiabilityWeight,
111
104
  maintenanceLiabilityWeight,
105
+ imfFactor,
106
+ liquidationFee,
112
107
  {
113
108
  accounts: {
114
109
  admin: this.wallet.publicKey,
115
110
  state: await this.getStatePublicKey(),
116
- bank,
117
- bankVault,
118
- bankVaultAuthority,
119
- bankMint: mint,
111
+ spotMarket,
112
+ spotMarketVault,
113
+ insuranceFundVault,
114
+ clearingHouseSigner: this.getSignerPublicKey(),
115
+ spotMarketMint: mint,
120
116
  oracle,
121
117
  rent: SYSVAR_RENT_PUBKEY,
122
118
  systemProgram: anchor.web3.SystemProgram.programId,
@@ -127,7 +123,7 @@ export class Admin extends ClearingHouse {
127
123
 
128
124
  const { txSig } = await this.txSender.send(initializeTx, [], this.opts);
129
125
 
130
- await this.accountSubscriber.addBank(bankIndex);
126
+ await this.accountSubscriber.addSpotMarket(spotMarketIndex);
131
127
  await this.accountSubscriber.addOracle({
132
128
  source: oracleSource,
133
129
  publicKey: oracle,
@@ -136,6 +132,41 @@ export class Admin extends ClearingHouse {
136
132
  return txSig;
137
133
  }
138
134
 
135
+ public async initializeSerumFulfillmentConfig(
136
+ marketIndex: number,
137
+ serumMarket: PublicKey,
138
+ serumProgram: PublicKey
139
+ ): Promise<TransactionSignature> {
140
+ const serumOpenOrders = getSerumOpenOrdersPublicKey(
141
+ this.program.programId,
142
+ serumMarket
143
+ );
144
+
145
+ const serumFulfillmentConfig = getSerumFulfillmentConfigPublicKey(
146
+ this.program.programId,
147
+ serumMarket
148
+ );
149
+
150
+ return await this.program.rpc.initializeSerumFulfillmentConfig(
151
+ marketIndex,
152
+ {
153
+ accounts: {
154
+ admin: this.wallet.publicKey,
155
+ state: await this.getStatePublicKey(),
156
+ baseSpotMarket: this.getSpotMarketAccount(marketIndex).pubkey,
157
+ quoteSpotMarket: this.getQuoteSpotMarketAccount().pubkey,
158
+ clearingHouseSigner: this.getSignerPublicKey(),
159
+ serumProgram,
160
+ serumMarket,
161
+ serumOpenOrders,
162
+ rent: SYSVAR_RENT_PUBKEY,
163
+ systemProgram: anchor.web3.SystemProgram.programId,
164
+ serumFulfillmentConfig,
165
+ },
166
+ }
167
+ );
168
+ }
169
+
139
170
  public async initializeMarket(
140
171
  priceOracle: PublicKey,
141
172
  baseAssetReserve: BN,
@@ -144,8 +175,8 @@ export class Admin extends ClearingHouse {
144
175
  pegMultiplier: BN = PEG_PRECISION,
145
176
  oracleSource: OracleSource = OracleSource.PYTH,
146
177
  marginRatioInitial = 2000,
147
- marginRatioPartial = 625,
148
- marginRatioMaintenance = 500
178
+ marginRatioMaintenance = 500,
179
+ liquidationFee = ZERO
149
180
  ): Promise<TransactionSignature> {
150
181
  const marketPublicKey = await getMarketPublicKey(
151
182
  this.program.programId,
@@ -159,8 +190,8 @@ export class Admin extends ClearingHouse {
159
190
  pegMultiplier,
160
191
  oracleSource,
161
192
  marginRatioInitial,
162
- marginRatioPartial,
163
193
  marginRatioMaintenance,
194
+ liquidationFee,
164
195
  {
165
196
  accounts: {
166
197
  state: await this.getStatePublicKey(),
@@ -178,7 +209,7 @@ export class Admin extends ClearingHouse {
178
209
  this.opts
179
210
  );
180
211
 
181
- await this.accountSubscriber.addMarket(
212
+ await this.accountSubscriber.addPerpMarket(
182
213
  this.getStateAccount().numberOfMarkets
183
214
  );
184
215
  await this.accountSubscriber.addOracle({
@@ -190,23 +221,29 @@ export class Admin extends ClearingHouse {
190
221
  }
191
222
 
192
223
  public async moveAmmPrice(
224
+ marketIndex: number,
193
225
  baseAssetReserve: BN,
194
226
  quoteAssetReserve: BN,
195
- marketIndex: BN
227
+ sqrtK?: BN
196
228
  ): Promise<TransactionSignature> {
197
229
  const marketPublicKey = await getMarketPublicKey(
198
230
  this.program.programId,
199
231
  marketIndex
200
232
  );
201
233
 
234
+ if (sqrtK == undefined) {
235
+ sqrtK = squareRootBN(baseAssetReserve.mul(quoteAssetReserve));
236
+ }
237
+
202
238
  return await this.program.rpc.moveAmmPrice(
203
239
  baseAssetReserve,
204
240
  quoteAssetReserve,
241
+ sqrtK,
205
242
  {
206
243
  accounts: {
207
244
  state: await this.getStatePublicKey(),
208
245
  admin: this.wallet.publicKey,
209
- market: marketPublicKey,
246
+ perpMarket: marketPublicKey,
210
247
  },
211
248
  }
212
249
  );
@@ -214,23 +251,36 @@ export class Admin extends ClearingHouse {
214
251
 
215
252
  public async updateK(
216
253
  sqrtK: BN,
217
- marketIndex: BN
254
+ marketIndex: number
218
255
  ): Promise<TransactionSignature> {
219
256
  return await this.program.rpc.updateK(sqrtK, {
220
257
  accounts: {
221
258
  state: await this.getStatePublicKey(),
222
259
  admin: this.wallet.publicKey,
223
260
  market: await getMarketPublicKey(this.program.programId, marketIndex),
224
- oracle: this.getMarketAccount(marketIndex).amm.oracle,
261
+ oracle: this.getPerpMarketAccount(marketIndex).amm.oracle,
262
+ },
263
+ });
264
+ }
265
+
266
+ public async updateConcentrationScale(
267
+ marketIndex: number,
268
+ concentrationScale: BN
269
+ ): Promise<TransactionSignature> {
270
+ return await this.program.rpc.updateConcentrationCoef(concentrationScale, {
271
+ accounts: {
272
+ state: await this.getStatePublicKey(),
273
+ admin: this.wallet.publicKey,
274
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
225
275
  },
226
276
  });
227
277
  }
228
278
 
229
279
  public async moveAmmToPrice(
230
- marketIndex: BN,
280
+ perpMarketIndex: number,
231
281
  targetPrice: BN
232
282
  ): Promise<TransactionSignature> {
233
- const market = this.getMarketAccount(marketIndex);
283
+ const market = this.getPerpMarketAccount(perpMarketIndex);
234
284
 
235
285
  const [direction, tradeSize, _] = calculateTargetPriceTrade(
236
286
  market,
@@ -250,17 +300,18 @@ export class Admin extends ClearingHouse {
250
300
 
251
301
  const marketPublicKey = await getMarketPublicKey(
252
302
  this.program.programId,
253
- marketIndex
303
+ perpMarketIndex
254
304
  );
255
305
 
256
306
  return await this.program.rpc.moveAmmPrice(
257
307
  newBaseAssetAmount,
258
308
  newQuoteAssetAmount,
309
+ market.amm.sqrtK,
259
310
  {
260
311
  accounts: {
261
312
  state: await this.getStatePublicKey(),
262
313
  admin: this.wallet.publicKey,
263
- market: marketPublicKey,
314
+ perpMarket: marketPublicKey,
264
315
  },
265
316
  }
266
317
  );
@@ -268,13 +319,13 @@ export class Admin extends ClearingHouse {
268
319
 
269
320
  public async repegAmmCurve(
270
321
  newPeg: BN,
271
- marketIndex: BN
322
+ marketIndex: number
272
323
  ): Promise<TransactionSignature> {
273
324
  const marketPublicKey = await getMarketPublicKey(
274
325
  this.program.programId,
275
326
  marketIndex
276
327
  );
277
- const ammData = this.getMarketAccount(marketIndex).amm;
328
+ const ammData = this.getPerpMarketAccount(marketIndex).amm;
278
329
 
279
330
  return await this.program.rpc.repegAmmCurve(newPeg, {
280
331
  accounts: {
@@ -287,9 +338,9 @@ export class Admin extends ClearingHouse {
287
338
  }
288
339
 
289
340
  public async updateAmmOracleTwap(
290
- marketIndex: BN
341
+ marketIndex: number
291
342
  ): Promise<TransactionSignature> {
292
- const ammData = this.getMarketAccount(marketIndex).amm;
343
+ const ammData = this.getPerpMarketAccount(marketIndex).amm;
293
344
  const marketPublicKey = await getMarketPublicKey(
294
345
  this.program.programId,
295
346
  marketIndex
@@ -306,9 +357,9 @@ export class Admin extends ClearingHouse {
306
357
  }
307
358
 
308
359
  public async resetAmmOracleTwap(
309
- marketIndex: BN
360
+ marketIndex: number
310
361
  ): Promise<TransactionSignature> {
311
- const ammData = this.getMarketAccount(marketIndex).amm;
362
+ const ammData = this.getPerpMarketAccount(marketIndex).amm;
312
363
  const marketPublicKey = await getMarketPublicKey(
313
364
  this.program.programId,
314
365
  marketIndex
@@ -324,60 +375,22 @@ export class Admin extends ClearingHouse {
324
375
  });
325
376
  }
326
377
 
327
- public async withdrawFromInsuranceVault(
378
+ public async depositIntoMarketFeePool(
379
+ marketIndex: number,
328
380
  amount: BN,
329
- recipient: PublicKey
381
+ sourceVault: PublicKey
330
382
  ): Promise<TransactionSignature> {
331
- const state = await this.getStateAccount();
332
- return await this.program.rpc.withdrawFromInsuranceVault(amount, {
333
- accounts: {
334
- admin: this.wallet.publicKey,
335
- state: await this.getStatePublicKey(),
336
- insuranceVault: state.insuranceVault,
337
- insuranceVaultAuthority: state.insuranceVaultAuthority,
338
- recipient: recipient,
339
- tokenProgram: TOKEN_PROGRAM_ID,
340
- },
341
- });
342
- }
383
+ const spotMarket = this.getQuoteSpotMarketAccount();
343
384
 
344
- public async withdrawFees(
345
- marketIndex: BN,
346
- amount: BN,
347
- recipient: PublicKey
348
- ): Promise<TransactionSignature> {
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, {
355
- accounts: {
356
- admin: this.wallet.publicKey,
357
- state: await this.getStatePublicKey(),
358
- market: marketPublicKey,
359
- bank: bank.pubkey,
360
- bankVault: bank.vault,
361
- bankVaultAuthority: bank.vaultAuthority,
362
- recipient: recipient,
363
- tokenProgram: TOKEN_PROGRAM_ID,
364
- },
365
- });
366
- }
367
-
368
- public async withdrawFromInsuranceVaultToMarket(
369
- marketIndex: BN,
370
- amount: BN
371
- ): Promise<TransactionSignature> {
372
- const state = await this.getStateAccount();
373
- return await this.program.rpc.withdrawFromInsuranceVaultToMarket(amount, {
385
+ return await this.program.rpc.depositIntoMarketFeePool(amount, {
374
386
  accounts: {
375
387
  admin: this.wallet.publicKey,
376
388
  state: await this.getStatePublicKey(),
377
389
  market: await getMarketPublicKey(this.program.programId, marketIndex),
378
- insuranceVault: state.insuranceVault,
379
- insuranceVaultAuthority: state.insuranceVaultAuthority,
380
- bankVault: this.getQuoteAssetBankAccount().vault,
390
+ sourceVault,
391
+ clearingHouseSigner: this.getSignerPublicKey(),
392
+ quoteSpotMarket: spotMarket.pubkey,
393
+ spotMarketVault: spotMarket.vault,
381
394
  tokenProgram: TOKEN_PROGRAM_ID,
382
395
  },
383
396
  });
@@ -393,7 +406,7 @@ export class Admin extends ClearingHouse {
393
406
  }
394
407
 
395
408
  public async updateCurveUpdateIntensity(
396
- marketIndex: BN,
409
+ marketIndex: number,
397
410
  curveUpdateIntensity: number
398
411
  ): Promise<TransactionSignature> {
399
412
  // assert(curveUpdateIntensity >= 0 && curveUpdateIntensity <= 100);
@@ -412,14 +425,12 @@ export class Admin extends ClearingHouse {
412
425
  }
413
426
 
414
427
  public async updateMarginRatio(
415
- marketIndex: BN,
428
+ marketIndex: number,
416
429
  marginRatioInitial: number,
417
- marginRatioPartial: number,
418
430
  marginRatioMaintenance: number
419
431
  ): Promise<TransactionSignature> {
420
432
  return await this.program.rpc.updateMarginRatio(
421
433
  marginRatioInitial,
422
- marginRatioPartial,
423
434
  marginRatioMaintenance,
424
435
  {
425
436
  accounts: {
@@ -432,7 +443,7 @@ export class Admin extends ClearingHouse {
432
443
  }
433
444
 
434
445
  public async updateMarketBaseSpread(
435
- marketIndex: BN,
446
+ marketIndex: number,
436
447
  baseSpread: number
437
448
  ): Promise<TransactionSignature> {
438
449
  return await this.program.rpc.updateMarketBaseSpread(baseSpread, {
@@ -444,8 +455,21 @@ export class Admin extends ClearingHouse {
444
455
  });
445
456
  }
446
457
 
458
+ public async updateAmmJitIntensity(
459
+ marketIndex: number,
460
+ ammJitIntensity: number
461
+ ): Promise<TransactionSignature> {
462
+ return await this.program.rpc.updateAmmJitIntensity(ammJitIntensity, {
463
+ accounts: {
464
+ admin: this.wallet.publicKey,
465
+ state: await this.getStatePublicKey(),
466
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
467
+ },
468
+ });
469
+ }
470
+
447
471
  public async updateMarketMaxSpread(
448
- marketIndex: BN,
472
+ marketIndex: number,
449
473
  maxSpread: number
450
474
  ): Promise<TransactionSignature> {
451
475
  return await this.program.rpc.updateMarketMaxSpread(maxSpread, {
@@ -533,22 +557,21 @@ export class Admin extends ClearingHouse {
533
557
  );
534
558
  }
535
559
 
536
- public async updateOrderFillerRewardStructure(
537
- orderFillerRewardStructure: OrderFillerRewardStructure
560
+ public async updatePerpFeeStructure(
561
+ feeStructure: FeeStructure
538
562
  ): Promise<TransactionSignature> {
539
- return await this.program.rpc.updateOrderFillerRewardStructure(
540
- orderFillerRewardStructure,
541
- {
542
- accounts: {
543
- admin: this.wallet.publicKey,
544
- state: await this.getStatePublicKey(),
545
- },
546
- }
547
- );
563
+ return await this.program.rpc.updatePerpFeeStructure(feeStructure, {
564
+ accounts: {
565
+ admin: this.wallet.publicKey,
566
+ state: await this.getStatePublicKey(),
567
+ },
568
+ });
548
569
  }
549
570
 
550
- public async updateFee(fees: FeeStructure): Promise<TransactionSignature> {
551
- return await this.program.rpc.updateFee(fees, {
571
+ public async updateSpotFeeStructure(
572
+ feeStructure: FeeStructure
573
+ ): Promise<TransactionSignature> {
574
+ return await this.program.rpc.updateSpotFeeStructure(feeStructure, {
552
575
  accounts: {
553
576
  admin: this.wallet.publicKey,
554
577
  state: await this.getStatePublicKey(),
@@ -567,8 +590,100 @@ export class Admin extends ClearingHouse {
567
590
  });
568
591
  }
569
592
 
593
+ public async updateWithdrawGuardThreshold(
594
+ marketIndex: number,
595
+ withdrawGuardThreshold: BN
596
+ ): Promise<TransactionSignature> {
597
+ return await this.program.rpc.updateWithdrawGuardThreshold(
598
+ withdrawGuardThreshold,
599
+ {
600
+ accounts: {
601
+ admin: this.wallet.publicKey,
602
+ state: await this.getStatePublicKey(),
603
+ spotMarket: await getSpotMarketPublicKey(
604
+ this.program.programId,
605
+ marketIndex
606
+ ),
607
+ },
608
+ }
609
+ );
610
+ }
611
+
612
+ public async updateSpotMarketIfFactor(
613
+ marketIndex: number,
614
+ userIfFactor: BN,
615
+ totalIfFactor: BN
616
+ ): Promise<TransactionSignature> {
617
+ return await this.program.rpc.updateSpotMarketIfFactor(
618
+ marketIndex,
619
+ userIfFactor,
620
+ totalIfFactor,
621
+ {
622
+ accounts: {
623
+ admin: this.wallet.publicKey,
624
+ state: await this.getStatePublicKey(),
625
+ spotMarket: await getSpotMarketPublicKey(
626
+ this.program.programId,
627
+ marketIndex
628
+ ),
629
+ },
630
+ }
631
+ );
632
+ }
633
+
634
+ public async updateSpotMarketRevenueSettlePeriod(
635
+ marketIndex: number,
636
+ revenueSettlePeriod: BN
637
+ ): Promise<TransactionSignature> {
638
+ return await this.program.rpc.updateSpotMarketRevenueSettlePeriod(
639
+ revenueSettlePeriod,
640
+ {
641
+ accounts: {
642
+ admin: this.wallet.publicKey,
643
+ state: await this.getStatePublicKey(),
644
+ spotMarket: await getSpotMarketPublicKey(
645
+ this.program.programId,
646
+ marketIndex
647
+ ),
648
+ },
649
+ }
650
+ );
651
+ }
652
+
653
+ public async updateInsuranceWithdrawEscrowPeriod(
654
+ marketIndex: number,
655
+ insuranceWithdrawEscrowPeriod: BN
656
+ ): Promise<TransactionSignature> {
657
+ return await this.program.rpc.updateInsuranceWithdrawEscrowPeriod(
658
+ insuranceWithdrawEscrowPeriod,
659
+ {
660
+ accounts: {
661
+ admin: this.wallet.publicKey,
662
+ state: await this.getStatePublicKey(),
663
+ spotMarket: await getSpotMarketPublicKey(
664
+ this.program.programId,
665
+ marketIndex
666
+ ),
667
+ },
668
+ }
669
+ );
670
+ }
671
+
672
+ public async updateLpCooldownTime(
673
+ marketIndex: number,
674
+ cooldownTime: BN
675
+ ): Promise<TransactionSignature> {
676
+ return await this.program.rpc.updateLpCooldownTime(cooldownTime, {
677
+ accounts: {
678
+ admin: this.wallet.publicKey,
679
+ state: await this.getStatePublicKey(),
680
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
681
+ },
682
+ });
683
+ }
684
+
570
685
  public async updateMarketOracle(
571
- marketIndex: BN,
686
+ marketIndex: number,
572
687
  oracle: PublicKey,
573
688
  oracleSource: OracleSource
574
689
  ): Promise<TransactionSignature> {
@@ -582,7 +697,7 @@ export class Admin extends ClearingHouse {
582
697
  }
583
698
 
584
699
  public async updateMarketMinimumQuoteAssetTradeSize(
585
- marketIndex: BN,
700
+ marketIndex: number,
586
701
  minimumTradeSize: BN
587
702
  ): Promise<TransactionSignature> {
588
703
  return await this.program.rpc.updateMarketMinimumQuoteAssetTradeSize(
@@ -598,7 +713,7 @@ export class Admin extends ClearingHouse {
598
713
  }
599
714
 
600
715
  public async updateMarketBaseAssetAmountStepSize(
601
- marketIndex: BN,
716
+ marketIndex: number,
602
717
  stepSize: BN
603
718
  ): Promise<TransactionSignature> {
604
719
  return await this.program.rpc.updateMarketBaseAssetAmountStepSize(
@@ -613,6 +728,22 @@ export class Admin extends ClearingHouse {
613
728
  );
614
729
  }
615
730
 
731
+ public async updateMarketExpiry(
732
+ perpMarketIndex: number,
733
+ expiryTs: BN
734
+ ): Promise<TransactionSignature> {
735
+ return await this.program.rpc.updateMarketExpiry(expiryTs, {
736
+ accounts: {
737
+ admin: this.wallet.publicKey,
738
+ state: await this.getStatePublicKey(),
739
+ perpMarket: await getMarketPublicKey(
740
+ this.program.programId,
741
+ perpMarketIndex
742
+ ),
743
+ },
744
+ });
745
+ }
746
+
616
747
  public async updateWhitelistMint(
617
748
  whitelistMint?: PublicKey
618
749
  ): Promise<TransactionSignature> {
@@ -654,7 +785,6 @@ export class Admin extends ClearingHouse {
654
785
  },
655
786
  });
656
787
  }
657
-
658
788
  public async updateExchangePaused(
659
789
  exchangePaused: boolean
660
790
  ): Promise<TransactionSignature> {
@@ -675,11 +805,11 @@ export class Admin extends ClearingHouse {
675
805
  });
676
806
  }
677
807
 
678
- public async updateOrderAuctionTime(
679
- time: BN | number
808
+ public async updatePerpAuctionDuration(
809
+ minDuration: BN | number
680
810
  ): Promise<TransactionSignature> {
681
- return await this.program.rpc.updateOrderAuctionTime(
682
- typeof time === 'number' ? time : time.toNumber,
811
+ return await this.program.rpc.updatePerpAuctionDuration(
812
+ typeof minDuration === 'number' ? minDuration : minDuration.toNumber(),
683
813
  {
684
814
  accounts: {
685
815
  admin: this.wallet.publicKey,
@@ -688,4 +818,79 @@ export class Admin extends ClearingHouse {
688
818
  }
689
819
  );
690
820
  }
821
+
822
+ public async updateSpotAuctionDuration(
823
+ defaultAuctionDuration: number
824
+ ): Promise<TransactionSignature> {
825
+ return await this.program.rpc.updateSpotAuctionDuration(
826
+ defaultAuctionDuration,
827
+ {
828
+ accounts: {
829
+ admin: this.wallet.publicKey,
830
+ state: await this.getStatePublicKey(),
831
+ },
832
+ }
833
+ );
834
+ }
835
+
836
+ public async updateMaxBaseAssetAmountRatio(
837
+ marketIndex: number,
838
+ maxBaseAssetAmountRatio: number
839
+ ): Promise<TransactionSignature> {
840
+ return await this.program.rpc.updateMaxBaseAssetAmountRatio(
841
+ maxBaseAssetAmountRatio,
842
+ {
843
+ accounts: {
844
+ admin: this.wallet.publicKey,
845
+ state: await this.getStatePublicKey(),
846
+ market: this.getPerpMarketAccount(marketIndex).pubkey,
847
+ },
848
+ }
849
+ );
850
+ }
851
+
852
+ public async updateMaxSlippageRatio(
853
+ marketIndex: number,
854
+ maxSlippageRatio: number
855
+ ): Promise<TransactionSignature> {
856
+ return await this.program.rpc.updateMaxSlippageRatio(maxSlippageRatio, {
857
+ accounts: {
858
+ admin: this.wallet.publicKey,
859
+ state: await this.getStatePublicKey(),
860
+ market: this.getPerpMarketAccount(marketIndex).pubkey,
861
+ },
862
+ });
863
+ }
864
+
865
+ public async updateMarketMaxImbalances(
866
+ marketIndex: number,
867
+ unrealizedMaxImbalance: BN,
868
+ maxRevenueWithdrawPerPeriod: BN,
869
+ quoteMaxInsurance: BN
870
+ ): Promise<TransactionSignature> {
871
+ return await this.program.rpc.updateMarketMaxImbalances(
872
+ unrealizedMaxImbalance,
873
+ maxRevenueWithdrawPerPeriod,
874
+ quoteMaxInsurance,
875
+ {
876
+ accounts: {
877
+ admin: this.wallet.publicKey,
878
+ state: await this.getStatePublicKey(),
879
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
880
+ },
881
+ }
882
+ );
883
+ }
884
+
885
+ public async updateSerumVault(
886
+ srmVault: PublicKey
887
+ ): Promise<TransactionSignature> {
888
+ return await this.program.rpc.updateSerumVault({
889
+ accounts: {
890
+ admin: this.wallet.publicKey,
891
+ state: await this.getStatePublicKey(),
892
+ srmVault: srmVault,
893
+ },
894
+ });
895
+ }
691
896
  }