@drift-labs/sdk 2.40.0-beta.9 → 2.41.0-beta.1

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 (40) hide show
  1. package/VERSION +1 -1
  2. package/bun.lockb +0 -0
  3. package/lib/addresses/pda.d.ts +1 -0
  4. package/lib/addresses/pda.js +5 -1
  5. package/lib/adminClient.d.ts +2 -0
  6. package/lib/adminClient.js +20 -0
  7. package/lib/constants/spotMarkets.js +1 -1
  8. package/lib/dlob/DLOB.d.ts +2 -6
  9. package/lib/dlob/DLOB.js +9 -11
  10. package/lib/dlob/DLOBSubscriber.js +4 -7
  11. package/lib/dlob/orderBookLevels.d.ts +4 -2
  12. package/lib/dlob/orderBookLevels.js +79 -16
  13. package/lib/factory/bigNum.js +4 -2
  14. package/lib/idl/drift.json +171 -2
  15. package/lib/jupiter/jupiterClient.d.ts +3 -2
  16. package/lib/jupiter/jupiterClient.js +3 -2
  17. package/lib/math/auction.d.ts +12 -1
  18. package/lib/math/auction.js +22 -1
  19. package/lib/math/market.js +2 -4
  20. package/lib/math/superStake.js +1 -1
  21. package/lib/math/trade.js +2 -4
  22. package/lib/orderSubscriber/WebsocketSubscription.js +2 -0
  23. package/package.json +2 -1
  24. package/src/addresses/pda.ts +9 -0
  25. package/src/adminClient.ts +32 -0
  26. package/src/constants/spotMarkets.ts +1 -1
  27. package/src/dlob/DLOB.ts +9 -32
  28. package/src/dlob/DLOBSubscriber.ts +8 -7
  29. package/src/dlob/orderBookLevels.ts +133 -32
  30. package/src/factory/bigNum.ts +2 -0
  31. package/src/idl/drift.json +171 -2
  32. package/src/jupiter/jupiterClient.ts +4 -1
  33. package/src/math/auction.ts +36 -2
  34. package/src/math/market.ts +4 -9
  35. package/src/math/superStake.ts +2 -2
  36. package/src/math/trade.ts +3 -11
  37. package/src/orderSubscriber/WebsocketSubscription.ts +1 -0
  38. package/tests/amm/test.ts +402 -0
  39. package/tests/auctions/test.ts +66 -0
  40. package/tests/dlob/test.ts +1 -73
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.39.0",
2
+ "version": "2.40.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -2376,6 +2376,61 @@
2376
2376
  }
2377
2377
  ]
2378
2378
  },
2379
+ {
2380
+ "name": "transferProtocolIfShares",
2381
+ "accounts": [
2382
+ {
2383
+ "name": "signer",
2384
+ "isMut": false,
2385
+ "isSigner": true
2386
+ },
2387
+ {
2388
+ "name": "transferConfig",
2389
+ "isMut": true,
2390
+ "isSigner": false
2391
+ },
2392
+ {
2393
+ "name": "state",
2394
+ "isMut": false,
2395
+ "isSigner": false
2396
+ },
2397
+ {
2398
+ "name": "spotMarket",
2399
+ "isMut": false,
2400
+ "isSigner": false
2401
+ },
2402
+ {
2403
+ "name": "insuranceFundStake",
2404
+ "isMut": true,
2405
+ "isSigner": false
2406
+ },
2407
+ {
2408
+ "name": "userStats",
2409
+ "isMut": true,
2410
+ "isSigner": false
2411
+ },
2412
+ {
2413
+ "name": "authority",
2414
+ "isMut": false,
2415
+ "isSigner": true
2416
+ },
2417
+ {
2418
+ "name": "insuranceFundVault",
2419
+ "isMut": false,
2420
+ "isSigner": false
2421
+ }
2422
+ ],
2423
+ "args": [
2424
+ {
2425
+ "name": "marketIndex",
2426
+ "type": "u16"
2427
+ },
2428
+ {
2429
+ "name": "shares",
2430
+ "type": "u128"
2431
+ }
2432
+ ]
2433
+ },
2379
2434
  {
2380
2435
  "name": "initialize",
2381
2436
  "accounts": [
@@ -4542,6 +4597,76 @@
4542
4597
  "type": "u64"
4543
4598
  }
4544
4599
  ]
4600
+ },
4601
+ {
4602
+ "name": "initializeProtocolIfSharesTransferConfig",
4603
+ "accounts": [
4604
+ {
4605
+ "name": "admin",
4606
+ "isMut": true,
4607
+ "isSigner": true
4608
+ },
4609
+ {
4610
+ "name": "protocolIfSharesTransferConfig",
4611
+ "isMut": true,
4612
+ "isSigner": false
4613
+ },
4614
+ {
4615
+ "name": "state",
4616
+ "isMut": false,
4617
+ "isSigner": false
4618
+ },
4619
+ {
4620
+ "name": "rent",
4621
+ "isMut": false,
4622
+ "isSigner": false
4623
+ },
4624
+ {
4625
+ "name": "systemProgram",
4626
+ "isMut": false,
4627
+ "isSigner": false
4628
+ }
4629
+ ],
4630
+ "args": []
4631
+ },
4632
+ {
4633
+ "name": "updateProtocolIfSharesTransferConfig",
4634
+ "accounts": [
4635
+ {
4636
+ "name": "admin",
4637
+ "isMut": true,
4638
+ "isSigner": true
4639
+ },
4640
+ {
4641
+ "name": "protocolIfSharesTransferConfig",
4642
+ "isMut": true,
4643
+ "isSigner": false
4644
+ },
4645
+ {
4646
+ "name": "state",
4647
+ "isMut": false,
4648
+ "isSigner": false
4649
+ }
4650
+ ],
4651
+ "args": [
4652
+ {
4653
+ "name": "whitelistedSigners",
4654
+ "type": {
4655
+ "option": {
4656
+ "array": [
4657
+ "publicKey",
4658
+ 4
4659
+ ]
4660
+ }
4661
+ }
4662
+ },
4663
+ {
4664
+ "name": "maxTransferPerEpoch",
4665
+ "type": {
4666
+ "option": "u128"
4667
+ }
4668
+ }
4669
+ ]
4545
4670
  }
4546
4671
  ],
4547
4672
  "accounts": [
@@ -4732,6 +4857,44 @@
4732
4857
  ]
4733
4858
  }
4734
4859
  },
4860
+ {
4861
+ "name": "ProtocolIfSharesTransferConfig",
4862
+ "type": {
4863
+ "kind": "struct",
4864
+ "fields": [
4865
+ {
4866
+ "name": "whitelistedSigners",
4867
+ "type": {
4868
+ "array": [
4869
+ "publicKey",
4870
+ 4
4871
+ ]
4872
+ }
4873
+ },
4874
+ {
4875
+ "name": "maxTransferPerEpoch",
4876
+ "type": "u128"
4877
+ },
4878
+ {
4879
+ "name": "currentEpochTransfer",
4880
+ "type": "u128"
4881
+ },
4882
+ {
4883
+ "name": "nextEpochTs",
4884
+ "type": "i64"
4885
+ },
4886
+ {
4887
+ "name": "padding",
4888
+ "type": {
4889
+ "array": [
4890
+ "u128",
4891
+ 8
4892
+ ]
4893
+ }
4894
+ }
4895
+ ]
4896
+ }
4897
+ },
4735
4898
  {
4736
4899
  "name": "PerpMarket",
4737
4900
  "type": {
@@ -5004,7 +5167,7 @@
5004
5167
  {
5005
5168
  "name": "name",
5006
5169
  "docs": [
5007
- "The encoded display name fo the market e.g. SOL"
5170
+ "The encoded display name for the market e.g. SOL"
5008
5171
  ],
5009
5172
  "type": {
5010
5173
  "array": [
@@ -8175,6 +8338,12 @@
8175
8338
  },
8176
8339
  {
8177
8340
  "name": "Unstake"
8341
+ },
8342
+ {
8343
+ "name": "UnstakeTransfer"
8344
+ },
8345
+ {
8346
+ "name": "StakeTransfer"
8178
8347
  }
8179
8348
  ]
8180
8349
  }
@@ -275,10 +275,11 @@ export class JupiterClient {
275
275
  inputMint,
276
276
  outputMint,
277
277
  amount,
278
- maxAccounts = 52, // 52 is an estimated amount with buffer
278
+ maxAccounts = 50, // 50 is an estimated amount with buffer
279
279
  slippageBps = 50,
280
280
  swapMode = 'ExactIn',
281
281
  onlyDirectRoutes = false,
282
+ excludeDexes = [],
282
283
  }: {
283
284
  inputMint: PublicKey;
284
285
  outputMint: PublicKey;
@@ -287,6 +288,7 @@ export class JupiterClient {
287
288
  slippageBps?: number;
288
289
  swapMode?: SwapMode;
289
290
  onlyDirectRoutes?: boolean;
291
+ excludeDexes?: string[];
290
292
  }): Promise<QuoteResponse> {
291
293
  const params = new URLSearchParams({
292
294
  inputMint: inputMint.toString(),
@@ -296,6 +298,7 @@ export class JupiterClient {
296
298
  swapMode,
297
299
  onlyDirectRoutes: onlyDirectRoutes.toString(),
298
300
  maxAccounts: maxAccounts.toString(),
301
+ excludeDexes: excludeDexes.join(','),
299
302
  }).toString();
300
303
  const quote = await (await fetch(`${this.url}/v6/quote?${params}`)).json();
301
304
  return quote;
@@ -1,5 +1,5 @@
1
- import { isOneOfVariant, isVariant, Order } from '../types';
2
- import { BN, ZERO } from '../.';
1
+ import { isOneOfVariant, isVariant, Order, PositionDirection } from '../types';
2
+ import { BN, ONE, ZERO } from '../.';
3
3
 
4
4
  export function isAuctionComplete(order: Order, slot: number): boolean {
5
5
  if (order.auctionDuration === 0) {
@@ -104,3 +104,37 @@ export function getAuctionPriceForOracleOffsetAuction(
104
104
 
105
105
  return oraclePrice.add(priceOffset);
106
106
  }
107
+
108
+ export function deriveOracleAuctionParams({
109
+ direction,
110
+ oraclePrice,
111
+ auctionStartPrice,
112
+ auctionEndPrice,
113
+ limitPrice,
114
+ }: {
115
+ direction: PositionDirection;
116
+ oraclePrice: BN;
117
+ auctionStartPrice: BN;
118
+ auctionEndPrice: BN;
119
+ limitPrice: BN;
120
+ }): { auctionStartPrice: BN; auctionEndPrice: BN; oraclePriceOffset: number } {
121
+ let oraclePriceOffset = limitPrice.sub(oraclePrice);
122
+ if (oraclePriceOffset.eq(ZERO)) {
123
+ oraclePriceOffset = isVariant(direction, 'long')
124
+ ? auctionEndPrice.sub(oraclePrice).add(ONE)
125
+ : auctionEndPrice.sub(oraclePrice).sub(ONE);
126
+ }
127
+
128
+ let oraclePriceOffsetNum;
129
+ try {
130
+ oraclePriceOffsetNum = oraclePriceOffset.toNumber();
131
+ } catch (e) {
132
+ oraclePriceOffsetNum = 0;
133
+ }
134
+
135
+ return {
136
+ auctionStartPrice: auctionStartPrice.sub(oraclePrice),
137
+ auctionEndPrice: auctionEndPrice.sub(oraclePrice),
138
+ oraclePriceOffset: oraclePriceOffsetNum,
139
+ };
140
+ }
@@ -279,27 +279,22 @@ export function calculateAvailablePerpLiquidity(
279
279
 
280
280
  asks = asks.abs();
281
281
 
282
- const bidPrice = calculateBidPrice(market, oraclePriceData);
283
- const askPrice = calculateAskPrice(market, oraclePriceData);
284
-
285
- for (const bid of dlob.getMakerLimitBids(
282
+ for (const bid of dlob.getRestingLimitBids(
286
283
  market.marketIndex,
287
284
  slot,
288
285
  MarketType.PERP,
289
- oraclePriceData,
290
- askPrice
286
+ oraclePriceData
291
287
  )) {
292
288
  bids = bids.add(
293
289
  bid.order.baseAssetAmount.sub(bid.order.baseAssetAmountFilled)
294
290
  );
295
291
  }
296
292
 
297
- for (const ask of dlob.getMakerLimitAsks(
293
+ for (const ask of dlob.getRestingLimitAsks(
298
294
  market.marketIndex,
299
295
  slot,
300
296
  MarketType.PERP,
301
- oraclePriceData,
302
- bidPrice
297
+ oraclePriceData
303
298
  )) {
304
299
  asks = asks.add(
305
300
  ask.order.baseAssetAmount.sub(ask.order.baseAssetAmountFilled)
@@ -10,7 +10,7 @@ import { getMarinadeFinanceProgram, getMarinadeMSolPrice } from '../marinade';
10
10
  import { BN } from '@coral-xyz/anchor';
11
11
  import { User } from '../user';
12
12
  import { DepositRecord, isVariant } from '../types';
13
- import { LAMPORTS_PRECISION, ZERO, NINE } from '../constants/numericConstants';
13
+ import { LAMPORTS_PRECISION, ZERO } from '../constants/numericConstants';
14
14
  import fetch from 'node-fetch';
15
15
  import { checkSameDate } from './utils';
16
16
 
@@ -266,7 +266,7 @@ const getJitoSolHistoricalPriceMap = async (timestamps: number[]) => {
266
266
  for (let i = 0; i < data.data.getStakePoolStats.supply.length; i++) {
267
267
  const priceInSol =
268
268
  data.data.getStakePoolStats.tvl[i].data /
269
- new BN(10).pow(NINE) /
269
+ 10 ** 9 /
270
270
  data.data.getStakePoolStats.supply[i].data;
271
271
  jitoSolHistoricalPriceInSol.push({
272
272
  price: priceInSol,
package/src/math/trade.ts CHANGED
@@ -411,16 +411,8 @@ export function calculateEstimatedPerpEntryPrice(
411
411
 
412
412
  const takerIsLong = isVariant(direction, 'long');
413
413
  const limitOrders = dlob[
414
- takerIsLong ? 'getMakerLimitAsks' : 'getMakerLimitBids'
415
- ](
416
- market.marketIndex,
417
- slot,
418
- MarketType.PERP,
419
- oraclePriceData,
420
- takerIsLong
421
- ? calculateBidPrice(market, oraclePriceData)
422
- : calculateAskPrice(market, oraclePriceData)
423
- );
414
+ takerIsLong ? 'getRestingLimitAsks' : 'getRestingLimitBids'
415
+ ](market.marketIndex, slot, MarketType.PERP, oraclePriceData);
424
416
 
425
417
  const swapDirection = getSwapDirection(assetType, direction);
426
418
 
@@ -726,7 +718,7 @@ export function calculateEstimatedSpotEntryPrice(
726
718
 
727
719
  const takerIsLong = isVariant(direction, 'long');
728
720
  const dlobLimitOrders = dlob[
729
- takerIsLong ? 'getMakerLimitAsks' : 'getMakerLimitBids'
721
+ takerIsLong ? 'getRestingLimitAsks' : 'getRestingLimitBids'
730
722
  ](market.marketIndex, slot, MarketType.SPOT, oraclePriceData);
731
723
  const serumLimitOrders = takerIsLong
732
724
  ? serumAsks.getL2(100)
@@ -59,6 +59,7 @@ export class WebsocketSubscription {
59
59
  }
60
60
 
61
61
  public async unsubscribe(): Promise<void> {
62
+ if (!this.subscriber) return;
62
63
  this.subscriber.unsubscribe();
63
64
  }
64
65
  }