@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.
- package/VERSION +1 -1
- package/bun.lockb +0 -0
- package/lib/addresses/pda.d.ts +1 -0
- package/lib/addresses/pda.js +5 -1
- package/lib/adminClient.d.ts +2 -0
- package/lib/adminClient.js +20 -0
- package/lib/constants/spotMarkets.js +1 -1
- package/lib/dlob/DLOB.d.ts +2 -6
- package/lib/dlob/DLOB.js +9 -11
- package/lib/dlob/DLOBSubscriber.js +4 -7
- package/lib/dlob/orderBookLevels.d.ts +4 -2
- package/lib/dlob/orderBookLevels.js +79 -16
- package/lib/factory/bigNum.js +4 -2
- package/lib/idl/drift.json +171 -2
- package/lib/jupiter/jupiterClient.d.ts +3 -2
- package/lib/jupiter/jupiterClient.js +3 -2
- package/lib/math/auction.d.ts +12 -1
- package/lib/math/auction.js +22 -1
- package/lib/math/market.js +2 -4
- package/lib/math/superStake.js +1 -1
- package/lib/math/trade.js +2 -4
- package/lib/orderSubscriber/WebsocketSubscription.js +2 -0
- package/package.json +2 -1
- package/src/addresses/pda.ts +9 -0
- package/src/adminClient.ts +32 -0
- package/src/constants/spotMarkets.ts +1 -1
- package/src/dlob/DLOB.ts +9 -32
- package/src/dlob/DLOBSubscriber.ts +8 -7
- package/src/dlob/orderBookLevels.ts +133 -32
- package/src/factory/bigNum.ts +2 -0
- package/src/idl/drift.json +171 -2
- package/src/jupiter/jupiterClient.ts +4 -1
- package/src/math/auction.ts +36 -2
- package/src/math/market.ts +4 -9
- package/src/math/superStake.ts +2 -2
- package/src/math/trade.ts +3 -11
- package/src/orderSubscriber/WebsocketSubscription.ts +1 -0
- package/tests/amm/test.ts +402 -0
- package/tests/auctions/test.ts +66 -0
- package/tests/dlob/test.ts +1 -73
package/src/idl/drift.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.
|
|
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
|
|
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 =
|
|
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;
|
package/src/math/auction.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/math/market.ts
CHANGED
|
@@ -279,27 +279,22 @@ export function calculateAvailablePerpLiquidity(
|
|
|
279
279
|
|
|
280
280
|
asks = asks.abs();
|
|
281
281
|
|
|
282
|
-
const
|
|
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.
|
|
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)
|
package/src/math/superStake.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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 ? '
|
|
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 ? '
|
|
721
|
+
takerIsLong ? 'getRestingLimitAsks' : 'getRestingLimitBids'
|
|
730
722
|
](market.marketIndex, slot, MarketType.SPOT, oraclePriceData);
|
|
731
723
|
const serumLimitOrders = takerIsLong
|
|
732
724
|
? serumAsks.getL2(100)
|