@drift-labs/sdk 0.2.0-temp.1 → 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 (274) 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.js +3 -0
  48. package/lib/events/eventSubscriber.d.ts +5 -2
  49. package/lib/events/eventSubscriber.js +25 -11
  50. package/lib/events/fetchLogs.d.ts +13 -2
  51. package/lib/events/fetchLogs.js +45 -14
  52. package/lib/events/pollingLogProvider.d.ts +2 -1
  53. package/lib/events/pollingLogProvider.js +7 -3
  54. package/lib/events/sort.js +8 -11
  55. package/lib/events/types.d.ts +11 -3
  56. package/lib/events/types.js +8 -0
  57. package/lib/events/webSocketLogProvider.js +1 -1
  58. package/lib/examples/makeTradeExample.js +30 -18
  59. package/lib/factory/bigNum.d.ts +8 -4
  60. package/lib/factory/bigNum.js +109 -19
  61. package/lib/idl/drift.json +8392 -0
  62. package/lib/index.d.ts +29 -12
  63. package/lib/index.js +29 -12
  64. package/lib/math/amm.d.ts +9 -6
  65. package/lib/math/amm.js +91 -38
  66. package/lib/math/conversion.js +1 -1
  67. package/lib/math/exchangeStatus.d.ts +4 -0
  68. package/lib/math/exchangeStatus.js +18 -0
  69. package/lib/math/funding.d.ts +6 -6
  70. package/lib/math/funding.js +23 -21
  71. package/lib/math/insurance.d.ts +4 -0
  72. package/lib/math/insurance.js +27 -0
  73. package/lib/math/margin.d.ts +11 -0
  74. package/lib/math/margin.js +82 -0
  75. package/lib/math/market.d.ts +14 -9
  76. package/lib/math/market.js +70 -10
  77. package/lib/math/oracles.d.ts +4 -0
  78. package/lib/math/oracles.js +36 -8
  79. package/lib/math/orders.d.ts +16 -6
  80. package/lib/math/orders.js +97 -17
  81. package/lib/math/position.d.ts +27 -13
  82. package/lib/math/position.js +91 -37
  83. package/lib/math/repeg.js +17 -8
  84. package/lib/math/spotBalance.d.ts +22 -0
  85. package/lib/math/spotBalance.js +192 -0
  86. package/lib/math/spotMarket.d.ts +4 -0
  87. package/lib/math/spotMarket.js +8 -0
  88. package/lib/math/spotPosition.d.ts +6 -0
  89. package/lib/math/spotPosition.js +23 -0
  90. package/lib/math/trade.d.ts +10 -10
  91. package/lib/math/trade.js +27 -31
  92. package/lib/oracles/pythClient.js +1 -1
  93. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  94. package/lib/oracles/switchboardClient.js +1 -1
  95. package/lib/orderParams.d.ts +4 -4
  96. package/lib/orderParams.js +12 -4
  97. package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
  98. package/lib/serum/serumFulfillmentConfigMap.js +17 -0
  99. package/lib/serum/serumSubscriber.d.ts +27 -0
  100. package/lib/serum/serumSubscriber.js +56 -0
  101. package/lib/serum/types.d.ts +11 -0
  102. package/{src/oracles → lib/serum}/types.js +0 -0
  103. package/lib/tokenFaucet.d.ts +1 -0
  104. package/lib/tokenFaucet.js +23 -12
  105. package/lib/tx/retryTxSender.d.ts +1 -1
  106. package/lib/tx/retryTxSender.js +13 -4
  107. package/lib/tx/types.d.ts +1 -1
  108. package/lib/types.d.ts +631 -222
  109. package/lib/types.js +137 -24
  110. package/lib/user.d.ts +228 -0
  111. package/lib/user.js +959 -0
  112. package/lib/userConfig.d.ts +14 -0
  113. package/lib/{clearingHouseUserConfig.js → userConfig.js} +0 -0
  114. package/lib/userMap/userMap.d.ts +41 -0
  115. package/lib/userMap/userMap.js +85 -0
  116. package/lib/userMap/userStatsMap.d.ts +19 -0
  117. package/lib/userMap/userStatsMap.js +68 -0
  118. package/lib/userName.d.ts +1 -0
  119. package/lib/userName.js +3 -2
  120. package/lib/userStats.d.ts +18 -0
  121. package/lib/userStats.js +49 -0
  122. package/lib/userStatsConfig.d.ts +14 -0
  123. package/{src/clearingHouseConfig.js → lib/userStatsConfig.js} +0 -0
  124. package/lib/util/getTokenAddress.d.ts +2 -0
  125. package/lib/util/getTokenAddress.js +9 -0
  126. package/package.json +12 -5
  127. package/src/accounts/bulkAccountLoader.ts +44 -34
  128. package/src/accounts/bulkUserStatsSubscription.ts +33 -0
  129. package/src/accounts/bulkUserSubscription.ts +2 -3
  130. package/src/accounts/fetch.ts +27 -2
  131. package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +65 -75
  132. package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
  133. package/src/accounts/pollingUserAccountSubscriber.ts +5 -12
  134. package/src/accounts/pollingUserStatsAccountSubscriber.ts +166 -0
  135. package/src/accounts/types.ts +35 -15
  136. package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +78 -73
  137. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  138. package/src/addresses/marketAddresses.ts +3 -4
  139. package/src/addresses/pda.ts +105 -33
  140. package/src/adminClient.ts +1207 -0
  141. package/src/config.ts +41 -34
  142. package/src/constants/numericConstants.ts +59 -26
  143. package/src/constants/{markets.ts → perpMarkets.ts} +22 -11
  144. package/src/constants/spotMarkets.ts +83 -0
  145. package/src/dlob/DLOB.ts +1120 -0
  146. package/src/dlob/DLOBNode.ts +155 -0
  147. package/src/dlob/NodeList.ts +195 -0
  148. package/src/driftClient.ts +3594 -0
  149. package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +9 -8
  150. package/src/events/eventList.ts +3 -0
  151. package/src/events/eventSubscriber.ts +36 -14
  152. package/src/events/fetchLogs.ts +60 -15
  153. package/src/events/pollingLogProvider.ts +11 -3
  154. package/src/events/sort.ts +11 -15
  155. package/src/events/types.ts +27 -2
  156. package/src/events/webSocketLogProvider.ts +1 -1
  157. package/src/examples/makeTradeExample.js +152 -75
  158. package/src/examples/makeTradeExample.ts +44 -28
  159. package/src/factory/bigNum.ts +150 -22
  160. package/src/idl/drift.json +8392 -0
  161. package/src/idl/pyth.json +98 -2
  162. package/src/index.ts +29 -12
  163. package/src/math/amm.ts +161 -48
  164. package/src/math/conversion.ts +2 -2
  165. package/src/math/exchangeStatus.ts +31 -0
  166. package/src/math/funding.ts +41 -31
  167. package/src/math/insurance.ts +35 -0
  168. package/src/math/margin.ts +133 -0
  169. package/src/math/market.ts +143 -14
  170. package/src/math/oracles.ts +63 -9
  171. package/src/math/orders.ts +168 -26
  172. package/src/math/position.ts +133 -59
  173. package/src/math/repeg.ts +19 -9
  174. package/src/math/spotBalance.ts +319 -0
  175. package/src/math/spotMarket.ts +9 -0
  176. package/src/math/spotPosition.ts +47 -0
  177. package/src/math/trade.ts +33 -37
  178. package/src/oracles/pythClient.ts +2 -2
  179. package/src/oracles/quoteAssetOracleClient.ts +2 -2
  180. package/src/oracles/switchboardClient.ts +2 -2
  181. package/src/orderParams.ts +16 -8
  182. package/src/serum/serumFulfillmentConfigMap.ts +26 -0
  183. package/src/serum/serumSubscriber.ts +99 -0
  184. package/src/serum/types.ts +13 -0
  185. package/src/tokenFaucet.ts +38 -15
  186. package/src/tx/retryTxSender.ts +16 -5
  187. package/src/tx/types.ts +2 -1
  188. package/src/types.ts +594 -195
  189. package/src/user.ts +1599 -0
  190. package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
  191. package/src/userMap/userMap.ts +124 -0
  192. package/src/userMap/userStatsMap.ts +108 -0
  193. package/src/userName.ts +2 -1
  194. package/src/userStats.ts +75 -0
  195. package/src/userStatsConfig.ts +18 -0
  196. package/src/util/getTokenAddress.ts +18 -0
  197. package/tests/bn/test.ts +46 -11
  198. package/tests/dlob/helpers.ts +619 -0
  199. package/tests/dlob/test.ts +4586 -0
  200. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +0 -49
  201. package/lib/admin.d.ts +0 -44
  202. package/lib/admin.js +0 -438
  203. package/lib/clearingHouse.d.ts +0 -146
  204. package/lib/clearingHouse.js +0 -1154
  205. package/lib/clearingHouseUser.d.ts +0 -187
  206. package/lib/clearingHouseUser.js +0 -634
  207. package/lib/clearingHouseUserConfig.d.ts +0 -14
  208. package/lib/constants/banks.d.ts +0 -16
  209. package/lib/constants/banks.js +0 -41
  210. package/lib/constants/markets.d.ts +0 -19
  211. package/lib/idl/clearing_house.json +0 -4464
  212. package/lib/math/bankBalance.d.ts +0 -9
  213. package/lib/math/bankBalance.js +0 -75
  214. package/lib/math/state.d.ts +0 -8
  215. package/lib/math/state.js +0 -15
  216. package/lib/orders.d.ts +0 -8
  217. package/lib/orders.js +0 -134
  218. package/src/accounts/bulkAccountLoader.js +0 -197
  219. package/src/accounts/bulkUserSubscription.js +0 -33
  220. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
  221. package/src/accounts/pollingOracleSubscriber.js +0 -93
  222. package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
  223. package/src/accounts/pollingUserAccountSubscriber.js +0 -132
  224. package/src/accounts/types.js +0 -10
  225. package/src/accounts/utils.js +0 -7
  226. package/src/accounts/webSocketAccountSubscriber.js +0 -93
  227. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
  228. package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
  229. package/src/addresses/marketAddresses.js +0 -26
  230. package/src/admin.js +0 -517
  231. package/src/admin.ts +0 -730
  232. package/src/clearingHouse.ts +0 -1828
  233. package/src/clearingHouseUser.ts +0 -978
  234. package/src/clearingHouseUserConfig.js +0 -2
  235. package/src/config.js +0 -67
  236. package/src/constants/banks.js +0 -42
  237. package/src/constants/banks.ts +0 -50
  238. package/src/constants/markets.js +0 -42
  239. package/src/constants/numericConstants.js +0 -41
  240. package/src/events/eventSubscriber.js +0 -139
  241. package/src/events/fetchLogs.js +0 -50
  242. package/src/events/pollingLogProvider.js +0 -64
  243. package/src/events/sort.js +0 -44
  244. package/src/events/txEventCache.js +0 -71
  245. package/src/events/types.js +0 -20
  246. package/src/events/webSocketLogProvider.js +0 -41
  247. package/src/factory/bigNum.js +0 -390
  248. package/src/factory/oracleClient.js +0 -20
  249. package/src/idl/clearing_house.json +0 -4464
  250. package/src/index.js +0 -69
  251. package/src/math/amm.js +0 -369
  252. package/src/math/auction.js +0 -42
  253. package/src/math/bankBalance.ts +0 -112
  254. package/src/math/conversion.js +0 -11
  255. package/src/math/funding.js +0 -248
  256. package/src/math/oracles.js +0 -26
  257. package/src/math/repeg.js +0 -128
  258. package/src/math/state.js +0 -15
  259. package/src/math/state.ts +0 -14
  260. package/src/math/trade.js +0 -253
  261. package/src/math/utils.js +0 -26
  262. package/src/math/utils.js.map +0 -1
  263. package/src/mockUSDCFaucet.js +0 -280
  264. package/src/oracles/oracleClientCache.js +0 -19
  265. package/src/oracles/pythClient.js +0 -46
  266. package/src/oracles/quoteAssetOracleClient.js +0 -32
  267. package/src/oracles/switchboardClient.js +0 -69
  268. package/src/orderParams.js +0 -20
  269. package/src/orders.ts +0 -245
  270. package/src/slot/SlotSubscriber.js +0 -39
  271. package/src/tokenFaucet.js +0 -189
  272. package/src/types.js +0 -125
  273. package/src/userName.js +0 -20
  274. package/src/wallet.js +0 -35
package/src/admin.ts DELETED
@@ -1,730 +0,0 @@
1
- import {
2
- PublicKey,
3
- SYSVAR_RENT_PUBKEY,
4
- TransactionSignature,
5
- } from '@solana/web3.js';
6
- import {
7
- FeeStructure,
8
- OracleGuardRails,
9
- OracleSource,
10
- OrderFillerRewardStructure,
11
- } from './types';
12
- import { BN } from '@project-serum/anchor';
13
- import * as anchor from '@project-serum/anchor';
14
- import {
15
- getClearingHouseStateAccountPublicKeyAndNonce,
16
- getBankVaultAuthorityPublicKey,
17
- getBankPublicKey,
18
- getBankVaultPublicKey,
19
- getMarketPublicKey,
20
- } from './addresses/pda';
21
- import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
22
- import { ClearingHouse } from './clearingHouse';
23
- import { PEG_PRECISION, ZERO } from './constants/numericConstants';
24
- import { calculateTargetPriceTrade } from './math/trade';
25
- import { calculateAmmReservesAfterSwap, getSwapDirection } from './math/amm';
26
-
27
- export class Admin extends ClearingHouse {
28
- public async initialize(
29
- usdcMint: PublicKey,
30
- adminControlsPrices: boolean
31
- ): Promise<[TransactionSignature]> {
32
- const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(
33
- await this.getStatePublicKey()
34
- );
35
- if (stateAccountRPCResponse.value !== null) {
36
- throw new Error('Clearing house already initialized');
37
- }
38
-
39
- const [insuranceVaultPublicKey] = await PublicKey.findProgramAddress(
40
- [Buffer.from(anchor.utils.bytes.utf8.encode('insurance_vault'))],
41
- this.program.programId
42
- );
43
-
44
- const [insuranceVaultAuthority] = await PublicKey.findProgramAddress(
45
- [insuranceVaultPublicKey.toBuffer()],
46
- this.program.programId
47
- );
48
-
49
- const [clearingHouseStatePublicKey] =
50
- await getClearingHouseStateAccountPublicKeyAndNonce(
51
- this.program.programId
52
- );
53
- const initializeTx = await this.program.transaction.initialize(
54
- adminControlsPrices,
55
- {
56
- accounts: {
57
- admin: this.wallet.publicKey,
58
- state: clearingHouseStatePublicKey,
59
- quoteAssetMint: usdcMint,
60
- insuranceVault: insuranceVaultPublicKey,
61
- insuranceVaultAuthority: insuranceVaultAuthority,
62
- rent: SYSVAR_RENT_PUBKEY,
63
- systemProgram: anchor.web3.SystemProgram.programId,
64
- tokenProgram: TOKEN_PROGRAM_ID,
65
- },
66
- }
67
- );
68
-
69
- const { txSig: initializeTxSig } = await this.txSender.send(
70
- initializeTx,
71
- [],
72
- this.opts
73
- );
74
-
75
- return [initializeTxSig];
76
- }
77
-
78
- public async initializeBank(
79
- mint: PublicKey,
80
- optimalUtilization: BN,
81
- optimalRate: BN,
82
- maxRate: BN,
83
- oracle: PublicKey,
84
- oracleSource: OracleSource,
85
- initialAssetWeight: BN,
86
- maintenanceAssetWeight: BN,
87
- initialLiabilityWeight: BN,
88
- maintenanceLiabilityWeight: BN,
89
- imfFactor = new BN(0),
90
- liquidationFee = ZERO
91
- ): Promise<TransactionSignature> {
92
- const bankIndex = this.getStateAccount().numberOfBanks;
93
- const bank = await getBankPublicKey(this.program.programId, bankIndex);
94
-
95
- const bankVault = await getBankVaultPublicKey(
96
- this.program.programId,
97
- bankIndex
98
- );
99
-
100
- const bankVaultAuthority = await getBankVaultAuthorityPublicKey(
101
- this.program.programId,
102
- bankIndex
103
- );
104
-
105
- const initializeTx = await this.program.transaction.initializeBank(
106
- optimalUtilization,
107
- optimalRate,
108
- maxRate,
109
- oracleSource,
110
- initialAssetWeight,
111
- maintenanceAssetWeight,
112
- initialLiabilityWeight,
113
- maintenanceLiabilityWeight,
114
- imfFactor,
115
- liquidationFee,
116
- {
117
- accounts: {
118
- admin: this.wallet.publicKey,
119
- state: await this.getStatePublicKey(),
120
- bank,
121
- bankVault,
122
- bankVaultAuthority,
123
- bankMint: mint,
124
- oracle,
125
- rent: SYSVAR_RENT_PUBKEY,
126
- systemProgram: anchor.web3.SystemProgram.programId,
127
- tokenProgram: TOKEN_PROGRAM_ID,
128
- },
129
- }
130
- );
131
-
132
- const { txSig } = await this.txSender.send(initializeTx, [], this.opts);
133
-
134
- await this.accountSubscriber.addBank(bankIndex);
135
- await this.accountSubscriber.addOracle({
136
- source: oracleSource,
137
- publicKey: oracle,
138
- });
139
-
140
- return txSig;
141
- }
142
-
143
- public async initializeMarket(
144
- priceOracle: PublicKey,
145
- baseAssetReserve: BN,
146
- quoteAssetReserve: BN,
147
- periodicity: BN,
148
- pegMultiplier: BN = PEG_PRECISION,
149
- oracleSource: OracleSource = OracleSource.PYTH,
150
- marginRatioInitial = 2000,
151
- marginRatioMaintenance = 500,
152
- liquidationFee = ZERO
153
- ): Promise<TransactionSignature> {
154
- const marketPublicKey = await getMarketPublicKey(
155
- this.program.programId,
156
- this.getStateAccount().numberOfMarkets
157
- );
158
-
159
- const initializeMarketTx = await this.program.transaction.initializeMarket(
160
- baseAssetReserve,
161
- quoteAssetReserve,
162
- periodicity,
163
- pegMultiplier,
164
- oracleSource,
165
- marginRatioInitial,
166
- marginRatioMaintenance,
167
- liquidationFee,
168
- {
169
- accounts: {
170
- state: await this.getStatePublicKey(),
171
- admin: this.wallet.publicKey,
172
- oracle: priceOracle,
173
- market: marketPublicKey,
174
- rent: SYSVAR_RENT_PUBKEY,
175
- systemProgram: anchor.web3.SystemProgram.programId,
176
- },
177
- }
178
- );
179
- const { txSig } = await this.txSender.send(
180
- initializeMarketTx,
181
- [],
182
- this.opts
183
- );
184
-
185
- await this.accountSubscriber.addMarket(
186
- this.getStateAccount().numberOfMarkets
187
- );
188
- await this.accountSubscriber.addOracle({
189
- source: oracleSource,
190
- publicKey: priceOracle,
191
- });
192
-
193
- return txSig;
194
- }
195
-
196
- public async moveAmmPrice(
197
- baseAssetReserve: BN,
198
- quoteAssetReserve: BN,
199
- marketIndex: BN
200
- ): Promise<TransactionSignature> {
201
- const marketPublicKey = await getMarketPublicKey(
202
- this.program.programId,
203
- marketIndex
204
- );
205
-
206
- return await this.program.rpc.moveAmmPrice(
207
- baseAssetReserve,
208
- quoteAssetReserve,
209
- {
210
- accounts: {
211
- state: await this.getStatePublicKey(),
212
- admin: this.wallet.publicKey,
213
- market: marketPublicKey,
214
- },
215
- }
216
- );
217
- }
218
-
219
- public async updateK(
220
- sqrtK: BN,
221
- marketIndex: BN
222
- ): Promise<TransactionSignature> {
223
- return await this.program.rpc.updateK(sqrtK, {
224
- accounts: {
225
- state: await this.getStatePublicKey(),
226
- admin: this.wallet.publicKey,
227
- market: await getMarketPublicKey(this.program.programId, marketIndex),
228
- oracle: this.getMarketAccount(marketIndex).amm.oracle,
229
- },
230
- });
231
- }
232
-
233
- public async moveAmmToPrice(
234
- marketIndex: BN,
235
- targetPrice: BN
236
- ): Promise<TransactionSignature> {
237
- const market = this.getMarketAccount(marketIndex);
238
-
239
- const [direction, tradeSize, _] = calculateTargetPriceTrade(
240
- market,
241
- targetPrice,
242
- new BN(1000),
243
- 'quote',
244
- undefined //todo
245
- );
246
-
247
- const [newQuoteAssetAmount, newBaseAssetAmount] =
248
- calculateAmmReservesAfterSwap(
249
- market.amm,
250
- 'quote',
251
- tradeSize,
252
- getSwapDirection('quote', direction)
253
- );
254
-
255
- const marketPublicKey = await getMarketPublicKey(
256
- this.program.programId,
257
- marketIndex
258
- );
259
-
260
- return await this.program.rpc.moveAmmPrice(
261
- newBaseAssetAmount,
262
- newQuoteAssetAmount,
263
- {
264
- accounts: {
265
- state: await this.getStatePublicKey(),
266
- admin: this.wallet.publicKey,
267
- market: marketPublicKey,
268
- },
269
- }
270
- );
271
- }
272
-
273
- public async repegAmmCurve(
274
- newPeg: BN,
275
- marketIndex: BN
276
- ): Promise<TransactionSignature> {
277
- const marketPublicKey = await getMarketPublicKey(
278
- this.program.programId,
279
- marketIndex
280
- );
281
- const ammData = this.getMarketAccount(marketIndex).amm;
282
-
283
- return await this.program.rpc.repegAmmCurve(newPeg, {
284
- accounts: {
285
- state: await this.getStatePublicKey(),
286
- admin: this.wallet.publicKey,
287
- oracle: ammData.oracle,
288
- market: marketPublicKey,
289
- },
290
- });
291
- }
292
-
293
- public async updateAmmOracleTwap(
294
- marketIndex: BN
295
- ): Promise<TransactionSignature> {
296
- const ammData = this.getMarketAccount(marketIndex).amm;
297
- const marketPublicKey = await getMarketPublicKey(
298
- this.program.programId,
299
- marketIndex
300
- );
301
-
302
- return await this.program.rpc.updateAmmOracleTwap({
303
- accounts: {
304
- state: await this.getStatePublicKey(),
305
- admin: this.wallet.publicKey,
306
- oracle: ammData.oracle,
307
- market: marketPublicKey,
308
- },
309
- });
310
- }
311
-
312
- public async resetAmmOracleTwap(
313
- marketIndex: BN
314
- ): Promise<TransactionSignature> {
315
- const ammData = this.getMarketAccount(marketIndex).amm;
316
- const marketPublicKey = await getMarketPublicKey(
317
- this.program.programId,
318
- marketIndex
319
- );
320
-
321
- return await this.program.rpc.resetAmmOracleTwap({
322
- accounts: {
323
- state: await this.getStatePublicKey(),
324
- admin: this.wallet.publicKey,
325
- oracle: ammData.oracle,
326
- market: marketPublicKey,
327
- },
328
- });
329
- }
330
-
331
- public async withdrawFromInsuranceVault(
332
- amount: BN,
333
- recipient: PublicKey
334
- ): Promise<TransactionSignature> {
335
- const state = await this.getStateAccount();
336
- const bank = this.getQuoteAssetBankAccount();
337
- return await this.program.rpc.withdrawFromInsuranceVault(amount, {
338
- accounts: {
339
- admin: this.wallet.publicKey,
340
- state: await this.getStatePublicKey(),
341
- bank: bank.pubkey,
342
- insuranceVault: state.insuranceVault,
343
- insuranceVaultAuthority: state.insuranceVaultAuthority,
344
- recipient: recipient,
345
- tokenProgram: TOKEN_PROGRAM_ID,
346
- },
347
- });
348
- }
349
-
350
- public async withdrawFromMarketToInsuranceVault(
351
- marketIndex: BN,
352
- amount: BN,
353
- recipient: PublicKey
354
- ): Promise<TransactionSignature> {
355
- const marketPublicKey = await getMarketPublicKey(
356
- this.program.programId,
357
- marketIndex
358
- );
359
- const bank = this.getQuoteAssetBankAccount();
360
- return await this.program.rpc.withdrawFromMarketToInsuranceVault(amount, {
361
- accounts: {
362
- admin: this.wallet.publicKey,
363
- state: await this.getStatePublicKey(),
364
- market: marketPublicKey,
365
- bank: bank.pubkey,
366
- bankVault: bank.vault,
367
- bankVaultAuthority: bank.vaultAuthority,
368
- recipient: recipient,
369
- tokenProgram: TOKEN_PROGRAM_ID,
370
- },
371
- });
372
- }
373
-
374
- public async withdrawFromInsuranceVaultToMarket(
375
- marketIndex: BN,
376
- amount: BN
377
- ): Promise<TransactionSignature> {
378
- const state = await this.getStateAccount();
379
- const bank = this.getQuoteAssetBankAccount();
380
-
381
- return await this.program.rpc.withdrawFromInsuranceVaultToMarket(amount, {
382
- accounts: {
383
- admin: this.wallet.publicKey,
384
- state: await this.getStatePublicKey(),
385
- market: await getMarketPublicKey(this.program.programId, marketIndex),
386
- insuranceVault: state.insuranceVault,
387
- insuranceVaultAuthority: state.insuranceVaultAuthority,
388
- bank: bank.pubkey,
389
- bankVault: bank.vault,
390
- bankVaultAuthority: bank.vaultAuthority,
391
- tokenProgram: TOKEN_PROGRAM_ID,
392
- },
393
- });
394
- }
395
-
396
- public async updateAdmin(admin: PublicKey): Promise<TransactionSignature> {
397
- return await this.program.rpc.updateAdmin(admin, {
398
- accounts: {
399
- admin: this.wallet.publicKey,
400
- state: await this.getStatePublicKey(),
401
- },
402
- });
403
- }
404
-
405
- public async updateCurveUpdateIntensity(
406
- marketIndex: BN,
407
- curveUpdateIntensity: number
408
- ): Promise<TransactionSignature> {
409
- // assert(curveUpdateIntensity >= 0 && curveUpdateIntensity <= 100);
410
- // assert(Number.isInteger(curveUpdateIntensity));
411
-
412
- return await this.program.rpc.updateCurveUpdateIntensity(
413
- curveUpdateIntensity,
414
- {
415
- accounts: {
416
- admin: this.wallet.publicKey,
417
- state: await this.getStatePublicKey(),
418
- market: await getMarketPublicKey(this.program.programId, marketIndex),
419
- },
420
- }
421
- );
422
- }
423
-
424
- public async updateMarginRatio(
425
- marketIndex: BN,
426
- marginRatioInitial: number,
427
- marginRatioMaintenance: number
428
- ): Promise<TransactionSignature> {
429
- return await this.program.rpc.updateMarginRatio(
430
- marginRatioInitial,
431
- marginRatioMaintenance,
432
- {
433
- accounts: {
434
- admin: this.wallet.publicKey,
435
- state: await this.getStatePublicKey(),
436
- market: await getMarketPublicKey(this.program.programId, marketIndex),
437
- },
438
- }
439
- );
440
- }
441
-
442
- public async updateMarketBaseSpread(
443
- marketIndex: BN,
444
- baseSpread: number
445
- ): Promise<TransactionSignature> {
446
- return await this.program.rpc.updateMarketBaseSpread(baseSpread, {
447
- accounts: {
448
- admin: this.wallet.publicKey,
449
- state: await this.getStatePublicKey(),
450
- market: await getMarketPublicKey(this.program.programId, marketIndex),
451
- },
452
- });
453
- }
454
-
455
- public async updateMarketMaxSpread(
456
- marketIndex: BN,
457
- maxSpread: number
458
- ): Promise<TransactionSignature> {
459
- return await this.program.rpc.updateMarketMaxSpread(maxSpread, {
460
- accounts: {
461
- admin: this.wallet.publicKey,
462
- state: await this.getStatePublicKey(),
463
- market: await getMarketPublicKey(this.program.programId, marketIndex),
464
- },
465
- });
466
- }
467
-
468
- public async updatePartialLiquidationClosePercentage(
469
- numerator: BN,
470
- denominator: BN
471
- ): Promise<TransactionSignature> {
472
- return await this.program.rpc.updatePartialLiquidationClosePercentage(
473
- numerator,
474
- denominator,
475
- {
476
- accounts: {
477
- admin: this.wallet.publicKey,
478
- state: await this.getStatePublicKey(),
479
- },
480
- }
481
- );
482
- }
483
-
484
- public async updatePartialLiquidationPenaltyPercentage(
485
- numerator: BN,
486
- denominator: BN
487
- ): Promise<TransactionSignature> {
488
- return await this.program.rpc.updatePartialLiquidationPenaltyPercentage(
489
- numerator,
490
- denominator,
491
- {
492
- accounts: {
493
- admin: this.wallet.publicKey,
494
- state: await this.getStatePublicKey(),
495
- },
496
- }
497
- );
498
- }
499
-
500
- public async updateFullLiquidationPenaltyPercentage(
501
- numerator: BN,
502
- denominator: BN
503
- ): Promise<TransactionSignature> {
504
- return await this.program.rpc.updateFullLiquidationPenaltyPercentage(
505
- numerator,
506
- denominator,
507
- {
508
- accounts: {
509
- admin: this.wallet.publicKey,
510
- state: await this.getStatePublicKey(),
511
- },
512
- }
513
- );
514
- }
515
-
516
- public async updatePartialLiquidationShareDenominator(
517
- denominator: BN
518
- ): Promise<TransactionSignature> {
519
- return await this.program.rpc.updatePartialLiquidationLiquidatorShareDenominator(
520
- denominator,
521
- {
522
- accounts: {
523
- admin: this.wallet.publicKey,
524
- state: await this.getStatePublicKey(),
525
- },
526
- }
527
- );
528
- }
529
-
530
- public async updateFullLiquidationShareDenominator(
531
- denominator: BN
532
- ): Promise<TransactionSignature> {
533
- return await this.program.rpc.updateFullLiquidationLiquidatorShareDenominator(
534
- denominator,
535
- {
536
- accounts: {
537
- admin: this.wallet.publicKey,
538
- state: await this.getStatePublicKey(),
539
- },
540
- }
541
- );
542
- }
543
-
544
- public async updateOrderFillerRewardStructure(
545
- orderFillerRewardStructure: OrderFillerRewardStructure
546
- ): Promise<TransactionSignature> {
547
- return await this.program.rpc.updateOrderFillerRewardStructure(
548
- orderFillerRewardStructure,
549
- {
550
- accounts: {
551
- admin: this.wallet.publicKey,
552
- state: await this.getStatePublicKey(),
553
- },
554
- }
555
- );
556
- }
557
-
558
- public async updateFee(fees: FeeStructure): Promise<TransactionSignature> {
559
- return await this.program.rpc.updateFee(fees, {
560
- accounts: {
561
- admin: this.wallet.publicKey,
562
- state: await this.getStatePublicKey(),
563
- },
564
- });
565
- }
566
-
567
- public async updateOracleGuardRails(
568
- oracleGuardRails: OracleGuardRails
569
- ): Promise<TransactionSignature> {
570
- return await this.program.rpc.updateOracleGuardRails(oracleGuardRails, {
571
- accounts: {
572
- admin: this.wallet.publicKey,
573
- state: await this.getStatePublicKey(),
574
- },
575
- });
576
- }
577
-
578
- public async updateMarketOracle(
579
- marketIndex: BN,
580
- oracle: PublicKey,
581
- oracleSource: OracleSource
582
- ): Promise<TransactionSignature> {
583
- return await this.program.rpc.updateMarketOracle(oracle, oracleSource, {
584
- accounts: {
585
- admin: this.wallet.publicKey,
586
- state: await this.getStatePublicKey(),
587
- market: await getMarketPublicKey(this.program.programId, marketIndex),
588
- },
589
- });
590
- }
591
-
592
- public async updateMarketMinimumQuoteAssetTradeSize(
593
- marketIndex: BN,
594
- minimumTradeSize: BN
595
- ): Promise<TransactionSignature> {
596
- return await this.program.rpc.updateMarketMinimumQuoteAssetTradeSize(
597
- minimumTradeSize,
598
- {
599
- accounts: {
600
- admin: this.wallet.publicKey,
601
- state: await this.getStatePublicKey(),
602
- market: await getMarketPublicKey(this.program.programId, marketIndex),
603
- },
604
- }
605
- );
606
- }
607
-
608
- public async updateMarketBaseAssetAmountStepSize(
609
- marketIndex: BN,
610
- stepSize: BN
611
- ): Promise<TransactionSignature> {
612
- return await this.program.rpc.updateMarketBaseAssetAmountStepSize(
613
- stepSize,
614
- {
615
- accounts: {
616
- admin: this.wallet.publicKey,
617
- state: await this.getStatePublicKey(),
618
- market: await getMarketPublicKey(this.program.programId, marketIndex),
619
- },
620
- }
621
- );
622
- }
623
-
624
- public async updateWhitelistMint(
625
- whitelistMint?: PublicKey
626
- ): Promise<TransactionSignature> {
627
- return await this.program.rpc.updateWhitelistMint(whitelistMint, {
628
- accounts: {
629
- admin: this.wallet.publicKey,
630
- state: await this.getStatePublicKey(),
631
- },
632
- });
633
- }
634
-
635
- public async updateDiscountMint(
636
- discountMint: PublicKey
637
- ): Promise<TransactionSignature> {
638
- return await this.program.rpc.updateDiscountMint(discountMint, {
639
- accounts: {
640
- admin: this.wallet.publicKey,
641
- state: await this.getStatePublicKey(),
642
- },
643
- });
644
- }
645
-
646
- public async updateMaxDeposit(maxDeposit: BN): Promise<TransactionSignature> {
647
- return await this.program.rpc.updateMaxDeposit(maxDeposit, {
648
- accounts: {
649
- admin: this.wallet.publicKey,
650
- state: await this.getStatePublicKey(),
651
- },
652
- });
653
- }
654
-
655
- public async updateFundingPaused(
656
- fundingPaused: boolean
657
- ): Promise<TransactionSignature> {
658
- return await this.program.rpc.updateFundingPaused(fundingPaused, {
659
- accounts: {
660
- admin: this.wallet.publicKey,
661
- state: await this.getStatePublicKey(),
662
- },
663
- });
664
- }
665
-
666
- public async updateExchangePaused(
667
- exchangePaused: boolean
668
- ): Promise<TransactionSignature> {
669
- return await this.program.rpc.updateExchangePaused(exchangePaused, {
670
- accounts: {
671
- admin: this.wallet.publicKey,
672
- state: await this.getStatePublicKey(),
673
- },
674
- });
675
- }
676
-
677
- public async disableAdminControlsPrices(): Promise<TransactionSignature> {
678
- return await this.program.rpc.disableAdminControlsPrices({
679
- accounts: {
680
- admin: this.wallet.publicKey,
681
- state: await this.getStatePublicKey(),
682
- },
683
- });
684
- }
685
-
686
- public async updateAuctionDuration(
687
- minDuration: BN | number,
688
- maxDuration: BN | number
689
- ): Promise<TransactionSignature> {
690
- return await this.program.rpc.updateAuctionDuration(
691
- typeof minDuration === 'number' ? minDuration : minDuration.toNumber(),
692
- typeof maxDuration === 'number' ? maxDuration : maxDuration.toNumber(),
693
- {
694
- accounts: {
695
- admin: this.wallet.publicKey,
696
- state: await this.getStatePublicKey(),
697
- },
698
- }
699
- );
700
- }
701
-
702
- public async updateMaxBaseAssetAmountRatio(
703
- marketIndex: BN,
704
- maxBaseAssetAmountRatio: number
705
- ): Promise<TransactionSignature> {
706
- return await this.program.rpc.updateMaxBaseAssetAmountRatio(
707
- maxBaseAssetAmountRatio,
708
- {
709
- accounts: {
710
- admin: this.wallet.publicKey,
711
- state: await this.getStatePublicKey(),
712
- market: this.getMarketAccount(marketIndex).pubkey,
713
- },
714
- }
715
- );
716
- }
717
-
718
- public async updateMaxSlippageRatio(
719
- marketIndex: BN,
720
- maxSlippageRatio: number
721
- ): Promise<TransactionSignature> {
722
- return await this.program.rpc.updateMaxSlippageRatio(maxSlippageRatio, {
723
- accounts: {
724
- admin: this.wallet.publicKey,
725
- state: await this.getStatePublicKey(),
726
- market: this.getMarketAccount(marketIndex).pubkey,
727
- },
728
- });
729
- }
730
- }