@drift-labs/sdk 2.33.1-beta.0 → 2.33.1-beta.2
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/lib/dlob/DLOBSubscriber.d.ts +2 -1
- package/lib/dlob/DLOBSubscriber.js +2 -2
- package/lib/driftClient.d.ts +2 -1
- package/lib/driftClient.js +2 -1
- package/lib/jupiter/jupiterClient.d.ts +3 -1
- package/lib/jupiter/jupiterClient.js +3 -1
- package/lib/math/superStake.d.ts +2 -1
- package/lib/math/superStake.js +2 -1
- package/lib/user.js +9 -6
- package/package.json +1 -1
- package/src/dlob/DLOBSubscriber.ts +3 -1
- package/src/driftClient.ts +3 -0
- package/src/jupiter/jupiterClient.ts +4 -0
- package/src/math/superStake.ts +3 -0
- package/src/user.ts +12 -8
- package/tests/dlob/test.ts +2 -1
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.33.1-beta.
|
|
1
|
+
2.33.1-beta.2
|
|
@@ -29,12 +29,13 @@ export declare class DLOBSubscriber {
|
|
|
29
29
|
* @param includeVamm Whether to include the VAMM orders in the order book. Defaults to false. If true, creates vAMM generator {@link getVammL2Generator} and adds it to fallbackL2Generators.
|
|
30
30
|
* @param fallbackL2Generators L2 generators for fallback liquidity e.g. vAMM {@link getVammL2Generator}, openbook {@link SerumSubscriber}
|
|
31
31
|
*/
|
|
32
|
-
getL2({ marketName, marketIndex, marketType, depth, includeVamm, fallbackL2Generators, }: {
|
|
32
|
+
getL2({ marketName, marketIndex, marketType, depth, includeVamm, numVammOrders, fallbackL2Generators, }: {
|
|
33
33
|
marketName?: string;
|
|
34
34
|
marketIndex?: number;
|
|
35
35
|
marketType?: MarketType;
|
|
36
36
|
depth?: number;
|
|
37
37
|
includeVamm?: boolean;
|
|
38
|
+
numVammOrders?: number;
|
|
38
39
|
fallbackL2Generators?: L2OrderBookGenerator[];
|
|
39
40
|
}): L2OrderBook;
|
|
40
41
|
/**
|
|
@@ -46,7 +46,7 @@ class DLOBSubscriber {
|
|
|
46
46
|
* @param includeVamm Whether to include the VAMM orders in the order book. Defaults to false. If true, creates vAMM generator {@link getVammL2Generator} and adds it to fallbackL2Generators.
|
|
47
47
|
* @param fallbackL2Generators L2 generators for fallback liquidity e.g. vAMM {@link getVammL2Generator}, openbook {@link SerumSubscriber}
|
|
48
48
|
*/
|
|
49
|
-
getL2({ marketName, marketIndex, marketType, depth = 10, includeVamm = false, fallbackL2Generators = [], }) {
|
|
49
|
+
getL2({ marketName, marketIndex, marketType, depth = 10, includeVamm = false, numVammOrders, fallbackL2Generators = [], }) {
|
|
50
50
|
if (marketName) {
|
|
51
51
|
const derivedMarketInfo = this.driftClient.getMarketIndexAndType(marketName);
|
|
52
52
|
if (!derivedMarketInfo) {
|
|
@@ -79,7 +79,7 @@ class DLOBSubscriber {
|
|
|
79
79
|
(0, orderBookLevels_1.getVammL2Generator)({
|
|
80
80
|
marketAccount: this.driftClient.getPerpMarketAccount(marketIndex),
|
|
81
81
|
oraclePriceData,
|
|
82
|
-
numOrders: depth,
|
|
82
|
+
numOrders: numVammOrders !== null && numVammOrders !== void 0 ? numVammOrders : depth,
|
|
83
83
|
}),
|
|
84
84
|
];
|
|
85
85
|
}
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -327,7 +327,7 @@ export declare class DriftClient {
|
|
|
327
327
|
reduceOnly?: SwapReduceOnly;
|
|
328
328
|
txParams?: TxParams;
|
|
329
329
|
}): Promise<TransactionSignature>;
|
|
330
|
-
getJupiterSwapIx({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, route, reduceOnly, userAccountPublicKey, }: {
|
|
330
|
+
getJupiterSwapIx({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, onlyDirectRoutes, route, reduceOnly, userAccountPublicKey, }: {
|
|
331
331
|
jupiterClient: JupiterClient;
|
|
332
332
|
outMarketIndex: number;
|
|
333
333
|
inMarketIndex: number;
|
|
@@ -336,6 +336,7 @@ export declare class DriftClient {
|
|
|
336
336
|
amount: BN;
|
|
337
337
|
slippageBps?: number;
|
|
338
338
|
swapMode?: SwapMode;
|
|
339
|
+
onlyDirectRoutes?: boolean;
|
|
339
340
|
route?: Route;
|
|
340
341
|
reduceOnly?: SwapReduceOnly;
|
|
341
342
|
userAccountPublicKey?: PublicKey;
|
package/lib/driftClient.js
CHANGED
|
@@ -1963,7 +1963,7 @@ class DriftClient {
|
|
|
1963
1963
|
this.spotMarketLastSlotCache.set(inMarketIndex, slot);
|
|
1964
1964
|
return txSig;
|
|
1965
1965
|
}
|
|
1966
|
-
async getJupiterSwapIx({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, route, reduceOnly, userAccountPublicKey, }) {
|
|
1966
|
+
async getJupiterSwapIx({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, onlyDirectRoutes, route, reduceOnly, userAccountPublicKey, }) {
|
|
1967
1967
|
const outMarket = this.getSpotMarketAccount(outMarketIndex);
|
|
1968
1968
|
const inMarket = this.getSpotMarketAccount(inMarketIndex);
|
|
1969
1969
|
if (!route) {
|
|
@@ -1973,6 +1973,7 @@ class DriftClient {
|
|
|
1973
1973
|
amount,
|
|
1974
1974
|
slippageBps,
|
|
1975
1975
|
swapMode,
|
|
1976
|
+
onlyDirectRoutes,
|
|
1976
1977
|
});
|
|
1977
1978
|
if (!routes || routes.length === 0) {
|
|
1978
1979
|
throw new Error('No jupiter routes found');
|
|
@@ -42,13 +42,15 @@ export declare class JupiterClient {
|
|
|
42
42
|
* @param amount the amount of the input token
|
|
43
43
|
* @param slippageBps the slippage tolerance in basis points
|
|
44
44
|
* @param swapMode the swap mode (ExactIn or ExactOut)
|
|
45
|
+
* @param onlyDirectRoutes whether to only return direct routes
|
|
45
46
|
*/
|
|
46
|
-
getRoutes({ inputMint, outputMint, amount, slippageBps, swapMode, }: {
|
|
47
|
+
getRoutes({ inputMint, outputMint, amount, slippageBps, swapMode, onlyDirectRoutes, }: {
|
|
47
48
|
inputMint: PublicKey;
|
|
48
49
|
outputMint: PublicKey;
|
|
49
50
|
amount: BN;
|
|
50
51
|
slippageBps?: number;
|
|
51
52
|
swapMode?: SwapMode;
|
|
53
|
+
onlyDirectRoutes?: boolean;
|
|
52
54
|
}): Promise<Route[]>;
|
|
53
55
|
/**
|
|
54
56
|
* Get a swap transaction for a route
|
|
@@ -19,14 +19,16 @@ class JupiterClient {
|
|
|
19
19
|
* @param amount the amount of the input token
|
|
20
20
|
* @param slippageBps the slippage tolerance in basis points
|
|
21
21
|
* @param swapMode the swap mode (ExactIn or ExactOut)
|
|
22
|
+
* @param onlyDirectRoutes whether to only return direct routes
|
|
22
23
|
*/
|
|
23
|
-
async getRoutes({ inputMint, outputMint, amount, slippageBps = 50, swapMode = 'ExactIn', }) {
|
|
24
|
+
async getRoutes({ inputMint, outputMint, amount, slippageBps = 50, swapMode = 'ExactIn', onlyDirectRoutes = false, }) {
|
|
24
25
|
const params = new URLSearchParams({
|
|
25
26
|
inputMint: inputMint.toString(),
|
|
26
27
|
outputMint: outputMint.toString(),
|
|
27
28
|
amount: amount.toString(),
|
|
28
29
|
slippageBps: slippageBps.toString(),
|
|
29
30
|
swapMode,
|
|
31
|
+
onlyDirectRoutes: onlyDirectRoutes.toString(),
|
|
30
32
|
}).toString();
|
|
31
33
|
const { data: routes } = await (await (0, node_fetch_1.default)(`https://quote-api.jup.ag/v4/quote?${params}`)).json();
|
|
32
34
|
return routes;
|
package/lib/math/superStake.d.ts
CHANGED
|
@@ -4,13 +4,14 @@ import { DriftClient } from '../driftClient';
|
|
|
4
4
|
import { BN } from '@coral-xyz/anchor';
|
|
5
5
|
import { User } from '../user';
|
|
6
6
|
import { DepositRecord } from '../types';
|
|
7
|
-
export declare function findBestSuperStakeIxs({ amount, jupiterClient, driftClient, userAccountPublicKey, marinadePrice, forceMarinade, }: {
|
|
7
|
+
export declare function findBestSuperStakeIxs({ amount, jupiterClient, driftClient, userAccountPublicKey, marinadePrice, forceMarinade, onlyDirectRoutes, }: {
|
|
8
8
|
amount: BN;
|
|
9
9
|
jupiterClient: JupiterClient;
|
|
10
10
|
driftClient: DriftClient;
|
|
11
11
|
marinadePrice?: number;
|
|
12
12
|
userAccountPublicKey?: PublicKey;
|
|
13
13
|
forceMarinade?: boolean;
|
|
14
|
+
onlyDirectRoutes?: boolean;
|
|
14
15
|
}): Promise<{
|
|
15
16
|
ixs: TransactionInstruction[];
|
|
16
17
|
lookupTables: AddressLookupTableAccount[];
|
package/lib/math/superStake.js
CHANGED
|
@@ -10,7 +10,7 @@ const anchor_1 = require("@coral-xyz/anchor");
|
|
|
10
10
|
const types_1 = require("../types");
|
|
11
11
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
12
12
|
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
13
|
-
async function findBestSuperStakeIxs({ amount, jupiterClient, driftClient, userAccountPublicKey, marinadePrice, forceMarinade, }) {
|
|
13
|
+
async function findBestSuperStakeIxs({ amount, jupiterClient, driftClient, userAccountPublicKey, marinadePrice, forceMarinade, onlyDirectRoutes, }) {
|
|
14
14
|
if (!marinadePrice) {
|
|
15
15
|
const marinadeProgram = (0, marinade_1.getMarinadeFinanceProgram)(driftClient.provider);
|
|
16
16
|
marinadePrice = await (0, marinade_1.getMarinadeMSolPrice)(marinadeProgram);
|
|
@@ -24,6 +24,7 @@ async function findBestSuperStakeIxs({ amount, jupiterClient, driftClient, userA
|
|
|
24
24
|
inputMint: solMint,
|
|
25
25
|
outputMint: mSOLMint,
|
|
26
26
|
amount,
|
|
27
|
+
onlyDirectRoutes,
|
|
27
28
|
});
|
|
28
29
|
bestRoute = jupiterRoutes[0];
|
|
29
30
|
jupiterPrice = bestRoute.inAmount / bestRoute.outAmount;
|
package/lib/user.js
CHANGED
|
@@ -1113,7 +1113,6 @@ class User {
|
|
|
1113
1113
|
const outOraclePrice = this.getOracleDataForSpotMarket(outMarketIndex).price;
|
|
1114
1114
|
const inPrecision = new _1.BN(10 ** inMarket.decimals);
|
|
1115
1115
|
const outPrecision = new _1.BN(10 ** outMarket.decimals);
|
|
1116
|
-
const outSaferThanIn = inMarket.initialAssetWeight < outMarket.initialAssetWeight;
|
|
1117
1116
|
const inSpotPosition = this.getSpotPosition(inMarketIndex) ||
|
|
1118
1117
|
this.getEmptySpotPosition(inMarketIndex);
|
|
1119
1118
|
const outSpotPosition = this.getSpotPosition(outMarketIndex) ||
|
|
@@ -1137,6 +1136,12 @@ class User {
|
|
|
1137
1136
|
let inSwap = numericConstants_1.ZERO;
|
|
1138
1137
|
let outSwap = numericConstants_1.ZERO;
|
|
1139
1138
|
const inTokenAmount = this.getTokenAmount(inMarketIndex);
|
|
1139
|
+
const outTokenAmount = this.getTokenAmount(outMarketIndex);
|
|
1140
|
+
const outSaferThanIn =
|
|
1141
|
+
// selling asset to close borrow
|
|
1142
|
+
(inTokenAmount.gt(numericConstants_1.ZERO) && outTokenAmount.lt(numericConstants_1.ZERO)) ||
|
|
1143
|
+
// buying asset with higher initial asset weight
|
|
1144
|
+
inMarket.initialAssetWeight < outMarket.initialAssetWeight;
|
|
1140
1145
|
if (freeCollateral.lt(numericConstants_1.ONE)) {
|
|
1141
1146
|
if (outSaferThanIn && inTokenAmount.gt(numericConstants_1.ZERO)) {
|
|
1142
1147
|
inSwap = inTokenAmount;
|
|
@@ -1145,11 +1150,9 @@ class User {
|
|
|
1145
1150
|
}
|
|
1146
1151
|
else {
|
|
1147
1152
|
let minSwap = numericConstants_1.ZERO;
|
|
1148
|
-
let maxSwap = freeCollateral
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
.div(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION.div(new _1.BN(100)))
|
|
1152
|
-
.div(inOraclePrice); // just assume user can go 100x
|
|
1153
|
+
let maxSwap = _1.BN.max(freeCollateral.mul(inPrecision).mul(new _1.BN(100)).div(inOraclePrice), // 100x current free collateral
|
|
1154
|
+
inTokenAmount.abs().mul(new _1.BN(10)) // 10x current position
|
|
1155
|
+
);
|
|
1153
1156
|
inSwap = maxSwap.div(numericConstants_1.TWO);
|
|
1154
1157
|
const error = freeCollateral.div(new _1.BN(10000));
|
|
1155
1158
|
let i = 0;
|
package/package.json
CHANGED
|
@@ -75,6 +75,7 @@ export class DLOBSubscriber {
|
|
|
75
75
|
marketType,
|
|
76
76
|
depth = 10,
|
|
77
77
|
includeVamm = false,
|
|
78
|
+
numVammOrders,
|
|
78
79
|
fallbackL2Generators = [],
|
|
79
80
|
}: {
|
|
80
81
|
marketName?: string;
|
|
@@ -82,6 +83,7 @@ export class DLOBSubscriber {
|
|
|
82
83
|
marketType?: MarketType;
|
|
83
84
|
depth?: number;
|
|
84
85
|
includeVamm?: boolean;
|
|
86
|
+
numVammOrders?: number;
|
|
85
87
|
fallbackL2Generators?: L2OrderBookGenerator[];
|
|
86
88
|
}): L2OrderBook {
|
|
87
89
|
if (marketName) {
|
|
@@ -122,7 +124,7 @@ export class DLOBSubscriber {
|
|
|
122
124
|
getVammL2Generator({
|
|
123
125
|
marketAccount: this.driftClient.getPerpMarketAccount(marketIndex),
|
|
124
126
|
oraclePriceData,
|
|
125
|
-
numOrders: depth,
|
|
127
|
+
numOrders: numVammOrders ?? depth,
|
|
126
128
|
}),
|
|
127
129
|
];
|
|
128
130
|
}
|
package/src/driftClient.ts
CHANGED
|
@@ -3418,6 +3418,7 @@ export class DriftClient {
|
|
|
3418
3418
|
amount,
|
|
3419
3419
|
slippageBps,
|
|
3420
3420
|
swapMode,
|
|
3421
|
+
onlyDirectRoutes,
|
|
3421
3422
|
route,
|
|
3422
3423
|
reduceOnly,
|
|
3423
3424
|
userAccountPublicKey,
|
|
@@ -3430,6 +3431,7 @@ export class DriftClient {
|
|
|
3430
3431
|
amount: BN;
|
|
3431
3432
|
slippageBps?: number;
|
|
3432
3433
|
swapMode?: SwapMode;
|
|
3434
|
+
onlyDirectRoutes?: boolean;
|
|
3433
3435
|
route?: Route;
|
|
3434
3436
|
reduceOnly?: SwapReduceOnly;
|
|
3435
3437
|
userAccountPublicKey?: PublicKey;
|
|
@@ -3447,6 +3449,7 @@ export class DriftClient {
|
|
|
3447
3449
|
amount,
|
|
3448
3450
|
slippageBps,
|
|
3449
3451
|
swapMode,
|
|
3452
|
+
onlyDirectRoutes,
|
|
3450
3453
|
});
|
|
3451
3454
|
|
|
3452
3455
|
if (!routes || routes.length === 0) {
|
|
@@ -57,6 +57,7 @@ export class JupiterClient {
|
|
|
57
57
|
* @param amount the amount of the input token
|
|
58
58
|
* @param slippageBps the slippage tolerance in basis points
|
|
59
59
|
* @param swapMode the swap mode (ExactIn or ExactOut)
|
|
60
|
+
* @param onlyDirectRoutes whether to only return direct routes
|
|
60
61
|
*/
|
|
61
62
|
public async getRoutes({
|
|
62
63
|
inputMint,
|
|
@@ -64,12 +65,14 @@ export class JupiterClient {
|
|
|
64
65
|
amount,
|
|
65
66
|
slippageBps = 50,
|
|
66
67
|
swapMode = 'ExactIn',
|
|
68
|
+
onlyDirectRoutes = false,
|
|
67
69
|
}: {
|
|
68
70
|
inputMint: PublicKey;
|
|
69
71
|
outputMint: PublicKey;
|
|
70
72
|
amount: BN;
|
|
71
73
|
slippageBps?: number;
|
|
72
74
|
swapMode?: SwapMode;
|
|
75
|
+
onlyDirectRoutes?: boolean;
|
|
73
76
|
}): Promise<Route[]> {
|
|
74
77
|
const params = new URLSearchParams({
|
|
75
78
|
inputMint: inputMint.toString(),
|
|
@@ -77,6 +80,7 @@ export class JupiterClient {
|
|
|
77
80
|
amount: amount.toString(),
|
|
78
81
|
slippageBps: slippageBps.toString(),
|
|
79
82
|
swapMode,
|
|
83
|
+
onlyDirectRoutes: onlyDirectRoutes.toString(),
|
|
80
84
|
}).toString();
|
|
81
85
|
|
|
82
86
|
const { data: routes } = await (
|
package/src/math/superStake.ts
CHANGED
|
@@ -20,6 +20,7 @@ export async function findBestSuperStakeIxs({
|
|
|
20
20
|
userAccountPublicKey,
|
|
21
21
|
marinadePrice,
|
|
22
22
|
forceMarinade,
|
|
23
|
+
onlyDirectRoutes,
|
|
23
24
|
}: {
|
|
24
25
|
amount: BN;
|
|
25
26
|
jupiterClient: JupiterClient;
|
|
@@ -27,6 +28,7 @@ export async function findBestSuperStakeIxs({
|
|
|
27
28
|
marinadePrice?: number;
|
|
28
29
|
userAccountPublicKey?: PublicKey;
|
|
29
30
|
forceMarinade?: boolean;
|
|
31
|
+
onlyDirectRoutes?: boolean;
|
|
30
32
|
}): Promise<{
|
|
31
33
|
ixs: TransactionInstruction[];
|
|
32
34
|
lookupTables: AddressLookupTableAccount[];
|
|
@@ -48,6 +50,7 @@ export async function findBestSuperStakeIxs({
|
|
|
48
50
|
inputMint: solMint,
|
|
49
51
|
outputMint: mSOLMint,
|
|
50
52
|
amount,
|
|
53
|
+
onlyDirectRoutes,
|
|
51
54
|
});
|
|
52
55
|
|
|
53
56
|
bestRoute = jupiterRoutes[0];
|
package/src/user.ts
CHANGED
|
@@ -2009,9 +2009,6 @@ export class User {
|
|
|
2009
2009
|
const inPrecision = new BN(10 ** inMarket.decimals);
|
|
2010
2010
|
const outPrecision = new BN(10 ** outMarket.decimals);
|
|
2011
2011
|
|
|
2012
|
-
const outSaferThanIn =
|
|
2013
|
-
inMarket.initialAssetWeight < outMarket.initialAssetWeight;
|
|
2014
|
-
|
|
2015
2012
|
const inSpotPosition =
|
|
2016
2013
|
this.getSpotPosition(inMarketIndex) ||
|
|
2017
2014
|
this.getEmptySpotPosition(inMarketIndex);
|
|
@@ -2053,6 +2050,14 @@ export class User {
|
|
|
2053
2050
|
let inSwap = ZERO;
|
|
2054
2051
|
let outSwap = ZERO;
|
|
2055
2052
|
const inTokenAmount = this.getTokenAmount(inMarketIndex);
|
|
2053
|
+
const outTokenAmount = this.getTokenAmount(outMarketIndex);
|
|
2054
|
+
|
|
2055
|
+
const outSaferThanIn =
|
|
2056
|
+
// selling asset to close borrow
|
|
2057
|
+
(inTokenAmount.gt(ZERO) && outTokenAmount.lt(ZERO)) ||
|
|
2058
|
+
// buying asset with higher initial asset weight
|
|
2059
|
+
inMarket.initialAssetWeight < outMarket.initialAssetWeight;
|
|
2060
|
+
|
|
2056
2061
|
if (freeCollateral.lt(ONE)) {
|
|
2057
2062
|
if (outSaferThanIn && inTokenAmount.gt(ZERO)) {
|
|
2058
2063
|
inSwap = inTokenAmount;
|
|
@@ -2060,11 +2065,10 @@ export class User {
|
|
|
2060
2065
|
}
|
|
2061
2066
|
} else {
|
|
2062
2067
|
let minSwap = ZERO;
|
|
2063
|
-
let maxSwap =
|
|
2064
|
-
.mul(inPrecision)
|
|
2065
|
-
.mul(
|
|
2066
|
-
|
|
2067
|
-
.div(inOraclePrice); // just assume user can go 100x
|
|
2068
|
+
let maxSwap = BN.max(
|
|
2069
|
+
freeCollateral.mul(inPrecision).mul(new BN(100)).div(inOraclePrice), // 100x current free collateral
|
|
2070
|
+
inTokenAmount.abs().mul(new BN(10)) // 10x current position
|
|
2071
|
+
);
|
|
2068
2072
|
inSwap = maxSwap.div(TWO);
|
|
2069
2073
|
const error = freeCollateral.div(new BN(10000));
|
|
2070
2074
|
|
package/tests/dlob/test.ts
CHANGED
|
@@ -22,10 +22,11 @@ import {
|
|
|
22
22
|
ZERO,
|
|
23
23
|
convertToNumber,
|
|
24
24
|
QUOTE_PRECISION,
|
|
25
|
+
isVariant,
|
|
26
|
+
TWO
|
|
25
27
|
} from '../../src';
|
|
26
28
|
|
|
27
29
|
import { mockPerpMarkets, mockSpotMarkets, mockStateAccount } from './helpers';
|
|
28
|
-
import { isVariant, TWO } from '../../lib';
|
|
29
30
|
import { DLOBOrdersCoder } from '../../src/dlob/DLOBOrders';
|
|
30
31
|
|
|
31
32
|
function insertOrderToDLOB(
|