@drift-labs/sdk 0.2.0-master.26 → 0.2.0-master.27

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 (143) hide show
  1. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +1 -0
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +3 -0
  3. package/lib/accounts/types.d.ts +1 -0
  4. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +1 -0
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +3 -0
  6. package/lib/admin.d.ts +5 -2
  7. package/lib/admin.js +30 -4
  8. package/lib/clearingHouse.d.ts +2 -0
  9. package/lib/clearingHouse.js +14 -1
  10. package/lib/clearingHouseUser.d.ts +11 -9
  11. package/lib/clearingHouseUser.js +182 -106
  12. package/lib/config.js +1 -1
  13. package/lib/dlob/DLOB.d.ts +73 -0
  14. package/lib/dlob/DLOB.js +557 -0
  15. package/lib/dlob/DLOBNode.d.ts +52 -0
  16. package/lib/dlob/DLOBNode.js +82 -0
  17. package/lib/dlob/NodeList.d.ts +26 -0
  18. package/lib/dlob/NodeList.js +138 -0
  19. package/lib/idl/clearing_house.json +122 -202
  20. package/lib/index.d.ts +5 -0
  21. package/lib/index.js +5 -0
  22. package/lib/math/market.d.ts +1 -1
  23. package/lib/math/market.js +1 -1
  24. package/lib/math/orders.d.ts +3 -3
  25. package/lib/math/orders.js +31 -16
  26. package/lib/math/spotBalance.d.ts +3 -0
  27. package/lib/math/spotBalance.js +18 -1
  28. package/lib/math/spotPosition.d.ts +5 -1
  29. package/lib/math/spotPosition.js +16 -1
  30. package/lib/types.d.ts +24 -23
  31. package/lib/types.js +9 -3
  32. package/lib/userMap/userMap.d.ts +25 -0
  33. package/lib/userMap/userMap.js +73 -0
  34. package/lib/userMap/userStatsMap.d.ts +19 -0
  35. package/lib/userMap/userStatsMap.js +68 -0
  36. package/package.json +6 -3
  37. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +4 -0
  38. package/src/accounts/types.ts +1 -0
  39. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +6 -0
  40. package/src/addresses/marketAddresses.js +26 -0
  41. package/src/admin.ts +50 -8
  42. package/src/assert/assert.js +1 -1
  43. package/src/clearingHouse.ts +23 -1
  44. package/src/clearingHouseUser.ts +364 -165
  45. package/src/config.ts +1 -1
  46. package/src/constants/banks.js +42 -0
  47. package/src/constants/{perpMarkets.js → markets.js} +11 -11
  48. package/src/dlob/DLOB.ts +884 -0
  49. package/src/dlob/DLOBNode.ts +163 -0
  50. package/src/dlob/NodeList.ts +185 -0
  51. package/src/events/eventList.js +23 -66
  52. package/src/events/txEventCache.js +19 -22
  53. package/src/examples/makeTradeExample.js +157 -0
  54. package/src/factory/bigNum.js +180 -183
  55. package/src/factory/oracleClient.js +9 -9
  56. package/src/idl/clearing_house.json +122 -202
  57. package/src/index.ts +5 -0
  58. package/src/math/auction.js +10 -10
  59. package/src/math/conversion.js +3 -4
  60. package/src/math/funding.js +175 -223
  61. package/src/math/market.ts +1 -1
  62. package/src/math/orders.ts +29 -21
  63. package/src/math/repeg.js +40 -40
  64. package/src/math/spotBalance.ts +26 -0
  65. package/src/math/spotPosition.ts +42 -1
  66. package/src/math/trade.js +74 -81
  67. package/src/math/utils.js +7 -8
  68. package/src/math/utils.js.map +1 -0
  69. package/src/oracles/oracleClientCache.js +9 -10
  70. package/src/oracles/pythClient.js +17 -52
  71. package/src/oracles/quoteAssetOracleClient.js +13 -44
  72. package/src/oracles/switchboardClient.js +37 -69
  73. package/src/oracles/types.js +1 -1
  74. package/src/token/index.js +4 -4
  75. package/src/tx/types.js +1 -1
  76. package/src/tx/utils.js +6 -7
  77. package/src/types.ts +25 -23
  78. package/src/userMap/userMap.ts +100 -0
  79. package/src/userMap/userStatsMap.ts +110 -0
  80. package/src/userName.js +5 -5
  81. package/src/util/computeUnits.js +11 -46
  82. package/src/util/getTokenAddress.js +9 -0
  83. package/src/util/promiseTimeout.js +5 -5
  84. package/src/util/tps.js +12 -46
  85. package/src/wallet.js +18 -55
  86. package/tests/bn/test.ts +2 -3
  87. package/tests/dlob/helpers.ts +322 -0
  88. package/tests/dlob/test.ts +2865 -0
  89. package/my-script/.env +0 -7
  90. package/my-script/getUserStats.ts +0 -106
  91. package/my-script/multiConnections.ts +0 -119
  92. package/my-script/test-regex.ts +0 -11
  93. package/my-script/utils.ts +0 -52
  94. package/src/accounts/bulkAccountLoader.js +0 -249
  95. package/src/accounts/bulkUserStatsSubscription.js +0 -75
  96. package/src/accounts/bulkUserSubscription.js +0 -75
  97. package/src/accounts/fetch.js +0 -92
  98. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -465
  99. package/src/accounts/pollingOracleSubscriber.js +0 -156
  100. package/src/accounts/pollingTokenAccountSubscriber.js +0 -141
  101. package/src/accounts/pollingUserAccountSubscriber.js +0 -208
  102. package/src/accounts/pollingUserStatsAccountSubscriber.js +0 -208
  103. package/src/accounts/types.js +0 -28
  104. package/src/accounts/utils.js +0 -7
  105. package/src/accounts/webSocketAccountSubscriber.js +0 -138
  106. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -433
  107. package/src/accounts/webSocketUserAccountSubscriber.js +0 -113
  108. package/src/accounts/webSocketUserStatsAccountSubsriber.js +0 -113
  109. package/src/addresses/pda.js +0 -186
  110. package/src/admin.js +0 -1284
  111. package/src/clearingHouse.js +0 -3433
  112. package/src/clearingHouseConfig.js +0 -2
  113. package/src/clearingHouseUser.js +0 -874
  114. package/src/clearingHouseUserConfig.js +0 -2
  115. package/src/clearingHouseUserStats.js +0 -115
  116. package/src/clearingHouseUserStatsConfig.js +0 -2
  117. package/src/config.js +0 -80
  118. package/src/constants/numericConstants.js +0 -48
  119. package/src/constants/spotMarkets.js +0 -51
  120. package/src/events/eventSubscriber.js +0 -202
  121. package/src/events/fetchLogs.js +0 -117
  122. package/src/events/pollingLogProvider.js +0 -113
  123. package/src/events/sort.js +0 -41
  124. package/src/events/types.js +0 -25
  125. package/src/events/webSocketLogProvider.js +0 -76
  126. package/src/index.js +0 -75
  127. package/src/math/amm.js +0 -422
  128. package/src/math/insurance.js +0 -27
  129. package/src/math/margin.js +0 -77
  130. package/src/math/market.js +0 -105
  131. package/src/math/oracles.js +0 -56
  132. package/src/math/orders.js +0 -153
  133. package/src/math/position.js +0 -172
  134. package/src/math/spotBalance.js +0 -176
  135. package/src/math/spotMarket.js +0 -8
  136. package/src/math/spotPosition.js +0 -8
  137. package/src/orderParams.js +0 -28
  138. package/src/serum/serumSubscriber.js +0 -102
  139. package/src/serum/types.js +0 -2
  140. package/src/slot/SlotSubscriber.js +0 -86
  141. package/src/tokenFaucet.js +0 -323
  142. package/src/tx/retryTxSender.js +0 -280
  143. package/src/types.js +0 -216
@@ -0,0 +1,322 @@
1
+ import { PublicKey } from '@solana/web3.js';
2
+ import {
3
+ BN,
4
+ PerpMarketAccount,
5
+ SpotMarketAccount,
6
+ MarketStatus,
7
+ ContractType,
8
+ DevnetPerpMarkets,
9
+ OracleSource,
10
+ DevnetSpotMarkets,
11
+ } from '../../src';
12
+
13
+ export const mockPerpPosition = {
14
+ baseAssetAmount: new BN(0),
15
+ remainderBaseAssetAmount: new BN(0),
16
+ lastCumulativeFundingRate: new BN(0),
17
+ marketIndex: new BN(0),
18
+ quoteAssetAmount: new BN(0),
19
+ quoteEntryAmount: new BN(0),
20
+ openOrders: new BN(0),
21
+ openBids: new BN(0),
22
+ openAsks: new BN(0),
23
+ realizedPnl: new BN(0),
24
+ lpShares: new BN(0),
25
+ lastFeePerLp: new BN(0),
26
+ lastNetBaseAssetAmountPerLp: new BN(0),
27
+ lastNetQuoteAssetAmountPerLp: new BN(0),
28
+ };
29
+
30
+ export const mockAMM = {
31
+ /* these values create a bid/ask price of 12 */
32
+ baseAssetReserve: new BN(2800000),
33
+ quoteAssetReserve: new BN(3),
34
+ sqrtK: new BN(1),
35
+ pegMultiplier: new BN(1),
36
+ maxSlippageRatio: 1_000_000,
37
+
38
+ cumulativeFundingRate: new BN(0),
39
+ lastFundingRate: new BN(0),
40
+ lastFundingRateTs: new BN(0),
41
+ lastMarkPriceTwap: new BN(0),
42
+ lastMarkPriceTwap5min: new BN(0),
43
+ lastMarkPriceTwapTs: new BN(0),
44
+ lastOraclePriceTwap: new BN(0),
45
+ lastOraclePriceTwap5min: new BN(0),
46
+ lastOraclePriceTwapTs: new BN(0),
47
+ lastOracleMarkSpreadPct: new BN(0),
48
+ lastOracleConfPct: new BN(0),
49
+ oracle: PublicKey.default,
50
+ oracleSource: OracleSource.PYTH,
51
+ fundingPeriod: new BN(0),
52
+ cumulativeFundingRateLong: new BN(0),
53
+ cumulativeFundingRateShort: new BN(0),
54
+ cumulativeFundingRateLp: new BN(0),
55
+ cumulativeRepegRebateLong: new BN(0),
56
+ cumulativeRepegRebateShort: new BN(0),
57
+ totalFeeMinusDistributions: new BN(0),
58
+ totalFeeWithdrawn: new BN(0),
59
+ totalFee: new BN(0),
60
+ cumulativeFundingPaymentPerLp: new BN(0),
61
+ cumulativeFeePerLp: new BN(0),
62
+ cumulativeNetBaseAssetAmountPerLp: new BN(0),
63
+ userLpShares: new BN(0),
64
+ netUnsettledLpBaseAssetAmount: new BN(0),
65
+ minimumQuoteAssetTradeSize: new BN(0),
66
+ baseAssetAmountStepSize: new BN(0),
67
+ maxBaseAssetAmountRatio: 0,
68
+ lastOraclePrice: new BN(0),
69
+ baseSpread: 0,
70
+ curveUpdateIntensity: 0,
71
+ netBaseAssetAmount: new BN(0),
72
+ quoteAssetAmountLong: new BN(0),
73
+ quoteAssetAmountShort: new BN(0),
74
+ terminalQuoteAssetReserve: new BN(0),
75
+ feePool: {
76
+ balance: new BN(0),
77
+ },
78
+ totalExchangeFee: new BN(0),
79
+ totalMmFee: new BN(0),
80
+ netRevenueSinceLastFunding: new BN(0),
81
+ lastUpdateSlot: new BN(0),
82
+ lastOracleValid: true,
83
+ lastBidPriceTwap: new BN(0),
84
+ lastAskPriceTwap: new BN(0),
85
+ longSpread: new BN(0),
86
+ shortSpread: new BN(0),
87
+ maxSpread: 0,
88
+ marketPosition: mockPerpPosition,
89
+ marketPositionPerLp: mockPerpPosition,
90
+ ammJitIntensity: 0,
91
+ maxBaseAssetReserve: new BN(0),
92
+ minBaseAssetReserve: new BN(0),
93
+ cumulativeSocialLoss: new BN(0),
94
+ };
95
+
96
+ export const mockPerpMarkets: Array<PerpMarketAccount> = [
97
+ {
98
+ status: MarketStatus.INITIALIZED,
99
+ contractType: ContractType.PERPETUAL,
100
+ expiryTs: new BN(0),
101
+ settlementPrice: new BN(0),
102
+ marketIndex: DevnetPerpMarkets[0].marketIndex,
103
+ pubkey: PublicKey.default,
104
+ amm: mockAMM,
105
+ baseAssetAmount: new BN(0),
106
+ baseAssetAmountLong: new BN(0),
107
+ baseAssetAmountShort: new BN(0),
108
+ openInterest: new BN(0),
109
+ marginRatioInitial: 0,
110
+ marginRatioMaintenance: 0,
111
+ nextFillRecordId: new BN(0),
112
+ pnlPool: {
113
+ balance: new BN(0),
114
+ },
115
+ ifLiquidationFee: new BN(0),
116
+ liquidatorFee: new BN(0),
117
+ imfFactor: new BN(0),
118
+ unrealizedImfFactor: new BN(0),
119
+ unrealizedMaxImbalance: new BN(0),
120
+ unrealizedInitialAssetWeight: 0,
121
+ unrealizedMaintenanceAssetWeight: 0,
122
+ revenueWithdrawSinceLastSettle: new BN(0),
123
+ maxRevenueWithdrawPerPeriod: new BN(0),
124
+ lastRevenueWithdrawTs: new BN(0),
125
+ quoteSettledInsurance: new BN(0),
126
+ quoteMaxInsurance: new BN(0),
127
+ },
128
+ {
129
+ status: MarketStatus.INITIALIZED,
130
+ contractType: ContractType.PERPETUAL,
131
+ expiryTs: new BN(0),
132
+ settlementPrice: new BN(0),
133
+ marketIndex: DevnetPerpMarkets[1].marketIndex,
134
+ pubkey: PublicKey.default,
135
+ amm: mockAMM,
136
+ baseAssetAmount: new BN(0),
137
+ baseAssetAmountLong: new BN(0),
138
+ baseAssetAmountShort: new BN(0),
139
+ openInterest: new BN(0),
140
+ marginRatioInitial: 0,
141
+ marginRatioMaintenance: 0,
142
+ nextFillRecordId: new BN(0),
143
+ pnlPool: {
144
+ balance: new BN(0),
145
+ },
146
+ ifLiquidationFee: new BN(0),
147
+ liquidatorFee: new BN(0),
148
+ imfFactor: new BN(0),
149
+ unrealizedImfFactor: new BN(0),
150
+ unrealizedMaxImbalance: new BN(0),
151
+ unrealizedInitialAssetWeight: 0,
152
+ unrealizedMaintenanceAssetWeight: 0,
153
+ revenueWithdrawSinceLastSettle: new BN(0),
154
+ maxRevenueWithdrawPerPeriod: new BN(0),
155
+ lastRevenueWithdrawTs: new BN(0),
156
+ quoteSettledInsurance: new BN(0),
157
+ quoteMaxInsurance: new BN(0),
158
+ },
159
+ {
160
+ status: MarketStatus.INITIALIZED,
161
+ contractType: ContractType.PERPETUAL,
162
+ expiryTs: new BN(0),
163
+ settlementPrice: new BN(0),
164
+ marketIndex: DevnetPerpMarkets[2].marketIndex,
165
+ pubkey: PublicKey.default,
166
+ amm: mockAMM,
167
+ baseAssetAmount: new BN(0),
168
+ baseAssetAmountLong: new BN(0),
169
+ baseAssetAmountShort: new BN(0),
170
+ openInterest: new BN(0),
171
+ marginRatioInitial: 0,
172
+ marginRatioMaintenance: 0,
173
+ nextFillRecordId: new BN(0),
174
+ pnlPool: {
175
+ balance: new BN(0),
176
+ },
177
+ ifLiquidationFee: new BN(0),
178
+ liquidatorFee: new BN(0),
179
+ imfFactor: new BN(0),
180
+ unrealizedImfFactor: new BN(0),
181
+ unrealizedMaxImbalance: new BN(0),
182
+ unrealizedInitialAssetWeight: 0,
183
+ unrealizedMaintenanceAssetWeight: 0,
184
+ revenueWithdrawSinceLastSettle: new BN(0),
185
+ maxRevenueWithdrawPerPeriod: new BN(0),
186
+ lastRevenueWithdrawTs: new BN(0),
187
+ quoteSettledInsurance: new BN(0),
188
+ quoteMaxInsurance: new BN(0),
189
+ },
190
+ ];
191
+
192
+ export const mockSpotMarkets: Array<SpotMarketAccount> = [
193
+ {
194
+ marketIndex: new BN(0),
195
+ pubkey: PublicKey.default,
196
+ mint: DevnetSpotMarkets[0].mint,
197
+ vault: PublicKey.default,
198
+ insuranceFundVault: PublicKey.default,
199
+ insuranceWithdrawEscrowPeriod: new BN(0),
200
+ revenuePool: {
201
+ balance: new BN(0),
202
+ },
203
+ totalIfShares: new BN(0),
204
+ userIfShares: new BN(0),
205
+ userIfFactor: new BN(0),
206
+ totalIfFactor: new BN(0),
207
+ ifLiquidationFee: new BN(0),
208
+ liquidatorFee: new BN(0),
209
+ decimals: 6,
210
+ optimalUtilization: new BN(0),
211
+ optimalBorrowRate: new BN(0),
212
+ maxBorrowRate: new BN(0),
213
+ cumulativeDepositInterest: new BN(0),
214
+ cumulativeBorrowInterest: new BN(0),
215
+ depositBalance: new BN(0),
216
+ borrowBalance: new BN(0),
217
+ lastInterestTs: new BN(0),
218
+ lastTwapTs: new BN(0),
219
+ oracle: PublicKey.default,
220
+ initialAssetWeight: new BN(0),
221
+ maintenanceAssetWeight: new BN(0),
222
+ initialLiabilityWeight: new BN(0),
223
+ maintenanceLiabilityWeight: new BN(0),
224
+ imfFactor: new BN(0),
225
+ withdrawGuardThreshold: new BN(0),
226
+ depositTokenTwap: new BN(0),
227
+ borrowTokenTwap: new BN(0),
228
+ utilizationTwap: new BN(0),
229
+ orderStepSize: new BN(0),
230
+ nextFillRecordId: new BN(0),
231
+ spotFeePool: {
232
+ balance: new BN(0),
233
+ },
234
+ totalSpotFee: new BN(0),
235
+ },
236
+ {
237
+ marketIndex: new BN(1),
238
+ pubkey: PublicKey.default,
239
+ mint: DevnetSpotMarkets[1].mint,
240
+ vault: PublicKey.default,
241
+ insuranceFundVault: PublicKey.default,
242
+ insuranceWithdrawEscrowPeriod: new BN(0),
243
+ revenuePool: {
244
+ balance: new BN(0),
245
+ },
246
+ totalIfShares: new BN(0),
247
+ userIfShares: new BN(0),
248
+ userIfFactor: new BN(0),
249
+ totalIfFactor: new BN(0),
250
+ ifLiquidationFee: new BN(0),
251
+ liquidatorFee: new BN(0),
252
+ decimals: 9,
253
+ optimalUtilization: new BN(0),
254
+ optimalBorrowRate: new BN(0),
255
+ maxBorrowRate: new BN(0),
256
+ cumulativeDepositInterest: new BN(0),
257
+ cumulativeBorrowInterest: new BN(0),
258
+ depositBalance: new BN(0),
259
+ borrowBalance: new BN(0),
260
+ lastInterestTs: new BN(0),
261
+ lastTwapTs: new BN(0),
262
+ oracle: PublicKey.default,
263
+ initialAssetWeight: new BN(0),
264
+ maintenanceAssetWeight: new BN(0),
265
+ initialLiabilityWeight: new BN(0),
266
+ maintenanceLiabilityWeight: new BN(0),
267
+ imfFactor: new BN(0),
268
+ withdrawGuardThreshold: new BN(0),
269
+ depositTokenTwap: new BN(0),
270
+ borrowTokenTwap: new BN(0),
271
+ utilizationTwap: new BN(0),
272
+ orderStepSize: new BN(0),
273
+ nextFillRecordId: new BN(0),
274
+ spotFeePool: {
275
+ balance: new BN(0),
276
+ },
277
+ totalSpotFee: new BN(0),
278
+ },
279
+ {
280
+ marketIndex: new BN(2),
281
+ pubkey: PublicKey.default,
282
+ mint: DevnetSpotMarkets[2].mint,
283
+ vault: PublicKey.default,
284
+ insuranceFundVault: PublicKey.default,
285
+ insuranceWithdrawEscrowPeriod: new BN(0),
286
+ revenuePool: {
287
+ balance: new BN(0),
288
+ },
289
+ totalIfShares: new BN(0),
290
+ userIfShares: new BN(0),
291
+ userIfFactor: new BN(0),
292
+ totalIfFactor: new BN(0),
293
+ ifLiquidationFee: new BN(0),
294
+ liquidatorFee: new BN(0),
295
+ decimals: 6,
296
+ optimalUtilization: new BN(0),
297
+ optimalBorrowRate: new BN(0),
298
+ maxBorrowRate: new BN(0),
299
+ cumulativeDepositInterest: new BN(0),
300
+ cumulativeBorrowInterest: new BN(0),
301
+ depositBalance: new BN(0),
302
+ borrowBalance: new BN(0),
303
+ lastInterestTs: new BN(0),
304
+ lastTwapTs: new BN(0),
305
+ oracle: PublicKey.default,
306
+ initialAssetWeight: new BN(0),
307
+ maintenanceAssetWeight: new BN(0),
308
+ initialLiabilityWeight: new BN(0),
309
+ maintenanceLiabilityWeight: new BN(0),
310
+ imfFactor: new BN(0),
311
+ withdrawGuardThreshold: new BN(0),
312
+ depositTokenTwap: new BN(0),
313
+ borrowTokenTwap: new BN(0),
314
+ utilizationTwap: new BN(0),
315
+ orderStepSize: new BN(0),
316
+ nextFillRecordId: new BN(0),
317
+ spotFeePool: {
318
+ balance: new BN(0),
319
+ },
320
+ totalSpotFee: new BN(0),
321
+ },
322
+ ];