@drift-labs/sdk 0.1.36-master.7 → 0.2.0-master.10

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 (254) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +6 -6
  2. package/lib/accounts/bulkAccountLoader.js +81 -95
  3. package/lib/accounts/bulkUserSubscription.js +13 -57
  4. package/lib/accounts/fetch.d.ts +4 -0
  5. package/lib/accounts/fetch.js +18 -0
  6. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +34 -38
  7. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +224 -224
  8. package/lib/accounts/pollingOracleSubscriber.d.ts +3 -3
  9. package/lib/accounts/pollingOracleSubscriber.js +37 -49
  10. package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
  11. package/lib/accounts/pollingTokenAccountSubscriber.js +35 -50
  12. package/lib/accounts/pollingUserAccountSubscriber.d.ts +7 -13
  13. package/lib/accounts/pollingUserAccountSubscriber.js +71 -134
  14. package/lib/accounts/types.d.ts +34 -41
  15. package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -4
  16. package/lib/accounts/webSocketAccountSubscriber.js +39 -35
  17. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +33 -28
  18. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +153 -185
  19. package/lib/accounts/webSocketUserAccountSubscriber.d.ts +5 -11
  20. package/lib/accounts/webSocketUserAccountSubscriber.js +22 -67
  21. package/lib/addresses/marketAddresses.d.ts +4 -0
  22. package/lib/addresses/marketAddresses.js +15 -0
  23. package/lib/addresses/pda.d.ts +12 -0
  24. package/lib/addresses/pda.js +83 -0
  25. package/lib/admin.d.ts +12 -18
  26. package/lib/admin.js +398 -558
  27. package/lib/clearingHouse.d.ts +83 -109
  28. package/lib/clearingHouse.js +832 -893
  29. package/lib/clearingHouseConfig.d.ts +34 -0
  30. package/lib/clearingHouseConfig.js +2 -0
  31. package/lib/clearingHouseUser.d.ts +19 -22
  32. package/lib/clearingHouseUser.js +155 -141
  33. package/lib/clearingHouseUserConfig.d.ts +14 -0
  34. package/lib/clearingHouseUserConfig.js +2 -0
  35. package/lib/config.d.ts +12 -0
  36. package/lib/config.js +35 -4
  37. package/lib/constants/banks.d.ts +16 -0
  38. package/lib/constants/banks.js +41 -0
  39. package/lib/constants/markets.d.ts +8 -3
  40. package/lib/constants/markets.js +13 -206
  41. package/lib/constants/numericConstants.d.ts +17 -0
  42. package/lib/constants/numericConstants.js +23 -6
  43. package/lib/events/eventList.d.ts +22 -0
  44. package/lib/events/eventList.js +77 -0
  45. package/lib/events/eventSubscriber.d.ts +34 -0
  46. package/lib/events/eventSubscriber.js +126 -0
  47. package/lib/events/fetchLogs.d.ts +13 -0
  48. package/lib/events/fetchLogs.js +39 -0
  49. package/lib/events/pollingLogProvider.d.ts +15 -0
  50. package/lib/events/pollingLogProvider.js +53 -0
  51. package/lib/events/sort.d.ts +2 -0
  52. package/lib/events/sort.js +44 -0
  53. package/lib/events/txEventCache.d.ts +24 -0
  54. package/lib/events/txEventCache.js +71 -0
  55. package/lib/events/types.d.ts +49 -0
  56. package/lib/events/types.js +20 -0
  57. package/lib/events/webSocketLogProvider.d.ts +12 -0
  58. package/lib/events/webSocketLogProvider.js +30 -0
  59. package/lib/examples/makeTradeExample.js +26 -27
  60. package/lib/factory/bigNum.d.ts +118 -0
  61. package/lib/factory/bigNum.js +364 -0
  62. package/lib/factory/oracleClient.d.ts +1 -2
  63. package/lib/factory/oracleClient.js +6 -2
  64. package/lib/idl/clearing_house.json +2258 -2774
  65. package/lib/index.d.ts +14 -5
  66. package/lib/index.js +18 -5
  67. package/lib/math/amm.d.ts +24 -29
  68. package/lib/math/amm.js +209 -176
  69. package/lib/math/auction.d.ts +5 -0
  70. package/lib/math/auction.js +42 -0
  71. package/lib/math/bankBalance.d.ts +9 -0
  72. package/lib/math/bankBalance.js +75 -0
  73. package/lib/math/conversion.d.ts +0 -1
  74. package/lib/math/conversion.js +1 -5
  75. package/lib/math/funding.d.ts +6 -6
  76. package/lib/math/funding.js +158 -175
  77. package/lib/math/market.d.ts +6 -6
  78. package/lib/math/market.js +10 -9
  79. package/lib/math/orders.d.ts +6 -1
  80. package/lib/math/orders.js +38 -1
  81. package/lib/math/position.d.ts +7 -5
  82. package/lib/math/position.js +29 -27
  83. package/lib/math/repeg.d.ts +22 -0
  84. package/lib/math/repeg.js +128 -0
  85. package/lib/math/trade.d.ts +5 -4
  86. package/lib/math/trade.js +29 -24
  87. package/lib/mockUSDCFaucet.js +87 -116
  88. package/lib/oracles/oracleClientCache.d.ts +8 -0
  89. package/lib/oracles/oracleClientCache.js +19 -0
  90. package/lib/oracles/pythClient.d.ts +3 -5
  91. package/lib/oracles/pythClient.js +12 -31
  92. package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
  93. package/lib/oracles/quoteAssetOracleClient.js +21 -0
  94. package/lib/oracles/switchboardClient.d.ts +3 -5
  95. package/lib/oracles/switchboardClient.js +29 -50
  96. package/lib/oracles/types.d.ts +6 -1
  97. package/lib/orderParams.d.ts +14 -5
  98. package/lib/orderParams.js +8 -96
  99. package/lib/orders.d.ts +6 -7
  100. package/lib/orders.js +11 -89
  101. package/lib/slot/SlotSubscriber.d.ts +19 -0
  102. package/lib/slot/SlotSubscriber.js +26 -0
  103. package/lib/tx/retryTxSender.d.ts +2 -2
  104. package/lib/tx/retryTxSender.js +108 -123
  105. package/lib/tx/types.d.ts +5 -1
  106. package/lib/tx/utils.d.ts +1 -1
  107. package/lib/tx/utils.js +11 -2
  108. package/lib/types.d.ts +180 -110
  109. package/lib/types.js +54 -1
  110. package/lib/userName.d.ts +4 -0
  111. package/lib/userName.js +20 -0
  112. package/lib/util/computeUnits.js +10 -21
  113. package/lib/util/tps.js +11 -22
  114. package/lib/wallet.js +7 -20
  115. package/package.json +11 -4
  116. package/src/accounts/bulkAccountLoader.js +197 -0
  117. package/src/accounts/bulkAccountLoader.ts +21 -15
  118. package/src/accounts/bulkUserSubscription.js +33 -0
  119. package/src/accounts/bulkUserSubscription.ts +1 -45
  120. package/src/accounts/fetch.js +29 -0
  121. package/src/accounts/fetch.ts +33 -0
  122. package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
  123. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
  124. package/src/accounts/pollingOracleSubscriber.js +93 -0
  125. package/src/accounts/pollingOracleSubscriber.ts +16 -8
  126. package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
  127. package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
  128. package/src/accounts/pollingUserAccountSubscriber.js +132 -0
  129. package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
  130. package/src/accounts/types.js +10 -0
  131. package/src/accounts/types.ts +41 -70
  132. package/src/accounts/utils.js +7 -0
  133. package/src/accounts/webSocketAccountSubscriber.js +93 -0
  134. package/src/accounts/webSocketAccountSubscriber.ts +33 -16
  135. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
  136. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
  137. package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
  138. package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
  139. package/src/addresses/marketAddresses.js +26 -0
  140. package/src/addresses/marketAddresses.ts +18 -0
  141. package/{lib/addresses.js → src/addresses/pda.js} +45 -34
  142. package/src/addresses/pda.ts +118 -0
  143. package/src/admin.ts +247 -336
  144. package/src/assert/assert.js +9 -0
  145. package/src/clearingHouse.ts +1023 -1026
  146. package/src/clearingHouseConfig.ts +37 -0
  147. package/src/clearingHouseUser.ts +275 -185
  148. package/src/clearingHouseUserConfig.ts +18 -0
  149. package/src/config.ts +54 -1
  150. package/src/constants/banks.ts +50 -0
  151. package/src/constants/markets.ts +16 -207
  152. package/src/constants/numericConstants.ts +34 -5
  153. package/src/events/eventList.js +77 -0
  154. package/src/events/eventList.ts +94 -0
  155. package/src/events/eventSubscriber.js +139 -0
  156. package/src/events/eventSubscriber.ts +194 -0
  157. package/src/events/fetchLogs.js +50 -0
  158. package/src/events/fetchLogs.ts +80 -0
  159. package/src/events/pollingLogProvider.js +64 -0
  160. package/src/events/pollingLogProvider.ts +79 -0
  161. package/src/events/sort.js +44 -0
  162. package/src/events/sort.ts +65 -0
  163. package/src/events/txEventCache.js +71 -0
  164. package/src/events/txEventCache.ts +74 -0
  165. package/src/events/types.js +20 -0
  166. package/src/events/types.ts +98 -0
  167. package/src/events/webSocketLogProvider.js +41 -0
  168. package/src/events/webSocketLogProvider.ts +38 -0
  169. package/src/examples/makeTradeExample.js +80 -0
  170. package/src/examples/makeTradeExample.ts +20 -11
  171. package/src/factory/bigNum.js +364 -0
  172. package/src/factory/bigNum.ts +524 -0
  173. package/src/factory/oracleClient.js +20 -0
  174. package/src/factory/oracleClient.ts +7 -4
  175. package/src/idl/clearing_house.json +2258 -2774
  176. package/src/index.js +69 -0
  177. package/src/index.ts +14 -5
  178. package/src/math/amm.js +369 -0
  179. package/src/math/amm.ts +382 -243
  180. package/src/math/auction.js +42 -0
  181. package/src/math/auction.ts +43 -0
  182. package/src/math/bankBalance.js +75 -0
  183. package/src/math/bankBalance.ts +112 -0
  184. package/src/math/conversion.js +11 -0
  185. package/src/math/conversion.ts +1 -11
  186. package/src/math/funding.js +248 -0
  187. package/src/math/funding.ts +12 -9
  188. package/src/math/market.js +57 -0
  189. package/src/math/market.ts +37 -30
  190. package/src/math/oracles.js +26 -0
  191. package/src/math/orders.js +110 -0
  192. package/src/math/orders.ts +43 -1
  193. package/src/math/position.js +140 -0
  194. package/src/math/position.ts +52 -35
  195. package/src/math/repeg.js +128 -0
  196. package/src/math/repeg.ts +176 -0
  197. package/src/math/state.js +15 -0
  198. package/src/math/trade.js +253 -0
  199. package/src/math/trade.ts +55 -47
  200. package/src/math/utils.js +26 -0
  201. package/src/math/utils.js.map +1 -0
  202. package/src/mockUSDCFaucet.js +171 -0
  203. package/src/oracles/oracleClientCache.js +19 -0
  204. package/src/oracles/oracleClientCache.ts +20 -0
  205. package/src/oracles/pythClient.js +46 -0
  206. package/src/oracles/pythClient.ts +4 -11
  207. package/src/oracles/quoteAssetOracleClient.js +32 -0
  208. package/src/oracles/quoteAssetOracleClient.ts +25 -0
  209. package/src/oracles/switchboardClient.js +69 -0
  210. package/src/oracles/switchboardClient.ts +11 -24
  211. package/src/oracles/types.js +2 -0
  212. package/src/oracles/types.ts +7 -1
  213. package/src/orderParams.js +20 -0
  214. package/src/orderParams.ts +20 -141
  215. package/src/orders.js +134 -0
  216. package/src/orders.ts +25 -134
  217. package/src/slot/SlotSubscriber.js +39 -0
  218. package/src/slot/SlotSubscriber.ts +42 -0
  219. package/src/token/index.js +38 -0
  220. package/src/tx/retryTxSender.js +188 -0
  221. package/src/tx/retryTxSender.ts +6 -4
  222. package/src/tx/types.js +2 -0
  223. package/src/tx/types.ts +6 -1
  224. package/src/tx/utils.js +17 -0
  225. package/src/tx/utils.ts +22 -3
  226. package/src/types.js +114 -0
  227. package/src/types.ts +176 -124
  228. package/src/userName.js +20 -0
  229. package/src/userName.ts +20 -0
  230. package/src/util/promiseTimeout.js +14 -0
  231. package/src/util/tps.js +27 -0
  232. package/src/wallet.js +35 -0
  233. package/tests/bn/test.ts +255 -0
  234. package/tsconfig.json +12 -12
  235. package/lib/addresses.d.ts +0 -10
  236. package/lib/constants/accounts.d.ts +0 -15
  237. package/lib/constants/accounts.js +0 -18
  238. package/lib/factory/clearingHouse.d.ts +0 -35
  239. package/lib/factory/clearingHouse.js +0 -81
  240. package/lib/factory/clearingHouseUser.d.ts +0 -19
  241. package/lib/factory/clearingHouseUser.js +0 -34
  242. package/lib/math/insuranceFund.d.ts +0 -15
  243. package/lib/math/insuranceFund.js +0 -33
  244. package/lib/settlement.d.ts +0 -4
  245. package/lib/settlement.js +0 -10
  246. package/lib/tx/defaultTxSender.d.ts +0 -8
  247. package/lib/tx/defaultTxSender.js +0 -12
  248. package/src/addresses.ts +0 -82
  249. package/src/constants/accounts.ts +0 -26
  250. package/src/factory/clearingHouse.ts +0 -173
  251. package/src/factory/clearingHouseUser.ts +0 -73
  252. package/src/math/insuranceFund.ts +0 -29
  253. package/src/settlement.ts +0 -9
  254. package/src/tx/defaultTxSender.ts +0 -24
package/lib/admin.js CHANGED
@@ -22,576 +22,416 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
25
  Object.defineProperty(exports, "__esModule", { value: true });
35
26
  exports.Admin = void 0;
36
27
  const web3_js_1 = require("@solana/web3.js");
37
28
  const types_1 = require("./types");
38
29
  const anchor_1 = require("@project-serum/anchor");
39
30
  const anchor = __importStar(require("@project-serum/anchor"));
40
- const addresses_1 = require("./addresses");
31
+ const pda_1 = require("./addresses/pda");
41
32
  const spl_token_1 = require("@solana/spl-token");
42
33
  const clearingHouse_1 = require("./clearingHouse");
43
34
  const numericConstants_1 = require("./constants/numericConstants");
44
35
  const trade_1 = require("./math/trade");
45
36
  const amm_1 = require("./math/amm");
46
- const clearingHouse_2 = require("./factory/clearingHouse");
47
37
  class Admin extends clearingHouse_1.ClearingHouse {
48
- static from(connection, wallet, clearingHouseProgramId, opts = anchor_1.AnchorProvider.defaultOptions()) {
49
- const config = (0, clearingHouse_2.getWebSocketClearingHouseConfig)(connection, wallet, clearingHouseProgramId, opts);
50
- return (0, clearingHouse_2.getAdmin)(config);
51
- }
52
- initialize(usdcMint, adminControlsPrices) {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- const stateAccountRPCResponse = yield this.connection.getParsedAccountInfo(yield this.getStatePublicKey());
55
- if (stateAccountRPCResponse.value !== null) {
56
- throw new Error('Clearing house already initialized');
57
- }
58
- const [collateralVaultPublicKey, collateralVaultNonce] = yield web3_js_1.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('collateral_vault'))], this.program.programId);
59
- const [collateralVaultAuthority, _collateralVaultAuthorityNonce] = yield web3_js_1.PublicKey.findProgramAddress([collateralVaultPublicKey.toBuffer()], this.program.programId);
60
- const [insuranceVaultPublicKey, insuranceVaultNonce] = yield web3_js_1.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('insurance_vault'))], this.program.programId);
61
- const [insuranceVaultAuthority, _insuranceVaultAuthorityNonce] = yield web3_js_1.PublicKey.findProgramAddress([insuranceVaultPublicKey.toBuffer()], this.program.programId);
62
- const markets = anchor.web3.Keypair.generate();
63
- const depositHistory = anchor.web3.Keypair.generate();
64
- const fundingRateHistory = anchor.web3.Keypair.generate();
65
- const fundingPaymentHistory = anchor.web3.Keypair.generate();
66
- const tradeHistory = anchor.web3.Keypair.generate();
67
- const liquidationHistory = anchor.web3.Keypair.generate();
68
- const curveHistory = anchor.web3.Keypair.generate();
69
- const [clearingHouseStatePublicKey, clearingHouseNonce] = yield (0, addresses_1.getClearingHouseStateAccountPublicKeyAndNonce)(this.program.programId);
70
- const initializeTx = yield this.program.transaction.initialize(clearingHouseNonce, collateralVaultNonce, insuranceVaultNonce, adminControlsPrices, {
71
- accounts: {
72
- admin: this.wallet.publicKey,
73
- state: clearingHouseStatePublicKey,
74
- collateralMint: usdcMint,
75
- collateralVault: collateralVaultPublicKey,
76
- collateralVaultAuthority: collateralVaultAuthority,
77
- insuranceVault: insuranceVaultPublicKey,
78
- insuranceVaultAuthority: insuranceVaultAuthority,
79
- markets: markets.publicKey,
80
- rent: web3_js_1.SYSVAR_RENT_PUBKEY,
81
- systemProgram: anchor.web3.SystemProgram.programId,
82
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
83
- },
84
- instructions: [
85
- yield this.program.account.markets.createInstruction(markets),
86
- ],
87
- });
88
- const initializeTxSig = yield this.txSender.send(initializeTx, [markets], this.opts);
89
- const initializeHistoryTx = yield this.program.transaction.initializeHistory({
90
- accounts: {
91
- admin: this.wallet.publicKey,
92
- state: clearingHouseStatePublicKey,
93
- depositHistory: depositHistory.publicKey,
94
- fundingRateHistory: fundingRateHistory.publicKey,
95
- fundingPaymentHistory: fundingPaymentHistory.publicKey,
96
- tradeHistory: tradeHistory.publicKey,
97
- liquidationHistory: liquidationHistory.publicKey,
98
- curveHistory: curveHistory.publicKey,
99
- rent: web3_js_1.SYSVAR_RENT_PUBKEY,
100
- systemProgram: anchor.web3.SystemProgram.programId,
101
- },
102
- instructions: [
103
- yield this.program.account.fundingRateHistory.createInstruction(fundingRateHistory),
104
- yield this.program.account.fundingPaymentHistory.createInstruction(fundingPaymentHistory),
105
- yield this.program.account.tradeHistory.createInstruction(tradeHistory),
106
- yield this.program.account.liquidationHistory.createInstruction(liquidationHistory),
107
- yield this.program.account.depositHistory.createInstruction(depositHistory),
108
- yield this.program.account.extendedCurveHistory.createInstruction(curveHistory),
109
- ],
110
- });
111
- const initializeHistoryTxSig = yield this.txSender.send(initializeHistoryTx, [
112
- depositHistory,
113
- fundingPaymentHistory,
114
- tradeHistory,
115
- liquidationHistory,
116
- fundingRateHistory,
117
- curveHistory,
118
- ], this.opts);
119
- const initializeOrderStateTxSig = yield this.initializeOrderState();
120
- return [initializeTxSig, initializeHistoryTxSig, initializeOrderStateTxSig];
121
- });
122
- }
123
- initializeOrderState() {
124
- return __awaiter(this, void 0, void 0, function* () {
125
- const orderHistory = anchor.web3.Keypair.generate();
126
- const [orderStatePublicKey, orderStateNonce] = yield (0, addresses_1.getOrderStateAccountPublicKeyAndNonce)(this.program.programId);
127
- const clearingHouseStatePublicKey = yield (0, addresses_1.getClearingHouseStateAccountPublicKey)(this.program.programId);
128
- const initializeOrderStateTx = yield this.program.transaction.initializeOrderState(orderStateNonce, {
129
- accounts: {
130
- admin: this.wallet.publicKey,
131
- state: clearingHouseStatePublicKey,
132
- orderHistory: orderHistory.publicKey,
133
- orderState: orderStatePublicKey,
134
- rent: web3_js_1.SYSVAR_RENT_PUBKEY,
135
- systemProgram: anchor.web3.SystemProgram.programId,
136
- },
137
- instructions: [
138
- yield this.program.account.orderHistory.createInstruction(orderHistory),
139
- ],
140
- });
141
- return yield this.txSender.send(initializeOrderStateTx, [orderHistory], this.opts);
142
- });
143
- }
144
- initializeMarket(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH, marginRatioInitial = 2000, marginRatioPartial = 625, marginRatioMaintenance = 500) {
145
- return __awaiter(this, void 0, void 0, function* () {
146
- if (this.getMarketsAccount().markets[marketIndex.toNumber()].initialized) {
147
- throw Error(`MarketIndex ${marketIndex.toNumber()} already initialized`);
148
- }
149
- const initializeMarketTx = yield this.program.transaction.initializeMarket(marketIndex, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, marginRatioInitial, marginRatioPartial, marginRatioMaintenance, {
150
- accounts: {
151
- state: yield this.getStatePublicKey(),
152
- admin: this.wallet.publicKey,
153
- oracle: priceOracle,
154
- markets: this.getStateAccount().markets,
155
- },
156
- });
157
- return yield this.txSender.send(initializeMarketTx, [], this.opts);
158
- });
159
- }
160
- moveAmmPrice(baseAssetReserve, quoteAssetReserve, marketIndex) {
161
- return __awaiter(this, void 0, void 0, function* () {
162
- const state = this.getStateAccount();
163
- return yield this.program.rpc.moveAmmPrice(baseAssetReserve, quoteAssetReserve, marketIndex, {
164
- accounts: {
165
- state: yield this.getStatePublicKey(),
166
- admin: this.wallet.publicKey,
167
- markets: state.markets,
168
- },
169
- });
170
- });
171
- }
172
- updateK(sqrtK, marketIndex) {
173
- return __awaiter(this, void 0, void 0, function* () {
174
- const state = this.getStateAccount();
175
- const markets = this.getMarketsAccount();
176
- const marketData = markets.markets[marketIndex.toNumber()];
177
- const ammData = marketData.amm;
178
- return yield this.program.rpc.updateK(sqrtK, marketIndex, {
179
- accounts: {
180
- state: yield this.getStatePublicKey(),
181
- admin: this.wallet.publicKey,
182
- markets: state.markets,
183
- curveHistory: state.extendedCurveHistory,
184
- oracle: ammData.oracle,
185
- },
186
- });
187
- });
188
- }
189
- updateCurveHistory() {
190
- return __awaiter(this, void 0, void 0, function* () {
191
- const extendedCurveHistory = anchor.web3.Keypair.generate();
192
- const state = this.getStateAccount();
193
- return yield this.program.rpc.updateCurveHistory({
194
- accounts: {
195
- state: yield this.getStatePublicKey(),
196
- admin: this.wallet.publicKey,
197
- curveHistory: state.curveHistory,
198
- extendedCurveHistory: extendedCurveHistory.publicKey,
199
- },
200
- instructions: [
201
- yield this.program.account.extendedCurveHistory.createInstruction(extendedCurveHistory),
202
- ],
203
- signers: [extendedCurveHistory],
204
- });
205
- });
206
- }
207
- moveAmmToPrice(marketIndex, targetPrice) {
208
- return __awaiter(this, void 0, void 0, function* () {
209
- const market = this.getMarket(marketIndex);
210
- const [direction, tradeSize, _] = (0, trade_1.calculateTargetPriceTrade)(market, targetPrice);
211
- const [newQuoteAssetAmount, newBaseAssetAmount] = (0, amm_1.calculateAmmReservesAfterSwap)(market.amm, 'quote', tradeSize, (0, amm_1.getSwapDirection)('quote', direction));
212
- const state = this.getStateAccount();
213
- return yield this.program.rpc.moveAmmPrice(newBaseAssetAmount, newQuoteAssetAmount, marketIndex, {
214
- accounts: {
215
- state: yield this.getStatePublicKey(),
216
- admin: this.wallet.publicKey,
217
- markets: state.markets,
218
- },
219
- });
220
- });
221
- }
222
- repegAmmCurve(newPeg, marketIndex) {
223
- return __awaiter(this, void 0, void 0, function* () {
224
- const state = this.getStateAccount();
225
- const markets = this.getMarketsAccount();
226
- const marketData = markets.markets[marketIndex.toNumber()];
227
- const ammData = marketData.amm;
228
- return yield this.program.rpc.repegAmmCurve(newPeg, marketIndex, {
229
- accounts: {
230
- state: yield this.getStatePublicKey(),
231
- admin: this.wallet.publicKey,
232
- oracle: ammData.oracle,
233
- markets: state.markets,
234
- curveHistory: state.extendedCurveHistory,
235
- },
236
- });
237
- });
238
- }
239
- updateAmmOracleTwap(marketIndex) {
240
- return __awaiter(this, void 0, void 0, function* () {
241
- const state = this.getStateAccount();
242
- const markets = this.getMarketsAccount();
243
- const marketData = markets.markets[marketIndex.toNumber()];
244
- const ammData = marketData.amm;
245
- return yield this.program.rpc.updateAmmOracleTwap(marketIndex, {
246
- accounts: {
247
- state: yield this.getStatePublicKey(),
248
- admin: this.wallet.publicKey,
249
- oracle: ammData.oracle,
250
- markets: state.markets,
251
- curveHistory: state.extendedCurveHistory,
252
- },
253
- });
254
- });
255
- }
256
- resetAmmOracleTwap(marketIndex) {
257
- return __awaiter(this, void 0, void 0, function* () {
258
- const state = this.getStateAccount();
259
- const markets = this.getMarketsAccount();
260
- const marketData = markets.markets[marketIndex.toNumber()];
261
- const ammData = marketData.amm;
262
- return yield this.program.rpc.resetAmmOracleTwap(marketIndex, {
263
- accounts: {
264
- state: yield this.getStatePublicKey(),
265
- admin: this.wallet.publicKey,
266
- oracle: ammData.oracle,
267
- markets: state.markets,
268
- curveHistory: state.extendedCurveHistory,
269
- },
270
- });
271
- });
272
- }
273
- withdrawFromInsuranceVault(amount, recipient) {
274
- return __awaiter(this, void 0, void 0, function* () {
275
- const state = yield this.getStateAccount();
276
- return yield this.program.rpc.withdrawFromInsuranceVault(amount, {
277
- accounts: {
278
- admin: this.wallet.publicKey,
279
- state: yield this.getStatePublicKey(),
280
- insuranceVault: state.insuranceVault,
281
- insuranceVaultAuthority: state.insuranceVaultAuthority,
282
- recipient: recipient,
283
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
284
- },
285
- });
286
- });
287
- }
288
- withdrawFees(marketIndex, amount, recipient) {
289
- return __awaiter(this, void 0, void 0, function* () {
290
- const state = yield this.getStateAccount();
291
- return yield this.program.rpc.withdrawFees(marketIndex, amount, {
292
- accounts: {
293
- admin: this.wallet.publicKey,
294
- state: yield this.getStatePublicKey(),
295
- markets: state.markets,
296
- collateralVault: state.collateralVault,
297
- collateralVaultAuthority: state.collateralVaultAuthority,
298
- recipient: recipient,
299
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
300
- },
301
- });
302
- });
303
- }
304
- withdrawFromInsuranceVaultToMarket(marketIndex, amount) {
305
- return __awaiter(this, void 0, void 0, function* () {
306
- const state = yield this.getStateAccount();
307
- return yield this.program.rpc.withdrawFromInsuranceVaultToMarket(marketIndex, amount, {
308
- accounts: {
309
- admin: this.wallet.publicKey,
310
- state: yield this.getStatePublicKey(),
311
- markets: state.markets,
312
- insuranceVault: state.insuranceVault,
313
- insuranceVaultAuthority: state.insuranceVaultAuthority,
314
- collateralVault: state.collateralVault,
315
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
316
- },
317
- });
318
- });
319
- }
320
- updateAdmin(admin) {
321
- return __awaiter(this, void 0, void 0, function* () {
322
- return yield this.program.rpc.updateAdmin(admin, {
323
- accounts: {
324
- admin: this.wallet.publicKey,
325
- state: yield this.getStatePublicKey(),
326
- },
327
- });
328
- });
329
- }
330
- updateMarginRatio(marketIndex, marginRatioInitial, marginRatioPartial, marginRatioMaintenance) {
331
- return __awaiter(this, void 0, void 0, function* () {
332
- return yield this.program.rpc.updateMarginRatio(marketIndex, marginRatioInitial, marginRatioPartial, marginRatioMaintenance, {
333
- accounts: {
334
- admin: this.wallet.publicKey,
335
- state: yield this.getStatePublicKey(),
336
- markets: this.getStateAccount().markets,
337
- },
338
- });
339
- });
340
- }
341
- updateMarketBaseSpread(marketIndex, baseSpread) {
342
- return __awaiter(this, void 0, void 0, function* () {
343
- return yield this.program.rpc.updateMarketBaseSpread(marketIndex, baseSpread, {
344
- accounts: {
345
- admin: this.wallet.publicKey,
346
- state: yield this.getStatePublicKey(),
347
- markets: this.getStateAccount().markets,
348
- },
349
- });
350
- });
351
- }
352
- updatePartialLiquidationClosePercentage(numerator, denominator) {
353
- return __awaiter(this, void 0, void 0, function* () {
354
- return yield this.program.rpc.updatePartialLiquidationClosePercentage(numerator, denominator, {
355
- accounts: {
356
- admin: this.wallet.publicKey,
357
- state: yield this.getStatePublicKey(),
358
- },
359
- });
360
- });
361
- }
362
- updatePartialLiquidationPenaltyPercentage(numerator, denominator) {
363
- return __awaiter(this, void 0, void 0, function* () {
364
- return yield this.program.rpc.updatePartialLiquidationPenaltyPercentage(numerator, denominator, {
365
- accounts: {
366
- admin: this.wallet.publicKey,
367
- state: yield this.getStatePublicKey(),
368
- },
369
- });
370
- });
371
- }
372
- updateFullLiquidationPenaltyPercentage(numerator, denominator) {
373
- return __awaiter(this, void 0, void 0, function* () {
374
- return yield this.program.rpc.updateFullLiquidationPenaltyPercentage(numerator, denominator, {
375
- accounts: {
376
- admin: this.wallet.publicKey,
377
- state: yield this.getStatePublicKey(),
378
- },
379
- });
380
- });
381
- }
382
- updatePartialLiquidationShareDenominator(denominator) {
383
- return __awaiter(this, void 0, void 0, function* () {
384
- return yield this.program.rpc.updatePartialLiquidationLiquidatorShareDenominator(denominator, {
385
- accounts: {
386
- admin: this.wallet.publicKey,
387
- state: yield this.getStatePublicKey(),
388
- },
389
- });
390
- });
391
- }
392
- updateFullLiquidationShareDenominator(denominator) {
393
- return __awaiter(this, void 0, void 0, function* () {
394
- return yield this.program.rpc.updateFullLiquidationLiquidatorShareDenominator(denominator, {
395
- accounts: {
396
- admin: this.wallet.publicKey,
397
- state: yield this.getStatePublicKey(),
398
- },
399
- });
400
- });
401
- }
402
- updateOrderFillerRewardStructure(orderFillerRewardStructure) {
403
- return __awaiter(this, void 0, void 0, function* () {
404
- return yield this.program.rpc.updateOrderFillerRewardStructure(orderFillerRewardStructure, {
405
- accounts: {
406
- admin: this.wallet.publicKey,
407
- state: yield this.getStatePublicKey(),
408
- orderState: yield this.getOrderStatePublicKey(),
409
- },
410
- });
411
- });
412
- }
413
- updateFee(fees) {
414
- return __awaiter(this, void 0, void 0, function* () {
415
- return yield this.program.rpc.updateFee(fees, {
416
- accounts: {
417
- admin: this.wallet.publicKey,
418
- state: yield this.getStatePublicKey(),
419
- },
420
- });
421
- });
422
- }
423
- updateOracleGuardRails(oracleGuardRails) {
424
- return __awaiter(this, void 0, void 0, function* () {
425
- return yield this.program.rpc.updateOracleGuardRails(oracleGuardRails, {
426
- accounts: {
427
- admin: this.wallet.publicKey,
428
- state: yield this.getStatePublicKey(),
429
- },
430
- });
431
- });
432
- }
433
- updateMarketOracle(marketIndex, oracle, oracleSource) {
434
- return __awaiter(this, void 0, void 0, function* () {
435
- const state = this.getStateAccount();
436
- return yield this.program.rpc.updateMarketOracle(marketIndex, oracle, oracleSource, {
437
- accounts: {
438
- admin: this.wallet.publicKey,
439
- state: yield this.getStatePublicKey(),
440
- markets: state.markets,
441
- },
442
- });
443
- });
444
- }
445
- updateMarketMinimumQuoteAssetTradeSize(marketIndex, minimumTradeSize) {
446
- return __awaiter(this, void 0, void 0, function* () {
447
- const state = this.getStateAccount();
448
- return yield this.program.rpc.updateMarketMinimumQuoteAssetTradeSize(marketIndex, minimumTradeSize, {
449
- accounts: {
450
- admin: this.wallet.publicKey,
451
- state: yield this.getStatePublicKey(),
452
- markets: state.markets,
453
- },
454
- });
455
- });
456
- }
457
- updateMarketMinimumBaseAssetTradeSize(marketIndex, minimumTradeSize) {
458
- return __awaiter(this, void 0, void 0, function* () {
459
- const state = this.getStateAccount();
460
- return yield this.program.rpc.updateMarketMinimumBaseAssetTradeSize(marketIndex, minimumTradeSize, {
461
- accounts: {
462
- admin: this.wallet.publicKey,
463
- state: yield this.getStatePublicKey(),
464
- markets: state.markets,
465
- },
466
- });
467
- });
468
- }
469
- updateWhitelistMint(whitelistMint) {
470
- return __awaiter(this, void 0, void 0, function* () {
471
- return yield this.program.rpc.updateWhitelistMint(whitelistMint, {
472
- accounts: {
473
- admin: this.wallet.publicKey,
474
- state: yield this.getStatePublicKey(),
475
- },
476
- });
477
- });
478
- }
479
- updateDiscountMint(discountMint) {
480
- return __awaiter(this, void 0, void 0, function* () {
481
- return yield this.program.rpc.updateDiscountMint(discountMint, {
482
- accounts: {
483
- admin: this.wallet.publicKey,
484
- state: yield this.getStatePublicKey(),
485
- },
486
- });
487
- });
488
- }
489
- updateMaxDeposit(maxDeposit) {
490
- return __awaiter(this, void 0, void 0, function* () {
491
- return yield this.program.rpc.updateMaxDeposit(maxDeposit, {
492
- accounts: {
493
- admin: this.wallet.publicKey,
494
- state: yield this.getStatePublicKey(),
495
- },
496
- });
497
- });
498
- }
499
- updateFundingPaused(fundingPaused) {
500
- return __awaiter(this, void 0, void 0, function* () {
501
- return yield this.program.rpc.updateFundingPaused(fundingPaused, {
502
- accounts: {
503
- admin: this.wallet.publicKey,
504
- state: yield this.getStatePublicKey(),
505
- },
506
- });
507
- });
508
- }
509
- updateExchangePaused(exchangePaused) {
510
- return __awaiter(this, void 0, void 0, function* () {
511
- return yield this.program.rpc.updateExchangePaused(exchangePaused, {
512
- accounts: {
513
- admin: this.wallet.publicKey,
514
- state: yield this.getStatePublicKey(),
515
- },
516
- });
517
- });
518
- }
519
- disableAdminControlsPrices() {
520
- return __awaiter(this, void 0, void 0, function* () {
521
- return yield this.program.rpc.disableAdminControlsPrices({
522
- accounts: {
523
- admin: this.wallet.publicKey,
524
- state: yield this.getStatePublicKey(),
525
- },
526
- });
527
- });
528
- }
529
- transferFromInsuranceVaultToCollateralVault() {
530
- return __awaiter(this, void 0, void 0, function* () {
531
- const state = yield this.getStateAccount();
532
- return yield this.program.rpc.transferFromInsuranceVaultToCollateralVault({
533
- accounts: {
534
- admin: this.wallet.publicKey,
535
- state: yield this.getStatePublicKey(),
536
- insuranceVault: state.insuranceVault,
537
- insuranceVaultAuthority: state.insuranceVaultAuthority,
538
- collateralVault: state.collateralVault,
539
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
540
- },
541
- });
542
- });
543
- }
544
- adminUpdateUserForgoSettlement(authority) {
545
- return __awaiter(this, void 0, void 0, function* () {
546
- const user = yield (0, addresses_1.getUserAccountPublicKey)(this.program.programId, authority);
547
- return yield this.program.rpc.adminUpdateUserForgoSettlement({
548
- accounts: {
549
- admin: this.wallet.publicKey,
550
- state: yield this.getStatePublicKey(),
551
- user: user,
552
- },
553
- });
554
- });
555
- }
556
- initializeSettlementState() {
557
- return __awaiter(this, void 0, void 0, function* () {
558
- const settlementState = yield (0, addresses_1.getSettlementStatePublicKey)(this.program.programId);
559
- const settlementSize = yield this.getTotalSettlementSize();
560
- return yield this.program.rpc.initializeSettlementState(settlementSize, {
561
- accounts: {
562
- admin: this.wallet.publicKey,
563
- state: yield this.getStatePublicKey(),
564
- settlementState: settlementState,
565
- rent: web3_js_1.SYSVAR_RENT_PUBKEY,
566
- systemProgram: anchor.web3.SystemProgram.programId,
567
- collateralVault: (yield this.getStateAccount()).collateralVault,
568
- },
569
- });
570
- });
571
- }
572
- updateSettlementState() {
573
- return __awaiter(this, void 0, void 0, function* () {
574
- const settlementState = yield (0, addresses_1.getSettlementStatePublicKey)(this.program.programId);
575
- return yield this.program.rpc.updateSettlementState({
576
- accounts: {
577
- admin: this.wallet.publicKey,
578
- state: yield this.getStatePublicKey(),
579
- settlementState: settlementState,
580
- collateralVault: (yield this.getStateAccount()).collateralVault,
581
- },
582
- });
583
- });
584
- }
585
- updateSettlementStateEnabled(enabled) {
586
- return __awaiter(this, void 0, void 0, function* () {
587
- const settlementState = yield (0, addresses_1.getSettlementStatePublicKey)(this.program.programId);
588
- return yield this.program.rpc.updateSettlementStateEnabled(enabled, {
589
- accounts: {
590
- admin: this.wallet.publicKey,
591
- state: yield this.getStatePublicKey(),
592
- settlementState: settlementState,
593
- },
594
- });
38
+ async initialize(usdcMint, adminControlsPrices) {
39
+ const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(await this.getStatePublicKey());
40
+ if (stateAccountRPCResponse.value !== null) {
41
+ throw new Error('Clearing house already initialized');
42
+ }
43
+ const [insuranceVaultPublicKey] = await web3_js_1.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('insurance_vault'))], this.program.programId);
44
+ const [insuranceVaultAuthority] = await web3_js_1.PublicKey.findProgramAddress([insuranceVaultPublicKey.toBuffer()], this.program.programId);
45
+ const [clearingHouseStatePublicKey] = await (0, pda_1.getClearingHouseStateAccountPublicKeyAndNonce)(this.program.programId);
46
+ const initializeTx = await this.program.transaction.initialize(adminControlsPrices, {
47
+ accounts: {
48
+ admin: this.wallet.publicKey,
49
+ state: clearingHouseStatePublicKey,
50
+ quoteAssetMint: usdcMint,
51
+ insuranceVault: insuranceVaultPublicKey,
52
+ insuranceVaultAuthority: insuranceVaultAuthority,
53
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
54
+ systemProgram: anchor.web3.SystemProgram.programId,
55
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
56
+ },
57
+ });
58
+ const { txSig: initializeTxSig } = await this.txSender.send(initializeTx, [], this.opts);
59
+ return [initializeTxSig];
60
+ }
61
+ async initializeBank(mint, optimalUtilization, optimalRate, maxRate, oracle, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight, imfFactor = new anchor_1.BN(0)) {
62
+ const bankIndex = this.getStateAccount().numberOfBanks;
63
+ const bank = await (0, pda_1.getBankPublicKey)(this.program.programId, bankIndex);
64
+ const bankVault = await (0, pda_1.getBankVaultPublicKey)(this.program.programId, bankIndex);
65
+ const bankVaultAuthority = await (0, pda_1.getBankVaultAuthorityPublicKey)(this.program.programId, bankIndex);
66
+ const initializeTx = await this.program.transaction.initializeBank(optimalUtilization, optimalRate, maxRate, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight, imfFactor, {
67
+ accounts: {
68
+ admin: this.wallet.publicKey,
69
+ state: await this.getStatePublicKey(),
70
+ bank,
71
+ bankVault,
72
+ bankVaultAuthority,
73
+ bankMint: mint,
74
+ oracle,
75
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
76
+ systemProgram: anchor.web3.SystemProgram.programId,
77
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
78
+ },
79
+ });
80
+ const { txSig } = await this.txSender.send(initializeTx, [], this.opts);
81
+ await this.accountSubscriber.addBank(bankIndex);
82
+ await this.accountSubscriber.addOracle({
83
+ source: oracleSource,
84
+ publicKey: oracle,
85
+ });
86
+ return txSig;
87
+ }
88
+ async initializeMarket(priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH, marginRatioInitial = 2000, marginRatioPartial = 625, marginRatioMaintenance = 500) {
89
+ const marketPublicKey = await (0, pda_1.getMarketPublicKey)(this.program.programId, this.getStateAccount().numberOfMarkets);
90
+ const initializeMarketTx = await this.program.transaction.initializeMarket(baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, marginRatioInitial, marginRatioPartial, marginRatioMaintenance, {
91
+ accounts: {
92
+ state: await this.getStatePublicKey(),
93
+ admin: this.wallet.publicKey,
94
+ oracle: priceOracle,
95
+ market: marketPublicKey,
96
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
97
+ systemProgram: anchor.web3.SystemProgram.programId,
98
+ },
99
+ });
100
+ const { txSig } = await this.txSender.send(initializeMarketTx, [], this.opts);
101
+ await this.accountSubscriber.addMarket(this.getStateAccount().numberOfMarkets);
102
+ await this.accountSubscriber.addOracle({
103
+ source: oracleSource,
104
+ publicKey: priceOracle,
105
+ });
106
+ return txSig;
107
+ }
108
+ async moveAmmPrice(baseAssetReserve, quoteAssetReserve, marketIndex) {
109
+ const marketPublicKey = await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex);
110
+ return await this.program.rpc.moveAmmPrice(baseAssetReserve, quoteAssetReserve, {
111
+ accounts: {
112
+ state: await this.getStatePublicKey(),
113
+ admin: this.wallet.publicKey,
114
+ market: marketPublicKey,
115
+ },
116
+ });
117
+ }
118
+ async updateK(sqrtK, marketIndex) {
119
+ return await this.program.rpc.updateK(sqrtK, {
120
+ accounts: {
121
+ state: await this.getStatePublicKey(),
122
+ admin: this.wallet.publicKey,
123
+ market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
124
+ oracle: this.getMarketAccount(marketIndex).amm.oracle,
125
+ },
126
+ });
127
+ }
128
+ async moveAmmToPrice(marketIndex, targetPrice) {
129
+ const market = this.getMarketAccount(marketIndex);
130
+ const [direction, tradeSize, _] = (0, trade_1.calculateTargetPriceTrade)(market, targetPrice, new anchor_1.BN(1000), 'quote', undefined //todo
131
+ );
132
+ const [newQuoteAssetAmount, newBaseAssetAmount] = (0, amm_1.calculateAmmReservesAfterSwap)(market.amm, 'quote', tradeSize, (0, amm_1.getSwapDirection)('quote', direction));
133
+ const marketPublicKey = await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex);
134
+ return await this.program.rpc.moveAmmPrice(newBaseAssetAmount, newQuoteAssetAmount, {
135
+ accounts: {
136
+ state: await this.getStatePublicKey(),
137
+ admin: this.wallet.publicKey,
138
+ market: marketPublicKey,
139
+ },
140
+ });
141
+ }
142
+ async repegAmmCurve(newPeg, marketIndex) {
143
+ const marketPublicKey = await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex);
144
+ const ammData = this.getMarketAccount(marketIndex).amm;
145
+ return await this.program.rpc.repegAmmCurve(newPeg, {
146
+ accounts: {
147
+ state: await this.getStatePublicKey(),
148
+ admin: this.wallet.publicKey,
149
+ oracle: ammData.oracle,
150
+ market: marketPublicKey,
151
+ },
152
+ });
153
+ }
154
+ async updateAmmOracleTwap(marketIndex) {
155
+ const ammData = this.getMarketAccount(marketIndex).amm;
156
+ const marketPublicKey = await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex);
157
+ return await this.program.rpc.updateAmmOracleTwap({
158
+ accounts: {
159
+ state: await this.getStatePublicKey(),
160
+ admin: this.wallet.publicKey,
161
+ oracle: ammData.oracle,
162
+ market: marketPublicKey,
163
+ },
164
+ });
165
+ }
166
+ async resetAmmOracleTwap(marketIndex) {
167
+ const ammData = this.getMarketAccount(marketIndex).amm;
168
+ const marketPublicKey = await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex);
169
+ return await this.program.rpc.resetAmmOracleTwap({
170
+ accounts: {
171
+ state: await this.getStatePublicKey(),
172
+ admin: this.wallet.publicKey,
173
+ oracle: ammData.oracle,
174
+ market: marketPublicKey,
175
+ },
176
+ });
177
+ }
178
+ async withdrawFromInsuranceVault(amount, recipient) {
179
+ const state = await this.getStateAccount();
180
+ const bank = this.getQuoteAssetBankAccount();
181
+ return await this.program.rpc.withdrawFromInsuranceVault(amount, {
182
+ accounts: {
183
+ admin: this.wallet.publicKey,
184
+ state: await this.getStatePublicKey(),
185
+ bank: bank.pubkey,
186
+ insuranceVault: state.insuranceVault,
187
+ insuranceVaultAuthority: state.insuranceVaultAuthority,
188
+ recipient: recipient,
189
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
190
+ },
191
+ });
192
+ }
193
+ async withdrawFromMarketToInsuranceVault(marketIndex, amount, recipient) {
194
+ const marketPublicKey = await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex);
195
+ const bank = this.getQuoteAssetBankAccount();
196
+ return await this.program.rpc.withdrawFromMarketToInsuranceVault(amount, {
197
+ accounts: {
198
+ admin: this.wallet.publicKey,
199
+ state: await this.getStatePublicKey(),
200
+ market: marketPublicKey,
201
+ bank: bank.pubkey,
202
+ bankVault: bank.vault,
203
+ bankVaultAuthority: bank.vaultAuthority,
204
+ recipient: recipient,
205
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
206
+ },
207
+ });
208
+ }
209
+ async withdrawFromInsuranceVaultToMarket(marketIndex, amount) {
210
+ const state = await this.getStateAccount();
211
+ const bank = this.getQuoteAssetBankAccount();
212
+ return await this.program.rpc.withdrawFromInsuranceVaultToMarket(amount, {
213
+ accounts: {
214
+ admin: this.wallet.publicKey,
215
+ state: await this.getStatePublicKey(),
216
+ market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
217
+ insuranceVault: state.insuranceVault,
218
+ insuranceVaultAuthority: state.insuranceVaultAuthority,
219
+ bank: bank.pubkey,
220
+ bankVault: bank.vault,
221
+ bankVaultAuthority: bank.vaultAuthority,
222
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
223
+ },
224
+ });
225
+ }
226
+ async updateAdmin(admin) {
227
+ return await this.program.rpc.updateAdmin(admin, {
228
+ accounts: {
229
+ admin: this.wallet.publicKey,
230
+ state: await this.getStatePublicKey(),
231
+ },
232
+ });
233
+ }
234
+ async updateCurveUpdateIntensity(marketIndex, curveUpdateIntensity) {
235
+ // assert(curveUpdateIntensity >= 0 && curveUpdateIntensity <= 100);
236
+ // assert(Number.isInteger(curveUpdateIntensity));
237
+ return await this.program.rpc.updateCurveUpdateIntensity(curveUpdateIntensity, {
238
+ accounts: {
239
+ admin: this.wallet.publicKey,
240
+ state: await this.getStatePublicKey(),
241
+ market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
242
+ },
243
+ });
244
+ }
245
+ async updateMarginRatio(marketIndex, marginRatioInitial, marginRatioPartial, marginRatioMaintenance) {
246
+ return await this.program.rpc.updateMarginRatio(marginRatioInitial, marginRatioPartial, marginRatioMaintenance, {
247
+ accounts: {
248
+ admin: this.wallet.publicKey,
249
+ state: await this.getStatePublicKey(),
250
+ market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
251
+ },
252
+ });
253
+ }
254
+ async updateMarketBaseSpread(marketIndex, baseSpread) {
255
+ return await this.program.rpc.updateMarketBaseSpread(baseSpread, {
256
+ accounts: {
257
+ admin: this.wallet.publicKey,
258
+ state: await this.getStatePublicKey(),
259
+ market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
260
+ },
261
+ });
262
+ }
263
+ async updateMarketMaxSpread(marketIndex, maxSpread) {
264
+ return await this.program.rpc.updateMarketMaxSpread(maxSpread, {
265
+ accounts: {
266
+ admin: this.wallet.publicKey,
267
+ state: await this.getStatePublicKey(),
268
+ market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
269
+ },
270
+ });
271
+ }
272
+ async updatePartialLiquidationClosePercentage(numerator, denominator) {
273
+ return await this.program.rpc.updatePartialLiquidationClosePercentage(numerator, denominator, {
274
+ accounts: {
275
+ admin: this.wallet.publicKey,
276
+ state: await this.getStatePublicKey(),
277
+ },
278
+ });
279
+ }
280
+ async updatePartialLiquidationPenaltyPercentage(numerator, denominator) {
281
+ return await this.program.rpc.updatePartialLiquidationPenaltyPercentage(numerator, denominator, {
282
+ accounts: {
283
+ admin: this.wallet.publicKey,
284
+ state: await this.getStatePublicKey(),
285
+ },
286
+ });
287
+ }
288
+ async updateFullLiquidationPenaltyPercentage(numerator, denominator) {
289
+ return await this.program.rpc.updateFullLiquidationPenaltyPercentage(numerator, denominator, {
290
+ accounts: {
291
+ admin: this.wallet.publicKey,
292
+ state: await this.getStatePublicKey(),
293
+ },
294
+ });
295
+ }
296
+ async updatePartialLiquidationShareDenominator(denominator) {
297
+ return await this.program.rpc.updatePartialLiquidationLiquidatorShareDenominator(denominator, {
298
+ accounts: {
299
+ admin: this.wallet.publicKey,
300
+ state: await this.getStatePublicKey(),
301
+ },
302
+ });
303
+ }
304
+ async updateFullLiquidationShareDenominator(denominator) {
305
+ return await this.program.rpc.updateFullLiquidationLiquidatorShareDenominator(denominator, {
306
+ accounts: {
307
+ admin: this.wallet.publicKey,
308
+ state: await this.getStatePublicKey(),
309
+ },
310
+ });
311
+ }
312
+ async updateOrderFillerRewardStructure(orderFillerRewardStructure) {
313
+ return await this.program.rpc.updateOrderFillerRewardStructure(orderFillerRewardStructure, {
314
+ accounts: {
315
+ admin: this.wallet.publicKey,
316
+ state: await this.getStatePublicKey(),
317
+ },
318
+ });
319
+ }
320
+ async updateFee(fees) {
321
+ return await this.program.rpc.updateFee(fees, {
322
+ accounts: {
323
+ admin: this.wallet.publicKey,
324
+ state: await this.getStatePublicKey(),
325
+ },
326
+ });
327
+ }
328
+ async updateOracleGuardRails(oracleGuardRails) {
329
+ return await this.program.rpc.updateOracleGuardRails(oracleGuardRails, {
330
+ accounts: {
331
+ admin: this.wallet.publicKey,
332
+ state: await this.getStatePublicKey(),
333
+ },
334
+ });
335
+ }
336
+ async updateMarketOracle(marketIndex, oracle, oracleSource) {
337
+ return await this.program.rpc.updateMarketOracle(oracle, oracleSource, {
338
+ accounts: {
339
+ admin: this.wallet.publicKey,
340
+ state: await this.getStatePublicKey(),
341
+ market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
342
+ },
343
+ });
344
+ }
345
+ async updateMarketMinimumQuoteAssetTradeSize(marketIndex, minimumTradeSize) {
346
+ return await this.program.rpc.updateMarketMinimumQuoteAssetTradeSize(minimumTradeSize, {
347
+ accounts: {
348
+ admin: this.wallet.publicKey,
349
+ state: await this.getStatePublicKey(),
350
+ market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
351
+ },
352
+ });
353
+ }
354
+ async updateMarketBaseAssetAmountStepSize(marketIndex, stepSize) {
355
+ return await this.program.rpc.updateMarketBaseAssetAmountStepSize(stepSize, {
356
+ accounts: {
357
+ admin: this.wallet.publicKey,
358
+ state: await this.getStatePublicKey(),
359
+ market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
360
+ },
361
+ });
362
+ }
363
+ async updateWhitelistMint(whitelistMint) {
364
+ return await this.program.rpc.updateWhitelistMint(whitelistMint, {
365
+ accounts: {
366
+ admin: this.wallet.publicKey,
367
+ state: await this.getStatePublicKey(),
368
+ },
369
+ });
370
+ }
371
+ async updateDiscountMint(discountMint) {
372
+ return await this.program.rpc.updateDiscountMint(discountMint, {
373
+ accounts: {
374
+ admin: this.wallet.publicKey,
375
+ state: await this.getStatePublicKey(),
376
+ },
377
+ });
378
+ }
379
+ async updateMaxDeposit(maxDeposit) {
380
+ return await this.program.rpc.updateMaxDeposit(maxDeposit, {
381
+ accounts: {
382
+ admin: this.wallet.publicKey,
383
+ state: await this.getStatePublicKey(),
384
+ },
385
+ });
386
+ }
387
+ async updateFundingPaused(fundingPaused) {
388
+ return await this.program.rpc.updateFundingPaused(fundingPaused, {
389
+ accounts: {
390
+ admin: this.wallet.publicKey,
391
+ state: await this.getStatePublicKey(),
392
+ },
393
+ });
394
+ }
395
+ async updateExchangePaused(exchangePaused) {
396
+ return await this.program.rpc.updateExchangePaused(exchangePaused, {
397
+ accounts: {
398
+ admin: this.wallet.publicKey,
399
+ state: await this.getStatePublicKey(),
400
+ },
401
+ });
402
+ }
403
+ async disableAdminControlsPrices() {
404
+ return await this.program.rpc.disableAdminControlsPrices({
405
+ accounts: {
406
+ admin: this.wallet.publicKey,
407
+ state: await this.getStatePublicKey(),
408
+ },
409
+ });
410
+ }
411
+ async updateAuctionDuration(minDuration, maxDuration) {
412
+ return await this.program.rpc.updateAuctionDuration(typeof minDuration === 'number' ? minDuration : minDuration.toNumber(), typeof maxDuration === 'number' ? maxDuration : maxDuration.toNumber(), {
413
+ accounts: {
414
+ admin: this.wallet.publicKey,
415
+ state: await this.getStatePublicKey(),
416
+ },
417
+ });
418
+ }
419
+ async updateMaxBaseAssetAmountRatio(marketIndex, maxBaseAssetAmountRatio) {
420
+ return await this.program.rpc.updateMaxBaseAssetAmountRatio(maxBaseAssetAmountRatio, {
421
+ accounts: {
422
+ admin: this.wallet.publicKey,
423
+ state: await this.getStatePublicKey(),
424
+ market: this.getMarketAccount(marketIndex).pubkey,
425
+ },
426
+ });
427
+ }
428
+ async updateMaxSlippageRatio(marketIndex, maxSlippageRatio) {
429
+ return await this.program.rpc.updateMaxSlippageRatio(maxSlippageRatio, {
430
+ accounts: {
431
+ admin: this.wallet.publicKey,
432
+ state: await this.getStatePublicKey(),
433
+ market: this.getMarketAccount(marketIndex).pubkey,
434
+ },
595
435
  });
596
436
  }
597
437
  }