diff --git a/sdk/src/driftClient.ts b/sdk/src/driftClient.ts index ded7d1e7f..a7e675879 100644 --- a/sdk/src/driftClient.ts +++ b/sdk/src/driftClient.ts @@ -1738,6 +1738,24 @@ export class DriftClient { return ix; } + /** + * Checks if a Swift User Orders account exists for the given authority. + * The account pubkey is derived using the program ID and authority as seeds. + * Makes an RPC call to check if the account exists on-chain. + * + * @param authority The authority public key to check for + * @returns Promise that resolves to true if the account exists, false otherwise + */ + public async isSwiftUserOrdersAccountInitialized( + authority: PublicKey + ): Promise { + const swiftUserOrdersAccountPublicKey = getSwiftUserAccountPublicKey( + this.program.programId, + authority + ); + return this.checkIfAccountExists(swiftUserOrdersAccountPublicKey); + } + public async reclaimRent( subAccountId = 0, txParams?: TxParams @@ -2530,6 +2548,18 @@ export class DriftClient { referrerInfo ); + const isSwiftUserOrdersAccountInitialized = + await this.isSwiftUserOrdersAccountInitialized(this.wallet.publicKey); + + if (!isSwiftUserOrdersAccountInitialized) { + const [, initializeSwiftUserOrdersAccountIx] = + await this.getInitializeSwiftUserOrdersAccountIx( + this.wallet.publicKey, + 8 + ); + ixs.push(initializeSwiftUserOrdersAccountIx); + } + const spotMarket = this.getSpotMarketAccount(marketIndex); const isSolMarket = spotMarket.mint.equals(WRAPPED_SOL_MINT); diff --git a/tests/placeAndMakeSwiftPerp.ts b/tests/placeAndMakeSwiftPerp.ts index 09b2fbd68..4ab5109b7 100644 --- a/tests/placeAndMakeSwiftPerp.ts +++ b/tests/placeAndMakeSwiftPerp.ts @@ -199,10 +199,6 @@ describe('place and make swift order', () => { }, }); await takerDriftClientUser.subscribe(); - await takerDriftClient.initializeSwiftUserOrders( - takerDriftClientUser.getUserAccount().authority, - 32 - ); const marketIndex = 0; const baseAssetAmount = BASE_PRECISION; @@ -324,10 +320,6 @@ describe('place and make swift order', () => { }, }); await takerDriftClientUser.subscribe(); - await takerDriftClient.initializeSwiftUserOrders( - takerDriftClientUser.getUserAccount().authority, - 32 - ); const marketIndex = 0; const baseAssetAmount = BASE_PRECISION; diff --git a/tests/placeAndMakeSwiftPerpBankrun.ts b/tests/placeAndMakeSwiftPerpBankrun.ts index 06498f1d0..b922da77d 100644 --- a/tests/placeAndMakeSwiftPerpBankrun.ts +++ b/tests/placeAndMakeSwiftPerpBankrun.ts @@ -1119,10 +1119,6 @@ async function initializeNewTakerClientAndUser( }, }); await takerDriftClientUser.subscribe(); - await takerDriftClient.initializeSwiftUserOrders( - takerDriftClientUser.getUserAccount().authority, - 32 - ); return [takerDriftClient, takerDriftClientUser]; }