@drift-labs/sdk 0.2.0-temp.2 → 2.0.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 (278) hide show
  1. package/README.md +27 -29
  2. package/lib/accounts/bulkAccountLoader.d.ts +2 -0
  3. package/lib/accounts/bulkAccountLoader.js +36 -29
  4. package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
  5. package/lib/accounts/bulkUserStatsSubscription.js +21 -0
  6. package/lib/accounts/bulkUserSubscription.d.ts +2 -2
  7. package/lib/accounts/bulkUserSubscription.js +0 -1
  8. package/lib/accounts/fetch.d.ts +2 -1
  9. package/lib/accounts/fetch.js +9 -1
  10. package/lib/accounts/{pollingClearingHouseAccountSubscriber.d.ts → pollingDriftClientAccountSubscriber.d.ts} +20 -25
  11. package/lib/accounts/{pollingClearingHouseAccountSubscriber.js → pollingDriftClientAccountSubscriber.js} +45 -49
  12. package/lib/accounts/{pollingOracleSubscriber.d.ts → pollingOracleAccountSubscriber.d.ts} +2 -2
  13. package/lib/accounts/{pollingOracleSubscriber.js → pollingOracleAccountSubscriber.js} +3 -3
  14. package/lib/accounts/pollingUserAccountSubscriber.d.ts +1 -1
  15. package/lib/accounts/pollingUserAccountSubscriber.js +5 -11
  16. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
  17. package/lib/accounts/pollingUserStatsAccountSubscriber.js +107 -0
  18. package/lib/accounts/types.d.ts +26 -15
  19. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +49 -0
  20. package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +47 -45
  21. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  22. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
  23. package/lib/addresses/marketAddresses.d.ts +1 -3
  24. package/lib/addresses/marketAddresses.js +1 -1
  25. package/lib/addresses/pda.d.ts +15 -9
  26. package/lib/addresses/pda.js +73 -35
  27. package/lib/adminClient.d.ts +65 -0
  28. package/lib/adminClient.js +637 -0
  29. package/lib/config.d.ts +10 -10
  30. package/lib/config.js +26 -22
  31. package/lib/constants/numericConstants.d.ts +29 -12
  32. package/lib/constants/numericConstants.js +40 -21
  33. package/lib/constants/perpMarkets.d.ts +18 -0
  34. package/lib/constants/{markets.js → perpMarkets.js} +20 -9
  35. package/lib/constants/spotMarkets.d.ts +19 -0
  36. package/lib/constants/spotMarkets.js +63 -0
  37. package/lib/dlob/DLOB.d.ts +82 -0
  38. package/lib/dlob/DLOB.js +694 -0
  39. package/lib/dlob/DLOBNode.d.ts +54 -0
  40. package/lib/dlob/DLOBNode.js +77 -0
  41. package/lib/dlob/NodeList.d.ts +27 -0
  42. package/lib/dlob/NodeList.js +144 -0
  43. package/lib/driftClient.d.ts +234 -0
  44. package/lib/driftClient.js +2108 -0
  45. package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +9 -9
  46. package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
  47. package/lib/events/eventList.d.ts +0 -1
  48. package/lib/events/eventList.js +0 -7
  49. package/lib/events/eventSubscriber.d.ts +5 -2
  50. package/lib/events/eventSubscriber.js +25 -11
  51. package/lib/events/fetchLogs.d.ts +13 -2
  52. package/lib/events/fetchLogs.js +45 -14
  53. package/lib/events/pollingLogProvider.d.ts +2 -1
  54. package/lib/events/pollingLogProvider.js +7 -3
  55. package/lib/events/sort.js +8 -11
  56. package/lib/events/txEventCache.d.ts +0 -2
  57. package/lib/events/txEventCache.js +0 -14
  58. package/lib/events/types.d.ts +11 -3
  59. package/lib/events/types.js +8 -0
  60. package/lib/events/webSocketLogProvider.js +1 -1
  61. package/lib/examples/makeTradeExample.js +30 -18
  62. package/lib/factory/bigNum.d.ts +8 -4
  63. package/lib/factory/bigNum.js +109 -19
  64. package/lib/idl/drift.json +8392 -0
  65. package/lib/index.d.ts +29 -12
  66. package/lib/index.js +29 -12
  67. package/lib/math/amm.d.ts +9 -6
  68. package/lib/math/amm.js +91 -38
  69. package/lib/math/conversion.js +1 -1
  70. package/lib/math/exchangeStatus.d.ts +4 -0
  71. package/lib/math/exchangeStatus.js +18 -0
  72. package/lib/math/funding.d.ts +6 -6
  73. package/lib/math/funding.js +23 -21
  74. package/lib/math/insurance.d.ts +4 -0
  75. package/lib/math/insurance.js +27 -0
  76. package/lib/math/margin.d.ts +11 -0
  77. package/lib/math/margin.js +82 -0
  78. package/lib/math/market.d.ts +14 -9
  79. package/lib/math/market.js +70 -10
  80. package/lib/math/oracles.d.ts +4 -0
  81. package/lib/math/oracles.js +36 -8
  82. package/lib/math/orders.d.ts +16 -6
  83. package/lib/math/orders.js +97 -17
  84. package/lib/math/position.d.ts +27 -13
  85. package/lib/math/position.js +91 -37
  86. package/lib/math/repeg.js +17 -8
  87. package/lib/math/spotBalance.d.ts +22 -0
  88. package/lib/math/spotBalance.js +192 -0
  89. package/lib/math/spotMarket.d.ts +4 -0
  90. package/lib/math/spotMarket.js +8 -0
  91. package/lib/math/spotPosition.d.ts +6 -0
  92. package/lib/math/spotPosition.js +23 -0
  93. package/lib/math/trade.d.ts +10 -10
  94. package/lib/math/trade.js +27 -31
  95. package/lib/oracles/pythClient.js +1 -1
  96. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  97. package/lib/oracles/switchboardClient.js +1 -1
  98. package/lib/orderParams.d.ts +4 -4
  99. package/lib/orderParams.js +12 -4
  100. package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
  101. package/lib/serum/serumFulfillmentConfigMap.js +17 -0
  102. package/lib/serum/serumSubscriber.d.ts +27 -0
  103. package/lib/serum/serumSubscriber.js +56 -0
  104. package/lib/serum/types.d.ts +11 -0
  105. package/{src/oracles → lib/serum}/types.js +0 -0
  106. package/lib/tokenFaucet.d.ts +1 -0
  107. package/lib/tokenFaucet.js +23 -12
  108. package/lib/tx/retryTxSender.d.ts +1 -1
  109. package/lib/tx/retryTxSender.js +13 -4
  110. package/lib/tx/types.d.ts +1 -1
  111. package/lib/types.d.ts +631 -222
  112. package/lib/types.js +137 -24
  113. package/lib/user.d.ts +228 -0
  114. package/lib/user.js +959 -0
  115. package/lib/userConfig.d.ts +14 -0
  116. package/lib/{clearingHouseUserConfig.js → userConfig.js} +0 -0
  117. package/lib/userMap/userMap.d.ts +41 -0
  118. package/lib/userMap/userMap.js +85 -0
  119. package/lib/userMap/userStatsMap.d.ts +19 -0
  120. package/lib/userMap/userStatsMap.js +68 -0
  121. package/lib/userName.d.ts +1 -0
  122. package/lib/userName.js +3 -2
  123. package/lib/userStats.d.ts +18 -0
  124. package/lib/userStats.js +49 -0
  125. package/lib/userStatsConfig.d.ts +14 -0
  126. package/{src/clearingHouseConfig.js → lib/userStatsConfig.js} +0 -0
  127. package/lib/util/getTokenAddress.d.ts +2 -0
  128. package/lib/util/getTokenAddress.js +9 -0
  129. package/package.json +12 -5
  130. package/src/accounts/bulkAccountLoader.ts +44 -34
  131. package/src/accounts/bulkUserStatsSubscription.ts +33 -0
  132. package/src/accounts/bulkUserSubscription.ts +2 -3
  133. package/src/accounts/fetch.ts +27 -2
  134. package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +65 -75
  135. package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
  136. package/src/accounts/pollingUserAccountSubscriber.ts +5 -12
  137. package/src/accounts/pollingUserStatsAccountSubscriber.ts +166 -0
  138. package/src/accounts/types.ts +35 -15
  139. package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +78 -73
  140. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  141. package/src/addresses/marketAddresses.ts +3 -4
  142. package/src/addresses/pda.ts +105 -33
  143. package/src/adminClient.ts +1207 -0
  144. package/src/config.ts +41 -34
  145. package/src/constants/numericConstants.ts +59 -26
  146. package/src/constants/{markets.ts → perpMarkets.ts} +22 -11
  147. package/src/constants/spotMarkets.ts +83 -0
  148. package/src/dlob/DLOB.ts +1120 -0
  149. package/src/dlob/DLOBNode.ts +155 -0
  150. package/src/dlob/NodeList.ts +195 -0
  151. package/src/driftClient.ts +3594 -0
  152. package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +9 -8
  153. package/src/events/eventList.ts +1 -8
  154. package/src/events/eventSubscriber.ts +36 -14
  155. package/src/events/fetchLogs.ts +60 -15
  156. package/src/events/pollingLogProvider.ts +11 -3
  157. package/src/events/sort.ts +11 -15
  158. package/src/events/txEventCache.ts +0 -16
  159. package/src/events/types.ts +27 -2
  160. package/src/events/webSocketLogProvider.ts +1 -1
  161. package/src/examples/makeTradeExample.js +152 -75
  162. package/src/examples/makeTradeExample.ts +44 -28
  163. package/src/factory/bigNum.ts +150 -22
  164. package/src/idl/drift.json +8392 -0
  165. package/src/idl/pyth.json +98 -2
  166. package/src/index.ts +29 -12
  167. package/src/math/amm.ts +161 -48
  168. package/src/math/conversion.ts +2 -2
  169. package/src/math/exchangeStatus.ts +31 -0
  170. package/src/math/funding.ts +41 -31
  171. package/src/math/insurance.ts +35 -0
  172. package/src/math/margin.ts +133 -0
  173. package/src/math/market.ts +143 -14
  174. package/src/math/oracles.ts +63 -9
  175. package/src/math/orders.ts +168 -26
  176. package/src/math/position.ts +133 -59
  177. package/src/math/repeg.ts +19 -9
  178. package/src/math/spotBalance.ts +319 -0
  179. package/src/math/spotMarket.ts +9 -0
  180. package/src/math/spotPosition.ts +47 -0
  181. package/src/math/trade.ts +33 -37
  182. package/src/oracles/pythClient.ts +2 -2
  183. package/src/oracles/quoteAssetOracleClient.ts +2 -2
  184. package/src/oracles/switchboardClient.ts +2 -2
  185. package/src/orderParams.ts +16 -8
  186. package/src/serum/serumFulfillmentConfigMap.ts +26 -0
  187. package/src/serum/serumSubscriber.ts +99 -0
  188. package/src/serum/types.ts +13 -0
  189. package/src/tokenFaucet.ts +38 -15
  190. package/src/tx/retryTxSender.ts +16 -5
  191. package/src/tx/types.ts +2 -1
  192. package/src/types.ts +594 -195
  193. package/src/user.ts +1599 -0
  194. package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
  195. package/src/userMap/userMap.ts +124 -0
  196. package/src/userMap/userStatsMap.ts +108 -0
  197. package/src/userName.ts +2 -1
  198. package/src/userStats.ts +75 -0
  199. package/src/userStatsConfig.ts +18 -0
  200. package/src/util/getTokenAddress.ts +18 -0
  201. package/tests/bn/test.ts +46 -11
  202. package/tests/dlob/helpers.ts +619 -0
  203. package/tests/dlob/test.ts +4586 -0
  204. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +0 -49
  205. package/lib/admin.d.ts +0 -44
  206. package/lib/admin.js +0 -438
  207. package/lib/clearingHouse.d.ts +0 -146
  208. package/lib/clearingHouse.js +0 -1154
  209. package/lib/clearingHouseUser.d.ts +0 -187
  210. package/lib/clearingHouseUser.js +0 -634
  211. package/lib/clearingHouseUserConfig.d.ts +0 -14
  212. package/lib/constants/banks.d.ts +0 -16
  213. package/lib/constants/banks.js +0 -41
  214. package/lib/constants/markets.d.ts +0 -19
  215. package/lib/idl/clearing_house.json +0 -4464
  216. package/lib/math/bankBalance.d.ts +0 -9
  217. package/lib/math/bankBalance.js +0 -75
  218. package/lib/math/state.d.ts +0 -8
  219. package/lib/math/state.js +0 -15
  220. package/lib/orders.d.ts +0 -8
  221. package/lib/orders.js +0 -134
  222. package/src/accounts/bulkAccountLoader.js +0 -197
  223. package/src/accounts/bulkUserSubscription.js +0 -33
  224. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
  225. package/src/accounts/pollingOracleSubscriber.js +0 -93
  226. package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
  227. package/src/accounts/pollingUserAccountSubscriber.js +0 -132
  228. package/src/accounts/types.js +0 -10
  229. package/src/accounts/utils.js +0 -7
  230. package/src/accounts/webSocketAccountSubscriber.js +0 -93
  231. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
  232. package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
  233. package/src/addresses/marketAddresses.js +0 -26
  234. package/src/admin.js +0 -517
  235. package/src/admin.ts +0 -730
  236. package/src/clearingHouse.ts +0 -1828
  237. package/src/clearingHouseUser.ts +0 -978
  238. package/src/clearingHouseUserConfig.js +0 -2
  239. package/src/config.js +0 -67
  240. package/src/constants/banks.js +0 -42
  241. package/src/constants/banks.ts +0 -50
  242. package/src/constants/markets.js +0 -42
  243. package/src/constants/numericConstants.js +0 -41
  244. package/src/events/eventSubscriber.js +0 -139
  245. package/src/events/fetchLogs.js +0 -50
  246. package/src/events/pollingLogProvider.js +0 -64
  247. package/src/events/sort.js +0 -44
  248. package/src/events/txEventCache.js +0 -71
  249. package/src/events/types.js +0 -20
  250. package/src/events/webSocketLogProvider.js +0 -41
  251. package/src/factory/bigNum.js +0 -390
  252. package/src/factory/oracleClient.js +0 -20
  253. package/src/idl/clearing_house.json +0 -4464
  254. package/src/index.js +0 -69
  255. package/src/math/amm.js +0 -369
  256. package/src/math/auction.js +0 -42
  257. package/src/math/bankBalance.ts +0 -112
  258. package/src/math/conversion.js +0 -11
  259. package/src/math/funding.js +0 -248
  260. package/src/math/oracles.js +0 -26
  261. package/src/math/repeg.js +0 -128
  262. package/src/math/state.js +0 -15
  263. package/src/math/state.ts +0 -14
  264. package/src/math/trade.js +0 -253
  265. package/src/math/utils.js +0 -26
  266. package/src/math/utils.js.map +0 -1
  267. package/src/mockUSDCFaucet.js +0 -280
  268. package/src/oracles/oracleClientCache.js +0 -19
  269. package/src/oracles/pythClient.js +0 -46
  270. package/src/oracles/quoteAssetOracleClient.js +0 -32
  271. package/src/oracles/switchboardClient.js +0 -69
  272. package/src/orderParams.js +0 -20
  273. package/src/orders.ts +0 -245
  274. package/src/slot/SlotSubscriber.js +0 -39
  275. package/src/tokenFaucet.js +0 -189
  276. package/src/types.js +0 -125
  277. package/src/userName.js +0 -20
  278. package/src/wallet.js +0 -35
@@ -0,0 +1,1207 @@
1
+ import {
2
+ PublicKey,
3
+ SYSVAR_RENT_PUBKEY,
4
+ TransactionSignature,
5
+ } from '@solana/web3.js';
6
+ import {
7
+ FeeStructure,
8
+ OracleGuardRails,
9
+ OracleSource,
10
+ ExchangeStatus,
11
+ MarketStatus,
12
+ ContractTier,
13
+ AssetTier,
14
+ } from './types';
15
+ import { DEFAULT_MARKET_NAME, encodeName } from './userName';
16
+ import { BN } from '@project-serum/anchor';
17
+ import * as anchor from '@project-serum/anchor';
18
+ import {
19
+ getDriftStateAccountPublicKeyAndNonce,
20
+ getSpotMarketPublicKey,
21
+ getSpotMarketVaultPublicKey,
22
+ getPerpMarketPublicKey,
23
+ getInsuranceFundVaultPublicKey,
24
+ getSerumOpenOrdersPublicKey,
25
+ getSerumFulfillmentConfigPublicKey,
26
+ } from './addresses/pda';
27
+ import { squareRootBN } from './math/utils';
28
+ import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
29
+ import { DriftClient } from './driftClient';
30
+ import { PEG_PRECISION } from './constants/numericConstants';
31
+ import { calculateTargetPriceTrade } from './math/trade';
32
+ import { calculateAmmReservesAfterSwap, getSwapDirection } from './math/amm';
33
+
34
+ export class AdminClient extends DriftClient {
35
+ public async initialize(
36
+ usdcMint: PublicKey,
37
+ _adminControlsPrices: boolean
38
+ ): Promise<[TransactionSignature]> {
39
+ const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(
40
+ await this.getStatePublicKey()
41
+ );
42
+ if (stateAccountRPCResponse.value !== null) {
43
+ throw new Error('Clearing house already initialized');
44
+ }
45
+
46
+ const [driftStatePublicKey] = await getDriftStateAccountPublicKeyAndNonce(
47
+ this.program.programId
48
+ );
49
+
50
+ const initializeTx = await this.program.transaction.initialize({
51
+ accounts: {
52
+ admin: this.wallet.publicKey,
53
+ state: driftStatePublicKey,
54
+ quoteAssetMint: usdcMint,
55
+ rent: SYSVAR_RENT_PUBKEY,
56
+ driftSigner: this.getSignerPublicKey(),
57
+ systemProgram: anchor.web3.SystemProgram.programId,
58
+ tokenProgram: TOKEN_PROGRAM_ID,
59
+ },
60
+ });
61
+
62
+ const { txSig: initializeTxSig } = await this.txSender.send(
63
+ initializeTx,
64
+ [],
65
+ this.opts
66
+ );
67
+
68
+ return [initializeTxSig];
69
+ }
70
+
71
+ public async initializeSpotMarket(
72
+ mint: PublicKey,
73
+ optimalUtilization: number,
74
+ optimalRate: number,
75
+ maxRate: number,
76
+ oracle: PublicKey,
77
+ oracleSource: OracleSource,
78
+ initialAssetWeight: number,
79
+ maintenanceAssetWeight: number,
80
+ initialLiabilityWeight: number,
81
+ maintenanceLiabilityWeight: number,
82
+ imfFactor = 0,
83
+ liquidatorFee = 0,
84
+ activeStatus = true,
85
+ name = DEFAULT_MARKET_NAME
86
+ ): Promise<TransactionSignature> {
87
+ const spotMarketIndex = this.getStateAccount().numberOfSpotMarkets;
88
+ const spotMarket = await getSpotMarketPublicKey(
89
+ this.program.programId,
90
+ spotMarketIndex
91
+ );
92
+
93
+ const spotMarketVault = await getSpotMarketVaultPublicKey(
94
+ this.program.programId,
95
+ spotMarketIndex
96
+ );
97
+
98
+ const insuranceFundVault = await getInsuranceFundVaultPublicKey(
99
+ this.program.programId,
100
+ spotMarketIndex
101
+ );
102
+
103
+ const nameBuffer = encodeName(name);
104
+ const initializeTx = await this.program.transaction.initializeSpotMarket(
105
+ optimalUtilization,
106
+ optimalRate,
107
+ maxRate,
108
+ oracleSource,
109
+ initialAssetWeight,
110
+ maintenanceAssetWeight,
111
+ initialLiabilityWeight,
112
+ maintenanceLiabilityWeight,
113
+ imfFactor,
114
+ liquidatorFee,
115
+ activeStatus,
116
+ nameBuffer,
117
+ {
118
+ accounts: {
119
+ admin: this.wallet.publicKey,
120
+ state: await this.getStatePublicKey(),
121
+ spotMarket,
122
+ spotMarketVault,
123
+ insuranceFundVault,
124
+ driftSigner: this.getSignerPublicKey(),
125
+ spotMarketMint: mint,
126
+ oracle,
127
+ rent: SYSVAR_RENT_PUBKEY,
128
+ systemProgram: anchor.web3.SystemProgram.programId,
129
+ tokenProgram: TOKEN_PROGRAM_ID,
130
+ },
131
+ }
132
+ );
133
+
134
+ const { txSig } = await this.txSender.send(initializeTx, [], this.opts);
135
+
136
+ await this.accountSubscriber.addSpotMarket(spotMarketIndex);
137
+ await this.accountSubscriber.addOracle({
138
+ source: oracleSource,
139
+ publicKey: oracle,
140
+ });
141
+
142
+ return txSig;
143
+ }
144
+
145
+ public async initializeSerumFulfillmentConfig(
146
+ marketIndex: number,
147
+ serumMarket: PublicKey,
148
+ serumProgram: PublicKey
149
+ ): Promise<TransactionSignature> {
150
+ const serumOpenOrders = getSerumOpenOrdersPublicKey(
151
+ this.program.programId,
152
+ serumMarket
153
+ );
154
+
155
+ const serumFulfillmentConfig = getSerumFulfillmentConfigPublicKey(
156
+ this.program.programId,
157
+ serumMarket
158
+ );
159
+
160
+ return await this.program.rpc.initializeSerumFulfillmentConfig(
161
+ marketIndex,
162
+ {
163
+ accounts: {
164
+ admin: this.wallet.publicKey,
165
+ state: await this.getStatePublicKey(),
166
+ baseSpotMarket: this.getSpotMarketAccount(marketIndex).pubkey,
167
+ quoteSpotMarket: this.getQuoteSpotMarketAccount().pubkey,
168
+ driftSigner: this.getSignerPublicKey(),
169
+ serumProgram,
170
+ serumMarket,
171
+ serumOpenOrders,
172
+ rent: SYSVAR_RENT_PUBKEY,
173
+ systemProgram: anchor.web3.SystemProgram.programId,
174
+ serumFulfillmentConfig,
175
+ },
176
+ }
177
+ );
178
+ }
179
+
180
+ public async initializePerpMarket(
181
+ priceOracle: PublicKey,
182
+ baseAssetReserve: BN,
183
+ quoteAssetReserve: BN,
184
+ periodicity: BN,
185
+ pegMultiplier: BN = PEG_PRECISION,
186
+ oracleSource: OracleSource = OracleSource.PYTH,
187
+ marginRatioInitial = 2000,
188
+ marginRatioMaintenance = 500,
189
+ liquidatorFee = 0,
190
+ activeStatus = true,
191
+ name = DEFAULT_MARKET_NAME
192
+ ): Promise<TransactionSignature> {
193
+ const perpMarketPublicKey = await getPerpMarketPublicKey(
194
+ this.program.programId,
195
+ this.getStateAccount().numberOfMarkets
196
+ );
197
+
198
+ const nameBuffer = encodeName(name);
199
+ const initializeMarketTx =
200
+ await this.program.transaction.initializePerpMarket(
201
+ baseAssetReserve,
202
+ quoteAssetReserve,
203
+ periodicity,
204
+ pegMultiplier,
205
+ oracleSource,
206
+ marginRatioInitial,
207
+ marginRatioMaintenance,
208
+ liquidatorFee,
209
+ activeStatus,
210
+ nameBuffer,
211
+ {
212
+ accounts: {
213
+ state: await this.getStatePublicKey(),
214
+ admin: this.wallet.publicKey,
215
+ oracle: priceOracle,
216
+ perpMarket: perpMarketPublicKey,
217
+ rent: SYSVAR_RENT_PUBKEY,
218
+ systemProgram: anchor.web3.SystemProgram.programId,
219
+ },
220
+ }
221
+ );
222
+ const { txSig } = await this.txSender.send(
223
+ initializeMarketTx,
224
+ [],
225
+ this.opts
226
+ );
227
+
228
+ await this.accountSubscriber.addPerpMarket(
229
+ this.getStateAccount().numberOfMarkets
230
+ );
231
+ await this.accountSubscriber.addOracle({
232
+ source: oracleSource,
233
+ publicKey: priceOracle,
234
+ });
235
+
236
+ return txSig;
237
+ }
238
+
239
+ public async moveAmmPrice(
240
+ perpMarketIndex: number,
241
+ baseAssetReserve: BN,
242
+ quoteAssetReserve: BN,
243
+ sqrtK?: BN
244
+ ): Promise<TransactionSignature> {
245
+ const marketPublicKey = await getPerpMarketPublicKey(
246
+ this.program.programId,
247
+ perpMarketIndex
248
+ );
249
+
250
+ if (sqrtK == undefined) {
251
+ sqrtK = squareRootBN(baseAssetReserve.mul(quoteAssetReserve));
252
+ }
253
+
254
+ return await this.program.rpc.moveAmmPrice(
255
+ baseAssetReserve,
256
+ quoteAssetReserve,
257
+ sqrtK,
258
+ {
259
+ accounts: {
260
+ state: await this.getStatePublicKey(),
261
+ admin: this.wallet.publicKey,
262
+ perpMarket: marketPublicKey,
263
+ },
264
+ }
265
+ );
266
+ }
267
+
268
+ public async updateK(
269
+ perpMarketIndex: number,
270
+ sqrtK: BN
271
+ ): Promise<TransactionSignature> {
272
+ return await this.program.rpc.updateK(sqrtK, {
273
+ accounts: {
274
+ state: await this.getStatePublicKey(),
275
+ admin: this.wallet.publicKey,
276
+ perpMarket: await getPerpMarketPublicKey(
277
+ this.program.programId,
278
+ perpMarketIndex
279
+ ),
280
+ oracle: this.getPerpMarketAccount(perpMarketIndex).amm.oracle,
281
+ },
282
+ });
283
+ }
284
+
285
+ public async updatePerpMarketConcentrationScale(
286
+ perpMarketIndex: number,
287
+ concentrationScale: BN
288
+ ): Promise<TransactionSignature> {
289
+ return await this.program.rpc.updatePerpMarketConcentrationCoef(
290
+ concentrationScale,
291
+ {
292
+ accounts: {
293
+ state: await this.getStatePublicKey(),
294
+ admin: this.wallet.publicKey,
295
+ perpMarket: await getPerpMarketPublicKey(
296
+ this.program.programId,
297
+ perpMarketIndex
298
+ ),
299
+ },
300
+ }
301
+ );
302
+ }
303
+
304
+ public async moveAmmToPrice(
305
+ perpMarketIndex: number,
306
+ targetPrice: BN
307
+ ): Promise<TransactionSignature> {
308
+ const perpMarket = this.getPerpMarketAccount(perpMarketIndex);
309
+
310
+ const [direction, tradeSize, _] = calculateTargetPriceTrade(
311
+ perpMarket,
312
+ targetPrice,
313
+ new BN(1000),
314
+ 'quote',
315
+ undefined //todo
316
+ );
317
+
318
+ const [newQuoteAssetAmount, newBaseAssetAmount] =
319
+ calculateAmmReservesAfterSwap(
320
+ perpMarket.amm,
321
+ 'quote',
322
+ tradeSize,
323
+ getSwapDirection('quote', direction)
324
+ );
325
+
326
+ const perpMarketPublicKey = await getPerpMarketPublicKey(
327
+ this.program.programId,
328
+ perpMarketIndex
329
+ );
330
+
331
+ return await this.program.rpc.moveAmmPrice(
332
+ newBaseAssetAmount,
333
+ newQuoteAssetAmount,
334
+ perpMarket.amm.sqrtK,
335
+ {
336
+ accounts: {
337
+ state: await this.getStatePublicKey(),
338
+ admin: this.wallet.publicKey,
339
+ perpMarket: perpMarketPublicKey,
340
+ },
341
+ }
342
+ );
343
+ }
344
+
345
+ public async repegAmmCurve(
346
+ newPeg: BN,
347
+ perpMarketIndex: number
348
+ ): Promise<TransactionSignature> {
349
+ const perpMarketPublicKey = await getPerpMarketPublicKey(
350
+ this.program.programId,
351
+ perpMarketIndex
352
+ );
353
+ const ammData = this.getPerpMarketAccount(perpMarketIndex).amm;
354
+
355
+ return await this.program.rpc.repegAmmCurve(newPeg, {
356
+ accounts: {
357
+ state: await this.getStatePublicKey(),
358
+ admin: this.wallet.publicKey,
359
+ oracle: ammData.oracle,
360
+ perpMarket: perpMarketPublicKey,
361
+ },
362
+ });
363
+ }
364
+
365
+ public async updatePerpMarketAmmOracleTwap(
366
+ perpMarketIndex: number
367
+ ): Promise<TransactionSignature> {
368
+ const ammData = this.getPerpMarketAccount(perpMarketIndex).amm;
369
+ const perpMarketPublicKey = await getPerpMarketPublicKey(
370
+ this.program.programId,
371
+ perpMarketIndex
372
+ );
373
+
374
+ return await this.program.rpc.updatePerpMarketAmmOracleTwap({
375
+ accounts: {
376
+ state: await this.getStatePublicKey(),
377
+ admin: this.wallet.publicKey,
378
+ oracle: ammData.oracle,
379
+ perpMarket: perpMarketPublicKey,
380
+ },
381
+ });
382
+ }
383
+
384
+ public async resetPerpMarketAmmOracleTwap(
385
+ perpMarketIndex: number
386
+ ): Promise<TransactionSignature> {
387
+ const ammData = this.getPerpMarketAccount(perpMarketIndex).amm;
388
+ const perpMarketPublicKey = await getPerpMarketPublicKey(
389
+ this.program.programId,
390
+ perpMarketIndex
391
+ );
392
+
393
+ return await this.program.rpc.resetPerpMarketAmmOracleTwap({
394
+ accounts: {
395
+ state: await this.getStatePublicKey(),
396
+ admin: this.wallet.publicKey,
397
+ oracle: ammData.oracle,
398
+ perpMarket: perpMarketPublicKey,
399
+ },
400
+ });
401
+ }
402
+
403
+ public async depositIntoPerpMarketFeePool(
404
+ perpMarketIndex: number,
405
+ amount: BN,
406
+ sourceVault: PublicKey
407
+ ): Promise<TransactionSignature> {
408
+ const spotMarket = this.getQuoteSpotMarketAccount();
409
+
410
+ return await this.program.rpc.depositIntoPerpMarketFeePool(amount, {
411
+ accounts: {
412
+ admin: this.wallet.publicKey,
413
+ state: await this.getStatePublicKey(),
414
+ perpMarket: await getPerpMarketPublicKey(
415
+ this.program.programId,
416
+ perpMarketIndex
417
+ ),
418
+ sourceVault,
419
+ driftSigner: this.getSignerPublicKey(),
420
+ quoteSpotMarket: spotMarket.pubkey,
421
+ spotMarketVault: spotMarket.vault,
422
+ tokenProgram: TOKEN_PROGRAM_ID,
423
+ },
424
+ });
425
+ }
426
+
427
+ public async updateAdmin(admin: PublicKey): Promise<TransactionSignature> {
428
+ return await this.program.rpc.updateAdmin(admin, {
429
+ accounts: {
430
+ admin: this.wallet.publicKey,
431
+ state: await this.getStatePublicKey(),
432
+ },
433
+ });
434
+ }
435
+
436
+ public async updatePerpMarketCurveUpdateIntensity(
437
+ perpMarketIndex: number,
438
+ curveUpdateIntensity: number
439
+ ): Promise<TransactionSignature> {
440
+ // assert(curveUpdateIntensity >= 0 && curveUpdateIntensity <= 100);
441
+ // assert(Number.isInteger(curveUpdateIntensity));
442
+
443
+ return await this.program.rpc.updatePerpMarketCurveUpdateIntensity(
444
+ curveUpdateIntensity,
445
+ {
446
+ accounts: {
447
+ admin: this.wallet.publicKey,
448
+ state: await this.getStatePublicKey(),
449
+ perpMarket: await getPerpMarketPublicKey(
450
+ this.program.programId,
451
+ perpMarketIndex
452
+ ),
453
+ },
454
+ }
455
+ );
456
+ }
457
+
458
+ public async updatePerpMarketMarginRatio(
459
+ perpMarketIndex: number,
460
+ marginRatioInitial: number,
461
+ marginRatioMaintenance: number
462
+ ): Promise<TransactionSignature> {
463
+ return await this.program.rpc.updatePerpMarketMarginRatio(
464
+ marginRatioInitial,
465
+ marginRatioMaintenance,
466
+ {
467
+ accounts: {
468
+ admin: this.wallet.publicKey,
469
+ state: await this.getStatePublicKey(),
470
+ perpMarket: await getPerpMarketPublicKey(
471
+ this.program.programId,
472
+ perpMarketIndex
473
+ ),
474
+ },
475
+ }
476
+ );
477
+ }
478
+
479
+ public async updatePerpMarketImfFactor(
480
+ perpMarketIndex: number,
481
+ imfFactor: number,
482
+ unrealizedPnlImfFactor: number
483
+ ): Promise<TransactionSignature> {
484
+ return await this.program.rpc.updatePerpMarketImfFactor(
485
+ imfFactor,
486
+ unrealizedPnlImfFactor,
487
+ {
488
+ accounts: {
489
+ admin: this.wallet.publicKey,
490
+ state: await this.getStatePublicKey(),
491
+ perpMarket: await getPerpMarketPublicKey(
492
+ this.program.programId,
493
+ perpMarketIndex
494
+ ),
495
+ },
496
+ }
497
+ );
498
+ }
499
+
500
+ public async updatePerpMarketBaseSpread(
501
+ perpMarketIndex: number,
502
+ baseSpread: number
503
+ ): Promise<TransactionSignature> {
504
+ return await this.program.rpc.updatePerpMarketBaseSpread(baseSpread, {
505
+ accounts: {
506
+ admin: this.wallet.publicKey,
507
+ state: await this.getStatePublicKey(),
508
+ perpMarket: await getPerpMarketPublicKey(
509
+ this.program.programId,
510
+ perpMarketIndex
511
+ ),
512
+ },
513
+ });
514
+ }
515
+
516
+ public async updateAmmJitIntensity(
517
+ perpMarketIndex: number,
518
+ ammJitIntensity: number
519
+ ): Promise<TransactionSignature> {
520
+ return await this.program.rpc.updateAmmJitIntensity(ammJitIntensity, {
521
+ accounts: {
522
+ admin: this.wallet.publicKey,
523
+ state: await this.getStatePublicKey(),
524
+ perpMarket: await getPerpMarketPublicKey(
525
+ this.program.programId,
526
+ perpMarketIndex
527
+ ),
528
+ },
529
+ });
530
+ }
531
+
532
+ public async updatePerpMarketName(
533
+ perpMarketIndex: number,
534
+ name: string
535
+ ): Promise<TransactionSignature> {
536
+ const nameBuffer = encodeName(name);
537
+
538
+ return await this.program.rpc.updatePerpMarketName(nameBuffer, {
539
+ accounts: {
540
+ admin: this.wallet.publicKey,
541
+ state: await this.getStatePublicKey(),
542
+ perpMarket: await getPerpMarketPublicKey(
543
+ this.program.programId,
544
+ perpMarketIndex
545
+ ),
546
+ },
547
+ });
548
+ }
549
+
550
+ public async updateSpotMarketName(
551
+ spotMarketIndex: number,
552
+ name: string
553
+ ): Promise<TransactionSignature> {
554
+ const nameBuffer = encodeName(name);
555
+
556
+ return await this.program.rpc.updateSpotMarketName(nameBuffer, {
557
+ accounts: {
558
+ admin: this.wallet.publicKey,
559
+ state: await this.getStatePublicKey(),
560
+ spotMarket: await getSpotMarketPublicKey(
561
+ this.program.programId,
562
+ spotMarketIndex
563
+ ),
564
+ },
565
+ });
566
+ }
567
+
568
+ public async updatePerpMarketMaxSpread(
569
+ perpMarketIndex: number,
570
+ maxSpread: number
571
+ ): Promise<TransactionSignature> {
572
+ const perpMarketPublicKey = await getPerpMarketPublicKey(
573
+ this.program.programId,
574
+ perpMarketIndex
575
+ );
576
+
577
+ return await this.program.rpc.updatePerpMarketMaxSpread(maxSpread, {
578
+ accounts: {
579
+ admin: this.wallet.publicKey,
580
+ state: await this.getStatePublicKey(),
581
+ perpMarket: perpMarketPublicKey,
582
+ },
583
+ });
584
+ }
585
+
586
+ public async updatePerpFeeStructure(
587
+ feeStructure: FeeStructure
588
+ ): Promise<TransactionSignature> {
589
+ return await this.program.rpc.updatePerpFeeStructure(feeStructure, {
590
+ accounts: {
591
+ admin: this.wallet.publicKey,
592
+ state: await this.getStatePublicKey(),
593
+ },
594
+ });
595
+ }
596
+
597
+ public async updateSpotFeeStructure(
598
+ feeStructure: FeeStructure
599
+ ): Promise<TransactionSignature> {
600
+ return await this.program.rpc.updateSpotFeeStructure(feeStructure, {
601
+ accounts: {
602
+ admin: this.wallet.publicKey,
603
+ state: await this.getStatePublicKey(),
604
+ },
605
+ });
606
+ }
607
+
608
+ public async updateOracleGuardRails(
609
+ oracleGuardRails: OracleGuardRails
610
+ ): Promise<TransactionSignature> {
611
+ return await this.program.rpc.updateOracleGuardRails(oracleGuardRails, {
612
+ accounts: {
613
+ admin: this.wallet.publicKey,
614
+ state: await this.getStatePublicKey(),
615
+ },
616
+ });
617
+ }
618
+
619
+ public async updateStateSettlementDuration(
620
+ settlementDuration: number
621
+ ): Promise<TransactionSignature> {
622
+ return await this.program.rpc.updateStateSettlementDuration(
623
+ settlementDuration,
624
+ {
625
+ accounts: {
626
+ admin: this.wallet.publicKey,
627
+ state: await this.getStatePublicKey(),
628
+ },
629
+ }
630
+ );
631
+ }
632
+
633
+ public async updateWithdrawGuardThreshold(
634
+ spotMarketIndex: number,
635
+ withdrawGuardThreshold: BN
636
+ ): Promise<TransactionSignature> {
637
+ return await this.program.rpc.updateWithdrawGuardThreshold(
638
+ withdrawGuardThreshold,
639
+ {
640
+ accounts: {
641
+ admin: this.wallet.publicKey,
642
+ state: await this.getStatePublicKey(),
643
+ spotMarket: await getSpotMarketPublicKey(
644
+ this.program.programId,
645
+ spotMarketIndex
646
+ ),
647
+ },
648
+ }
649
+ );
650
+ }
651
+
652
+ public async updateSpotMarketIfFactor(
653
+ spotMarketIndex: number,
654
+ userIfFactor: BN,
655
+ totalIfFactor: BN
656
+ ): Promise<TransactionSignature> {
657
+ return await this.program.rpc.updateSpotMarketIfFactor(
658
+ spotMarketIndex,
659
+ userIfFactor,
660
+ totalIfFactor,
661
+ {
662
+ accounts: {
663
+ admin: this.wallet.publicKey,
664
+ state: await this.getStatePublicKey(),
665
+ spotMarket: await getSpotMarketPublicKey(
666
+ this.program.programId,
667
+ spotMarketIndex
668
+ ),
669
+ },
670
+ }
671
+ );
672
+ }
673
+
674
+ public async updateSpotMarketRevenueSettlePeriod(
675
+ spotMarketIndex: number,
676
+ revenueSettlePeriod: BN
677
+ ): Promise<TransactionSignature> {
678
+ return await this.program.rpc.updateSpotMarketRevenueSettlePeriod(
679
+ revenueSettlePeriod,
680
+ {
681
+ accounts: {
682
+ admin: this.wallet.publicKey,
683
+ state: await this.getStatePublicKey(),
684
+ spotMarket: await getSpotMarketPublicKey(
685
+ this.program.programId,
686
+ spotMarketIndex
687
+ ),
688
+ },
689
+ }
690
+ );
691
+ }
692
+
693
+ public async updateSpotMarketMaxTokenDeposits(
694
+ spotMarketIndex: number,
695
+ maxTokenDeposits: BN
696
+ ): Promise<TransactionSignature> {
697
+ return await this.program.rpc.updateSpotMarketMaxTokenDeposits(
698
+ maxTokenDeposits,
699
+ {
700
+ accounts: {
701
+ admin: this.wallet.publicKey,
702
+ state: await this.getStatePublicKey(),
703
+ spotMarket: await getSpotMarketPublicKey(
704
+ this.program.programId,
705
+ spotMarketIndex
706
+ ),
707
+ },
708
+ }
709
+ );
710
+ }
711
+
712
+ public async updateInsuranceFundUnstakingPeriod(
713
+ spotMarketIndex: number,
714
+ insuranceWithdrawEscrowPeriod: BN
715
+ ): Promise<TransactionSignature> {
716
+ return await this.program.rpc.updateInsuranceFundUnstakingPeriod(
717
+ insuranceWithdrawEscrowPeriod,
718
+ {
719
+ accounts: {
720
+ admin: this.wallet.publicKey,
721
+ state: await this.getStatePublicKey(),
722
+ spotMarket: await getSpotMarketPublicKey(
723
+ this.program.programId,
724
+ spotMarketIndex
725
+ ),
726
+ },
727
+ }
728
+ );
729
+ }
730
+
731
+ public async updateLpCooldownTime(
732
+ cooldownTime: BN
733
+ ): Promise<TransactionSignature> {
734
+ return await this.program.rpc.updateLpCooldownTime(cooldownTime, {
735
+ accounts: {
736
+ admin: this.wallet.publicKey,
737
+ state: await this.getStatePublicKey(),
738
+ },
739
+ });
740
+ }
741
+
742
+ public async updatePerpMarketOracle(
743
+ perpMarketIndex: number,
744
+ oracle: PublicKey,
745
+ oracleSource: OracleSource
746
+ ): Promise<TransactionSignature> {
747
+ return await this.program.rpc.updatePerpMarketOracle(oracle, oracleSource, {
748
+ accounts: {
749
+ admin: this.wallet.publicKey,
750
+ state: await this.getStatePublicKey(),
751
+ perpMarket: await getPerpMarketPublicKey(
752
+ this.program.programId,
753
+ perpMarketIndex
754
+ ),
755
+ oracle: oracle,
756
+ },
757
+ });
758
+ }
759
+
760
+ public async updatePerpMarketStepSizeAndTickSize(
761
+ perpMarketIndex: number,
762
+ stepSize: BN,
763
+ tickSize: BN
764
+ ): Promise<TransactionSignature> {
765
+ return await this.program.rpc.updatePerpMarketStepSizeAndTickSize(
766
+ stepSize,
767
+ tickSize,
768
+ {
769
+ accounts: {
770
+ admin: this.wallet.publicKey,
771
+ state: await this.getStatePublicKey(),
772
+ perpMarket: await getPerpMarketPublicKey(
773
+ this.program.programId,
774
+ perpMarketIndex
775
+ ),
776
+ },
777
+ }
778
+ );
779
+ }
780
+
781
+ public async updatePerpMarketMinOrderSize(
782
+ perpMarketIndex: number,
783
+ orderSize: BN
784
+ ): Promise<TransactionSignature> {
785
+ return await this.program.rpc.updatePerpMarketMinOrderSize(orderSize, {
786
+ accounts: {
787
+ admin: this.wallet.publicKey,
788
+ state: await this.getStatePublicKey(),
789
+ perpMarket: await getPerpMarketPublicKey(
790
+ this.program.programId,
791
+ perpMarketIndex
792
+ ),
793
+ },
794
+ });
795
+ }
796
+
797
+ public async updateSpotMarketStepSizeAndTickSize(
798
+ spotMarketIndex: number,
799
+ stepSize: BN,
800
+ tickSize: BN
801
+ ): Promise<TransactionSignature> {
802
+ return await this.program.rpc.updateSpotMarketStepSizeAndTickSize(
803
+ stepSize,
804
+ tickSize,
805
+ {
806
+ accounts: {
807
+ admin: this.wallet.publicKey,
808
+ state: await this.getStatePublicKey(),
809
+ spotMarket: await getSpotMarketPublicKey(
810
+ this.program.programId,
811
+ spotMarketIndex
812
+ ),
813
+ },
814
+ }
815
+ );
816
+ }
817
+
818
+ public async updateSpotMarketMinOrderSize(
819
+ spotMarketIndex: number,
820
+ orderSize: BN
821
+ ): Promise<TransactionSignature> {
822
+ return await this.program.rpc.updateSpotMarketMinOrderSize(orderSize, {
823
+ accounts: {
824
+ admin: this.wallet.publicKey,
825
+ state: await this.getStatePublicKey(),
826
+ spotMarket: await getSpotMarketPublicKey(
827
+ this.program.programId,
828
+ spotMarketIndex
829
+ ),
830
+ },
831
+ });
832
+ }
833
+
834
+ public async updatePerpMarketExpiry(
835
+ perpMarketIndex: number,
836
+ expiryTs: BN
837
+ ): Promise<TransactionSignature> {
838
+ return await this.program.rpc.updatePerpMarketExpiry(expiryTs, {
839
+ accounts: {
840
+ admin: this.wallet.publicKey,
841
+ state: await this.getStatePublicKey(),
842
+ perpMarket: await getPerpMarketPublicKey(
843
+ this.program.programId,
844
+ perpMarketIndex
845
+ ),
846
+ },
847
+ });
848
+ }
849
+
850
+ public async updateSpotMarketOracle(
851
+ spotMarketIndex: number,
852
+ oracle: PublicKey,
853
+ oracleSource: OracleSource
854
+ ): Promise<TransactionSignature> {
855
+ return await this.program.rpc.updatePerpMarketOracle(oracle, oracleSource, {
856
+ accounts: {
857
+ admin: this.wallet.publicKey,
858
+ state: await this.getStatePublicKey(),
859
+ spotMarket: await getSpotMarketPublicKey(
860
+ this.program.programId,
861
+ spotMarketIndex
862
+ ),
863
+ oracle: oracle,
864
+ },
865
+ });
866
+ }
867
+
868
+ public async updateSpotMarketExpiry(
869
+ spotMarketIndex: number,
870
+ expiryTs: BN
871
+ ): Promise<TransactionSignature> {
872
+ return await this.program.rpc.updateSpotMarketExpiry(expiryTs, {
873
+ accounts: {
874
+ admin: this.wallet.publicKey,
875
+ state: await this.getStatePublicKey(),
876
+ spotMarket: await getSpotMarketPublicKey(
877
+ this.program.programId,
878
+ spotMarketIndex
879
+ ),
880
+ },
881
+ });
882
+ }
883
+
884
+ public async updateWhitelistMint(
885
+ whitelistMint?: PublicKey
886
+ ): Promise<TransactionSignature> {
887
+ return await this.program.rpc.updateWhitelistMint(whitelistMint, {
888
+ accounts: {
889
+ admin: this.wallet.publicKey,
890
+ state: await this.getStatePublicKey(),
891
+ },
892
+ });
893
+ }
894
+
895
+ public async updateDiscountMint(
896
+ discountMint: PublicKey
897
+ ): Promise<TransactionSignature> {
898
+ return await this.program.rpc.updateDiscountMint(discountMint, {
899
+ accounts: {
900
+ admin: this.wallet.publicKey,
901
+ state: await this.getStatePublicKey(),
902
+ },
903
+ });
904
+ }
905
+
906
+ public async updateSpotMarketMarginWeights(
907
+ spotMarketIndex: number,
908
+ initialAssetWeight: number,
909
+ maintenanceAssetWeight: number,
910
+ initialLiabilityWeight: number,
911
+ maintenanceLiabilityWeight: number,
912
+ imfFactor = 0
913
+ ): Promise<TransactionSignature> {
914
+ return await this.program.rpc.updateSpotMarketMarginWeights(
915
+ initialAssetWeight,
916
+ maintenanceAssetWeight,
917
+ initialLiabilityWeight,
918
+ maintenanceLiabilityWeight,
919
+ imfFactor,
920
+ {
921
+ accounts: {
922
+ admin: this.wallet.publicKey,
923
+ state: await this.getStatePublicKey(),
924
+ spotMarket: await getSpotMarketPublicKey(
925
+ this.program.programId,
926
+ spotMarketIndex
927
+ ),
928
+ },
929
+ }
930
+ );
931
+ }
932
+
933
+ public async updateSpotMarketBorrowRate(
934
+ spotMarketIndex: number,
935
+ optimalUtilization: number,
936
+ optimalBorrowRate: number,
937
+ optimalMaxRate: number
938
+ ): Promise<TransactionSignature> {
939
+ return await this.program.rpc.updateSpotMarketBorrowRate(
940
+ optimalUtilization,
941
+ optimalBorrowRate,
942
+ optimalMaxRate,
943
+ {
944
+ accounts: {
945
+ admin: this.wallet.publicKey,
946
+ state: await this.getStatePublicKey(),
947
+ spotMarket: await getSpotMarketPublicKey(
948
+ this.program.programId,
949
+ spotMarketIndex
950
+ ),
951
+ },
952
+ }
953
+ );
954
+ }
955
+
956
+ public async updateSpotMarketAssetTier(
957
+ spotMarketIndex: number,
958
+ assetTier: AssetTier
959
+ ): Promise<TransactionSignature> {
960
+ return await this.program.rpc.updateSpotMarketAssetTier(assetTier, {
961
+ accounts: {
962
+ admin: this.wallet.publicKey,
963
+ state: await this.getStatePublicKey(),
964
+ spotMarket: await getSpotMarketPublicKey(
965
+ this.program.programId,
966
+ spotMarketIndex
967
+ ),
968
+ },
969
+ });
970
+ }
971
+
972
+ public async updateSpotMarketStatus(
973
+ spotMarketIndex: number,
974
+ marketStatus: MarketStatus
975
+ ): Promise<TransactionSignature> {
976
+ return await this.program.rpc.updateSpotMarketStatus(marketStatus, {
977
+ accounts: {
978
+ admin: this.wallet.publicKey,
979
+ state: await this.getStatePublicKey(),
980
+ spotMarket: await getSpotMarketPublicKey(
981
+ this.program.programId,
982
+ spotMarketIndex
983
+ ),
984
+ },
985
+ });
986
+ }
987
+
988
+ public async updatePerpMarketStatus(
989
+ perpMarketIndex: number,
990
+ marketStatus: MarketStatus
991
+ ): Promise<TransactionSignature> {
992
+ return await this.program.rpc.updatePerpMarketStatus(marketStatus, {
993
+ accounts: {
994
+ admin: this.wallet.publicKey,
995
+ state: await this.getStatePublicKey(),
996
+ perpMarket: await getPerpMarketPublicKey(
997
+ this.program.programId,
998
+ perpMarketIndex
999
+ ),
1000
+ },
1001
+ });
1002
+ }
1003
+
1004
+ public async updatePerpMarketContractTier(
1005
+ perpMarketIndex: number,
1006
+ contractTier: ContractTier
1007
+ ): Promise<TransactionSignature> {
1008
+ return await this.program.rpc.updatePerpMarketContractTier(contractTier, {
1009
+ accounts: {
1010
+ admin: this.wallet.publicKey,
1011
+ state: await this.getStatePublicKey(),
1012
+ perpMarket: await getPerpMarketPublicKey(
1013
+ this.program.programId,
1014
+ perpMarketIndex
1015
+ ),
1016
+ },
1017
+ });
1018
+ }
1019
+
1020
+ public async updateExchangeStatus(
1021
+ exchangeStatus: ExchangeStatus
1022
+ ): Promise<TransactionSignature> {
1023
+ return await this.program.rpc.updateExchangeStatus(exchangeStatus, {
1024
+ accounts: {
1025
+ admin: this.wallet.publicKey,
1026
+ state: await this.getStatePublicKey(),
1027
+ },
1028
+ });
1029
+ }
1030
+
1031
+ public async updatePerpAuctionDuration(
1032
+ minDuration: BN | number
1033
+ ): Promise<TransactionSignature> {
1034
+ return await this.program.rpc.updatePerpAuctionDuration(
1035
+ typeof minDuration === 'number' ? minDuration : minDuration.toNumber(),
1036
+ {
1037
+ accounts: {
1038
+ admin: this.wallet.publicKey,
1039
+ state: await this.getStatePublicKey(),
1040
+ },
1041
+ }
1042
+ );
1043
+ }
1044
+
1045
+ public async updateSpotAuctionDuration(
1046
+ defaultAuctionDuration: number
1047
+ ): Promise<TransactionSignature> {
1048
+ return await this.program.rpc.updateSpotAuctionDuration(
1049
+ defaultAuctionDuration,
1050
+ {
1051
+ accounts: {
1052
+ admin: this.wallet.publicKey,
1053
+ state: await this.getStatePublicKey(),
1054
+ },
1055
+ }
1056
+ );
1057
+ }
1058
+
1059
+ public async updatePerpMarketMaxFillReserveFraction(
1060
+ perpMarketIndex: number,
1061
+ maxBaseAssetAmountRatio: number
1062
+ ): Promise<TransactionSignature> {
1063
+ return await this.program.rpc.updatePerpMarketMaxFillReserveFraction(
1064
+ maxBaseAssetAmountRatio,
1065
+ {
1066
+ accounts: {
1067
+ admin: this.wallet.publicKey,
1068
+ state: await this.getStatePublicKey(),
1069
+ perpMarket: await getPerpMarketPublicKey(
1070
+ this.program.programId,
1071
+ perpMarketIndex
1072
+ ),
1073
+ },
1074
+ }
1075
+ );
1076
+ }
1077
+
1078
+ public async updateMaxSlippageRatio(
1079
+ perpMarketIndex: number,
1080
+ maxSlippageRatio: number
1081
+ ): Promise<TransactionSignature> {
1082
+ return await this.program.rpc.updateMaxSlippageRatio(maxSlippageRatio, {
1083
+ accounts: {
1084
+ admin: this.wallet.publicKey,
1085
+ state: await this.getStatePublicKey(),
1086
+ perpMarket: this.getPerpMarketAccount(perpMarketIndex).pubkey,
1087
+ },
1088
+ });
1089
+ }
1090
+
1091
+ public async updatePerpMarketUnrealizedAssetWeight(
1092
+ perpMarketIndex: number,
1093
+ unrealizedInitialAssetWeight: number,
1094
+ unrealizedMaintenanceAssetWeight: number
1095
+ ): Promise<TransactionSignature> {
1096
+ return await this.program.rpc.updatePerpMarketUnrealizedAssetWeight(
1097
+ unrealizedInitialAssetWeight,
1098
+ unrealizedMaintenanceAssetWeight,
1099
+ {
1100
+ accounts: {
1101
+ admin: this.wallet.publicKey,
1102
+ state: await this.getStatePublicKey(),
1103
+ perpMarket: await getPerpMarketPublicKey(
1104
+ this.program.programId,
1105
+ perpMarketIndex
1106
+ ),
1107
+ },
1108
+ }
1109
+ );
1110
+ }
1111
+
1112
+ public async updatePerpMarketMaxImbalances(
1113
+ perpMarketIndex: number,
1114
+ unrealizedMaxImbalance: BN,
1115
+ maxRevenueWithdrawPerPeriod: BN,
1116
+ quoteMaxInsurance: BN
1117
+ ): Promise<TransactionSignature> {
1118
+ return await this.program.rpc.updatePerpMarketMaxImbalances(
1119
+ unrealizedMaxImbalance,
1120
+ maxRevenueWithdrawPerPeriod,
1121
+ quoteMaxInsurance,
1122
+ {
1123
+ accounts: {
1124
+ admin: this.wallet.publicKey,
1125
+ state: await this.getStatePublicKey(),
1126
+ perpMarket: await getPerpMarketPublicKey(
1127
+ this.program.programId,
1128
+ perpMarketIndex
1129
+ ),
1130
+ },
1131
+ }
1132
+ );
1133
+ }
1134
+
1135
+ public async updatePerpMarketMaxOpenInterest(
1136
+ perpMarketIndex: number,
1137
+ maxOpenInterest: BN
1138
+ ): Promise<TransactionSignature> {
1139
+ return await this.program.rpc.updatePerpMarketMaxOpenInterest(
1140
+ maxOpenInterest,
1141
+ {
1142
+ accounts: {
1143
+ admin: this.wallet.publicKey,
1144
+ state: await this.getStatePublicKey(),
1145
+ perpMarket: await getPerpMarketPublicKey(
1146
+ this.program.programId,
1147
+ perpMarketIndex
1148
+ ),
1149
+ },
1150
+ }
1151
+ );
1152
+ }
1153
+
1154
+ public async updateSerumVault(
1155
+ srmVault: PublicKey
1156
+ ): Promise<TransactionSignature> {
1157
+ return await this.program.rpc.updateSerumVault({
1158
+ accounts: {
1159
+ admin: this.wallet.publicKey,
1160
+ state: await this.getStatePublicKey(),
1161
+ srmVault: srmVault,
1162
+ },
1163
+ });
1164
+ }
1165
+
1166
+ public async updatePerpMarketLiquidationFee(
1167
+ perpMarketIndex: number,
1168
+ liquidatorFee: number,
1169
+ ifLiquidationFee: number
1170
+ ): Promise<TransactionSignature> {
1171
+ return await this.program.rpc.updatePerpMarketLiquidationFee(
1172
+ liquidatorFee,
1173
+ ifLiquidationFee,
1174
+ {
1175
+ accounts: {
1176
+ admin: this.wallet.publicKey,
1177
+ state: await this.getStatePublicKey(),
1178
+ perpMarket: await getPerpMarketPublicKey(
1179
+ this.program.programId,
1180
+ perpMarketIndex
1181
+ ),
1182
+ },
1183
+ }
1184
+ );
1185
+ }
1186
+
1187
+ public async updateSpotMarketLiquidationFee(
1188
+ spotMarketIndex: number,
1189
+ liquidatorFee: number,
1190
+ ifLiquidationFee: number
1191
+ ): Promise<TransactionSignature> {
1192
+ return await this.program.rpc.updateSpotMarketLiquidationFee(
1193
+ liquidatorFee,
1194
+ ifLiquidationFee,
1195
+ {
1196
+ accounts: {
1197
+ admin: this.wallet.publicKey,
1198
+ state: await this.getStatePublicKey(),
1199
+ spotMarket: await getSpotMarketPublicKey(
1200
+ this.program.programId,
1201
+ spotMarketIndex
1202
+ ),
1203
+ },
1204
+ }
1205
+ );
1206
+ }
1207
+ }