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

feat: init swift user orders on user account creation if needed #1448

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
const swiftUserOrdersAccountPublicKey = getSwiftUserAccountPublicKey(
this.program.programId,
authority
);
return this.checkIfAccountExists(swiftUserOrdersAccountPublicKey);
}

public async reclaimRent(
subAccountId = 0,
txParams?: TxParams
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 0 additions & 8 deletions tests/placeAndMakeSwiftPerp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions tests/placeAndMakeSwiftPerpBankrun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1119,10 +1119,6 @@ async function initializeNewTakerClientAndUser(
},
});
await takerDriftClientUser.subscribe();
await takerDriftClient.initializeSwiftUserOrders(
takerDriftClientUser.getUserAccount().authority,
32
);
return [takerDriftClient, takerDriftClientUser];
}

Expand Down
Loading