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

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 +42 -26
  20. package/lib/admin.js +228 -81
  21. package/lib/clearingHouse.d.ts +130 -49
  22. package/lib/clearingHouse.js +1256 -367
  23. package/lib/clearingHouseConfig.d.ts +4 -4
  24. package/lib/clearingHouseUser.d.ts +51 -39
  25. package/lib/clearingHouseUser.js +408 -191
  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 +25 -12
  33. package/lib/constants/numericConstants.js +35 -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 +74 -0
  39. package/lib/dlob/DLOB.js +595 -0
  40. package/lib/dlob/DLOBNode.d.ts +54 -0
  41. package/lib/dlob/DLOBNode.js +85 -0
  42. package/lib/dlob/NodeList.d.ts +26 -0
  43. package/lib/dlob/NodeList.js +139 -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 +8 -2
  53. package/lib/events/types.js +6 -0
  54. package/lib/examples/makeTradeExample.js +20 -8
  55. package/lib/factory/bigNum.d.ts +2 -0
  56. package/lib/factory/bigNum.js +48 -10
  57. package/lib/idl/clearing_house.json +4889 -1529
  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 +9 -6
  62. package/lib/math/amm.js +91 -66
  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 +72 -10
  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 +192 -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 +32 -39
  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 +566 -138
  106. package/lib/types.js +129 -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 +12 -6
  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 +452 -147
  126. package/src/assert/assert.js +9 -0
  127. package/src/clearingHouse.ts +2066 -467
  128. package/src/clearingHouseConfig.ts +4 -3
  129. package/src/clearingHouseUser.ts +747 -294
  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 +48 -25
  134. package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
  135. package/src/constants/spotMarkets.ts +73 -0
  136. package/src/dlob/DLOB.ts +953 -0
  137. package/src/dlob/DLOBNode.ts +167 -0
  138. package/src/dlob/NodeList.ts +189 -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 +19 -1
  146. package/src/examples/makeTradeExample.js +157 -0
  147. package/src/examples/makeTradeExample.ts +32 -14
  148. package/src/factory/bigNum.ts +65 -13
  149. package/src/idl/clearing_house.json +4889 -1529
  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 +171 -88
  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 +145 -14
  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 +319 -0
  164. package/src/math/spotMarket.ts +9 -0
  165. package/src/math/spotPosition.ts +47 -0
  166. package/src/math/trade.ts +53 -59
  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 +533 -140
  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 +24 -10
  191. package/tests/dlob/helpers.ts +397 -0
  192. package/tests/dlob/test.ts +3688 -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
@@ -7,27 +7,33 @@ import {
7
7
  FeeStructure,
8
8
  OracleGuardRails,
9
9
  OracleSource,
10
- OrderFillerRewardStructure,
10
+ ExchangeStatus,
11
+ MarketStatus,
12
+ ContractTier,
13
+ AssetTier,
11
14
  } from './types';
12
15
  import { BN } from '@project-serum/anchor';
13
16
  import * as anchor from '@project-serum/anchor';
14
17
  import {
15
18
  getClearingHouseStateAccountPublicKeyAndNonce,
16
- getBankVaultAuthorityPublicKey,
17
- getBankPublicKey,
18
- getBankVaultPublicKey,
19
+ getSpotMarketPublicKey,
20
+ getSpotMarketVaultPublicKey,
19
21
  getMarketPublicKey,
22
+ getInsuranceFundVaultPublicKey,
23
+ getSerumOpenOrdersPublicKey,
24
+ getSerumFulfillmentConfigPublicKey,
20
25
  } from './addresses/pda';
26
+ import { squareRootBN } from './math/utils';
21
27
  import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
22
28
  import { ClearingHouse } from './clearingHouse';
23
- import { PEG_PRECISION } from './constants/numericConstants';
29
+ import { PEG_PRECISION, ZERO } from './constants/numericConstants';
24
30
  import { calculateTargetPriceTrade } from './math/trade';
25
31
  import { calculateAmmReservesAfterSwap, getSwapDirection } from './math/amm';
26
32
 
27
33
  export class Admin extends ClearingHouse {
28
34
  public async initialize(
29
35
  usdcMint: PublicKey,
30
- adminControlsPrices: boolean
36
+ _adminControlsPrices: boolean
31
37
  ): Promise<[TransactionSignature]> {
32
38
  const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(
33
39
  await this.getStatePublicKey()
@@ -36,35 +42,22 @@ export class Admin extends ClearingHouse {
36
42
  throw new Error('Clearing house already initialized');
37
43
  }
38
44
 
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
45
  const [clearingHouseStatePublicKey] =
50
46
  await getClearingHouseStateAccountPublicKeyAndNonce(
51
47
  this.program.programId
52
48
  );
53
- const initializeTx = await this.program.transaction.initialize(
54
- adminControlsPrices,
55
- {
56
- accounts: {
57
- admin: this.wallet.publicKey,
58
- state: clearingHouseStatePublicKey,
59
- quoteAssetMint: usdcMint,
60
- insuranceVault: insuranceVaultPublicKey,
61
- insuranceVaultAuthority: insuranceVaultAuthority,
62
- rent: SYSVAR_RENT_PUBKEY,
63
- systemProgram: anchor.web3.SystemProgram.programId,
64
- tokenProgram: TOKEN_PROGRAM_ID,
65
- },
66
- }
67
- );
49
+
50
+ const initializeTx = await this.program.transaction.initialize({
51
+ accounts: {
52
+ admin: this.wallet.publicKey,
53
+ state: clearingHouseStatePublicKey,
54
+ quoteAssetMint: usdcMint,
55
+ rent: SYSVAR_RENT_PUBKEY,
56
+ clearingHouseSigner: this.getSignerPublicKey(),
57
+ systemProgram: anchor.web3.SystemProgram.programId,
58
+ tokenProgram: TOKEN_PROGRAM_ID,
59
+ },
60
+ });
68
61
 
69
62
  const { txSig: initializeTxSig } = await this.txSender.send(
70
63
  initializeTx,
@@ -75,32 +68,38 @@ export class Admin extends ClearingHouse {
75
68
  return [initializeTxSig];
76
69
  }
77
70
 
78
- public async initializeBank(
71
+ public async initializeSpotMarket(
79
72
  mint: PublicKey,
80
- optimalUtilization: BN,
81
- optimalRate: BN,
82
- maxRate: BN,
73
+ optimalUtilization: number,
74
+ optimalRate: number,
75
+ maxRate: number,
83
76
  oracle: PublicKey,
84
77
  oracleSource: OracleSource,
85
78
  initialAssetWeight: BN,
86
79
  maintenanceAssetWeight: BN,
87
80
  initialLiabilityWeight: BN,
88
- maintenanceLiabilityWeight: BN
81
+ maintenanceLiabilityWeight: BN,
82
+ imfFactor = new BN(0),
83
+ liquidationFee = ZERO,
84
+ activeStatus = true
89
85
  ): Promise<TransactionSignature> {
90
- const bankIndex = this.getStateAccount().numberOfBanks;
91
- const bank = await getBankPublicKey(this.program.programId, bankIndex);
86
+ const spotMarketIndex = this.getStateAccount().numberOfSpotMarkets;
87
+ const spotMarket = await getSpotMarketPublicKey(
88
+ this.program.programId,
89
+ spotMarketIndex
90
+ );
92
91
 
93
- const bankVault = await getBankVaultPublicKey(
92
+ const spotMarketVault = await getSpotMarketVaultPublicKey(
94
93
  this.program.programId,
95
- bankIndex
94
+ spotMarketIndex
96
95
  );
97
96
 
98
- const bankVaultAuthority = await getBankVaultAuthorityPublicKey(
97
+ const insuranceFundVault = await getInsuranceFundVaultPublicKey(
99
98
  this.program.programId,
100
- bankIndex
99
+ spotMarketIndex
101
100
  );
102
101
 
103
- const initializeTx = await this.program.transaction.initializeBank(
102
+ const initializeTx = await this.program.transaction.initializeSpotMarket(
104
103
  optimalUtilization,
105
104
  optimalRate,
106
105
  maxRate,
@@ -109,14 +108,18 @@ export class Admin extends ClearingHouse {
109
108
  maintenanceAssetWeight,
110
109
  initialLiabilityWeight,
111
110
  maintenanceLiabilityWeight,
111
+ imfFactor,
112
+ liquidationFee,
113
+ activeStatus,
112
114
  {
113
115
  accounts: {
114
116
  admin: this.wallet.publicKey,
115
117
  state: await this.getStatePublicKey(),
116
- bank,
117
- bankVault,
118
- bankVaultAuthority,
119
- bankMint: mint,
118
+ spotMarket,
119
+ spotMarketVault,
120
+ insuranceFundVault,
121
+ clearingHouseSigner: this.getSignerPublicKey(),
122
+ spotMarketMint: mint,
120
123
  oracle,
121
124
  rent: SYSVAR_RENT_PUBKEY,
122
125
  systemProgram: anchor.web3.SystemProgram.programId,
@@ -127,7 +130,7 @@ export class Admin extends ClearingHouse {
127
130
 
128
131
  const { txSig } = await this.txSender.send(initializeTx, [], this.opts);
129
132
 
130
- await this.accountSubscriber.addBank(bankIndex);
133
+ await this.accountSubscriber.addSpotMarket(spotMarketIndex);
131
134
  await this.accountSubscriber.addOracle({
132
135
  source: oracleSource,
133
136
  publicKey: oracle,
@@ -136,6 +139,41 @@ export class Admin extends ClearingHouse {
136
139
  return txSig;
137
140
  }
138
141
 
142
+ public async initializeSerumFulfillmentConfig(
143
+ marketIndex: number,
144
+ serumMarket: PublicKey,
145
+ serumProgram: PublicKey
146
+ ): Promise<TransactionSignature> {
147
+ const serumOpenOrders = getSerumOpenOrdersPublicKey(
148
+ this.program.programId,
149
+ serumMarket
150
+ );
151
+
152
+ const serumFulfillmentConfig = getSerumFulfillmentConfigPublicKey(
153
+ this.program.programId,
154
+ serumMarket
155
+ );
156
+
157
+ return await this.program.rpc.initializeSerumFulfillmentConfig(
158
+ marketIndex,
159
+ {
160
+ accounts: {
161
+ admin: this.wallet.publicKey,
162
+ state: await this.getStatePublicKey(),
163
+ baseSpotMarket: this.getSpotMarketAccount(marketIndex).pubkey,
164
+ quoteSpotMarket: this.getQuoteSpotMarketAccount().pubkey,
165
+ clearingHouseSigner: this.getSignerPublicKey(),
166
+ serumProgram,
167
+ serumMarket,
168
+ serumOpenOrders,
169
+ rent: SYSVAR_RENT_PUBKEY,
170
+ systemProgram: anchor.web3.SystemProgram.programId,
171
+ serumFulfillmentConfig,
172
+ },
173
+ }
174
+ );
175
+ }
176
+
139
177
  public async initializeMarket(
140
178
  priceOracle: PublicKey,
141
179
  baseAssetReserve: BN,
@@ -144,8 +182,9 @@ export class Admin extends ClearingHouse {
144
182
  pegMultiplier: BN = PEG_PRECISION,
145
183
  oracleSource: OracleSource = OracleSource.PYTH,
146
184
  marginRatioInitial = 2000,
147
- marginRatioPartial = 625,
148
- marginRatioMaintenance = 500
185
+ marginRatioMaintenance = 500,
186
+ liquidationFee = ZERO,
187
+ activeStatus = true
149
188
  ): Promise<TransactionSignature> {
150
189
  const marketPublicKey = await getMarketPublicKey(
151
190
  this.program.programId,
@@ -159,8 +198,9 @@ export class Admin extends ClearingHouse {
159
198
  pegMultiplier,
160
199
  oracleSource,
161
200
  marginRatioInitial,
162
- marginRatioPartial,
163
201
  marginRatioMaintenance,
202
+ liquidationFee,
203
+ activeStatus,
164
204
  {
165
205
  accounts: {
166
206
  state: await this.getStatePublicKey(),
@@ -178,7 +218,7 @@ export class Admin extends ClearingHouse {
178
218
  this.opts
179
219
  );
180
220
 
181
- await this.accountSubscriber.addMarket(
221
+ await this.accountSubscriber.addPerpMarket(
182
222
  this.getStateAccount().numberOfMarkets
183
223
  );
184
224
  await this.accountSubscriber.addOracle({
@@ -190,23 +230,29 @@ export class Admin extends ClearingHouse {
190
230
  }
191
231
 
192
232
  public async moveAmmPrice(
233
+ marketIndex: number,
193
234
  baseAssetReserve: BN,
194
235
  quoteAssetReserve: BN,
195
- marketIndex: BN
236
+ sqrtK?: BN
196
237
  ): Promise<TransactionSignature> {
197
238
  const marketPublicKey = await getMarketPublicKey(
198
239
  this.program.programId,
199
240
  marketIndex
200
241
  );
201
242
 
243
+ if (sqrtK == undefined) {
244
+ sqrtK = squareRootBN(baseAssetReserve.mul(quoteAssetReserve));
245
+ }
246
+
202
247
  return await this.program.rpc.moveAmmPrice(
203
248
  baseAssetReserve,
204
249
  quoteAssetReserve,
250
+ sqrtK,
205
251
  {
206
252
  accounts: {
207
253
  state: await this.getStatePublicKey(),
208
254
  admin: this.wallet.publicKey,
209
- market: marketPublicKey,
255
+ perpMarket: marketPublicKey,
210
256
  },
211
257
  }
212
258
  );
@@ -214,23 +260,36 @@ export class Admin extends ClearingHouse {
214
260
 
215
261
  public async updateK(
216
262
  sqrtK: BN,
217
- marketIndex: BN
263
+ marketIndex: number
218
264
  ): Promise<TransactionSignature> {
219
265
  return await this.program.rpc.updateK(sqrtK, {
220
266
  accounts: {
221
267
  state: await this.getStatePublicKey(),
222
268
  admin: this.wallet.publicKey,
223
269
  market: await getMarketPublicKey(this.program.programId, marketIndex),
224
- oracle: this.getMarketAccount(marketIndex).amm.oracle,
270
+ oracle: this.getPerpMarketAccount(marketIndex).amm.oracle,
271
+ },
272
+ });
273
+ }
274
+
275
+ public async updateConcentrationScale(
276
+ marketIndex: number,
277
+ concentrationScale: BN
278
+ ): Promise<TransactionSignature> {
279
+ return await this.program.rpc.updateConcentrationCoef(concentrationScale, {
280
+ accounts: {
281
+ state: await this.getStatePublicKey(),
282
+ admin: this.wallet.publicKey,
283
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
225
284
  },
226
285
  });
227
286
  }
228
287
 
229
288
  public async moveAmmToPrice(
230
- marketIndex: BN,
289
+ perpMarketIndex: number,
231
290
  targetPrice: BN
232
291
  ): Promise<TransactionSignature> {
233
- const market = this.getMarketAccount(marketIndex);
292
+ const market = this.getPerpMarketAccount(perpMarketIndex);
234
293
 
235
294
  const [direction, tradeSize, _] = calculateTargetPriceTrade(
236
295
  market,
@@ -250,17 +309,18 @@ export class Admin extends ClearingHouse {
250
309
 
251
310
  const marketPublicKey = await getMarketPublicKey(
252
311
  this.program.programId,
253
- marketIndex
312
+ perpMarketIndex
254
313
  );
255
314
 
256
315
  return await this.program.rpc.moveAmmPrice(
257
316
  newBaseAssetAmount,
258
317
  newQuoteAssetAmount,
318
+ market.amm.sqrtK,
259
319
  {
260
320
  accounts: {
261
321
  state: await this.getStatePublicKey(),
262
322
  admin: this.wallet.publicKey,
263
- market: marketPublicKey,
323
+ perpMarket: marketPublicKey,
264
324
  },
265
325
  }
266
326
  );
@@ -268,13 +328,13 @@ export class Admin extends ClearingHouse {
268
328
 
269
329
  public async repegAmmCurve(
270
330
  newPeg: BN,
271
- marketIndex: BN
331
+ marketIndex: number
272
332
  ): Promise<TransactionSignature> {
273
333
  const marketPublicKey = await getMarketPublicKey(
274
334
  this.program.programId,
275
335
  marketIndex
276
336
  );
277
- const ammData = this.getMarketAccount(marketIndex).amm;
337
+ const ammData = this.getPerpMarketAccount(marketIndex).amm;
278
338
 
279
339
  return await this.program.rpc.repegAmmCurve(newPeg, {
280
340
  accounts: {
@@ -287,9 +347,9 @@ export class Admin extends ClearingHouse {
287
347
  }
288
348
 
289
349
  public async updateAmmOracleTwap(
290
- marketIndex: BN
350
+ marketIndex: number
291
351
  ): Promise<TransactionSignature> {
292
- const ammData = this.getMarketAccount(marketIndex).amm;
352
+ const ammData = this.getPerpMarketAccount(marketIndex).amm;
293
353
  const marketPublicKey = await getMarketPublicKey(
294
354
  this.program.programId,
295
355
  marketIndex
@@ -306,9 +366,9 @@ export class Admin extends ClearingHouse {
306
366
  }
307
367
 
308
368
  public async resetAmmOracleTwap(
309
- marketIndex: BN
369
+ marketIndex: number
310
370
  ): Promise<TransactionSignature> {
311
- const ammData = this.getMarketAccount(marketIndex).amm;
371
+ const ammData = this.getPerpMarketAccount(marketIndex).amm;
312
372
  const marketPublicKey = await getMarketPublicKey(
313
373
  this.program.programId,
314
374
  marketIndex
@@ -324,60 +384,22 @@ export class Admin extends ClearingHouse {
324
384
  });
325
385
  }
326
386
 
327
- public async withdrawFromInsuranceVault(
387
+ public async depositIntoMarketFeePool(
388
+ marketIndex: number,
328
389
  amount: BN,
329
- recipient: PublicKey
390
+ sourceVault: PublicKey
330
391
  ): 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
- }
392
+ const spotMarket = this.getQuoteSpotMarketAccount();
343
393
 
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, {
394
+ return await this.program.rpc.depositIntoMarketFeePool(amount, {
374
395
  accounts: {
375
396
  admin: this.wallet.publicKey,
376
397
  state: await this.getStatePublicKey(),
377
398
  market: await getMarketPublicKey(this.program.programId, marketIndex),
378
- insuranceVault: state.insuranceVault,
379
- insuranceVaultAuthority: state.insuranceVaultAuthority,
380
- bankVault: this.getQuoteAssetBankAccount().vault,
399
+ sourceVault,
400
+ clearingHouseSigner: this.getSignerPublicKey(),
401
+ quoteSpotMarket: spotMarket.pubkey,
402
+ spotMarketVault: spotMarket.vault,
381
403
  tokenProgram: TOKEN_PROGRAM_ID,
382
404
  },
383
405
  });
@@ -393,7 +415,7 @@ export class Admin extends ClearingHouse {
393
415
  }
394
416
 
395
417
  public async updateCurveUpdateIntensity(
396
- marketIndex: BN,
418
+ marketIndex: number,
397
419
  curveUpdateIntensity: number
398
420
  ): Promise<TransactionSignature> {
399
421
  // assert(curveUpdateIntensity >= 0 && curveUpdateIntensity <= 100);
@@ -412,14 +434,12 @@ export class Admin extends ClearingHouse {
412
434
  }
413
435
 
414
436
  public async updateMarginRatio(
415
- marketIndex: BN,
437
+ marketIndex: number,
416
438
  marginRatioInitial: number,
417
- marginRatioPartial: number,
418
439
  marginRatioMaintenance: number
419
440
  ): Promise<TransactionSignature> {
420
441
  return await this.program.rpc.updateMarginRatio(
421
442
  marginRatioInitial,
422
- marginRatioPartial,
423
443
  marginRatioMaintenance,
424
444
  {
425
445
  accounts: {
@@ -432,7 +452,7 @@ export class Admin extends ClearingHouse {
432
452
  }
433
453
 
434
454
  public async updateMarketBaseSpread(
435
- marketIndex: BN,
455
+ marketIndex: number,
436
456
  baseSpread: number
437
457
  ): Promise<TransactionSignature> {
438
458
  return await this.program.rpc.updateMarketBaseSpread(baseSpread, {
@@ -444,8 +464,21 @@ export class Admin extends ClearingHouse {
444
464
  });
445
465
  }
446
466
 
467
+ public async updateAmmJitIntensity(
468
+ marketIndex: number,
469
+ ammJitIntensity: number
470
+ ): Promise<TransactionSignature> {
471
+ return await this.program.rpc.updateAmmJitIntensity(ammJitIntensity, {
472
+ accounts: {
473
+ admin: this.wallet.publicKey,
474
+ state: await this.getStatePublicKey(),
475
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
476
+ },
477
+ });
478
+ }
479
+
447
480
  public async updateMarketMaxSpread(
448
- marketIndex: BN,
481
+ marketIndex: number,
449
482
  maxSpread: number
450
483
  ): Promise<TransactionSignature> {
451
484
  return await this.program.rpc.updateMarketMaxSpread(maxSpread, {
@@ -533,22 +566,21 @@ export class Admin extends ClearingHouse {
533
566
  );
534
567
  }
535
568
 
536
- public async updateOrderFillerRewardStructure(
537
- orderFillerRewardStructure: OrderFillerRewardStructure
569
+ public async updatePerpFeeStructure(
570
+ feeStructure: FeeStructure
538
571
  ): 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
- );
572
+ return await this.program.rpc.updatePerpFeeStructure(feeStructure, {
573
+ accounts: {
574
+ admin: this.wallet.publicKey,
575
+ state: await this.getStatePublicKey(),
576
+ },
577
+ });
548
578
  }
549
579
 
550
- public async updateFee(fees: FeeStructure): Promise<TransactionSignature> {
551
- return await this.program.rpc.updateFee(fees, {
580
+ public async updateSpotFeeStructure(
581
+ feeStructure: FeeStructure
582
+ ): Promise<TransactionSignature> {
583
+ return await this.program.rpc.updateSpotFeeStructure(feeStructure, {
552
584
  accounts: {
553
585
  admin: this.wallet.publicKey,
554
586
  state: await this.getStatePublicKey(),
@@ -567,8 +599,119 @@ export class Admin extends ClearingHouse {
567
599
  });
568
600
  }
569
601
 
602
+ public async updateWithdrawGuardThreshold(
603
+ marketIndex: number,
604
+ withdrawGuardThreshold: BN
605
+ ): Promise<TransactionSignature> {
606
+ return await this.program.rpc.updateWithdrawGuardThreshold(
607
+ withdrawGuardThreshold,
608
+ {
609
+ accounts: {
610
+ admin: this.wallet.publicKey,
611
+ state: await this.getStatePublicKey(),
612
+ spotMarket: await getSpotMarketPublicKey(
613
+ this.program.programId,
614
+ marketIndex
615
+ ),
616
+ },
617
+ }
618
+ );
619
+ }
620
+
621
+ public async updateSpotMarketIfFactor(
622
+ marketIndex: number,
623
+ userIfFactor: BN,
624
+ totalIfFactor: BN
625
+ ): Promise<TransactionSignature> {
626
+ return await this.program.rpc.updateSpotMarketIfFactor(
627
+ marketIndex,
628
+ userIfFactor,
629
+ totalIfFactor,
630
+ {
631
+ accounts: {
632
+ admin: this.wallet.publicKey,
633
+ state: await this.getStatePublicKey(),
634
+ spotMarket: await getSpotMarketPublicKey(
635
+ this.program.programId,
636
+ marketIndex
637
+ ),
638
+ },
639
+ }
640
+ );
641
+ }
642
+
643
+ public async updateSpotMarketRevenueSettlePeriod(
644
+ marketIndex: number,
645
+ revenueSettlePeriod: BN
646
+ ): Promise<TransactionSignature> {
647
+ return await this.program.rpc.updateSpotMarketRevenueSettlePeriod(
648
+ revenueSettlePeriod,
649
+ {
650
+ accounts: {
651
+ admin: this.wallet.publicKey,
652
+ state: await this.getStatePublicKey(),
653
+ spotMarket: await getSpotMarketPublicKey(
654
+ this.program.programId,
655
+ marketIndex
656
+ ),
657
+ },
658
+ }
659
+ );
660
+ }
661
+
662
+ public async updateSpotMarketMaxTokenDeposits(
663
+ marketIndex: number,
664
+ maxTokenDeposits: BN
665
+ ): Promise<TransactionSignature> {
666
+ return await this.program.rpc.updateSpotMarketMaxTokenDeposits(
667
+ maxTokenDeposits,
668
+ {
669
+ accounts: {
670
+ admin: this.wallet.publicKey,
671
+ state: await this.getStatePublicKey(),
672
+ spotMarket: await getSpotMarketPublicKey(
673
+ this.program.programId,
674
+ marketIndex
675
+ ),
676
+ },
677
+ }
678
+ );
679
+ }
680
+
681
+ public async updateInsuranceWithdrawEscrowPeriod(
682
+ marketIndex: number,
683
+ insuranceWithdrawEscrowPeriod: BN
684
+ ): Promise<TransactionSignature> {
685
+ return await this.program.rpc.updateInsuranceWithdrawEscrowPeriod(
686
+ insuranceWithdrawEscrowPeriod,
687
+ {
688
+ accounts: {
689
+ admin: this.wallet.publicKey,
690
+ state: await this.getStatePublicKey(),
691
+ spotMarket: await getSpotMarketPublicKey(
692
+ this.program.programId,
693
+ marketIndex
694
+ ),
695
+ },
696
+ }
697
+ );
698
+ }
699
+
700
+ public async updateLpCooldownTime(
701
+ marketIndex: number,
702
+ cooldownTime: BN
703
+ ): Promise<TransactionSignature> {
704
+ return await this.program.rpc.updateLpCooldownTime(cooldownTime, {
705
+ accounts: {
706
+ admin: this.wallet.publicKey,
707
+ state: await this.getStatePublicKey(),
708
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
709
+ },
710
+ });
711
+ }
712
+
570
713
  public async updateMarketOracle(
571
- marketIndex: BN,
714
+ marketIndex: number,
572
715
  oracle: PublicKey,
573
716
  oracleSource: OracleSource
574
717
  ): Promise<TransactionSignature> {
@@ -582,7 +725,7 @@ export class Admin extends ClearingHouse {
582
725
  }
583
726
 
584
727
  public async updateMarketMinimumQuoteAssetTradeSize(
585
- marketIndex: BN,
728
+ marketIndex: number,
586
729
  minimumTradeSize: BN
587
730
  ): Promise<TransactionSignature> {
588
731
  return await this.program.rpc.updateMarketMinimumQuoteAssetTradeSize(
@@ -598,7 +741,7 @@ export class Admin extends ClearingHouse {
598
741
  }
599
742
 
600
743
  public async updateMarketBaseAssetAmountStepSize(
601
- marketIndex: BN,
744
+ marketIndex: number,
602
745
  stepSize: BN
603
746
  ): Promise<TransactionSignature> {
604
747
  return await this.program.rpc.updateMarketBaseAssetAmountStepSize(
@@ -613,6 +756,22 @@ export class Admin extends ClearingHouse {
613
756
  );
614
757
  }
615
758
 
759
+ public async updateMarketExpiry(
760
+ perpMarketIndex: number,
761
+ expiryTs: BN
762
+ ): Promise<TransactionSignature> {
763
+ return await this.program.rpc.updateMarketExpiry(expiryTs, {
764
+ accounts: {
765
+ admin: this.wallet.publicKey,
766
+ state: await this.getStatePublicKey(),
767
+ perpMarket: await getMarketPublicKey(
768
+ this.program.programId,
769
+ perpMarketIndex
770
+ ),
771
+ },
772
+ });
773
+ }
774
+
616
775
  public async updateWhitelistMint(
617
776
  whitelistMint?: PublicKey
618
777
  ): Promise<TransactionSignature> {
@@ -644,42 +803,113 @@ export class Admin extends ClearingHouse {
644
803
  });
645
804
  }
646
805
 
647
- public async updateFundingPaused(
648
- fundingPaused: boolean
806
+ public async updateSpotMarketMarginWeights(
807
+ spotMarketIndex: number,
808
+ initialAssetWeight: BN,
809
+ maintenanceAssetWeight: BN,
810
+ initialLiabilityWeight: BN,
811
+ maintenanceLiabilityWeight: BN,
812
+ imfFactor = new BN(0)
649
813
  ): Promise<TransactionSignature> {
650
- return await this.program.rpc.updateFundingPaused(fundingPaused, {
814
+ return await this.program.rpc.updateSpotMarketMarginWeights(
815
+ initialAssetWeight,
816
+ maintenanceAssetWeight,
817
+ initialLiabilityWeight,
818
+ maintenanceLiabilityWeight,
819
+ imfFactor,
820
+ {
821
+ accounts: {
822
+ admin: this.wallet.publicKey,
823
+ state: await this.getStatePublicKey(),
824
+ spotMarket: await getSpotMarketPublicKey(
825
+ this.program.programId,
826
+ spotMarketIndex
827
+ ),
828
+ },
829
+ }
830
+ );
831
+ }
832
+
833
+ public async updateSpotMarketAssetTier(
834
+ spotMarketIndex: number,
835
+ assetTier: AssetTier
836
+ ): Promise<TransactionSignature> {
837
+ return await this.program.rpc.updateSpotMarketAssetTier(assetTier, {
651
838
  accounts: {
652
839
  admin: this.wallet.publicKey,
653
840
  state: await this.getStatePublicKey(),
841
+ spotMarket: await getSpotMarketPublicKey(
842
+ this.program.programId,
843
+ spotMarketIndex
844
+ ),
654
845
  },
655
846
  });
656
847
  }
657
848
 
658
- public async updateExchangePaused(
659
- exchangePaused: boolean
849
+ public async updateSpotMarketStatus(
850
+ spotMarketIndex: number,
851
+ marketStatus: MarketStatus
660
852
  ): Promise<TransactionSignature> {
661
- return await this.program.rpc.updateExchangePaused(exchangePaused, {
853
+ return await this.program.rpc.updateSpotMarketStatus(marketStatus, {
662
854
  accounts: {
663
855
  admin: this.wallet.publicKey,
664
856
  state: await this.getStatePublicKey(),
857
+ spotMarket: await getSpotMarketPublicKey(
858
+ this.program.programId,
859
+ spotMarketIndex
860
+ ),
665
861
  },
666
862
  });
667
863
  }
668
864
 
669
- public async disableAdminControlsPrices(): Promise<TransactionSignature> {
670
- return await this.program.rpc.disableAdminControlsPrices({
865
+ public async updatePerpMarketStatus(
866
+ perpMarketIndex: number,
867
+ marketStatus: MarketStatus
868
+ ): Promise<TransactionSignature> {
869
+ return await this.program.rpc.updatePerpMarketStatus(marketStatus, {
671
870
  accounts: {
672
871
  admin: this.wallet.publicKey,
673
872
  state: await this.getStatePublicKey(),
873
+ market: await getMarketPublicKey(
874
+ this.program.programId,
875
+ perpMarketIndex
876
+ ),
674
877
  },
675
878
  });
676
879
  }
677
880
 
678
- public async updateOrderAuctionTime(
679
- time: BN | number
881
+ public async updatePerpMarketContractTier(
882
+ perpMarketIndex: number,
883
+ contractTier: ContractTier
680
884
  ): Promise<TransactionSignature> {
681
- return await this.program.rpc.updateOrderAuctionTime(
682
- typeof time === 'number' ? time : time.toNumber,
885
+ return await this.program.rpc.updatePerpMarketContractTier(contractTier, {
886
+ accounts: {
887
+ admin: this.wallet.publicKey,
888
+ state: await this.getStatePublicKey(),
889
+ market: await getMarketPublicKey(
890
+ this.program.programId,
891
+ perpMarketIndex
892
+ ),
893
+ },
894
+ });
895
+ }
896
+
897
+ public async updateExchangeStatus(
898
+ exchangeStatus: ExchangeStatus
899
+ ): Promise<TransactionSignature> {
900
+ return await this.program.rpc.updateExchangeStatus(exchangeStatus, {
901
+ accounts: {
902
+ admin: this.wallet.publicKey,
903
+ state: await this.getStatePublicKey(),
904
+ },
905
+ });
906
+ }
907
+
908
+ public async updatePerpAuctionDuration(
909
+ minDuration: BN | number
910
+ ): Promise<TransactionSignature> {
911
+ return await this.program.rpc.updatePerpAuctionDuration(
912
+ typeof minDuration === 'number' ? minDuration : minDuration.toNumber(),
683
913
  {
684
914
  accounts: {
685
915
  admin: this.wallet.publicKey,
@@ -688,4 +918,79 @@ export class Admin extends ClearingHouse {
688
918
  }
689
919
  );
690
920
  }
921
+
922
+ public async updateSpotAuctionDuration(
923
+ defaultAuctionDuration: number
924
+ ): Promise<TransactionSignature> {
925
+ return await this.program.rpc.updateSpotAuctionDuration(
926
+ defaultAuctionDuration,
927
+ {
928
+ accounts: {
929
+ admin: this.wallet.publicKey,
930
+ state: await this.getStatePublicKey(),
931
+ },
932
+ }
933
+ );
934
+ }
935
+
936
+ public async updateMaxBaseAssetAmountRatio(
937
+ marketIndex: number,
938
+ maxBaseAssetAmountRatio: number
939
+ ): Promise<TransactionSignature> {
940
+ return await this.program.rpc.updateMaxBaseAssetAmountRatio(
941
+ maxBaseAssetAmountRatio,
942
+ {
943
+ accounts: {
944
+ admin: this.wallet.publicKey,
945
+ state: await this.getStatePublicKey(),
946
+ market: this.getPerpMarketAccount(marketIndex).pubkey,
947
+ },
948
+ }
949
+ );
950
+ }
951
+
952
+ public async updateMaxSlippageRatio(
953
+ marketIndex: number,
954
+ maxSlippageRatio: number
955
+ ): Promise<TransactionSignature> {
956
+ return await this.program.rpc.updateMaxSlippageRatio(maxSlippageRatio, {
957
+ accounts: {
958
+ admin: this.wallet.publicKey,
959
+ state: await this.getStatePublicKey(),
960
+ market: this.getPerpMarketAccount(marketIndex).pubkey,
961
+ },
962
+ });
963
+ }
964
+
965
+ public async updateMarketMaxImbalances(
966
+ marketIndex: number,
967
+ unrealizedMaxImbalance: BN,
968
+ maxRevenueWithdrawPerPeriod: BN,
969
+ quoteMaxInsurance: BN
970
+ ): Promise<TransactionSignature> {
971
+ return await this.program.rpc.updateMarketMaxImbalances(
972
+ unrealizedMaxImbalance,
973
+ maxRevenueWithdrawPerPeriod,
974
+ quoteMaxInsurance,
975
+ {
976
+ accounts: {
977
+ admin: this.wallet.publicKey,
978
+ state: await this.getStatePublicKey(),
979
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
980
+ },
981
+ }
982
+ );
983
+ }
984
+
985
+ public async updateSerumVault(
986
+ srmVault: PublicKey
987
+ ): Promise<TransactionSignature> {
988
+ return await this.program.rpc.updateSerumVault({
989
+ accounts: {
990
+ admin: this.wallet.publicKey,
991
+ state: await this.getStatePublicKey(),
992
+ srmVault: srmVault,
993
+ },
994
+ });
995
+ }
691
996
  }