@drift-labs/sdk 2.11.0-beta.2 → 2.12.0-beta.0
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/lib/dlob/DLOB.d.ts +15 -0
- package/lib/dlob/DLOB.js +30 -0
- package/lib/examples/makeTradeExample.js +2 -3
- package/lib/idl/drift.json +1 -1
- package/lib/math/trade.d.ts +1 -1
- package/package.json +1 -1
- package/src/dlob/DLOB.ts +61 -0
- package/src/examples/makeTradeExample.ts +3 -6
- package/src/idl/drift.json +1 -1
- package/src/math/trade.ts +1 -1
package/lib/dlob/DLOB.d.ts
CHANGED
|
@@ -64,7 +64,22 @@ export declare class DLOB {
|
|
|
64
64
|
getMarketAsks(marketIndex: number, marketType: MarketType): Generator<DLOBNode>;
|
|
65
65
|
private getBestNode;
|
|
66
66
|
getLimitAsks(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
|
|
67
|
+
/**
|
|
68
|
+
* Filters the limit asks that are post only or have been place for sufficiently long
|
|
69
|
+
* Useful for displaying order book that doesn't have taker limit orders crossing spread
|
|
70
|
+
*
|
|
71
|
+
* @returns
|
|
72
|
+
*/
|
|
73
|
+
getRestingLimitAsks(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData, minPerpAuctionDuration: number): Generator<DLOBNode>;
|
|
74
|
+
isRestingLimitOrder(order: Order, slot: number, minPerpAuctionDuration: number): boolean;
|
|
67
75
|
getLimitBids(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
|
|
76
|
+
/**
|
|
77
|
+
* Filters the limit bids that are post only or have been place for sufficiently long
|
|
78
|
+
* Useful for displaying order book that doesn't have taker limit orders crossing spread
|
|
79
|
+
*
|
|
80
|
+
* @returns
|
|
81
|
+
*/
|
|
82
|
+
getRestingLimitBids(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData, minPerpAuctionDuration: number): Generator<DLOBNode>;
|
|
68
83
|
getAsks(marketIndex: number, fallbackAsk: BN | undefined, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
|
|
69
84
|
getBids(marketIndex: number, fallbackBid: BN | undefined, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
|
|
70
85
|
findCrossingLimitOrders(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData, fallbackAsk: BN | undefined, fallbackBid: BN | undefined): NodeToFill[];
|
package/lib/dlob/DLOB.js
CHANGED
|
@@ -535,6 +535,23 @@ class DLOB {
|
|
|
535
535
|
return bestPrice.lt(currentPrice);
|
|
536
536
|
});
|
|
537
537
|
}
|
|
538
|
+
/**
|
|
539
|
+
* Filters the limit asks that are post only or have been place for sufficiently long
|
|
540
|
+
* Useful for displaying order book that doesn't have taker limit orders crossing spread
|
|
541
|
+
*
|
|
542
|
+
* @returns
|
|
543
|
+
*/
|
|
544
|
+
*getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData, minPerpAuctionDuration) {
|
|
545
|
+
for (const node of this.getLimitAsks(marketIndex, slot, marketType, oraclePriceData)) {
|
|
546
|
+
if (this.isRestingLimitOrder(node.order, slot, minPerpAuctionDuration)) {
|
|
547
|
+
yield node;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
isRestingLimitOrder(order, slot, minPerpAuctionDuration) {
|
|
552
|
+
return (order.postOnly ||
|
|
553
|
+
new __1.BN(slot).sub(order.slot).gte(new __1.BN(minPerpAuctionDuration * 1.5)));
|
|
554
|
+
}
|
|
538
555
|
*getLimitBids(marketIndex, slot, marketType, oraclePriceData) {
|
|
539
556
|
if ((0, __1.isVariant)(marketType, 'spot') && !oraclePriceData) {
|
|
540
557
|
throw new Error('Must provide OraclePriceData to get spot bids');
|
|
@@ -552,6 +569,19 @@ class DLOB {
|
|
|
552
569
|
return bestPrice.gt(currentPrice);
|
|
553
570
|
});
|
|
554
571
|
}
|
|
572
|
+
/**
|
|
573
|
+
* Filters the limit bids that are post only or have been place for sufficiently long
|
|
574
|
+
* Useful for displaying order book that doesn't have taker limit orders crossing spread
|
|
575
|
+
*
|
|
576
|
+
* @returns
|
|
577
|
+
*/
|
|
578
|
+
*getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData, minPerpAuctionDuration) {
|
|
579
|
+
for (const node of this.getLimitBids(marketIndex, slot, marketType, oraclePriceData)) {
|
|
580
|
+
if (this.isRestingLimitOrder(node.order, slot, minPerpAuctionDuration)) {
|
|
581
|
+
yield node;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
555
585
|
*getAsks(marketIndex, fallbackAsk, slot, marketType, oraclePriceData) {
|
|
556
586
|
if ((0, __1.isVariant)(marketType, 'spot') && !oraclePriceData) {
|
|
557
587
|
throw new Error('Must provide OraclePriceData to get spot asks');
|
|
@@ -36,8 +36,7 @@ const main = async () => {
|
|
|
36
36
|
connection,
|
|
37
37
|
wallet: provider.wallet,
|
|
38
38
|
programID: driftPublicKey,
|
|
39
|
-
|
|
40
|
-
spotMarketIndexes: spotMarkets_1.SpotMarkets[cluster].map((spotMarket) => spotMarket.marketIndex),
|
|
39
|
+
...(0, __2.getMarketsAndOraclesForSubscription)(cluster),
|
|
41
40
|
accountSubscription: {
|
|
42
41
|
type: 'polling',
|
|
43
42
|
accountLoader: bulkAccountLoader,
|
|
@@ -69,7 +68,7 @@ const main = async () => {
|
|
|
69
68
|
// Estimate the slippage for a $5000 LONG trade
|
|
70
69
|
const solMarketAccount = driftClient.getPerpMarketAccount(solMarketInfo.marketIndex);
|
|
71
70
|
const longAmount = new anchor_1.BN(5000).mul(__2.QUOTE_PRECISION);
|
|
72
|
-
const slippage = (0, __2.convertToNumber)((0, __2.calculateTradeSlippage)(__2.PositionDirection.LONG, longAmount, solMarketAccount, 'quote',
|
|
71
|
+
const slippage = (0, __2.convertToNumber)((0, __2.calculateTradeSlippage)(__2.PositionDirection.LONG, longAmount, solMarketAccount, 'quote', driftClient.getOracleDataForPerpMarket(solMarketInfo.marketIndex))[0], __2.PRICE_PRECISION);
|
|
73
72
|
console.log(`Slippage for a $5000 LONG on the SOL market would be $${slippage}`);
|
|
74
73
|
// Make a $5000 LONG trade
|
|
75
74
|
await driftClient.openPosition(__2.PositionDirection.LONG, longAmount, solMarketInfo.marketIndex);
|
package/lib/idl/drift.json
CHANGED
package/lib/math/trade.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export declare type PriceImpactUnit = 'entryPrice' | 'maxPrice' | 'priceDelta' |
|
|
|
21
21
|
*
|
|
22
22
|
* 'newPrice' => the price of the asset after the trade : Precision PRICE_PRECISION
|
|
23
23
|
*/
|
|
24
|
-
export declare function calculateTradeSlippage(direction: PositionDirection, amount: BN, market: PerpMarketAccount, inputAssetType
|
|
24
|
+
export declare function calculateTradeSlippage(direction: PositionDirection, amount: BN, market: PerpMarketAccount, inputAssetType: AssetType, oraclePriceData: OraclePriceData, useSpread?: boolean): [BN, BN, BN, BN];
|
|
25
25
|
/**
|
|
26
26
|
* Calculates acquired amounts for trade executed
|
|
27
27
|
* @param direction
|
package/package.json
CHANGED
package/src/dlob/DLOB.ts
CHANGED
|
@@ -915,6 +915,42 @@ export class DLOB {
|
|
|
915
915
|
);
|
|
916
916
|
}
|
|
917
917
|
|
|
918
|
+
/**
|
|
919
|
+
* Filters the limit asks that are post only or have been place for sufficiently long
|
|
920
|
+
* Useful for displaying order book that doesn't have taker limit orders crossing spread
|
|
921
|
+
*
|
|
922
|
+
* @returns
|
|
923
|
+
*/
|
|
924
|
+
*getRestingLimitAsks(
|
|
925
|
+
marketIndex: number,
|
|
926
|
+
slot: number,
|
|
927
|
+
marketType: MarketType,
|
|
928
|
+
oraclePriceData: OraclePriceData,
|
|
929
|
+
minPerpAuctionDuration: number
|
|
930
|
+
): Generator<DLOBNode> {
|
|
931
|
+
for (const node of this.getLimitAsks(
|
|
932
|
+
marketIndex,
|
|
933
|
+
slot,
|
|
934
|
+
marketType,
|
|
935
|
+
oraclePriceData
|
|
936
|
+
)) {
|
|
937
|
+
if (this.isRestingLimitOrder(node.order, slot, minPerpAuctionDuration)) {
|
|
938
|
+
yield node;
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
isRestingLimitOrder(
|
|
944
|
+
order: Order,
|
|
945
|
+
slot: number,
|
|
946
|
+
minPerpAuctionDuration: number
|
|
947
|
+
): boolean {
|
|
948
|
+
return (
|
|
949
|
+
order.postOnly ||
|
|
950
|
+
new BN(slot).sub(order.slot).gte(new BN(minPerpAuctionDuration * 1.5))
|
|
951
|
+
);
|
|
952
|
+
}
|
|
953
|
+
|
|
918
954
|
*getLimitBids(
|
|
919
955
|
marketIndex: number,
|
|
920
956
|
slot: number,
|
|
@@ -947,6 +983,31 @@ export class DLOB {
|
|
|
947
983
|
);
|
|
948
984
|
}
|
|
949
985
|
|
|
986
|
+
/**
|
|
987
|
+
* Filters the limit bids that are post only or have been place for sufficiently long
|
|
988
|
+
* Useful for displaying order book that doesn't have taker limit orders crossing spread
|
|
989
|
+
*
|
|
990
|
+
* @returns
|
|
991
|
+
*/
|
|
992
|
+
*getRestingLimitBids(
|
|
993
|
+
marketIndex: number,
|
|
994
|
+
slot: number,
|
|
995
|
+
marketType: MarketType,
|
|
996
|
+
oraclePriceData: OraclePriceData,
|
|
997
|
+
minPerpAuctionDuration: number
|
|
998
|
+
): Generator<DLOBNode> {
|
|
999
|
+
for (const node of this.getLimitBids(
|
|
1000
|
+
marketIndex,
|
|
1001
|
+
slot,
|
|
1002
|
+
marketType,
|
|
1003
|
+
oraclePriceData
|
|
1004
|
+
)) {
|
|
1005
|
+
if (this.isRestingLimitOrder(node.order, slot, minPerpAuctionDuration)) {
|
|
1006
|
+
yield node;
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
|
|
950
1011
|
*getAsks(
|
|
951
1012
|
marketIndex: number,
|
|
952
1013
|
fallbackAsk: BN | undefined,
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
convertToNumber,
|
|
12
12
|
calculateTradeSlippage,
|
|
13
13
|
BulkAccountLoader,
|
|
14
|
-
|
|
14
|
+
getMarketsAndOraclesForSubscription,
|
|
15
15
|
PRICE_PRECISION,
|
|
16
16
|
QUOTE_PRECISION,
|
|
17
17
|
} from '..';
|
|
@@ -74,10 +74,7 @@ const main = async () => {
|
|
|
74
74
|
connection,
|
|
75
75
|
wallet: provider.wallet,
|
|
76
76
|
programID: driftPublicKey,
|
|
77
|
-
|
|
78
|
-
spotMarketIndexes: SpotMarkets[cluster].map(
|
|
79
|
-
(spotMarket) => spotMarket.marketIndex
|
|
80
|
-
),
|
|
77
|
+
...getMarketsAndOraclesForSubscription(cluster),
|
|
81
78
|
accountSubscription: {
|
|
82
79
|
type: 'polling',
|
|
83
80
|
accountLoader: bulkAccountLoader,
|
|
@@ -139,7 +136,7 @@ const main = async () => {
|
|
|
139
136
|
longAmount,
|
|
140
137
|
solMarketAccount,
|
|
141
138
|
'quote',
|
|
142
|
-
|
|
139
|
+
driftClient.getOracleDataForPerpMarket(solMarketInfo.marketIndex)
|
|
143
140
|
)[0],
|
|
144
141
|
PRICE_PRECISION
|
|
145
142
|
);
|
package/src/idl/drift.json
CHANGED
package/src/math/trade.ts
CHANGED
|
@@ -61,7 +61,7 @@ export function calculateTradeSlippage(
|
|
|
61
61
|
amount: BN,
|
|
62
62
|
market: PerpMarketAccount,
|
|
63
63
|
inputAssetType: AssetType = 'quote',
|
|
64
|
-
oraclePriceData
|
|
64
|
+
oraclePriceData: OraclePriceData,
|
|
65
65
|
useSpread = true
|
|
66
66
|
): [BN, BN, BN, BN] {
|
|
67
67
|
let oldPrice: BN;
|