From 47807da4b81fe473ebde23964cb0d6c39969b479 Mon Sep 17 00:00:00 2001 From: metaproph3t Date: Fri, 27 Oct 2023 00:00:00 +0000 Subject: [PATCH] Add `getOrCreateOpenOrders` function An analogue to `getOrCreateAssociatedTokenAccount`. It helps me, as a user of the TS client. --- ts/client/src/client.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ts/client/src/client.ts b/ts/client/src/client.ts index 8a715483..eebac353 100644 --- a/ts/client/src/client.ts +++ b/ts/client/src/client.ts @@ -329,6 +329,16 @@ export class OpenBookV2Client { return await this.sendAndConfirmTransaction([ix]); } + public async getOrCreateOpenOrders(market: PublicKey, accountIndex: BN, name: string): Promise { + const openOrders = this.findOpenOrders(market, accountIndex); + + if ((await this.connection.getAccountInfo(openOrders)) != null) { + return openOrders; + } + + return await this.createOpenOrders(market, accountIndex, name); + } + public findOpenOrders(market: PublicKey, accountIndex: BN): PublicKey { const [openOrders] = PublicKey.findProgramAddressSync( [