@drift-labs/sdk 2.41.0-beta.0 → 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/README.md +0 -1
- package/VERSION +1 -1
- package/bun.lockb +0 -0
- package/lib/accounts/types.d.ts +5 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -1
- package/lib/accounts/webSocketAccountSubscriber.js +28 -2
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +2 -1
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +6 -5
- package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +32 -0
- package/lib/accounts/webSocketProgramAccountSubscriber.js +99 -0
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +2 -1
- package/lib/accounts/webSocketUserAccountSubscriber.js +3 -2
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +2 -1
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +3 -2
- 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/auctionSubscriber/auctionSubscriber.d.ts +3 -2
- package/lib/auctionSubscriber/auctionSubscriber.js +15 -7
- package/lib/auctionSubscriber/types.d.ts +1 -0
- 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/driftClient.d.ts +2 -1
- package/lib/driftClient.js +5 -4
- package/lib/driftClientConfig.d.ts +1 -0
- package/lib/factory/bigNum.js +4 -2
- package/lib/idl/drift.json +222 -2
- package/lib/jupiter/jupiterClient.d.ts +4 -1
- package/lib/jupiter/jupiterClient.js +4 -1
- package/lib/math/amm.js +7 -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.d.ts +51 -4
- package/lib/math/superStake.js +173 -21
- package/lib/math/trade.js +2 -4
- package/lib/math/utils.d.ts +1 -0
- package/lib/math/utils.js +10 -1
- package/lib/orderSubscriber/OrderSubscriber.d.ts +1 -3
- package/lib/orderSubscriber/OrderSubscriber.js +4 -3
- package/lib/orderSubscriber/WebsocketSubscription.d.ts +4 -2
- package/lib/orderSubscriber/WebsocketSubscription.js +15 -12
- package/lib/orderSubscriber/types.d.ts +1 -0
- package/lib/user.d.ts +2 -2
- package/lib/user.js +5 -5
- package/package.json +2 -1
- package/src/accounts/types.ts +8 -1
- package/src/accounts/webSocketAccountSubscriber.ts +36 -2
- package/src/accounts/webSocketDriftClientAccountSubscriber.ts +15 -5
- package/src/accounts/webSocketProgramAccountSubscriber.ts +152 -0
- package/src/accounts/webSocketUserAccountSubscriber.ts +10 -2
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +10 -2
- package/src/addresses/pda.ts +9 -0
- package/src/adminClient.ts +32 -0
- package/src/auctionSubscriber/auctionSubscriber.ts +30 -21
- package/src/auctionSubscriber/types.ts +1 -0
- package/src/dlob/DLOB.ts +9 -32
- package/src/dlob/DLOBSubscriber.ts +8 -7
- package/src/dlob/orderBookLevels.ts +133 -32
- package/src/driftClient.ts +5 -1
- package/src/driftClientConfig.ts +1 -0
- package/src/factory/bigNum.ts +2 -0
- package/src/idl/drift.json +222 -2
- package/src/jupiter/jupiterClient.ts +6 -0
- package/src/math/amm.ts +9 -2
- package/src/math/auction.ts +36 -2
- package/src/math/market.ts +4 -9
- package/src/math/superStake.ts +247 -23
- package/src/math/trade.ts +3 -11
- package/src/math/utils.ts +12 -0
- package/src/orderSubscriber/OrderSubscriber.ts +12 -7
- package/src/orderSubscriber/WebsocketSubscription.ts +34 -23
- package/src/orderSubscriber/types.ts +1 -0
- package/src/user.ts +7 -5
- package/tests/amm/test.ts +402 -0
- package/tests/auctions/test.ts +66 -0
- package/tests/dlob/test.ts +1 -73
|
@@ -5,7 +5,6 @@ const DLOB_1 = require("./DLOB");
|
|
|
5
5
|
const events_1 = require("events");
|
|
6
6
|
const types_1 = require("../types");
|
|
7
7
|
const orderBookLevels_1 = require("./orderBookLevels");
|
|
8
|
-
const market_1 = require("../math/market");
|
|
9
8
|
class DLOBSubscriber {
|
|
10
9
|
constructor(config) {
|
|
11
10
|
this.dlob = new DLOB_1.DLOB();
|
|
@@ -61,25 +60,25 @@ class DLOBSubscriber {
|
|
|
61
60
|
}
|
|
62
61
|
}
|
|
63
62
|
let oraclePriceData;
|
|
64
|
-
let fallbackBid;
|
|
65
|
-
let fallbackAsk;
|
|
66
63
|
const isPerp = (0, types_1.isVariant)(marketType, 'perp');
|
|
67
64
|
if (isPerp) {
|
|
68
65
|
const perpMarketAccount = this.driftClient.getPerpMarketAccount(marketIndex);
|
|
69
66
|
oraclePriceData = this.driftClient.getOracleDataForPerpMarket(perpMarketAccount.marketIndex);
|
|
70
|
-
fallbackBid = (0, market_1.calculateBidPrice)(perpMarketAccount, oraclePriceData);
|
|
71
|
-
fallbackAsk = (0, market_1.calculateAskPrice)(perpMarketAccount, oraclePriceData);
|
|
72
67
|
}
|
|
73
68
|
else {
|
|
74
69
|
oraclePriceData =
|
|
75
70
|
this.driftClient.getOracleDataForSpotMarket(marketIndex);
|
|
76
71
|
}
|
|
77
72
|
if (isPerp && includeVamm) {
|
|
73
|
+
if (fallbackL2Generators.length > 0) {
|
|
74
|
+
throw new Error('includeVamm can only be used if fallbackL2Generators is empty');
|
|
75
|
+
}
|
|
78
76
|
fallbackL2Generators = [
|
|
79
77
|
(0, orderBookLevels_1.getVammL2Generator)({
|
|
80
78
|
marketAccount: this.driftClient.getPerpMarketAccount(marketIndex),
|
|
81
79
|
oraclePriceData,
|
|
82
80
|
numOrders: numVammOrders !== null && numVammOrders !== void 0 ? numVammOrders : depth,
|
|
81
|
+
topOfBookQuoteAmounts: orderBookLevels_1.DEFAULT_TOP_OF_BOOK_QUOTE_AMOUNTS,
|
|
83
82
|
}),
|
|
84
83
|
];
|
|
85
84
|
}
|
|
@@ -89,8 +88,6 @@ class DLOBSubscriber {
|
|
|
89
88
|
depth,
|
|
90
89
|
oraclePriceData,
|
|
91
90
|
slot: this.slotSource.getSlot(),
|
|
92
|
-
fallbackBid,
|
|
93
|
-
fallbackAsk,
|
|
94
91
|
fallbackL2Generators: fallbackL2Generators,
|
|
95
92
|
});
|
|
96
93
|
}
|
|
@@ -26,20 +26,22 @@ export type L3OrderBook = {
|
|
|
26
26
|
asks: L3Level[];
|
|
27
27
|
bids: L3Level[];
|
|
28
28
|
};
|
|
29
|
+
export declare const DEFAULT_TOP_OF_BOOK_QUOTE_AMOUNTS: any[];
|
|
29
30
|
/**
|
|
30
31
|
* Get an {@link Generator<L2Level>} generator from a {@link Generator<DLOBNode>}
|
|
31
|
-
* @param dlobNodes e.g. {@link DLOB#
|
|
32
|
+
* @param dlobNodes e.g. {@link DLOB#getRestingLimitAsks} or {@link DLOB#getRestingLimitBids}
|
|
32
33
|
* @param oraclePriceData
|
|
33
34
|
* @param slot
|
|
34
35
|
*/
|
|
35
36
|
export declare function getL2GeneratorFromDLOBNodes(dlobNodes: Generator<DLOBNode>, oraclePriceData: OraclePriceData, slot: number): Generator<L2Level>;
|
|
36
37
|
export declare function mergeL2LevelGenerators(l2LevelGenerators: Generator<L2Level>[], compare: (a: L2Level, b: L2Level) => boolean): Generator<L2Level>;
|
|
37
38
|
export declare function createL2Levels(generator: Generator<L2Level>, depth: number): L2Level[];
|
|
38
|
-
export declare function getVammL2Generator({ marketAccount, oraclePriceData, numOrders, now, }: {
|
|
39
|
+
export declare function getVammL2Generator({ marketAccount, oraclePriceData, numOrders, now, topOfBookQuoteAmounts, }: {
|
|
39
40
|
marketAccount: PerpMarketAccount;
|
|
40
41
|
oraclePriceData: OraclePriceData;
|
|
41
42
|
numOrders: number;
|
|
42
43
|
now?: BN;
|
|
44
|
+
topOfBookQuoteAmounts?: BN[];
|
|
43
45
|
}): L2OrderBookGenerator;
|
|
44
46
|
export declare function groupL2(l2: L2OrderBook, grouping: BN, depth: number): L2OrderBook;
|
|
45
47
|
export {};
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.groupL2 = exports.getVammL2Generator = exports.createL2Levels = exports.mergeL2LevelGenerators = exports.getL2GeneratorFromDLOBNodes = void 0;
|
|
3
|
+
exports.groupL2 = exports.getVammL2Generator = exports.createL2Levels = exports.mergeL2LevelGenerators = exports.getL2GeneratorFromDLOBNodes = exports.DEFAULT_TOP_OF_BOOK_QUOTE_AMOUNTS = void 0;
|
|
4
4
|
const __1 = require("..");
|
|
5
|
+
const assert_1 = require("../assert/assert");
|
|
6
|
+
exports.DEFAULT_TOP_OF_BOOK_QUOTE_AMOUNTS = [
|
|
7
|
+
new __1.BN(500).mul(__1.QUOTE_PRECISION),
|
|
8
|
+
new __1.BN(1000).mul(__1.QUOTE_PRECISION),
|
|
9
|
+
new __1.BN(2000).mul(__1.QUOTE_PRECISION),
|
|
10
|
+
new __1.BN(5000).mul(__1.QUOTE_PRECISION),
|
|
11
|
+
];
|
|
5
12
|
/**
|
|
6
13
|
* Get an {@link Generator<L2Level>} generator from a {@link Generator<DLOBNode>}
|
|
7
|
-
* @param dlobNodes e.g. {@link DLOB#
|
|
14
|
+
* @param dlobNodes e.g. {@link DLOB#getRestingLimitAsks} or {@link DLOB#getRestingLimitBids}
|
|
8
15
|
* @param oraclePriceData
|
|
9
16
|
* @param slot
|
|
10
17
|
*/
|
|
@@ -78,13 +85,19 @@ function createL2Levels(generator, depth) {
|
|
|
78
85
|
return levels;
|
|
79
86
|
}
|
|
80
87
|
exports.createL2Levels = createL2Levels;
|
|
81
|
-
function getVammL2Generator({ marketAccount, oraclePriceData, numOrders, now, }) {
|
|
88
|
+
function getVammL2Generator({ marketAccount, oraclePriceData, numOrders, now, topOfBookQuoteAmounts, }) {
|
|
89
|
+
let numBaseOrders = numOrders;
|
|
90
|
+
if (topOfBookQuoteAmounts) {
|
|
91
|
+
numBaseOrders = numOrders - topOfBookQuoteAmounts.length;
|
|
92
|
+
(0, assert_1.assert)(topOfBookQuoteAmounts.length < numOrders);
|
|
93
|
+
}
|
|
82
94
|
const updatedAmm = (0, __1.calculateUpdatedAMM)(marketAccount.amm, oraclePriceData);
|
|
83
95
|
const [openBids, openAsks] = (0, __1.calculateMarketOpenBidAsk)(updatedAmm.baseAssetReserve, updatedAmm.minBaseAssetReserve, updatedAmm.maxBaseAssetReserve, updatedAmm.orderStepSize);
|
|
84
96
|
now = now !== null && now !== void 0 ? now : new __1.BN(Date.now() / 1000);
|
|
85
97
|
const [bidReserves, askReserves] = (0, __1.calculateSpreadReserves)(updatedAmm, oraclePriceData, now);
|
|
86
98
|
let numBids = 0;
|
|
87
|
-
|
|
99
|
+
let topOfBookBidSize = __1.ZERO;
|
|
100
|
+
let bidSize = openBids.div(new __1.BN(numBaseOrders));
|
|
88
101
|
const bidAmm = {
|
|
89
102
|
baseAssetReserve: bidReserves.baseAssetReserve,
|
|
90
103
|
quoteAssetReserve: bidReserves.quoteAssetReserve,
|
|
@@ -92,22 +105,45 @@ function getVammL2Generator({ marketAccount, oraclePriceData, numOrders, now, })
|
|
|
92
105
|
pegMultiplier: updatedAmm.pegMultiplier,
|
|
93
106
|
};
|
|
94
107
|
const getL2Bids = function* () {
|
|
95
|
-
while (numBids < numOrders &&
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
108
|
+
while (numBids < numOrders && bidSize.gt(__1.ZERO)) {
|
|
109
|
+
let quoteSwapped = __1.ZERO;
|
|
110
|
+
let baseSwapped = __1.ZERO;
|
|
111
|
+
let [afterSwapQuoteReserves, afterSwapBaseReserves] = [__1.ZERO, __1.ZERO];
|
|
112
|
+
if (topOfBookQuoteAmounts && numBids < (topOfBookQuoteAmounts === null || topOfBookQuoteAmounts === void 0 ? void 0 : topOfBookQuoteAmounts.length)) {
|
|
113
|
+
const remainingBaseLiquidity = openBids.sub(topOfBookBidSize);
|
|
114
|
+
quoteSwapped = topOfBookQuoteAmounts[numBids];
|
|
115
|
+
[afterSwapQuoteReserves, afterSwapBaseReserves] =
|
|
116
|
+
(0, __1.calculateAmmReservesAfterSwap)(bidAmm, 'quote', quoteSwapped, __1.SwapDirection.REMOVE);
|
|
117
|
+
baseSwapped = bidAmm.baseAssetReserve.sub(afterSwapBaseReserves).abs();
|
|
118
|
+
if (remainingBaseLiquidity.lt(baseSwapped)) {
|
|
119
|
+
baseSwapped = remainingBaseLiquidity;
|
|
120
|
+
[afterSwapQuoteReserves, afterSwapBaseReserves] =
|
|
121
|
+
(0, __1.calculateAmmReservesAfterSwap)(bidAmm, 'base', baseSwapped, __1.SwapDirection.ADD);
|
|
122
|
+
quoteSwapped = (0, __1.calculateQuoteAssetAmountSwapped)(bidAmm.quoteAssetReserve.sub(afterSwapQuoteReserves).abs(), bidAmm.pegMultiplier, __1.SwapDirection.ADD);
|
|
123
|
+
}
|
|
124
|
+
topOfBookBidSize = topOfBookBidSize.add(baseSwapped);
|
|
125
|
+
bidSize = openBids.sub(topOfBookBidSize).div(new __1.BN(numBaseOrders));
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
baseSwapped = bidSize;
|
|
129
|
+
[afterSwapQuoteReserves, afterSwapBaseReserves] =
|
|
130
|
+
(0, __1.calculateAmmReservesAfterSwap)(bidAmm, 'base', baseSwapped, __1.SwapDirection.ADD);
|
|
131
|
+
quoteSwapped = (0, __1.calculateQuoteAssetAmountSwapped)(bidAmm.quoteAssetReserve.sub(afterSwapQuoteReserves).abs(), bidAmm.pegMultiplier, __1.SwapDirection.ADD);
|
|
132
|
+
}
|
|
133
|
+
const price = quoteSwapped.mul(__1.BASE_PRECISION).div(baseSwapped);
|
|
99
134
|
bidAmm.baseAssetReserve = afterSwapBaseReserves;
|
|
100
135
|
bidAmm.quoteAssetReserve = afterSwapQuoteReserves;
|
|
101
136
|
yield {
|
|
102
137
|
price,
|
|
103
|
-
size:
|
|
104
|
-
sources: { vamm:
|
|
138
|
+
size: baseSwapped,
|
|
139
|
+
sources: { vamm: baseSwapped },
|
|
105
140
|
};
|
|
106
141
|
numBids++;
|
|
107
142
|
}
|
|
108
143
|
};
|
|
109
144
|
let numAsks = 0;
|
|
110
|
-
|
|
145
|
+
let topOfBookAskSize = __1.ZERO;
|
|
146
|
+
let askSize = openAsks.abs().div(new __1.BN(numBaseOrders));
|
|
111
147
|
const askAmm = {
|
|
112
148
|
baseAssetReserve: askReserves.baseAssetReserve,
|
|
113
149
|
quoteAssetReserve: askReserves.quoteAssetReserve,
|
|
@@ -116,15 +152,42 @@ function getVammL2Generator({ marketAccount, oraclePriceData, numOrders, now, })
|
|
|
116
152
|
};
|
|
117
153
|
const getL2Asks = function* () {
|
|
118
154
|
while (numAsks < numOrders && askSize.gt(__1.ZERO)) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
155
|
+
let quoteSwapped = __1.ZERO;
|
|
156
|
+
let baseSwapped = __1.ZERO;
|
|
157
|
+
let [afterSwapQuoteReserves, afterSwapBaseReserves] = [__1.ZERO, __1.ZERO];
|
|
158
|
+
if (topOfBookQuoteAmounts && numAsks < (topOfBookQuoteAmounts === null || topOfBookQuoteAmounts === void 0 ? void 0 : topOfBookQuoteAmounts.length)) {
|
|
159
|
+
const remainingBaseLiquidity = openAsks
|
|
160
|
+
.mul(new __1.BN(-1))
|
|
161
|
+
.sub(topOfBookAskSize);
|
|
162
|
+
quoteSwapped = topOfBookQuoteAmounts[numAsks];
|
|
163
|
+
[afterSwapQuoteReserves, afterSwapBaseReserves] =
|
|
164
|
+
(0, __1.calculateAmmReservesAfterSwap)(askAmm, 'quote', quoteSwapped, __1.SwapDirection.ADD);
|
|
165
|
+
baseSwapped = askAmm.baseAssetReserve.sub(afterSwapBaseReserves).abs();
|
|
166
|
+
if (remainingBaseLiquidity.lt(baseSwapped)) {
|
|
167
|
+
baseSwapped = remainingBaseLiquidity;
|
|
168
|
+
[afterSwapQuoteReserves, afterSwapBaseReserves] =
|
|
169
|
+
(0, __1.calculateAmmReservesAfterSwap)(bidAmm, 'base', baseSwapped, __1.SwapDirection.REMOVE);
|
|
170
|
+
quoteSwapped = (0, __1.calculateQuoteAssetAmountSwapped)(bidAmm.quoteAssetReserve.sub(afterSwapQuoteReserves).abs(), bidAmm.pegMultiplier, __1.SwapDirection.REMOVE);
|
|
171
|
+
}
|
|
172
|
+
topOfBookAskSize = topOfBookAskSize.add(baseSwapped);
|
|
173
|
+
askSize = openAsks
|
|
174
|
+
.abs()
|
|
175
|
+
.sub(topOfBookAskSize)
|
|
176
|
+
.div(new __1.BN(numBaseOrders));
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
baseSwapped = askSize;
|
|
180
|
+
[afterSwapQuoteReserves, afterSwapBaseReserves] =
|
|
181
|
+
(0, __1.calculateAmmReservesAfterSwap)(askAmm, 'base', askSize, __1.SwapDirection.REMOVE);
|
|
182
|
+
quoteSwapped = (0, __1.calculateQuoteAssetAmountSwapped)(askAmm.quoteAssetReserve.sub(afterSwapQuoteReserves).abs(), askAmm.pegMultiplier, __1.SwapDirection.REMOVE);
|
|
183
|
+
}
|
|
184
|
+
const price = quoteSwapped.mul(__1.BASE_PRECISION).div(baseSwapped);
|
|
122
185
|
askAmm.baseAssetReserve = afterSwapBaseReserves;
|
|
123
186
|
askAmm.quoteAssetReserve = afterSwapQuoteReserves;
|
|
124
187
|
yield {
|
|
125
188
|
price,
|
|
126
|
-
size:
|
|
127
|
-
sources: { vamm:
|
|
189
|
+
size: baseSwapped,
|
|
190
|
+
sources: { vamm: baseSwapped },
|
|
128
191
|
};
|
|
129
192
|
numAsks++;
|
|
130
193
|
}
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -319,7 +319,7 @@ export declare class DriftClient {
|
|
|
319
319
|
* @param reduceOnly specify if In or Out token on the drift account must reduceOnly, checked at end of swap
|
|
320
320
|
* @param txParams
|
|
321
321
|
*/
|
|
322
|
-
swap({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, route, reduceOnly, txParams, v6, }: {
|
|
322
|
+
swap({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, route, reduceOnly, txParams, v6, onlyDirectRoutes, }: {
|
|
323
323
|
jupiterClient: JupiterClient;
|
|
324
324
|
outMarketIndex: number;
|
|
325
325
|
inMarketIndex: number;
|
|
@@ -331,6 +331,7 @@ export declare class DriftClient {
|
|
|
331
331
|
route?: Route;
|
|
332
332
|
reduceOnly?: SwapReduceOnly;
|
|
333
333
|
txParams?: TxParams;
|
|
334
|
+
onlyDirectRoutes?: boolean;
|
|
334
335
|
v6?: {
|
|
335
336
|
quote?: QuoteResponse;
|
|
336
337
|
};
|
package/lib/driftClient.js
CHANGED
|
@@ -66,7 +66,7 @@ class DriftClient {
|
|
|
66
66
|
this._isSubscribed = val;
|
|
67
67
|
}
|
|
68
68
|
constructor(config) {
|
|
69
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
|
|
69
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
70
70
|
this.users = new Map();
|
|
71
71
|
this._isSubscribed = false;
|
|
72
72
|
this.perpMarketLastSlotCache = new Map();
|
|
@@ -139,11 +139,11 @@ class DriftClient {
|
|
|
139
139
|
this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (_o = config.perpMarketIndexes) !== null && _o !== void 0 ? _o : [], (_p = config.spotMarketIndexes) !== null && _p !== void 0 ? _p : [], (_q = config.oracleInfos) !== null && _q !== void 0 ? _q : [], noMarketsAndOraclesSpecified);
|
|
140
140
|
}
|
|
141
141
|
else {
|
|
142
|
-
this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (_r = config.perpMarketIndexes) !== null && _r !== void 0 ? _r : [], (_s = config.spotMarketIndexes) !== null && _s !== void 0 ? _s : [], (_t = config.oracleInfos) !== null && _t !== void 0 ? _t : [], noMarketsAndOraclesSpecified);
|
|
142
|
+
this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (_r = config.perpMarketIndexes) !== null && _r !== void 0 ? _r : [], (_s = config.spotMarketIndexes) !== null && _s !== void 0 ? _s : [], (_t = config.oracleInfos) !== null && _t !== void 0 ? _t : [], noMarketsAndOraclesSpecified, (_u = config.accountSubscription) === null || _u === void 0 ? void 0 : _u.resubTimeoutMs);
|
|
143
143
|
}
|
|
144
144
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
145
145
|
this.txSender =
|
|
146
|
-
(
|
|
146
|
+
(_v = config.txSender) !== null && _v !== void 0 ? _v : new retryTxSender_1.RetryTxSender({
|
|
147
147
|
connection: this.connection,
|
|
148
148
|
wallet: this.wallet,
|
|
149
149
|
opts: this.opts,
|
|
@@ -1975,7 +1975,7 @@ class DriftClient {
|
|
|
1975
1975
|
* @param reduceOnly specify if In or Out token on the drift account must reduceOnly, checked at end of swap
|
|
1976
1976
|
* @param txParams
|
|
1977
1977
|
*/
|
|
1978
|
-
async swap({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, route, reduceOnly, txParams, v6, }) {
|
|
1978
|
+
async swap({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, route, reduceOnly, txParams, v6, onlyDirectRoutes = false, }) {
|
|
1979
1979
|
let ixs;
|
|
1980
1980
|
let lookupTables;
|
|
1981
1981
|
if (v6) {
|
|
@@ -1990,6 +1990,7 @@ class DriftClient {
|
|
|
1990
1990
|
swapMode,
|
|
1991
1991
|
quote: v6.quote,
|
|
1992
1992
|
reduceOnly,
|
|
1993
|
+
onlyDirectRoutes,
|
|
1993
1994
|
});
|
|
1994
1995
|
ixs = res.ixs;
|
|
1995
1996
|
lookupTables = res.lookupTables;
|
package/lib/factory/bigNum.js
CHANGED
|
@@ -419,19 +419,21 @@ class BigNum {
|
|
|
419
419
|
* @returns
|
|
420
420
|
*/
|
|
421
421
|
static fromPrint(val, precisionShift) {
|
|
422
|
-
var _a;
|
|
422
|
+
var _a, _b;
|
|
423
423
|
// Handle empty number edge cases
|
|
424
424
|
if (!val)
|
|
425
425
|
return BigNum.from(numericConstants_1.ZERO, precisionShift);
|
|
426
426
|
if (!val.replace(BigNum.delim, '')) {
|
|
427
427
|
return BigNum.from(numericConstants_1.ZERO, precisionShift);
|
|
428
428
|
}
|
|
429
|
+
if (val.includes('e'))
|
|
430
|
+
val = (+val).toFixed((_a = precisionShift === null || precisionShift === void 0 ? void 0 : precisionShift.toNumber()) !== null && _a !== void 0 ? _a : 9); // prevent small numbers e.g. 3.1e-8, use assume max precision 9 as default
|
|
429
431
|
const sides = val.split(BigNum.delim);
|
|
430
432
|
const rightSide = sides[1];
|
|
431
433
|
const leftSide = sides[0].replace(/\s/g, '');
|
|
432
434
|
const bnInput = `${leftSide !== null && leftSide !== void 0 ? leftSide : ''}${rightSide !== null && rightSide !== void 0 ? rightSide : ''}`;
|
|
433
435
|
const rawBn = new anchor_1.BN(bnInput);
|
|
434
|
-
const rightSideLength = (
|
|
436
|
+
const rightSideLength = (_b = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _b !== void 0 ? _b : 0;
|
|
435
437
|
const totalShift = precisionShift
|
|
436
438
|
? precisionShift.sub(new anchor_1.BN(rightSideLength))
|
|
437
439
|
: numericConstants_1.ZERO;
|
package/lib/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": [
|
|
@@ -6314,6 +6477,24 @@
|
|
|
6314
6477
|
]
|
|
6315
6478
|
}
|
|
6316
6479
|
},
|
|
6480
|
+
{
|
|
6481
|
+
"name": "MarketIdentifier",
|
|
6482
|
+
"type": {
|
|
6483
|
+
"kind": "struct",
|
|
6484
|
+
"fields": [
|
|
6485
|
+
{
|
|
6486
|
+
"name": "marketType",
|
|
6487
|
+
"type": {
|
|
6488
|
+
"defined": "MarketType"
|
|
6489
|
+
}
|
|
6490
|
+
},
|
|
6491
|
+
{
|
|
6492
|
+
"name": "marketIndex",
|
|
6493
|
+
"type": "u16"
|
|
6494
|
+
}
|
|
6495
|
+
]
|
|
6496
|
+
}
|
|
6497
|
+
},
|
|
6317
6498
|
{
|
|
6318
6499
|
"name": "HistoricalOracleData",
|
|
6319
6500
|
"type": {
|
|
@@ -8157,6 +8338,12 @@
|
|
|
8157
8338
|
},
|
|
8158
8339
|
{
|
|
8159
8340
|
"name": "Unstake"
|
|
8341
|
+
},
|
|
8342
|
+
{
|
|
8343
|
+
"name": "UnstakeTransfer"
|
|
8344
|
+
},
|
|
8345
|
+
{
|
|
8346
|
+
"name": "StakeTransfer"
|
|
8160
8347
|
}
|
|
8161
8348
|
]
|
|
8162
8349
|
}
|
|
@@ -8198,6 +8385,34 @@
|
|
|
8198
8385
|
]
|
|
8199
8386
|
}
|
|
8200
8387
|
},
|
|
8388
|
+
{
|
|
8389
|
+
"name": "MarginCalculationMode",
|
|
8390
|
+
"type": {
|
|
8391
|
+
"kind": "enum",
|
|
8392
|
+
"variants": [
|
|
8393
|
+
{
|
|
8394
|
+
"name": "Standard"
|
|
8395
|
+
},
|
|
8396
|
+
{
|
|
8397
|
+
"name": "Liquidation",
|
|
8398
|
+
"fields": [
|
|
8399
|
+
{
|
|
8400
|
+
"name": "margin_buffer",
|
|
8401
|
+
"type": "u128"
|
|
8402
|
+
},
|
|
8403
|
+
{
|
|
8404
|
+
"name": "market_to_track_margin_requirement",
|
|
8405
|
+
"type": {
|
|
8406
|
+
"option": {
|
|
8407
|
+
"defined": "MarketIdentifier"
|
|
8408
|
+
}
|
|
8409
|
+
}
|
|
8410
|
+
}
|
|
8411
|
+
]
|
|
8412
|
+
}
|
|
8413
|
+
]
|
|
8414
|
+
}
|
|
8415
|
+
},
|
|
8201
8416
|
{
|
|
8202
8417
|
"name": "OracleSource",
|
|
8203
8418
|
"type": {
|
|
@@ -10781,6 +10996,11 @@
|
|
|
10781
10996
|
"code": 6254,
|
|
10782
10997
|
"name": "UserReduceOnly",
|
|
10783
10998
|
"msg": "UserReduceOnly"
|
|
10999
|
+
},
|
|
11000
|
+
{
|
|
11001
|
+
"code": 6255,
|
|
11002
|
+
"name": "InvalidMarginCalculation",
|
|
11003
|
+
"msg": "InvalidMarginCalculation"
|
|
10784
11004
|
}
|
|
10785
11005
|
]
|
|
10786
11006
|
}
|
|
@@ -229,13 +229,16 @@ export declare class JupiterClient {
|
|
|
229
229
|
* @param swapMode the swap mode (ExactIn or ExactOut)
|
|
230
230
|
* @param onlyDirectRoutes whether to only return direct routes
|
|
231
231
|
*/
|
|
232
|
-
getQuote({ inputMint, outputMint, amount,
|
|
232
|
+
getQuote({ inputMint, outputMint, amount, maxAccounts, // 50 is an estimated amount with buffer
|
|
233
|
+
slippageBps, swapMode, onlyDirectRoutes, excludeDexes, }: {
|
|
233
234
|
inputMint: PublicKey;
|
|
234
235
|
outputMint: PublicKey;
|
|
235
236
|
amount: BN;
|
|
237
|
+
maxAccounts?: number;
|
|
236
238
|
slippageBps?: number;
|
|
237
239
|
swapMode?: SwapMode;
|
|
238
240
|
onlyDirectRoutes?: boolean;
|
|
241
|
+
excludeDexes?: string[];
|
|
239
242
|
}): Promise<QuoteResponse>;
|
|
240
243
|
/**
|
|
241
244
|
* Get a swap transaction for quote
|
|
@@ -43,7 +43,8 @@ class JupiterClient {
|
|
|
43
43
|
* @param swapMode the swap mode (ExactIn or ExactOut)
|
|
44
44
|
* @param onlyDirectRoutes whether to only return direct routes
|
|
45
45
|
*/
|
|
46
|
-
async getQuote({ inputMint, outputMint, amount,
|
|
46
|
+
async getQuote({ inputMint, outputMint, amount, maxAccounts = 50, // 50 is an estimated amount with buffer
|
|
47
|
+
slippageBps = 50, swapMode = 'ExactIn', onlyDirectRoutes = false, excludeDexes = [], }) {
|
|
47
48
|
const params = new URLSearchParams({
|
|
48
49
|
inputMint: inputMint.toString(),
|
|
49
50
|
outputMint: outputMint.toString(),
|
|
@@ -51,6 +52,8 @@ class JupiterClient {
|
|
|
51
52
|
slippageBps: slippageBps.toString(),
|
|
52
53
|
swapMode,
|
|
53
54
|
onlyDirectRoutes: onlyDirectRoutes.toString(),
|
|
55
|
+
maxAccounts: maxAccounts.toString(),
|
|
56
|
+
excludeDexes: excludeDexes.join(','),
|
|
54
57
|
}).toString();
|
|
55
58
|
const quote = await (await (0, node_fetch_1.default)(`${this.url}/v6/quote?${params}`)).json();
|
|
56
59
|
return quote;
|
package/lib/math/amm.js
CHANGED
|
@@ -253,8 +253,13 @@ function calculateVolSpreadBN(lastOracleConfPct, reservePrice, markStd, oracleSt
|
|
|
253
253
|
const clampMax = numericConstants_1.PERCENTAGE_PRECISION.mul(new anchor_1.BN(16)).div(new anchor_1.BN(10));
|
|
254
254
|
const longVolSpreadFactor = (0, __1.clampBN)(longIntensity.mul(numericConstants_1.PERCENTAGE_PRECISION).div(anchor_1.BN.max(numericConstants_1.ONE, volume24H)), clampMin, clampMax);
|
|
255
255
|
const shortVolSpreadFactor = (0, __1.clampBN)(shortIntensity.mul(numericConstants_1.PERCENTAGE_PRECISION).div(anchor_1.BN.max(numericConstants_1.ONE, volume24H)), clampMin, clampMax);
|
|
256
|
-
|
|
257
|
-
|
|
256
|
+
// only consider confidence interval at full value when above 25 bps
|
|
257
|
+
let confComponent = lastOracleConfPct;
|
|
258
|
+
if (lastOracleConfPct.lte(numericConstants_1.PRICE_PRECISION.div(new anchor_1.BN(400)))) {
|
|
259
|
+
confComponent = lastOracleConfPct.div(new anchor_1.BN(10));
|
|
260
|
+
}
|
|
261
|
+
const longVolSpread = anchor_1.BN.max(confComponent, volSpread.mul(longVolSpreadFactor).div(numericConstants_1.PERCENTAGE_PRECISION));
|
|
262
|
+
const shortVolSpread = anchor_1.BN.max(confComponent, volSpread.mul(shortVolSpreadFactor).div(numericConstants_1.PERCENTAGE_PRECISION));
|
|
258
263
|
return [longVolSpread, shortVolSpread];
|
|
259
264
|
}
|
|
260
265
|
exports.calculateVolSpreadBN = calculateVolSpreadBN;
|
package/lib/math/auction.d.ts
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
|
-
import { Order } from '../types';
|
|
1
|
+
import { Order, PositionDirection } from '../types';
|
|
2
2
|
import { BN } from '../.';
|
|
3
3
|
export declare function isAuctionComplete(order: Order, slot: number): boolean;
|
|
4
4
|
export declare function isFallbackAvailableLiquiditySource(order: Order, minAuctionDuration: number, slot: number): boolean;
|
|
5
5
|
export declare function getAuctionPrice(order: Order, slot: number, oraclePrice: BN): BN;
|
|
6
6
|
export declare function getAuctionPriceForFixedAuction(order: Order, slot: number): BN;
|
|
7
7
|
export declare function getAuctionPriceForOracleOffsetAuction(order: Order, slot: number, oraclePrice: BN): BN;
|
|
8
|
+
export declare function deriveOracleAuctionParams({ direction, oraclePrice, auctionStartPrice, auctionEndPrice, limitPrice, }: {
|
|
9
|
+
direction: PositionDirection;
|
|
10
|
+
oraclePrice: BN;
|
|
11
|
+
auctionStartPrice: BN;
|
|
12
|
+
auctionEndPrice: BN;
|
|
13
|
+
limitPrice: BN;
|
|
14
|
+
}): {
|
|
15
|
+
auctionStartPrice: BN;
|
|
16
|
+
auctionEndPrice: BN;
|
|
17
|
+
oraclePriceOffset: number;
|
|
18
|
+
};
|