@drift-labs/sdk 0.2.0-master.30 → 0.2.0-master.31

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 (50) hide show
  1. package/lib/admin.d.ts +11 -7
  2. package/lib/admin.js +50 -12
  3. package/lib/clearingHouse.d.ts +26 -23
  4. package/lib/clearingHouse.js +290 -652
  5. package/lib/clearingHouseUser.d.ts +1 -1
  6. package/lib/clearingHouseUser.js +9 -12
  7. package/lib/config.js +1 -1
  8. package/lib/constants/numericConstants.d.ts +7 -0
  9. package/lib/constants/numericConstants.js +8 -1
  10. package/lib/constants/spotMarkets.js +4 -4
  11. package/lib/dlob/DLOB.d.ts +6 -5
  12. package/lib/dlob/DLOB.js +130 -88
  13. package/lib/dlob/DLOBNode.d.ts +2 -0
  14. package/lib/dlob/DLOBNode.js +3 -0
  15. package/lib/dlob/NodeList.d.ts +1 -1
  16. package/lib/dlob/NodeList.js +5 -4
  17. package/lib/events/types.d.ts +2 -1
  18. package/lib/events/types.js +1 -0
  19. package/lib/factory/bigNum.d.ts +1 -0
  20. package/lib/factory/bigNum.js +14 -0
  21. package/lib/idl/clearing_house.json +1091 -611
  22. package/lib/math/amm.d.ts +2 -2
  23. package/lib/math/amm.js +31 -28
  24. package/lib/math/market.d.ts +1 -1
  25. package/lib/math/market.js +6 -6
  26. package/lib/math/spotBalance.js +7 -8
  27. package/lib/math/trade.js +11 -11
  28. package/lib/types.d.ts +117 -40
  29. package/lib/types.js +33 -3
  30. package/package.json +4 -2
  31. package/src/admin.ts +129 -29
  32. package/src/clearingHouse.ts +380 -787
  33. package/src/clearingHouseUser.ts +11 -14
  34. package/src/config.ts +1 -1
  35. package/src/constants/numericConstants.ts +7 -0
  36. package/src/constants/spotMarkets.ts +5 -4
  37. package/src/dlob/DLOB.ts +188 -103
  38. package/src/dlob/DLOBNode.ts +5 -0
  39. package/src/dlob/NodeList.ts +9 -5
  40. package/src/events/types.ts +3 -0
  41. package/src/factory/bigNum.ts +23 -0
  42. package/src/idl/clearing_house.json +1091 -611
  43. package/src/math/amm.ts +42 -29
  44. package/src/math/market.ts +9 -4
  45. package/src/math/spotBalance.ts +11 -8
  46. package/src/math/trade.ts +11 -11
  47. package/src/types.ts +81 -40
  48. package/tests/bn/test.ts +12 -7
  49. package/tests/dlob/helpers.ts +56 -33
  50. package/tests/dlob/test.ts +1227 -404
@@ -1,36 +1,42 @@
1
1
  import { PublicKey } from '@solana/web3.js';
2
2
  import {
3
+ AMM,
4
+ AssetTier,
5
+ PerpPosition,
3
6
  BN,
4
7
  PerpMarketAccount,
5
8
  SpotMarketAccount,
6
9
  MarketStatus,
7
10
  ContractType,
8
- DevnetPerpMarkets,
9
11
  OracleSource,
10
12
  DevnetSpotMarkets,
13
+ BASE_PRECISION,
14
+ QUOTE_PRECISION,
15
+ AMM_TO_QUOTE_PRECISION_RATIO,
11
16
  } from '../../src';
12
17
 
13
- export const mockPerpPosition = {
18
+ export const mockPerpPosition: PerpPosition = {
14
19
  baseAssetAmount: new BN(0),
15
- remainderBaseAssetAmount: new BN(0),
16
20
  lastCumulativeFundingRate: new BN(0),
17
- marketIndex: new BN(0),
21
+ marketIndex: 0,
18
22
  quoteAssetAmount: new BN(0),
19
23
  quoteEntryAmount: new BN(0),
20
- openOrders: new BN(0),
24
+ openOrders: 0,
21
25
  openBids: new BN(0),
22
26
  openAsks: new BN(0),
23
27
  settledPnl: new BN(0),
24
28
  lpShares: new BN(0),
25
- lastFeePerLp: new BN(0),
29
+ remainderBaseAssetAmount: 0,
26
30
  lastNetBaseAssetAmountPerLp: new BN(0),
27
31
  lastNetQuoteAssetAmountPerLp: new BN(0),
28
32
  };
29
33
 
30
- export const mockAMM = {
34
+ export const mockAMM: AMM = {
31
35
  /* these values create a bid/ask price of 12 */
32
- baseAssetReserve: new BN(2800000),
33
- quoteAssetReserve: new BN(3),
36
+ baseAssetReserve: new BN(1).mul(BASE_PRECISION),
37
+ quoteAssetReserve: new BN(12)
38
+ .mul(QUOTE_PRECISION)
39
+ .mul(AMM_TO_QUOTE_PRECISION_RATIO),
34
40
  sqrtK: new BN(1),
35
41
  pegMultiplier: new BN(1),
36
42
  maxSlippageRatio: 1_000_000,
@@ -49,7 +55,7 @@ export const mockAMM = {
49
55
  lastOraclePriceTwap5min: new BN(0),
50
56
  lastOraclePriceTwapTs: new BN(0),
51
57
  },
52
- lastOracleMarkSpreadPct: new BN(0),
58
+ lastOracleReservePriceSpreadPct: new BN(0),
53
59
  lastOracleConfPct: new BN(0),
54
60
  oracle: PublicKey.default,
55
61
  oracleSource: OracleSource.PYTH,
@@ -57,8 +63,6 @@ export const mockAMM = {
57
63
  cumulativeFundingRateLong: new BN(0),
58
64
  cumulativeFundingRateShort: new BN(0),
59
65
  cumulativeFundingRateLp: new BN(0),
60
- cumulativeRepegRebateLong: new BN(0),
61
- cumulativeRepegRebateShort: new BN(0),
62
66
  totalFeeMinusDistributions: new BN(0),
63
67
  totalFeeWithdrawn: new BN(0),
64
68
  totalFee: new BN(0),
@@ -78,6 +82,7 @@ export const mockAMM = {
78
82
  terminalQuoteAssetReserve: new BN(0),
79
83
  feePool: {
80
84
  balance: new BN(0),
85
+ marketIndex: 0,
81
86
  },
82
87
  totalExchangeFee: new BN(0),
83
88
  totalMmFee: new BN(0),
@@ -103,7 +108,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
103
108
  contractType: ContractType.PERPETUAL,
104
109
  expiryTs: new BN(0),
105
110
  settlementPrice: new BN(0),
106
- marketIndex: DevnetPerpMarkets[0].marketIndex,
111
+ marketIndex: 0,
107
112
  pubkey: PublicKey.default,
108
113
  amm: mockAMM,
109
114
  baseAssetAmount: new BN(0),
@@ -115,6 +120,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
115
120
  nextFillRecordId: new BN(0),
116
121
  pnlPool: {
117
122
  balance: new BN(0),
123
+ marketIndex: 0,
118
124
  },
119
125
  ifLiquidationFee: new BN(0),
120
126
  liquidatorFee: new BN(0),
@@ -134,7 +140,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
134
140
  contractType: ContractType.PERPETUAL,
135
141
  expiryTs: new BN(0),
136
142
  settlementPrice: new BN(0),
137
- marketIndex: DevnetPerpMarkets[1].marketIndex,
143
+ marketIndex: 1,
138
144
  pubkey: PublicKey.default,
139
145
  amm: mockAMM,
140
146
  baseAssetAmount: new BN(0),
@@ -146,6 +152,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
146
152
  nextFillRecordId: new BN(0),
147
153
  pnlPool: {
148
154
  balance: new BN(0),
155
+ marketIndex: 0,
149
156
  },
150
157
  ifLiquidationFee: new BN(0),
151
158
  liquidatorFee: new BN(0),
@@ -165,7 +172,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
165
172
  contractType: ContractType.PERPETUAL,
166
173
  expiryTs: new BN(0),
167
174
  settlementPrice: new BN(0),
168
- marketIndex: DevnetPerpMarkets[2].marketIndex,
175
+ marketIndex: 2,
169
176
  pubkey: PublicKey.default,
170
177
  amm: mockAMM,
171
178
  baseAssetAmount: new BN(0),
@@ -177,6 +184,7 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
177
184
  nextFillRecordId: new BN(0),
178
185
  pnlPool: {
179
186
  balance: new BN(0),
187
+ marketIndex: 0,
180
188
  },
181
189
  ifLiquidationFee: new BN(0),
182
190
  liquidatorFee: new BN(0),
@@ -195,7 +203,10 @@ export const mockPerpMarkets: Array<PerpMarketAccount> = [
195
203
 
196
204
  export const mockSpotMarkets: Array<SpotMarketAccount> = [
197
205
  {
198
- marketIndex: new BN(0),
206
+ status: MarketStatus.ACTIVE,
207
+ assetTier: AssetTier.COLLATERAL,
208
+ maxTokenDeposits: new BN(100),
209
+ marketIndex: 0,
199
210
  pubkey: PublicKey.default,
200
211
  mint: DevnetSpotMarkets[0].mint,
201
212
  vault: PublicKey.default,
@@ -203,17 +214,18 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
203
214
  insuranceWithdrawEscrowPeriod: new BN(0),
204
215
  revenuePool: {
205
216
  balance: new BN(0),
217
+ marketIndex: 0,
206
218
  },
207
219
  totalIfShares: new BN(0),
208
220
  userIfShares: new BN(0),
209
- userIfFactor: new BN(0),
210
- totalIfFactor: new BN(0),
221
+ userIfFactor: 0,
222
+ totalIfFactor: 0,
211
223
  ifLiquidationFee: new BN(0),
212
224
  liquidatorFee: new BN(0),
213
225
  decimals: 6,
214
- optimalUtilization: new BN(0),
215
- optimalBorrowRate: new BN(0),
216
- maxBorrowRate: new BN(0),
226
+ optimalUtilization: 0,
227
+ optimalBorrowRate: 0,
228
+ maxBorrowRate: 0,
217
229
  cumulativeDepositInterest: new BN(0),
218
230
  cumulativeBorrowInterest: new BN(0),
219
231
  depositBalance: new BN(0),
@@ -234,6 +246,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
234
246
  nextFillRecordId: new BN(0),
235
247
  spotFeePool: {
236
248
  balance: new BN(0),
249
+ marketIndex: 0,
237
250
  },
238
251
  totalSpotFee: new BN(0),
239
252
  oracleSource: OracleSource.PYTH,
@@ -254,7 +267,10 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
254
267
  },
255
268
  },
256
269
  {
257
- marketIndex: new BN(1),
270
+ status: MarketStatus.ACTIVE,
271
+ assetTier: AssetTier.CROSS,
272
+ maxTokenDeposits: new BN(100),
273
+ marketIndex: 1,
258
274
  pubkey: PublicKey.default,
259
275
  mint: DevnetSpotMarkets[1].mint,
260
276
  vault: PublicKey.default,
@@ -262,17 +278,18 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
262
278
  insuranceWithdrawEscrowPeriod: new BN(0),
263
279
  revenuePool: {
264
280
  balance: new BN(0),
281
+ marketIndex: 0,
265
282
  },
266
283
  totalIfShares: new BN(0),
267
284
  userIfShares: new BN(0),
268
- userIfFactor: new BN(0),
269
- totalIfFactor: new BN(0),
285
+ userIfFactor: 0,
286
+ totalIfFactor: 0,
270
287
  ifLiquidationFee: new BN(0),
271
288
  liquidatorFee: new BN(0),
272
289
  decimals: 9,
273
- optimalUtilization: new BN(0),
274
- optimalBorrowRate: new BN(0),
275
- maxBorrowRate: new BN(0),
290
+ optimalUtilization: 0,
291
+ optimalBorrowRate: 0,
292
+ maxBorrowRate: 0,
276
293
  cumulativeDepositInterest: new BN(0),
277
294
  cumulativeBorrowInterest: new BN(0),
278
295
  depositBalance: new BN(0),
@@ -293,6 +310,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
293
310
  nextFillRecordId: new BN(0),
294
311
  spotFeePool: {
295
312
  balance: new BN(0),
313
+ marketIndex: 0,
296
314
  },
297
315
  totalSpotFee: new BN(0),
298
316
  oracleSource: OracleSource.PYTH,
@@ -313,7 +331,10 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
313
331
  },
314
332
  },
315
333
  {
316
- marketIndex: new BN(2),
334
+ status: MarketStatus.ACTIVE,
335
+ assetTier: AssetTier.PROTECTED,
336
+ maxTokenDeposits: new BN(100),
337
+ marketIndex: 2,
317
338
  pubkey: PublicKey.default,
318
339
  mint: DevnetSpotMarkets[2].mint,
319
340
  vault: PublicKey.default,
@@ -321,17 +342,18 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
321
342
  insuranceWithdrawEscrowPeriod: new BN(0),
322
343
  revenuePool: {
323
344
  balance: new BN(0),
345
+ marketIndex: 0,
324
346
  },
325
347
  totalIfShares: new BN(0),
326
348
  userIfShares: new BN(0),
327
- userIfFactor: new BN(0),
328
- totalIfFactor: new BN(0),
349
+ userIfFactor: 0,
350
+ totalIfFactor: 0,
329
351
  ifLiquidationFee: new BN(0),
330
352
  liquidatorFee: new BN(0),
331
353
  decimals: 6,
332
- optimalUtilization: new BN(0),
333
- optimalBorrowRate: new BN(0),
334
- maxBorrowRate: new BN(0),
354
+ optimalUtilization: 0,
355
+ optimalBorrowRate: 0,
356
+ maxBorrowRate: 0,
335
357
  cumulativeDepositInterest: new BN(0),
336
358
  cumulativeBorrowInterest: new BN(0),
337
359
  depositBalance: new BN(0),
@@ -352,6 +374,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
352
374
  nextFillRecordId: new BN(0),
353
375
  spotFeePool: {
354
376
  balance: new BN(0),
377
+ marketIndex: 0,
355
378
  },
356
379
  totalSpotFee: new BN(0),
357
380
  oracleSource: OracleSource.PYTH,