@drift-labs/sdk 2.43.0-beta.13 → 2.43.0-beta.15
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/driftClient.d.ts +3 -1
- package/lib/driftClient.js +21 -9
- package/lib/math/trade.js +18 -24
- package/lib/tx/fastSingleTxSender.js +6 -1
- package/package.json +1 -1
- package/src/driftClient.ts +49 -12
- package/src/math/trade.ts +26 -26
- package/src/tx/fastSingleTxSender.ts +7 -3
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.43.0-beta.
|
|
1
|
+
2.43.0-beta.15
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -275,11 +275,13 @@ export declare class DriftClient {
|
|
|
275
275
|
* @param makerInfo
|
|
276
276
|
* @param txParams
|
|
277
277
|
* @param bracketOrdersParams
|
|
278
|
+
* @param cancelExistingOrders - Builds and returns an extra transaciton to cancel the existing orders in the same market. Intended use is to auto-cancel TP/SL orders when closing a position
|
|
278
279
|
* @returns
|
|
279
280
|
*/
|
|
280
|
-
sendMarketOrderAndGetSignedFillTx(orderParams: OptionalOrderParams, userAccountPublicKey: PublicKey, userAccount: UserAccount, makerInfo?: MakerInfo | MakerInfo[], txParams?: TxParams, bracketOrdersParams?: OptionalOrderParams[], referrerInfo?: ReferrerInfo): Promise<{
|
|
281
|
+
sendMarketOrderAndGetSignedFillTx(orderParams: OptionalOrderParams, userAccountPublicKey: PublicKey, userAccount: UserAccount, makerInfo?: MakerInfo | MakerInfo[], txParams?: TxParams, bracketOrdersParams?: OptionalOrderParams[], referrerInfo?: ReferrerInfo, cancelExistingOrders?: boolean): Promise<{
|
|
281
282
|
txSig: TransactionSignature;
|
|
282
283
|
signedFillTx: Transaction;
|
|
284
|
+
signedCancelExistingOrdersTx?: Transaction;
|
|
283
285
|
}>;
|
|
284
286
|
placePerpOrder(orderParams: OptionalOrderParams, txParams?: TxParams): Promise<TransactionSignature>;
|
|
285
287
|
getPlacePerpOrderIx(orderParams: OptionalOrderParams): Promise<TransactionInstruction>;
|
package/lib/driftClient.js
CHANGED
|
@@ -1366,9 +1366,10 @@ class DriftClient {
|
|
|
1366
1366
|
* @param makerInfo
|
|
1367
1367
|
* @param txParams
|
|
1368
1368
|
* @param bracketOrdersParams
|
|
1369
|
+
* @param cancelExistingOrders - Builds and returns an extra transaciton to cancel the existing orders in the same market. Intended use is to auto-cancel TP/SL orders when closing a position
|
|
1369
1370
|
* @returns
|
|
1370
1371
|
*/
|
|
1371
|
-
async sendMarketOrderAndGetSignedFillTx(orderParams, userAccountPublicKey, userAccount, makerInfo, txParams, bracketOrdersParams = new Array(), referrerInfo) {
|
|
1372
|
+
async sendMarketOrderAndGetSignedFillTx(orderParams, userAccountPublicKey, userAccount, makerInfo, txParams, bracketOrdersParams = new Array(), referrerInfo, cancelExistingOrders) {
|
|
1372
1373
|
const marketIndex = orderParams.marketIndex;
|
|
1373
1374
|
const orderId = userAccount.nextOrderId;
|
|
1374
1375
|
const bracketOrderIxs = [];
|
|
@@ -1381,20 +1382,31 @@ class DriftClient {
|
|
|
1381
1382
|
orderId,
|
|
1382
1383
|
marketIndex,
|
|
1383
1384
|
}, makerInfo, referrerInfo);
|
|
1385
|
+
let cancelOrdersIx;
|
|
1386
|
+
let cancelExistingOrdersTx;
|
|
1387
|
+
if (cancelExistingOrders) {
|
|
1388
|
+
cancelOrdersIx = await this.getCancelOrdersIx(orderParams.marketType, orderParams.marketIndex, null);
|
|
1389
|
+
//@ts-ignore
|
|
1390
|
+
cancelExistingOrdersTx = await this.buildTransaction([cancelOrdersIx], txParams, this.txVersion);
|
|
1391
|
+
}
|
|
1384
1392
|
// use versioned transactions if there is a lookup table account and wallet is compatible
|
|
1385
1393
|
if (this.txVersion === 0) {
|
|
1386
1394
|
const versionedMarketOrderTx = await this.buildTransaction([placePerpOrderIx].concat(bracketOrderIxs), txParams, 0);
|
|
1387
1395
|
const versionedFillTx = await this.buildTransaction([fillPerpOrderIx], txParams, 0);
|
|
1388
|
-
const [signedVersionedMarketOrderTx, signedVersionedFillTx] = await this.provider.wallet.signAllTransactions([
|
|
1389
|
-
//@ts-ignore
|
|
1396
|
+
const [signedVersionedMarketOrderTx, signedVersionedFillTx, signedCancelExistingOrdersTx,] = await this.provider.wallet.signAllTransactions([
|
|
1390
1397
|
versionedMarketOrderTx,
|
|
1391
|
-
//@ts-ignore
|
|
1392
1398
|
versionedFillTx,
|
|
1393
|
-
|
|
1399
|
+
cancelExistingOrdersTx,
|
|
1400
|
+
].filter((tx) => tx !== undefined));
|
|
1394
1401
|
const { txSig, slot } = await this.txSender.sendRawTransaction(signedVersionedMarketOrderTx.serialize(), this.opts);
|
|
1395
1402
|
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
1396
|
-
|
|
1397
|
-
|
|
1403
|
+
return {
|
|
1404
|
+
txSig,
|
|
1405
|
+
// @ts-ignore
|
|
1406
|
+
signedFillTx: signedVersionedFillTx,
|
|
1407
|
+
// @ts-ignore
|
|
1408
|
+
signedCancelExistingOrdersTx,
|
|
1409
|
+
};
|
|
1398
1410
|
}
|
|
1399
1411
|
else {
|
|
1400
1412
|
const marketOrderTx = (0, utils_1.wrapInTx)(placePerpOrderIx, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice);
|
|
@@ -1408,10 +1420,10 @@ class DriftClient {
|
|
|
1408
1420
|
fillTx.recentBlockhash = currentBlockHash;
|
|
1409
1421
|
marketOrderTx.feePayer = userAccount.authority;
|
|
1410
1422
|
fillTx.feePayer = userAccount.authority;
|
|
1411
|
-
const [signedMarketOrderTx, signedFillTx] = await this.provider.wallet.signAllTransactions([marketOrderTx, fillTx]);
|
|
1423
|
+
const [signedMarketOrderTx, signedFillTx, signedCancelExistingOrdersTx] = await this.provider.wallet.signAllTransactions([marketOrderTx, fillTx, cancelExistingOrdersTx].filter((tx) => tx !== undefined));
|
|
1412
1424
|
const { txSig, slot } = await this.sendTransaction(signedMarketOrderTx, [], this.opts, true);
|
|
1413
1425
|
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
1414
|
-
return { txSig, signedFillTx };
|
|
1426
|
+
return { txSig, signedFillTx, signedCancelExistingOrdersTx };
|
|
1415
1427
|
}
|
|
1416
1428
|
}
|
|
1417
1429
|
async placePerpOrder(orderParams, txParams) {
|
package/lib/math/trade.js
CHANGED
|
@@ -415,14 +415,12 @@ function calculateEstimatedPerpEntryPrice(assetType, amount, direction, market,
|
|
|
415
415
|
limitOrder = limitOrders.next().value;
|
|
416
416
|
}
|
|
417
417
|
}
|
|
418
|
-
const entryPrice =
|
|
419
|
-
.mul(numericConstants_1.BASE_PRECISION)
|
|
420
|
-
.
|
|
421
|
-
const priceImpact =
|
|
422
|
-
.sub(bestPrice)
|
|
423
|
-
|
|
424
|
-
.div(bestPrice)
|
|
425
|
-
.abs();
|
|
418
|
+
const entryPrice = cumulativeBaseFilled && cumulativeBaseFilled.gt(numericConstants_1.ZERO)
|
|
419
|
+
? cumulativeQuoteFilled.mul(numericConstants_1.BASE_PRECISION).div(cumulativeBaseFilled)
|
|
420
|
+
: numericConstants_1.ZERO;
|
|
421
|
+
const priceImpact = bestPrice && bestPrice.gt(numericConstants_1.ZERO)
|
|
422
|
+
? entryPrice.sub(bestPrice).mul(numericConstants_1.PRICE_PRECISION).div(bestPrice).abs()
|
|
423
|
+
: numericConstants_1.ZERO;
|
|
426
424
|
return {
|
|
427
425
|
entryPrice,
|
|
428
426
|
priceImpact,
|
|
@@ -551,14 +549,12 @@ function calculateEstimatedSpotEntryPrice(assetType, amount, direction, market,
|
|
|
551
549
|
}
|
|
552
550
|
}
|
|
553
551
|
}
|
|
554
|
-
const entryPrice =
|
|
555
|
-
.mul(basePrecision)
|
|
556
|
-
.
|
|
557
|
-
const priceImpact =
|
|
558
|
-
.sub(bestPrice)
|
|
559
|
-
|
|
560
|
-
.div(bestPrice)
|
|
561
|
-
.abs();
|
|
552
|
+
const entryPrice = cumulativeBaseFilled && cumulativeBaseFilled.gt(numericConstants_1.ZERO)
|
|
553
|
+
? cumulativeQuoteFilled.mul(basePrecision).div(cumulativeBaseFilled)
|
|
554
|
+
: numericConstants_1.ZERO;
|
|
555
|
+
const priceImpact = bestPrice && bestPrice.gt(numericConstants_1.ZERO)
|
|
556
|
+
? entryPrice.sub(bestPrice).mul(numericConstants_1.PRICE_PRECISION).div(bestPrice).abs()
|
|
557
|
+
: numericConstants_1.ZERO;
|
|
562
558
|
return {
|
|
563
559
|
entryPrice,
|
|
564
560
|
priceImpact,
|
|
@@ -609,14 +605,12 @@ function calculateEstimatedEntryPriceWithL2(assetType, amount, direction, basePr
|
|
|
609
605
|
nextLevel = levels.shift();
|
|
610
606
|
}
|
|
611
607
|
}
|
|
612
|
-
const entryPrice =
|
|
613
|
-
.mul(basePrecision)
|
|
614
|
-
.
|
|
615
|
-
const priceImpact =
|
|
616
|
-
.sub(bestPrice)
|
|
617
|
-
|
|
618
|
-
.div(bestPrice)
|
|
619
|
-
.abs();
|
|
608
|
+
const entryPrice = cumulativeBaseFilled && cumulativeBaseFilled.gt(numericConstants_1.ZERO)
|
|
609
|
+
? cumulativeQuoteFilled.mul(basePrecision).div(cumulativeBaseFilled)
|
|
610
|
+
: numericConstants_1.ZERO;
|
|
611
|
+
const priceImpact = bestPrice && bestPrice.gt(numericConstants_1.ZERO)
|
|
612
|
+
? entryPrice.sub(bestPrice).mul(numericConstants_1.PRICE_PRECISION).div(bestPrice).abs()
|
|
613
|
+
: numericConstants_1.ZERO;
|
|
620
614
|
return {
|
|
621
615
|
entryPrice,
|
|
622
616
|
priceImpact,
|
|
@@ -20,7 +20,12 @@ class FastSingleTxSender extends baseTxSender_1.BaseTxSender {
|
|
|
20
20
|
}
|
|
21
21
|
startBlockhashRefreshLoop() {
|
|
22
22
|
setInterval(async () => {
|
|
23
|
-
|
|
23
|
+
try {
|
|
24
|
+
this.recentBlockhash = (await this.connection.getLatestBlockhash(this.opts)).blockhash;
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
console.error('Error in startBlockhashRefreshLoop: ', e);
|
|
28
|
+
}
|
|
24
29
|
}, this.blockhashRefreshInterval);
|
|
25
30
|
}
|
|
26
31
|
async prepareTx(tx, additionalSigners, opts) {
|
package/package.json
CHANGED
package/src/driftClient.ts
CHANGED
|
@@ -2448,6 +2448,7 @@ export class DriftClient {
|
|
|
2448
2448
|
* @param makerInfo
|
|
2449
2449
|
* @param txParams
|
|
2450
2450
|
* @param bracketOrdersParams
|
|
2451
|
+
* @param cancelExistingOrders - Builds and returns an extra transaciton to cancel the existing orders in the same market. Intended use is to auto-cancel TP/SL orders when closing a position
|
|
2451
2452
|
* @returns
|
|
2452
2453
|
*/
|
|
2453
2454
|
public async sendMarketOrderAndGetSignedFillTx(
|
|
@@ -2457,8 +2458,13 @@ export class DriftClient {
|
|
|
2457
2458
|
makerInfo?: MakerInfo | MakerInfo[],
|
|
2458
2459
|
txParams?: TxParams,
|
|
2459
2460
|
bracketOrdersParams = new Array<OptionalOrderParams>(),
|
|
2460
|
-
referrerInfo?: ReferrerInfo
|
|
2461
|
-
|
|
2461
|
+
referrerInfo?: ReferrerInfo,
|
|
2462
|
+
cancelExistingOrders?: boolean
|
|
2463
|
+
): Promise<{
|
|
2464
|
+
txSig: TransactionSignature;
|
|
2465
|
+
signedFillTx: Transaction;
|
|
2466
|
+
signedCancelExistingOrdersTx?: Transaction;
|
|
2467
|
+
}> {
|
|
2462
2468
|
const marketIndex = orderParams.marketIndex;
|
|
2463
2469
|
const orderId = userAccount.nextOrderId;
|
|
2464
2470
|
const bracketOrderIxs = [];
|
|
@@ -2483,6 +2489,23 @@ export class DriftClient {
|
|
|
2483
2489
|
referrerInfo
|
|
2484
2490
|
);
|
|
2485
2491
|
|
|
2492
|
+
let cancelOrdersIx: TransactionInstruction;
|
|
2493
|
+
let cancelExistingOrdersTx: Transaction;
|
|
2494
|
+
if (cancelExistingOrders) {
|
|
2495
|
+
cancelOrdersIx = await this.getCancelOrdersIx(
|
|
2496
|
+
orderParams.marketType,
|
|
2497
|
+
orderParams.marketIndex,
|
|
2498
|
+
null
|
|
2499
|
+
);
|
|
2500
|
+
|
|
2501
|
+
//@ts-ignore
|
|
2502
|
+
cancelExistingOrdersTx = await this.buildTransaction(
|
|
2503
|
+
[cancelOrdersIx],
|
|
2504
|
+
txParams,
|
|
2505
|
+
this.txVersion
|
|
2506
|
+
);
|
|
2507
|
+
}
|
|
2508
|
+
|
|
2486
2509
|
// use versioned transactions if there is a lookup table account and wallet is compatible
|
|
2487
2510
|
if (this.txVersion === 0) {
|
|
2488
2511
|
const versionedMarketOrderTx = await this.buildTransaction(
|
|
@@ -2495,21 +2518,31 @@ export class DriftClient {
|
|
|
2495
2518
|
txParams,
|
|
2496
2519
|
0
|
|
2497
2520
|
);
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2521
|
+
|
|
2522
|
+
const [
|
|
2523
|
+
signedVersionedMarketOrderTx,
|
|
2524
|
+
signedVersionedFillTx,
|
|
2525
|
+
signedCancelExistingOrdersTx,
|
|
2526
|
+
] = await this.provider.wallet.signAllTransactions(
|
|
2527
|
+
[
|
|
2501
2528
|
versionedMarketOrderTx,
|
|
2502
|
-
//@ts-ignore
|
|
2503
2529
|
versionedFillTx,
|
|
2504
|
-
|
|
2530
|
+
cancelExistingOrdersTx,
|
|
2531
|
+
].filter((tx) => tx !== undefined)
|
|
2532
|
+
);
|
|
2505
2533
|
const { txSig, slot } = await this.txSender.sendRawTransaction(
|
|
2506
2534
|
signedVersionedMarketOrderTx.serialize(),
|
|
2507
2535
|
this.opts
|
|
2508
2536
|
);
|
|
2509
2537
|
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
2510
2538
|
|
|
2511
|
-
|
|
2512
|
-
|
|
2539
|
+
return {
|
|
2540
|
+
txSig,
|
|
2541
|
+
// @ts-ignore
|
|
2542
|
+
signedFillTx: signedVersionedFillTx,
|
|
2543
|
+
// @ts-ignore
|
|
2544
|
+
signedCancelExistingOrdersTx,
|
|
2545
|
+
};
|
|
2513
2546
|
} else {
|
|
2514
2547
|
const marketOrderTx = wrapInTx(
|
|
2515
2548
|
placePerpOrderIx,
|
|
@@ -2537,8 +2570,12 @@ export class DriftClient {
|
|
|
2537
2570
|
marketOrderTx.feePayer = userAccount.authority;
|
|
2538
2571
|
fillTx.feePayer = userAccount.authority;
|
|
2539
2572
|
|
|
2540
|
-
const [signedMarketOrderTx, signedFillTx] =
|
|
2541
|
-
await this.provider.wallet.signAllTransactions(
|
|
2573
|
+
const [signedMarketOrderTx, signedFillTx, signedCancelExistingOrdersTx] =
|
|
2574
|
+
await this.provider.wallet.signAllTransactions(
|
|
2575
|
+
[marketOrderTx, fillTx, cancelExistingOrdersTx].filter(
|
|
2576
|
+
(tx) => tx !== undefined
|
|
2577
|
+
)
|
|
2578
|
+
);
|
|
2542
2579
|
const { txSig, slot } = await this.sendTransaction(
|
|
2543
2580
|
signedMarketOrderTx,
|
|
2544
2581
|
[],
|
|
@@ -2547,7 +2584,7 @@ export class DriftClient {
|
|
|
2547
2584
|
);
|
|
2548
2585
|
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
2549
2586
|
|
|
2550
|
-
return { txSig, signedFillTx };
|
|
2587
|
+
return { txSig, signedFillTx, signedCancelExistingOrdersTx };
|
|
2551
2588
|
}
|
|
2552
2589
|
}
|
|
2553
2590
|
|
package/src/math/trade.ts
CHANGED
|
@@ -649,15 +649,15 @@ export function calculateEstimatedPerpEntryPrice(
|
|
|
649
649
|
}
|
|
650
650
|
}
|
|
651
651
|
|
|
652
|
-
const entryPrice =
|
|
653
|
-
.
|
|
654
|
-
|
|
652
|
+
const entryPrice =
|
|
653
|
+
cumulativeBaseFilled && cumulativeBaseFilled.gt(ZERO)
|
|
654
|
+
? cumulativeQuoteFilled.mul(BASE_PRECISION).div(cumulativeBaseFilled)
|
|
655
|
+
: ZERO;
|
|
655
656
|
|
|
656
|
-
const priceImpact =
|
|
657
|
-
.
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
.abs();
|
|
657
|
+
const priceImpact =
|
|
658
|
+
bestPrice && bestPrice.gt(ZERO)
|
|
659
|
+
? entryPrice.sub(bestPrice).mul(PRICE_PRECISION).div(bestPrice).abs()
|
|
660
|
+
: ZERO;
|
|
661
661
|
|
|
662
662
|
return {
|
|
663
663
|
entryPrice,
|
|
@@ -858,15 +858,15 @@ export function calculateEstimatedSpotEntryPrice(
|
|
|
858
858
|
}
|
|
859
859
|
}
|
|
860
860
|
|
|
861
|
-
const entryPrice =
|
|
862
|
-
.
|
|
863
|
-
|
|
861
|
+
const entryPrice =
|
|
862
|
+
cumulativeBaseFilled && cumulativeBaseFilled.gt(ZERO)
|
|
863
|
+
? cumulativeQuoteFilled.mul(basePrecision).div(cumulativeBaseFilled)
|
|
864
|
+
: ZERO;
|
|
864
865
|
|
|
865
|
-
const priceImpact =
|
|
866
|
-
.
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
.abs();
|
|
866
|
+
const priceImpact =
|
|
867
|
+
bestPrice && bestPrice.gt(ZERO)
|
|
868
|
+
? entryPrice.sub(bestPrice).mul(PRICE_PRECISION).div(bestPrice).abs()
|
|
869
|
+
: ZERO;
|
|
870
870
|
|
|
871
871
|
return {
|
|
872
872
|
entryPrice,
|
|
@@ -900,8 +900,8 @@ export function calculateEstimatedEntryPriceWithL2(
|
|
|
900
900
|
const levels = [...(takerIsLong ? l2.asks : l2.bids)];
|
|
901
901
|
let nextLevel = levels.shift();
|
|
902
902
|
|
|
903
|
-
let bestPrice;
|
|
904
|
-
let worstPrice;
|
|
903
|
+
let bestPrice: BN;
|
|
904
|
+
let worstPrice: BN;
|
|
905
905
|
if (nextLevel) {
|
|
906
906
|
bestPrice = nextLevel.price;
|
|
907
907
|
worstPrice = nextLevel.price;
|
|
@@ -945,15 +945,15 @@ export function calculateEstimatedEntryPriceWithL2(
|
|
|
945
945
|
}
|
|
946
946
|
}
|
|
947
947
|
|
|
948
|
-
const entryPrice =
|
|
949
|
-
.
|
|
950
|
-
|
|
948
|
+
const entryPrice =
|
|
949
|
+
cumulativeBaseFilled && cumulativeBaseFilled.gt(ZERO)
|
|
950
|
+
? cumulativeQuoteFilled.mul(basePrecision).div(cumulativeBaseFilled)
|
|
951
|
+
: ZERO;
|
|
951
952
|
|
|
952
|
-
const priceImpact =
|
|
953
|
-
.
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
.abs();
|
|
953
|
+
const priceImpact =
|
|
954
|
+
bestPrice && bestPrice.gt(ZERO)
|
|
955
|
+
? entryPrice.sub(bestPrice).mul(PRICE_PRECISION).div(bestPrice).abs()
|
|
956
|
+
: ZERO;
|
|
957
957
|
|
|
958
958
|
return {
|
|
959
959
|
entryPrice,
|
|
@@ -54,9 +54,13 @@ export class FastSingleTxSender extends BaseTxSender {
|
|
|
54
54
|
|
|
55
55
|
startBlockhashRefreshLoop(): void {
|
|
56
56
|
setInterval(async () => {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
try {
|
|
58
|
+
this.recentBlockhash = (
|
|
59
|
+
await this.connection.getLatestBlockhash(this.opts)
|
|
60
|
+
).blockhash;
|
|
61
|
+
} catch (e) {
|
|
62
|
+
console.error('Error in startBlockhashRefreshLoop: ', e);
|
|
63
|
+
}
|
|
60
64
|
}, this.blockhashRefreshInterval);
|
|
61
65
|
}
|
|
62
66
|
|