diff --git a/src/accounts/margin/Margin.ts b/src/accounts/margin/Margin.ts index 78cd066..5414a6b 100644 --- a/src/accounts/margin/Margin.ts +++ b/src/accounts/margin/Margin.ts @@ -51,6 +51,23 @@ export default abstract class Margin extends MarginWeb3 { return await this.getPda(st, marginOwner, program.programId); } + + /** + * Loads the margin account, creating it first if needed + */ + static async createIfNeeded( + program: Program, + st: State, + commitment: Commitment = "finalized", + owner?: PublicKey, + ): Promise { + if (this.exists(program, st, owner)) { + return (await MarginWeb3.loadWeb3(program, st)) as Margin; + } else { + return (await MarginWeb3.create(program, st, commitment)) as Margin; + } + } + /** * get total funding amount from all the markets */