From c3e863bde0012a3ad6c2195c18852ac65c46ce19 Mon Sep 17 00:00:00 2001 From: lowkeynicc <85139158+lowkeynicc@users.noreply.github.com> Date: Tue, 17 Oct 2023 12:20:41 -0400 Subject: [PATCH] sdk: tx version updates (#651) * sdk: tx version updates * updates * improve logic * flip logic due to anchor test failing --- sdk/src/driftClient.ts | 45 +++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/sdk/src/driftClient.ts b/sdk/src/driftClient.ts index 80fc73c47..b86def378 100644 --- a/sdk/src/driftClient.ts +++ b/sdk/src/driftClient.ts @@ -539,6 +539,10 @@ export class DriftClient { this.activeSubAccountId = activeSubAccountId; this.userStatsAccountPublicKey = undefined; this.includeDelegates = includeDelegates ?? false; + const walletSupportsVersionedTxns = + //@ts-ignore + this.wallet.supportedTransactionVersions?.size ?? 0 > 1; + this.txVersion = walletSupportsVersionedTxns ? 0 : 'legacy'; if (includeDelegates && subAccountIds) { throw new Error( @@ -1574,13 +1578,6 @@ export class DriftClient { subAccountId?: number, reduceOnly = false ): Promise { - const tx = new Transaction(); - tx.add( - ComputeBudgetProgram.setComputeUnitLimit({ - units: 600_000, - }) - ); - const additionalSigners: Array = []; const spotMarketAccount = this.getSpotMarketAccount(marketIndex); @@ -1592,6 +1589,8 @@ export class DriftClient { const createWSOLTokenAccount = isSolMarket && associatedTokenAccount.equals(signerAuthority); + const instructions = []; + if (createWSOLTokenAccount) { const { ixs, pubkey } = await this.getWrappedSolAccountCreationIxs( amount, @@ -1600,9 +1599,7 @@ export class DriftClient { associatedTokenAccount = pubkey; - ixs.forEach((ix) => { - tx.add(ix); - }); + instructions.push(...ixs); } const depositCollateralIx = await this.getDepositInstruction( @@ -1614,11 +1611,11 @@ export class DriftClient { true ); - tx.add(depositCollateralIx); + instructions.push(depositCollateralIx); // Close the wrapped sol account at the end of the transaction if (createWSOLTokenAccount) { - tx.add( + instructions.push( createCloseAccountInstruction( associatedTokenAccount, signerAuthority, @@ -1628,6 +1625,10 @@ export class DriftClient { ); } + const txParams = { ...this.txParams, computeUnits: 600_000 }; + + const tx = await this.buildTransaction(instructions, txParams); + const { txSig, slot } = await this.sendTransaction( tx, additionalSigners, @@ -2442,8 +2443,7 @@ export class DriftClient { makerInfo?: MakerInfo | MakerInfo[], txParams?: TxParams, bracketOrdersParams = new Array(), - referrerInfo?: ReferrerInfo, - useVersionedTx = true + referrerInfo?: ReferrerInfo ): Promise<{ txSig: TransactionSignature; signedFillTx: Transaction }> { const marketIndex = orderParams.marketIndex; const orderId = userAccount.nextOrderId; @@ -2469,12 +2469,8 @@ export class DriftClient { referrerInfo ); - const walletSupportsVersionedTxns = - //@ts-ignore - this.wallet.supportedTransactionVersions?.size ?? 0 > 1; - // use versioned transactions if there is a lookup table account and wallet is compatible - if (walletSupportsVersionedTxns && useVersionedTx) { + if (this.txVersion === 0) { const versionedMarketOrderTx = await this.buildTransaction( [placePerpOrderIx].concat(bracketOrderIxs), txParams, @@ -5852,17 +5848,16 @@ export class DriftClient { opts?: ConfirmOptions, preSigned?: boolean ): Promise { - // @ts-ignore - if (!tx.message) { - return this.txSender.send( - tx as Transaction, + if (tx instanceof VersionedTransaction) { + return this.txSender.sendVersionedTransaction( + tx as VersionedTransaction, additionalSigners, opts, preSigned ); } else { - return this.txSender.sendVersionedTransaction( - tx as VersionedTransaction, + return this.txSender.send( + tx as Transaction, additionalSigners, opts, preSigned