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

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 +2 -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 +3 -0
  9. package/lib/clearingHouse.js +19 -1
  10. package/lib/clearingHouseUser.d.ts +11 -9
  11. package/lib/clearingHouseUser.js +184 -108
  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 +123 -203
  20. package/lib/index.d.ts +5 -0
  21. package/lib/index.js +5 -0
  22. package/lib/math/market.d.ts +3 -2
  23. package/lib/math/market.js +17 -12
  24. package/lib/math/orders.d.ts +3 -3
  25. package/lib/math/orders.js +31 -16
  26. package/lib/math/position.d.ts +3 -3
  27. package/lib/math/position.js +23 -16
  28. package/lib/math/repeg.js +8 -0
  29. package/lib/math/spotBalance.d.ts +3 -0
  30. package/lib/math/spotBalance.js +18 -1
  31. package/lib/math/spotPosition.d.ts +5 -1
  32. package/lib/math/spotPosition.js +16 -1
  33. package/lib/types.d.ts +25 -24
  34. package/lib/types.js +9 -3
  35. package/lib/userMap/userMap.d.ts +25 -0
  36. package/lib/userMap/userMap.js +73 -0
  37. package/lib/userMap/userStatsMap.d.ts +19 -0
  38. package/lib/userMap/userStatsMap.js +68 -0
  39. package/package.json +6 -3
  40. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +4 -0
  41. package/src/accounts/types.ts +1 -0
  42. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +6 -0
  43. package/src/admin.ts +50 -8
  44. package/src/clearingHouse.ts +30 -1
  45. package/src/clearingHouseUser.ts +366 -167
  46. package/src/config.ts +1 -1
  47. package/src/dlob/DLOB.ts +884 -0
  48. package/src/dlob/DLOBNode.ts +163 -0
  49. package/src/dlob/NodeList.ts +185 -0
  50. package/src/idl/clearing_house.json +123 -203
  51. package/src/index.ts +5 -0
  52. package/src/math/market.ts +22 -13
  53. package/src/math/orders.ts +29 -21
  54. package/src/math/position.ts +36 -22
  55. package/src/math/repeg.ts +9 -0
  56. package/src/math/spotBalance.ts +26 -0
  57. package/src/math/spotPosition.ts +42 -1
  58. package/src/types.ts +26 -24
  59. package/src/userMap/userMap.ts +100 -0
  60. package/src/userMap/userStatsMap.ts +110 -0
  61. package/tests/bn/test.ts +2 -3
  62. package/tests/dlob/helpers.ts +322 -0
  63. package/tests/dlob/test.ts +2865 -0
  64. package/my-script/.env +0 -7
  65. package/my-script/getUserStats.ts +0 -106
  66. package/my-script/multiConnections.ts +0 -119
  67. package/my-script/test-regex.ts +0 -11
  68. package/my-script/utils.ts +0 -52
  69. package/src/accounts/bulkAccountLoader.js +0 -249
  70. package/src/accounts/bulkUserStatsSubscription.js +0 -75
  71. package/src/accounts/bulkUserSubscription.js +0 -75
  72. package/src/accounts/fetch.js +0 -92
  73. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -465
  74. package/src/accounts/pollingOracleSubscriber.js +0 -156
  75. package/src/accounts/pollingTokenAccountSubscriber.js +0 -141
  76. package/src/accounts/pollingUserAccountSubscriber.js +0 -208
  77. package/src/accounts/pollingUserStatsAccountSubscriber.js +0 -208
  78. package/src/accounts/types.js +0 -28
  79. package/src/accounts/utils.js +0 -7
  80. package/src/accounts/webSocketAccountSubscriber.js +0 -138
  81. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -433
  82. package/src/accounts/webSocketUserAccountSubscriber.js +0 -113
  83. package/src/accounts/webSocketUserStatsAccountSubsriber.js +0 -113
  84. package/src/addresses/pda.js +0 -186
  85. package/src/admin.js +0 -1284
  86. package/src/assert/assert.js +0 -9
  87. package/src/clearingHouse.js +0 -3433
  88. package/src/clearingHouseConfig.js +0 -2
  89. package/src/clearingHouseUser.js +0 -874
  90. package/src/clearingHouseUserConfig.js +0 -2
  91. package/src/clearingHouseUserStats.js +0 -115
  92. package/src/clearingHouseUserStatsConfig.js +0 -2
  93. package/src/config.js +0 -80
  94. package/src/constants/numericConstants.js +0 -48
  95. package/src/constants/perpMarkets.js +0 -42
  96. package/src/constants/spotMarkets.js +0 -51
  97. package/src/events/eventList.js +0 -120
  98. package/src/events/eventSubscriber.js +0 -202
  99. package/src/events/fetchLogs.js +0 -117
  100. package/src/events/pollingLogProvider.js +0 -113
  101. package/src/events/sort.js +0 -41
  102. package/src/events/txEventCache.js +0 -74
  103. package/src/events/types.js +0 -25
  104. package/src/events/webSocketLogProvider.js +0 -76
  105. package/src/factory/bigNum.js +0 -393
  106. package/src/factory/oracleClient.js +0 -20
  107. package/src/index.js +0 -75
  108. package/src/math/amm.js +0 -422
  109. package/src/math/auction.js +0 -42
  110. package/src/math/conversion.js +0 -12
  111. package/src/math/funding.js +0 -296
  112. package/src/math/insurance.js +0 -27
  113. package/src/math/margin.js +0 -77
  114. package/src/math/market.js +0 -105
  115. package/src/math/oracles.js +0 -56
  116. package/src/math/orders.js +0 -153
  117. package/src/math/position.js +0 -172
  118. package/src/math/repeg.js +0 -128
  119. package/src/math/spotBalance.js +0 -176
  120. package/src/math/spotMarket.js +0 -8
  121. package/src/math/spotPosition.js +0 -8
  122. package/src/math/trade.js +0 -260
  123. package/src/math/utils.js +0 -27
  124. package/src/oracles/oracleClientCache.js +0 -20
  125. package/src/oracles/pythClient.js +0 -81
  126. package/src/oracles/quoteAssetOracleClient.js +0 -63
  127. package/src/oracles/switchboardClient.js +0 -101
  128. package/src/oracles/types.js +0 -2
  129. package/src/orderParams.js +0 -28
  130. package/src/serum/serumSubscriber.js +0 -102
  131. package/src/serum/types.js +0 -2
  132. package/src/slot/SlotSubscriber.js +0 -86
  133. package/src/token/index.js +0 -38
  134. package/src/tokenFaucet.js +0 -323
  135. package/src/tx/retryTxSender.js +0 -280
  136. package/src/tx/types.js +0 -2
  137. package/src/tx/utils.js +0 -18
  138. package/src/types.js +0 -216
  139. package/src/userName.js +0 -20
  140. package/src/util/computeUnits.js +0 -62
  141. package/src/util/promiseTimeout.js +0 -14
  142. package/src/util/tps.js +0 -61
  143. package/src/wallet.js +0 -72
package/tests/bn/test.ts CHANGED
@@ -70,8 +70,7 @@ describe('BigNum Tests', () => {
70
70
 
71
71
  expect(val.print()).to.equal('1234.56789');
72
72
 
73
- expect(val.toFixed(3)).to.equal('1234.567');
74
-
73
+ expect(val.toNum().toFixed(3)).to.equal('1234.568');
75
74
  expect(val.toPrecision(1)).to.equal('1e3');
76
75
  expect(val.toPrecision(3)).to.equal('123e1');
77
76
  expect(val.toPrecision(4)).to.equal('1234');
@@ -102,7 +101,7 @@ describe('BigNum Tests', () => {
102
101
 
103
102
  expect(val4.toString()).to.equal('250000000000');
104
103
  expect(val4.print()).to.equal('0.0250000000000');
105
- expect(val4.toFixed(3)).to.equal('0.025');
104
+ expect(val4.toNum().toFixed(3)).to.equal('0.025');
106
105
  expect(val4.toPrecision(4)).to.equal('0.025');
107
106
 
108
107
  // Case 5
@@ -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
+ settledPnl: 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
+ ];