Skip to content

Commit

Permalink
sdk: tx version updates (#651)
Browse files Browse the repository at this point in the history
* sdk: tx version updates

* updates

* improve logic

* flip logic due to anchor test failing
  • Loading branch information
lowkeynicc authored and crispheaney committed Oct 18, 2023
1 parent 8c26949 commit c3e863b
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -1574,13 +1578,6 @@ export class DriftClient {
subAccountId?: number,
reduceOnly = false
): Promise<TransactionSignature> {
const tx = new Transaction();
tx.add(
ComputeBudgetProgram.setComputeUnitLimit({
units: 600_000,
})
);

const additionalSigners: Array<Signer> = [];

const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
Expand All @@ -1592,6 +1589,8 @@ export class DriftClient {
const createWSOLTokenAccount =
isSolMarket && associatedTokenAccount.equals(signerAuthority);

const instructions = [];

if (createWSOLTokenAccount) {
const { ixs, pubkey } = await this.getWrappedSolAccountCreationIxs(
amount,
Expand All @@ -1600,9 +1599,7 @@ export class DriftClient {

associatedTokenAccount = pubkey;

ixs.forEach((ix) => {
tx.add(ix);
});
instructions.push(...ixs);
}

const depositCollateralIx = await this.getDepositInstruction(
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -2442,8 +2443,7 @@ export class DriftClient {
makerInfo?: MakerInfo | MakerInfo[],
txParams?: TxParams,
bracketOrdersParams = new Array<OptionalOrderParams>(),
referrerInfo?: ReferrerInfo,
useVersionedTx = true
referrerInfo?: ReferrerInfo
): Promise<{ txSig: TransactionSignature; signedFillTx: Transaction }> {
const marketIndex = orderParams.marketIndex;
const orderId = userAccount.nextOrderId;
Expand All @@ -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,
Expand Down Expand Up @@ -5852,17 +5848,16 @@ export class DriftClient {
opts?: ConfirmOptions,
preSigned?: boolean
): Promise<TxSigAndSlot> {
// @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
Expand Down

0 comments on commit c3e863b

Please sign in to comment.