@drift-labs/sdk 2.82.0-beta.9 → 2.83.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/lib/accounts/types.d.ts +0 -3
- package/lib/accounts/webSocketAccountSubscriber.js +1 -1
- package/lib/clock/clockSubscriber.d.ts +29 -0
- package/lib/clock/clockSubscriber.js +74 -0
- package/lib/constants/perpMarkets.js +10 -0
- package/lib/constants/spotMarkets.js +31 -0
- package/lib/dlob/DLOB.js +2 -2
- package/lib/dlob/orderBookLevels.js +1 -0
- package/lib/driftClient.d.ts +11 -14
- package/lib/driftClient.js +159 -261
- package/lib/driftClientConfig.d.ts +2 -0
- package/lib/idl/drift.json +1 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/jupiter/jupiterClient.d.ts +2 -1
- package/lib/jupiter/jupiterClient.js +10 -6
- package/lib/math/exchangeStatus.d.ts +2 -2
- package/lib/math/orders.d.ts +1 -1
- package/lib/math/orders.js +2 -2
- package/lib/tx/baseTxSender.d.ts +8 -6
- package/lib/tx/baseTxSender.js +6 -49
- package/lib/tx/fastSingleTxSender.d.ts +6 -6
- package/lib/tx/fastSingleTxSender.js +3 -31
- package/lib/tx/forwardOnlyTxSender.d.ts +4 -2
- package/lib/tx/forwardOnlyTxSender.js +2 -1
- package/lib/tx/retryTxSender.d.ts +4 -2
- package/lib/tx/retryTxSender.js +2 -1
- package/lib/tx/txHandler.d.ts +138 -0
- package/lib/tx/txHandler.js +396 -0
- package/lib/tx/txParamProcessor.d.ts +7 -11
- package/lib/tx/txParamProcessor.js +20 -22
- package/lib/tx/types.d.ts +3 -7
- package/lib/tx/whileValidTxSender.d.ts +7 -6
- package/lib/tx/whileValidTxSender.js +7 -28
- package/lib/types.d.ts +24 -4
- package/lib/types.js +10 -1
- package/lib/user.d.ts +1 -1
- package/lib/user.js +7 -7
- package/lib/util/chainClock.d.ts +17 -0
- package/lib/util/chainClock.js +29 -0
- package/package.json +3 -3
- package/src/accounts/types.ts +0 -4
- package/src/accounts/webSocketAccountSubscriber.ts +1 -1
- package/src/clock/clockSubscriber.ts +113 -0
- package/src/constants/perpMarkets.ts +10 -0
- package/src/constants/spotMarkets.ts +33 -0
- package/src/dlob/DLOB.ts +2 -2
- package/src/dlob/orderBookLevels.ts +2 -0
- package/src/driftClient.ts +251 -388
- package/src/driftClientConfig.ts +2 -0
- package/src/idl/drift.json +1 -1
- package/src/index.ts +2 -0
- package/src/jupiter/jupiterClient.ts +15 -6
- package/src/math/exchangeStatus.ts +2 -1
- package/src/math/orders.ts +3 -2
- package/src/tx/baseTxSender.ts +21 -75
- package/src/tx/fastSingleTxSender.ts +10 -55
- package/src/tx/forwardOnlyTxSender.ts +5 -1
- package/src/tx/retryTxSender.ts +5 -1
- package/src/tx/txHandler.ts +625 -0
- package/src/tx/txParamProcessor.ts +28 -36
- package/src/tx/types.ts +2 -18
- package/src/tx/whileValidTxSender.ts +24 -48
- package/src/types.ts +26 -2
- package/src/user.ts +10 -10
- package/src/util/chainClock.ts +41 -0
- package/tests/dlob/helpers.ts +3 -0
- package/lib/tx/utils.d.ts +0 -6
- package/lib/tx/utils.js +0 -43
- package/src/tx/utils.ts +0 -68
package/src/driftClient.ts
CHANGED
|
@@ -42,8 +42,7 @@ import {
|
|
|
42
42
|
PhoenixV1FulfillmentConfigAccount,
|
|
43
43
|
ModifyOrderPolicy,
|
|
44
44
|
SwapReduceOnly,
|
|
45
|
-
|
|
46
|
-
ProcessingTxParams,
|
|
45
|
+
SignedTxData,
|
|
47
46
|
} from './types';
|
|
48
47
|
import * as anchor from '@coral-xyz/anchor';
|
|
49
48
|
import driftIDL from './idl/drift.json';
|
|
@@ -60,11 +59,10 @@ import {
|
|
|
60
59
|
LAMPORTS_PER_SOL,
|
|
61
60
|
Signer,
|
|
62
61
|
SystemProgram,
|
|
63
|
-
ComputeBudgetProgram,
|
|
64
62
|
AddressLookupTableAccount,
|
|
65
63
|
TransactionVersion,
|
|
66
64
|
VersionedTransaction,
|
|
67
|
-
|
|
65
|
+
BlockhashWithExpiryBlockHeight,
|
|
68
66
|
} from '@solana/web3.js';
|
|
69
67
|
|
|
70
68
|
import { TokenFaucet } from './tokenFaucet';
|
|
@@ -88,10 +86,9 @@ import {
|
|
|
88
86
|
DriftClientAccountSubscriber,
|
|
89
87
|
DriftClientAccountEvents,
|
|
90
88
|
DataAndSlot,
|
|
91
|
-
DriftClientMetricsEvents,
|
|
92
89
|
} from './accounts/types';
|
|
93
|
-
import {
|
|
94
|
-
import {
|
|
90
|
+
import { DriftClientMetricsEvents } from './types';
|
|
91
|
+
import { TxSender, TxSigAndSlot } from './tx/types';
|
|
95
92
|
import {
|
|
96
93
|
BASE_PRECISION,
|
|
97
94
|
PRICE_PRECISION,
|
|
@@ -127,8 +124,9 @@ import { UserStatsSubscriptionConfig } from './userStatsConfig';
|
|
|
127
124
|
import { getMarinadeDepositIx, getMarinadeFinanceProgram } from './marinade';
|
|
128
125
|
import { getOrderParams } from './orderParams';
|
|
129
126
|
import { numberToSafeBN } from './math/utils';
|
|
130
|
-
import {
|
|
127
|
+
import { TransactionParamProcessor } from './tx/txParamProcessor';
|
|
131
128
|
import { isOracleValid } from './math/oracles';
|
|
129
|
+
import { TxHandler } from './tx/txHandler';
|
|
132
130
|
|
|
133
131
|
type RemainingAccountParams = {
|
|
134
132
|
userAccounts: UserAccount[];
|
|
@@ -176,6 +174,8 @@ export class DriftClient {
|
|
|
176
174
|
txParams: TxParams;
|
|
177
175
|
enableMetricsEvents?: boolean;
|
|
178
176
|
|
|
177
|
+
txHandler: TxHandler;
|
|
178
|
+
|
|
179
179
|
public get isSubscribed() {
|
|
180
180
|
return this._isSubscribed && this.accountSubscriber.isSubscribed;
|
|
181
181
|
}
|
|
@@ -213,6 +213,19 @@ export class DriftClient {
|
|
|
213
213
|
computeUnitsPrice: config.txParams?.computeUnitsPrice ?? 0,
|
|
214
214
|
};
|
|
215
215
|
|
|
216
|
+
this.txHandler =
|
|
217
|
+
config?.txHandler ??
|
|
218
|
+
new TxHandler({
|
|
219
|
+
connection: this.connection,
|
|
220
|
+
wallet: this.provider.wallet,
|
|
221
|
+
confirmationOptions: this.opts,
|
|
222
|
+
opts: {
|
|
223
|
+
returnBlockHeightsWithSignedTxCallbackData:
|
|
224
|
+
config.enableMetricsEvents,
|
|
225
|
+
onSignedCb: this.handleSignedTransaction.bind(this),
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
|
|
216
229
|
if (config.includeDelegates && config.subAccountIds) {
|
|
217
230
|
throw new Error(
|
|
218
231
|
'Can only pass one of includeDelegates or subAccountIds. If you want to specify subaccount ids for multiple authorities, pass authoritySubaccountMap instead'
|
|
@@ -309,9 +322,10 @@ export class DriftClient {
|
|
|
309
322
|
}
|
|
310
323
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
311
324
|
|
|
325
|
+
this.metricsEventEmitter = new EventEmitter();
|
|
326
|
+
|
|
312
327
|
if (config.enableMetricsEvents) {
|
|
313
328
|
this.enableMetricsEvents = true;
|
|
314
|
-
this.metricsEventEmitter = new EventEmitter();
|
|
315
329
|
}
|
|
316
330
|
|
|
317
331
|
this.txSender =
|
|
@@ -320,6 +334,7 @@ export class DriftClient {
|
|
|
320
334
|
connection: this.connection,
|
|
321
335
|
wallet: this.wallet,
|
|
322
336
|
opts: this.opts,
|
|
337
|
+
txHandler: this.txHandler,
|
|
323
338
|
});
|
|
324
339
|
}
|
|
325
340
|
|
|
@@ -564,6 +579,7 @@ export class DriftClient {
|
|
|
564
579
|
// Update provider for txSender with new wallet details
|
|
565
580
|
this.txSender.wallet = newWallet;
|
|
566
581
|
this.wallet = newWallet;
|
|
582
|
+
this.txHandler.updateWallet(newWallet);
|
|
567
583
|
this.provider = newProvider;
|
|
568
584
|
this.program = newProgram;
|
|
569
585
|
this.authority = newWallet.publicKey;
|
|
@@ -747,39 +763,6 @@ export class DriftClient {
|
|
|
747
763
|
return result;
|
|
748
764
|
}
|
|
749
765
|
|
|
750
|
-
private async getProcessedTransactionParams(
|
|
751
|
-
txParams: {
|
|
752
|
-
instructions: TransactionInstruction | TransactionInstruction[];
|
|
753
|
-
txParams?: BaseTxParams;
|
|
754
|
-
txVersion?: TransactionVersion;
|
|
755
|
-
lookupTables?: AddressLookupTableAccount[];
|
|
756
|
-
},
|
|
757
|
-
txParamProcessingParams: ProcessingTxParams
|
|
758
|
-
): Promise<BaseTxParams> {
|
|
759
|
-
const tx = await TransactionParamProcessor.process({
|
|
760
|
-
txProps: {
|
|
761
|
-
instructions: txParams.instructions,
|
|
762
|
-
txParams: txParams.txParams,
|
|
763
|
-
txVersion: txParams.txVersion,
|
|
764
|
-
lookupTables: txParams.lookupTables,
|
|
765
|
-
},
|
|
766
|
-
txBuilder: (updatedTxParams) =>
|
|
767
|
-
this.buildTransaction(
|
|
768
|
-
updatedTxParams.instructions,
|
|
769
|
-
updatedTxParams?.txParams,
|
|
770
|
-
updatedTxParams.txVersion,
|
|
771
|
-
updatedTxParams.lookupTables,
|
|
772
|
-
true
|
|
773
|
-
) as Promise<VersionedTransaction>,
|
|
774
|
-
processConfig: txParamProcessingParams,
|
|
775
|
-
processParams: {
|
|
776
|
-
connection: this.connection,
|
|
777
|
-
},
|
|
778
|
-
});
|
|
779
|
-
|
|
780
|
-
return tx;
|
|
781
|
-
}
|
|
782
|
-
|
|
783
766
|
public async initializeUserAccount(
|
|
784
767
|
subAccountId = 0,
|
|
785
768
|
name?: string,
|
|
@@ -2763,11 +2746,14 @@ export class DriftClient {
|
|
|
2763
2746
|
);
|
|
2764
2747
|
}
|
|
2765
2748
|
|
|
2766
|
-
public async sendSignedTx(
|
|
2749
|
+
public async sendSignedTx(
|
|
2750
|
+
tx: Transaction,
|
|
2751
|
+
opts?: ConfirmOptions
|
|
2752
|
+
): Promise<TransactionSignature> {
|
|
2767
2753
|
const { txSig } = await this.sendTransaction(
|
|
2768
2754
|
tx,
|
|
2769
2755
|
undefined,
|
|
2770
|
-
this.opts,
|
|
2756
|
+
opts ?? this.opts,
|
|
2771
2757
|
true
|
|
2772
2758
|
);
|
|
2773
2759
|
|
|
@@ -2809,173 +2795,90 @@ export class DriftClient {
|
|
|
2809
2795
|
userAccount.subAccountId
|
|
2810
2796
|
);
|
|
2811
2797
|
|
|
2798
|
+
const ixsToSign: { key: string; ix: TransactionInstruction }[] = [];
|
|
2799
|
+
|
|
2800
|
+
const keys = {
|
|
2801
|
+
signedCancelExistingOrdersTx: 'signedCancelExistingOrdersTx',
|
|
2802
|
+
signedSettlePnlTx: 'signedSettlePnlTx',
|
|
2803
|
+
signedFillTx: 'signedFillTx',
|
|
2804
|
+
signedMarketOrderTx: 'signedMarketOrderTx',
|
|
2805
|
+
};
|
|
2806
|
+
|
|
2812
2807
|
/* Cancel open orders in market if requested */
|
|
2813
|
-
let cancelExistingOrdersTx;
|
|
2814
|
-
if (cancelExistingOrders && isVariant(orderParams.marketType, 'perp')) {
|
|
2815
|
-
const cancelOrdersIx = await this.getCancelOrdersIx(
|
|
2816
|
-
orderParams.marketType,
|
|
2817
|
-
orderParams.marketIndex,
|
|
2818
|
-
null,
|
|
2819
|
-
userAccount.subAccountId
|
|
2820
|
-
);
|
|
2821
2808
|
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2809
|
+
if (cancelExistingOrders && isVariant(orderParams.marketType, 'perp')) {
|
|
2810
|
+
ixsToSign.push({
|
|
2811
|
+
key: keys.signedCancelExistingOrdersTx,
|
|
2812
|
+
ix: await this.getCancelOrdersIx(
|
|
2813
|
+
orderParams.marketType,
|
|
2814
|
+
orderParams.marketIndex,
|
|
2815
|
+
null,
|
|
2816
|
+
userAccount.subAccountId
|
|
2817
|
+
),
|
|
2818
|
+
});
|
|
2828
2819
|
}
|
|
2829
2820
|
|
|
2830
2821
|
/* Settle PnL after fill if requested */
|
|
2831
|
-
let settlePnlTx;
|
|
2832
2822
|
if (settlePnl && isVariant(orderParams.marketType, 'perp')) {
|
|
2833
|
-
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
[settlePnlIx],
|
|
2842
|
-
txParams,
|
|
2843
|
-
this.txVersion
|
|
2844
|
-
);
|
|
2823
|
+
ixsToSign.push({
|
|
2824
|
+
key: keys.signedSettlePnlTx,
|
|
2825
|
+
ix: await this.settlePNLIx(
|
|
2826
|
+
userAccountPublicKey,
|
|
2827
|
+
userAccount,
|
|
2828
|
+
marketIndex
|
|
2829
|
+
),
|
|
2830
|
+
});
|
|
2845
2831
|
}
|
|
2846
2832
|
|
|
2847
2833
|
// use versioned transactions if there is a lookup table account and wallet is compatible
|
|
2848
2834
|
if (this.txVersion === 0) {
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
userAccount.subAccountId
|
|
2865
|
-
);
|
|
2866
|
-
|
|
2867
|
-
const versionedFillTx = await this.buildTransaction(
|
|
2868
|
-
[fillPerpOrderIx],
|
|
2869
|
-
txParams,
|
|
2870
|
-
0
|
|
2871
|
-
);
|
|
2872
|
-
|
|
2873
|
-
const allPossibleTxs = [
|
|
2874
|
-
versionedMarketOrderTx,
|
|
2875
|
-
versionedFillTx,
|
|
2876
|
-
cancelExistingOrdersTx,
|
|
2877
|
-
settlePnlTx,
|
|
2878
|
-
];
|
|
2879
|
-
const txKeys = [
|
|
2880
|
-
'signedVersionedMarketOrderTx',
|
|
2881
|
-
'signedVersionedFillTx',
|
|
2882
|
-
'signedCancelExistingOrdersTx',
|
|
2883
|
-
'signedSettlePnlTx',
|
|
2884
|
-
];
|
|
2885
|
-
|
|
2886
|
-
const {
|
|
2887
|
-
signedVersionedMarketOrderTx,
|
|
2888
|
-
signedVersionedFillTx,
|
|
2889
|
-
signedCancelExistingOrdersTx,
|
|
2890
|
-
signedSettlePnlTx,
|
|
2891
|
-
} = await getSignedTransactionMap(
|
|
2892
|
-
//@ts-ignore
|
|
2893
|
-
this.provider.wallet,
|
|
2894
|
-
allPossibleTxs,
|
|
2895
|
-
txKeys
|
|
2896
|
-
);
|
|
2897
|
-
|
|
2898
|
-
const { txSig, slot } = await this.sendTransaction(
|
|
2899
|
-
signedVersionedMarketOrderTx,
|
|
2900
|
-
[],
|
|
2901
|
-
this.opts,
|
|
2902
|
-
true
|
|
2903
|
-
);
|
|
2904
|
-
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
2905
|
-
|
|
2906
|
-
return {
|
|
2907
|
-
txSig,
|
|
2908
|
-
// @ts-ignore
|
|
2909
|
-
signedFillTx: signedVersionedFillTx,
|
|
2910
|
-
// @ts-ignore
|
|
2911
|
-
signedCancelExistingOrdersTx,
|
|
2912
|
-
// @ts-ignore
|
|
2913
|
-
signedSettlePnlTx,
|
|
2914
|
-
};
|
|
2915
|
-
} else {
|
|
2916
|
-
const marketOrderTx = wrapInTx(
|
|
2917
|
-
ordersIx,
|
|
2918
|
-
txParams?.computeUnits,
|
|
2919
|
-
txParams?.computeUnitsPrice
|
|
2920
|
-
);
|
|
2921
|
-
|
|
2922
|
-
// Apply the latest blockhash to the txs so that we can sign before sending them
|
|
2923
|
-
const currentBlockHash = (
|
|
2924
|
-
await this.connection.getLatestBlockhash('finalized')
|
|
2925
|
-
).blockhash;
|
|
2926
|
-
marketOrderTx.recentBlockhash = currentBlockHash;
|
|
2835
|
+
ixsToSign.push({
|
|
2836
|
+
key: keys.signedFillTx,
|
|
2837
|
+
ix: await this.getFillPerpOrderIx(
|
|
2838
|
+
userAccountPublicKey,
|
|
2839
|
+
userAccount,
|
|
2840
|
+
{
|
|
2841
|
+
orderId,
|
|
2842
|
+
marketIndex,
|
|
2843
|
+
},
|
|
2844
|
+
makerInfo,
|
|
2845
|
+
referrerInfo,
|
|
2846
|
+
userAccount.subAccountId
|
|
2847
|
+
),
|
|
2848
|
+
});
|
|
2849
|
+
}
|
|
2927
2850
|
|
|
2928
|
-
|
|
2851
|
+
// Apply the latest blockhash to the txs so that we can sign before sending them
|
|
2852
|
+
ixsToSign.push({
|
|
2853
|
+
key: keys.signedMarketOrderTx,
|
|
2854
|
+
ix: ordersIx,
|
|
2855
|
+
});
|
|
2929
2856
|
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2857
|
+
const signedTransactions = await this.buildAndSignBulkTransactions(
|
|
2858
|
+
ixsToSign.map((ix) => ix.ix),
|
|
2859
|
+
ixsToSign.map((ix) => ix.key),
|
|
2860
|
+
txParams
|
|
2861
|
+
);
|
|
2934
2862
|
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2863
|
+
const { txSig, slot } = await this.sendTransaction(
|
|
2864
|
+
signedTransactions[keys.signedMarketOrderTx],
|
|
2865
|
+
[],
|
|
2866
|
+
this.opts,
|
|
2867
|
+
true
|
|
2868
|
+
);
|
|
2939
2869
|
|
|
2940
|
-
|
|
2941
|
-
marketOrderTx,
|
|
2942
|
-
cancelExistingOrdersTx,
|
|
2943
|
-
settlePnlTx,
|
|
2944
|
-
];
|
|
2945
|
-
const txKeys = [
|
|
2946
|
-
'signedMarketOrderTx',
|
|
2947
|
-
'signedCancelExistingOrdersTx',
|
|
2948
|
-
'signedSettlePnlTx',
|
|
2949
|
-
];
|
|
2950
|
-
|
|
2951
|
-
const {
|
|
2952
|
-
signedMarketOrderTx,
|
|
2953
|
-
signedCancelExistingOrdersTx,
|
|
2954
|
-
signedSettlePnlTx,
|
|
2955
|
-
} = await getSignedTransactionMap(
|
|
2956
|
-
//@ts-ignore
|
|
2957
|
-
this.provider.wallet,
|
|
2958
|
-
allPossibleTxs,
|
|
2959
|
-
txKeys
|
|
2960
|
-
);
|
|
2870
|
+
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
2961
2871
|
|
|
2962
|
-
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
signedFillTx: undefined,
|
|
2973
|
-
//@ts-ignore
|
|
2974
|
-
signedCancelExistingOrdersTx,
|
|
2975
|
-
//@ts-ignore
|
|
2976
|
-
signedSettlePnlTx,
|
|
2977
|
-
};
|
|
2978
|
-
}
|
|
2872
|
+
return {
|
|
2873
|
+
txSig,
|
|
2874
|
+
signedFillTx: signedTransactions?.[keys.signedFillTx] as Transaction,
|
|
2875
|
+
signedCancelExistingOrdersTx: signedTransactions?.[
|
|
2876
|
+
keys.signedCancelExistingOrdersTx
|
|
2877
|
+
] as Transaction,
|
|
2878
|
+
signedSettlePnlTx: signedTransactions?.[
|
|
2879
|
+
keys.signedSettlePnlTx
|
|
2880
|
+
] as Transaction,
|
|
2881
|
+
};
|
|
2979
2882
|
}
|
|
2980
2883
|
|
|
2981
2884
|
public async placePerpOrder(
|
|
@@ -4086,7 +3989,7 @@ export class DriftClient {
|
|
|
4086
3989
|
const { beginSwapIx, endSwapIx } = await this.getSwapIx({
|
|
4087
3990
|
outMarketIndex,
|
|
4088
3991
|
inMarketIndex,
|
|
4089
|
-
amountIn:
|
|
3992
|
+
amountIn: new BN(route.inAmount),
|
|
4090
3993
|
inTokenAccount: inAssociatedTokenAccount,
|
|
4091
3994
|
outTokenAccount: outAssociatedTokenAccount,
|
|
4092
3995
|
reduceOnly,
|
|
@@ -4216,7 +4119,7 @@ export class DriftClient {
|
|
|
4216
4119
|
const { beginSwapIx, endSwapIx } = await this.getSwapIx({
|
|
4217
4120
|
outMarketIndex,
|
|
4218
4121
|
inMarketIndex,
|
|
4219
|
-
amountIn:
|
|
4122
|
+
amountIn: new BN(quote.inAmount),
|
|
4220
4123
|
inTokenAccount: inAssociatedTokenAccount,
|
|
4221
4124
|
outTokenAccount: outAssociatedTokenAccount,
|
|
4222
4125
|
reduceOnly,
|
|
@@ -4625,7 +4528,7 @@ export class DriftClient {
|
|
|
4625
4528
|
signedCancelExistingOrdersTx?: Transaction;
|
|
4626
4529
|
signedSettlePnlTx?: Transaction;
|
|
4627
4530
|
}> {
|
|
4628
|
-
const
|
|
4531
|
+
const placeAndTakeIxs: TransactionInstruction[] = [];
|
|
4629
4532
|
|
|
4630
4533
|
const placeAndTakeIx = await this.getPlaceAndTakePerpOrderIx(
|
|
4631
4534
|
orderParams,
|
|
@@ -4634,63 +4537,88 @@ export class DriftClient {
|
|
|
4634
4537
|
subAccountId
|
|
4635
4538
|
);
|
|
4636
4539
|
|
|
4637
|
-
|
|
4540
|
+
const txsToSign: {
|
|
4541
|
+
key: string;
|
|
4542
|
+
tx: Transaction | VersionedTransaction;
|
|
4543
|
+
}[] = [];
|
|
4544
|
+
|
|
4545
|
+
const keys = {
|
|
4546
|
+
placeAndTakeIx: 'placeAndTakeIx',
|
|
4547
|
+
cancelExistingOrdersTx: 'cancelExistingOrdersTx',
|
|
4548
|
+
settlePnlTx: 'settlePnlTx',
|
|
4549
|
+
};
|
|
4550
|
+
|
|
4551
|
+
placeAndTakeIxs.push(placeAndTakeIx);
|
|
4638
4552
|
|
|
4639
4553
|
if (bracketOrdersParams.length > 0) {
|
|
4640
4554
|
const bracketOrdersIx = await this.getPlaceOrdersIx(
|
|
4641
4555
|
bracketOrdersParams,
|
|
4642
4556
|
subAccountId
|
|
4643
4557
|
);
|
|
4644
|
-
|
|
4558
|
+
placeAndTakeIxs.push(bracketOrdersIx);
|
|
4645
4559
|
}
|
|
4646
4560
|
|
|
4647
4561
|
const shouldUseSimulationComputeUnits = txParams?.useSimulatedComputeUnits;
|
|
4648
4562
|
const shouldExitIfSimulationFails = simulateFirst;
|
|
4649
4563
|
|
|
4650
|
-
const txParamsWithoutImplicitSimulation = {
|
|
4564
|
+
const txParamsWithoutImplicitSimulation: TxParams = {
|
|
4651
4565
|
...txParams,
|
|
4652
|
-
|
|
4566
|
+
useSimulatedComputeUnits: false,
|
|
4653
4567
|
};
|
|
4654
4568
|
|
|
4655
|
-
|
|
4656
|
-
|
|
4657
|
-
|
|
4658
|
-
);
|
|
4569
|
+
// Get recent block hash so that we can re-use it for all transactions. Makes this logic run faster with fewer RPC requests
|
|
4570
|
+
const recentBlockHash =
|
|
4571
|
+
await this.txHandler.getLatestBlockhashForTransaction();
|
|
4659
4572
|
|
|
4660
4573
|
if (shouldUseSimulationComputeUnits || shouldExitIfSimulationFails) {
|
|
4661
|
-
|
|
4662
|
-
|
|
4663
|
-
|
|
4664
|
-
|
|
4665
|
-
|
|
4666
|
-
|
|
4667
|
-
|
|
4668
|
-
|
|
4669
|
-
undefined,
|
|
4670
|
-
undefined,
|
|
4671
|
-
true
|
|
4672
|
-
)) as VersionedTransaction;
|
|
4673
|
-
}
|
|
4574
|
+
const placeAndTakeTxToSim = (await this.buildTransaction(
|
|
4575
|
+
placeAndTakeIxs,
|
|
4576
|
+
txParamsWithoutImplicitSimulation,
|
|
4577
|
+
undefined,
|
|
4578
|
+
undefined,
|
|
4579
|
+
true,
|
|
4580
|
+
recentBlockHash
|
|
4581
|
+
)) as VersionedTransaction;
|
|
4674
4582
|
|
|
4675
4583
|
const simulationResult =
|
|
4676
4584
|
await TransactionParamProcessor.getTxSimComputeUnits(
|
|
4677
|
-
|
|
4678
|
-
this.connection
|
|
4585
|
+
placeAndTakeTxToSim,
|
|
4586
|
+
this.connection,
|
|
4587
|
+
txParams.computeUnitsBufferMultiplier ?? 1.2
|
|
4679
4588
|
);
|
|
4680
4589
|
|
|
4681
4590
|
if (shouldExitIfSimulationFails && !simulationResult.success) {
|
|
4682
4591
|
return;
|
|
4683
4592
|
}
|
|
4684
4593
|
|
|
4685
|
-
|
|
4686
|
-
|
|
4687
|
-
|
|
4688
|
-
|
|
4689
|
-
|
|
4690
|
-
|
|
4594
|
+
txsToSign.push({
|
|
4595
|
+
key: keys.placeAndTakeIx,
|
|
4596
|
+
tx: await this.buildTransaction(
|
|
4597
|
+
placeAndTakeIxs,
|
|
4598
|
+
{
|
|
4599
|
+
...txParamsWithoutImplicitSimulation,
|
|
4600
|
+
computeUnits: simulationResult.computeUnits,
|
|
4601
|
+
},
|
|
4602
|
+
undefined,
|
|
4603
|
+
undefined,
|
|
4604
|
+
undefined,
|
|
4605
|
+
recentBlockHash
|
|
4606
|
+
),
|
|
4607
|
+
});
|
|
4608
|
+
} else {
|
|
4609
|
+
txsToSign.push({
|
|
4610
|
+
key: keys.placeAndTakeIx,
|
|
4611
|
+
tx: await this.buildTransaction(
|
|
4612
|
+
placeAndTakeIxs,
|
|
4613
|
+
txParams,
|
|
4614
|
+
undefined,
|
|
4615
|
+
undefined,
|
|
4616
|
+
undefined,
|
|
4617
|
+
recentBlockHash
|
|
4618
|
+
),
|
|
4619
|
+
});
|
|
4691
4620
|
}
|
|
4692
4621
|
|
|
4693
|
-
let cancelExistingOrdersTx: Transaction;
|
|
4694
4622
|
if (cancelExistingOrders && isVariant(orderParams.marketType, 'perp')) {
|
|
4695
4623
|
const cancelOrdersIx = await this.getCancelOrdersIx(
|
|
4696
4624
|
orderParams.marketType,
|
|
@@ -4699,16 +4627,19 @@ export class DriftClient {
|
|
|
4699
4627
|
subAccountId
|
|
4700
4628
|
);
|
|
4701
4629
|
|
|
4702
|
-
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
|
|
4707
|
-
|
|
4630
|
+
txsToSign.push({
|
|
4631
|
+
key: keys.cancelExistingOrdersTx,
|
|
4632
|
+
tx: await this.buildTransaction(
|
|
4633
|
+
[cancelOrdersIx],
|
|
4634
|
+
txParams,
|
|
4635
|
+
this.txVersion,
|
|
4636
|
+
undefined,
|
|
4637
|
+
undefined,
|
|
4638
|
+
recentBlockHash
|
|
4639
|
+
),
|
|
4640
|
+
});
|
|
4708
4641
|
}
|
|
4709
4642
|
|
|
4710
|
-
/* Settle PnL after fill if requested */
|
|
4711
|
-
let settlePnlTx: Transaction;
|
|
4712
4643
|
if (settlePnl && isVariant(orderParams.marketType, 'perp')) {
|
|
4713
4644
|
const userAccountPublicKey = await this.getUserAccountPublicKey(
|
|
4714
4645
|
subAccountId
|
|
@@ -4720,46 +4651,40 @@ export class DriftClient {
|
|
|
4720
4651
|
orderParams.marketIndex
|
|
4721
4652
|
);
|
|
4722
4653
|
|
|
4723
|
-
|
|
4724
|
-
|
|
4725
|
-
|
|
4726
|
-
|
|
4727
|
-
|
|
4728
|
-
|
|
4654
|
+
txsToSign.push({
|
|
4655
|
+
key: keys.settlePnlTx,
|
|
4656
|
+
tx: await this.buildTransaction(
|
|
4657
|
+
[settlePnlIx],
|
|
4658
|
+
txParams,
|
|
4659
|
+
this.txVersion,
|
|
4660
|
+
undefined,
|
|
4661
|
+
undefined,
|
|
4662
|
+
recentBlockHash
|
|
4663
|
+
),
|
|
4664
|
+
});
|
|
4729
4665
|
}
|
|
4730
4666
|
|
|
4731
|
-
const
|
|
4732
|
-
|
|
4733
|
-
|
|
4734
|
-
|
|
4735
|
-
];
|
|
4736
|
-
const txKeys = [
|
|
4737
|
-
'signedPlaceAndTakeTx',
|
|
4738
|
-
'signedCancelExistingOrdersTx',
|
|
4739
|
-
'signedSettlePnlTx',
|
|
4740
|
-
];
|
|
4741
|
-
|
|
4742
|
-
const {
|
|
4743
|
-
signedPlaceAndTakeTx,
|
|
4744
|
-
signedCancelExistingOrdersTx,
|
|
4745
|
-
signedSettlePnlTx,
|
|
4746
|
-
} = await getSignedTransactionMap(
|
|
4747
|
-
//@ts-ignore
|
|
4748
|
-
this.provider.wallet,
|
|
4749
|
-
allPossibleTxs,
|
|
4750
|
-
txKeys
|
|
4667
|
+
const signedTxs = await this.txHandler.getSignedTransactionMap(
|
|
4668
|
+
txsToSign.map((tx) => tx.tx),
|
|
4669
|
+
txsToSign.map((tx) => tx.key),
|
|
4670
|
+
this.provider.wallet
|
|
4751
4671
|
);
|
|
4752
4672
|
|
|
4753
4673
|
const { txSig, slot } = await this.sendTransaction(
|
|
4754
|
-
|
|
4674
|
+
signedTxs[keys.placeAndTakeIx],
|
|
4755
4675
|
[],
|
|
4756
4676
|
this.opts,
|
|
4757
4677
|
true
|
|
4758
4678
|
);
|
|
4759
4679
|
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
4760
4680
|
|
|
4761
|
-
|
|
4762
|
-
|
|
4681
|
+
return {
|
|
4682
|
+
txSig,
|
|
4683
|
+
signedCancelExistingOrdersTx: signedTxs[
|
|
4684
|
+
keys.cancelExistingOrdersTx
|
|
4685
|
+
] as Transaction,
|
|
4686
|
+
signedSettlePnlTx: signedTxs[keys.settlePnlTx] as Transaction,
|
|
4687
|
+
};
|
|
4763
4688
|
}
|
|
4764
4689
|
|
|
4765
4690
|
public async getPlaceAndTakePerpOrderIx(
|
|
@@ -6718,8 +6643,10 @@ export class DriftClient {
|
|
|
6718
6643
|
return undefined;
|
|
6719
6644
|
}
|
|
6720
6645
|
|
|
6721
|
-
private handleSignedTransaction() {
|
|
6722
|
-
this.metricsEventEmitter
|
|
6646
|
+
private handleSignedTransaction(signedTxs: SignedTxData[]) {
|
|
6647
|
+
if (this.enableMetricsEvents && this.metricsEventEmitter) {
|
|
6648
|
+
this.metricsEventEmitter.emit('txSigned', signedTxs);
|
|
6649
|
+
}
|
|
6723
6650
|
}
|
|
6724
6651
|
|
|
6725
6652
|
private isVersionedTransaction(
|
|
@@ -6738,13 +6665,6 @@ export class DriftClient {
|
|
|
6738
6665
|
opts?: ConfirmOptions,
|
|
6739
6666
|
preSigned?: boolean
|
|
6740
6667
|
): Promise<TxSigAndSlot> {
|
|
6741
|
-
const extraConfirmationOptions: ExtraConfirmationOptions = this
|
|
6742
|
-
.enableMetricsEvents
|
|
6743
|
-
? {
|
|
6744
|
-
onSignedCb: this.handleSignedTransaction.bind(this),
|
|
6745
|
-
}
|
|
6746
|
-
: undefined;
|
|
6747
|
-
|
|
6748
6668
|
const isVersionedTx = this.isVersionedTransaction(tx);
|
|
6749
6669
|
|
|
6750
6670
|
if (isVersionedTx) {
|
|
@@ -6752,136 +6672,79 @@ export class DriftClient {
|
|
|
6752
6672
|
tx as VersionedTransaction,
|
|
6753
6673
|
additionalSigners,
|
|
6754
6674
|
opts,
|
|
6755
|
-
preSigned
|
|
6756
|
-
extraConfirmationOptions
|
|
6675
|
+
preSigned
|
|
6757
6676
|
);
|
|
6758
6677
|
} else {
|
|
6759
6678
|
return this.txSender.send(
|
|
6760
6679
|
tx as Transaction,
|
|
6761
6680
|
additionalSigners,
|
|
6762
6681
|
opts,
|
|
6763
|
-
preSigned
|
|
6764
|
-
extraConfirmationOptions
|
|
6682
|
+
preSigned
|
|
6765
6683
|
);
|
|
6766
6684
|
}
|
|
6767
6685
|
}
|
|
6768
6686
|
|
|
6769
|
-
/**
|
|
6770
|
-
*
|
|
6771
|
-
* @param instructions
|
|
6772
|
-
* @param txParams
|
|
6773
|
-
* @param txVersion
|
|
6774
|
-
* @param lookupTables
|
|
6775
|
-
* @param forceVersionedTransaction Return a VersionedTransaction instance even if the version of the transaction is Legacy
|
|
6776
|
-
* @returns
|
|
6777
|
-
*/
|
|
6778
6687
|
async buildTransaction(
|
|
6779
6688
|
instructions: TransactionInstruction | TransactionInstruction[],
|
|
6780
6689
|
txParams?: TxParams,
|
|
6781
6690
|
txVersion?: TransactionVersion,
|
|
6782
6691
|
lookupTables?: AddressLookupTableAccount[],
|
|
6783
|
-
forceVersionedTransaction?: boolean
|
|
6692
|
+
forceVersionedTransaction?: boolean,
|
|
6693
|
+
recentBlockHash?: BlockhashWithExpiryBlockHeight
|
|
6784
6694
|
): Promise<Transaction | VersionedTransaction> {
|
|
6785
|
-
|
|
6786
|
-
|
|
6787
|
-
|
|
6788
|
-
|
|
6789
|
-
|
|
6790
|
-
|
|
6791
|
-
|
|
6792
|
-
|
|
6793
|
-
|
|
6794
|
-
|
|
6795
|
-
|
|
6796
|
-
|
|
6797
|
-
|
|
6798
|
-
} = {
|
|
6799
|
-
baseTxParams: {
|
|
6800
|
-
computeUnits: txParams?.computeUnits,
|
|
6801
|
-
computeUnitsPrice: txParams?.computeUnitsPrice,
|
|
6802
|
-
},
|
|
6803
|
-
txParamProcessingParams: {
|
|
6804
|
-
useSimulatedComputeUnits: txParams?.useSimulatedComputeUnits,
|
|
6805
|
-
computeUnitsBufferMultiplier: txParams?.computeUnitsBufferMultiplier,
|
|
6806
|
-
useSimulatedComputeUnitsForCUPriceCalculation:
|
|
6807
|
-
txParams?.useSimulatedComputeUnitsForCUPriceCalculation,
|
|
6808
|
-
getCUPriceFromComputeUnits: txParams?.getCUPriceFromComputeUnits,
|
|
6809
|
-
},
|
|
6810
|
-
};
|
|
6811
|
-
|
|
6812
|
-
const processedTxParams = await this.getProcessedTransactionParams(
|
|
6813
|
-
{
|
|
6814
|
-
instructions,
|
|
6815
|
-
txParams: splitTxParams.baseTxParams,
|
|
6816
|
-
txVersion,
|
|
6817
|
-
lookupTables,
|
|
6818
|
-
},
|
|
6819
|
-
splitTxParams.txParamProcessingParams
|
|
6820
|
-
);
|
|
6821
|
-
|
|
6822
|
-
baseTxParams = {
|
|
6823
|
-
...baseTxParams,
|
|
6824
|
-
...processedTxParams,
|
|
6825
|
-
};
|
|
6826
|
-
}
|
|
6827
|
-
|
|
6828
|
-
// # Create Tx Instructions
|
|
6829
|
-
const allIx = [];
|
|
6830
|
-
const computeUnits = baseTxParams?.computeUnits;
|
|
6831
|
-
if (computeUnits !== 200_000) {
|
|
6832
|
-
allIx.push(
|
|
6833
|
-
ComputeBudgetProgram.setComputeUnitLimit({
|
|
6834
|
-
units: computeUnits,
|
|
6835
|
-
})
|
|
6836
|
-
);
|
|
6837
|
-
}
|
|
6838
|
-
|
|
6839
|
-
const computeUnitsPrice = baseTxParams?.computeUnitsPrice;
|
|
6840
|
-
|
|
6841
|
-
if (computeUnitsPrice !== 0) {
|
|
6842
|
-
allIx.push(
|
|
6843
|
-
ComputeBudgetProgram.setComputeUnitPrice({
|
|
6844
|
-
microLamports: computeUnitsPrice,
|
|
6845
|
-
})
|
|
6846
|
-
);
|
|
6847
|
-
}
|
|
6848
|
-
|
|
6849
|
-
if (Array.isArray(instructions)) {
|
|
6850
|
-
allIx.push(...instructions);
|
|
6851
|
-
} else {
|
|
6852
|
-
allIx.push(instructions);
|
|
6853
|
-
}
|
|
6854
|
-
|
|
6855
|
-
const latestBlockHashAndContext =
|
|
6856
|
-
await this.connection.getLatestBlockhashAndContext({
|
|
6857
|
-
commitment: this.opts.preflightCommitment,
|
|
6858
|
-
});
|
|
6695
|
+
return this.txHandler.buildTransaction({
|
|
6696
|
+
instructions,
|
|
6697
|
+
txVersion: txVersion ?? this.txVersion,
|
|
6698
|
+
txParams: txParams ?? this.txParams,
|
|
6699
|
+
connection: this.connection,
|
|
6700
|
+
preFlightCommitment: this.opts.preflightCommitment,
|
|
6701
|
+
fetchMarketLookupTableAccount:
|
|
6702
|
+
this.fetchMarketLookupTableAccount.bind(this),
|
|
6703
|
+
lookupTables,
|
|
6704
|
+
forceVersionedTransaction,
|
|
6705
|
+
recentBlockHash,
|
|
6706
|
+
});
|
|
6707
|
+
}
|
|
6859
6708
|
|
|
6860
|
-
|
|
6861
|
-
|
|
6862
|
-
|
|
6863
|
-
|
|
6864
|
-
|
|
6865
|
-
|
|
6866
|
-
|
|
6867
|
-
|
|
6709
|
+
async buildBulkTransactions(
|
|
6710
|
+
instructions: (TransactionInstruction | TransactionInstruction[])[],
|
|
6711
|
+
txParams?: TxParams,
|
|
6712
|
+
txVersion?: TransactionVersion,
|
|
6713
|
+
lookupTables?: AddressLookupTableAccount[],
|
|
6714
|
+
forceVersionedTransaction?: boolean
|
|
6715
|
+
): Promise<(Transaction | VersionedTransaction)[]> {
|
|
6716
|
+
return this.txHandler.buildBulkTransactions({
|
|
6717
|
+
instructions,
|
|
6718
|
+
txVersion: txVersion ?? this.txVersion,
|
|
6719
|
+
txParams: txParams ?? this.txParams,
|
|
6720
|
+
connection: this.connection,
|
|
6721
|
+
preFlightCommitment: this.opts.preflightCommitment,
|
|
6722
|
+
fetchMarketLookupTableAccount:
|
|
6723
|
+
this.fetchMarketLookupTableAccount.bind(this),
|
|
6724
|
+
lookupTables,
|
|
6725
|
+
forceVersionedTransaction,
|
|
6726
|
+
});
|
|
6727
|
+
}
|
|
6868
6728
|
|
|
6869
|
-
|
|
6870
|
-
|
|
6871
|
-
|
|
6872
|
-
|
|
6873
|
-
|
|
6874
|
-
|
|
6875
|
-
|
|
6876
|
-
|
|
6877
|
-
|
|
6878
|
-
|
|
6879
|
-
|
|
6880
|
-
|
|
6881
|
-
|
|
6882
|
-
|
|
6883
|
-
|
|
6884
|
-
|
|
6885
|
-
|
|
6729
|
+
async buildAndSignBulkTransactions(
|
|
6730
|
+
instructions: (TransactionInstruction | TransactionInstruction[])[],
|
|
6731
|
+
keys: string[],
|
|
6732
|
+
txParams?: TxParams,
|
|
6733
|
+
txVersion?: TransactionVersion,
|
|
6734
|
+
lookupTables?: AddressLookupTableAccount[],
|
|
6735
|
+
forceVersionedTransaction?: boolean
|
|
6736
|
+
) {
|
|
6737
|
+
return this.txHandler.buildAndSignTransactionMap({
|
|
6738
|
+
instructions,
|
|
6739
|
+
txVersion: txVersion ?? this.txVersion,
|
|
6740
|
+
txParams: txParams ?? this.txParams,
|
|
6741
|
+
connection: this.connection,
|
|
6742
|
+
preFlightCommitment: this.opts.preflightCommitment,
|
|
6743
|
+
fetchMarketLookupTableAccount:
|
|
6744
|
+
this.fetchMarketLookupTableAccount.bind(this),
|
|
6745
|
+
lookupTables,
|
|
6746
|
+
forceVersionedTransaction,
|
|
6747
|
+
keys,
|
|
6748
|
+
});
|
|
6886
6749
|
}
|
|
6887
6750
|
}
|