Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk: tx version updates #651

Merged
merged 5 commits into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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