From 4b52d9a0725ac4ecb3f88d227c57566beaa5344f Mon Sep 17 00:00:00 2001 From: microwavedcola1 <89031858+microwavedcola1@users.noreply.github.com> Date: Fri, 30 Sep 2022 12:16:24 +0200 Subject: [PATCH] ts: simplify getOrCreateMangoAccount (#256) Signed-off-by: microwavedcola1 Signed-off-by: microwavedcola1 --- ts/client/src/client.ts | 45 +++++-------------- .../example1-create-liquidation-candidate.ts | 11 +---- .../scripts/archive/example1-flash-loan.ts | 5 +-- ts/client/src/scripts/archive/example1-ob.ts | 5 +-- .../scripts/archive/example1-user-account.ts | 5 +-- .../src/scripts/archive/example1-user2.ts | 5 +-- .../src/scripts/archive/mb-flash-loan.ts | 5 +-- ts/client/src/scripts/devnet-user-2.ts | 5 +-- ts/client/src/scripts/devnet-user.ts | 5 +-- ts/client/src/scripts/mb-example1-admin.ts | 5 +-- ts/client/src/scripts/mb-example1-user.ts | 5 +-- 11 files changed, 22 insertions(+), 79 deletions(-) diff --git a/ts/client/src/client.ts b/ts/client/src/client.ts index d177e9b19..619c39dfa 100644 --- a/ts/client/src/client.ts +++ b/ts/client/src/client.ts @@ -505,41 +505,18 @@ export class MangoClient { // MangoAccount - public async getOrCreateMangoAccount( - group: Group, - ownerPk: PublicKey, - accountNumber?: number, - name?: string, - ): Promise { - // TODO: this function discards accountSize and name when the account exists already! - // TODO: this function always creates accounts for this.program.owner, and not - // ownerPk! It needs to get passed a keypair, and we need to add - // createMangoAccountForOwner - if (accountNumber === undefined) { - // Get any MangoAccount - // TODO: should probably sort by accountNum for deterministic output! - let mangoAccounts = await this.getMangoAccountsForOwner(group, ownerPk); - if (mangoAccounts.length === 0) { - await this.createMangoAccount(group, accountNumber, name); - mangoAccounts = await this.getMangoAccountsForOwner(group, ownerPk); - } - return mangoAccounts[0]; - } else { - let account = await this.getMangoAccountForOwner( - group, - ownerPk, - accountNumber, - ); - if (account === undefined) { - await this.createMangoAccount(group, accountNumber, name); - account = await this.getMangoAccountForOwner( - group, - ownerPk, - accountNumber, - ); - } - return account; + public async getOrCreateMangoAccount(group: Group): Promise { + const clientOwner = (this.program.provider as AnchorProvider).wallet + .publicKey; + let mangoAccounts = await this.getMangoAccountsForOwner( + group, + (this.program.provider as AnchorProvider).wallet.publicKey, + ); + if (mangoAccounts.length === 0) { + await this.createMangoAccount(group); + mangoAccounts = await this.getMangoAccountsForOwner(group, clientOwner); } + return mangoAccounts.sort((a, b) => a.accountNum - b.accountNum)[0]; } public async createMangoAccount( diff --git a/ts/client/src/scripts/archive/example1-create-liquidation-candidate.ts b/ts/client/src/scripts/archive/example1-create-liquidation-candidate.ts index 4598a00be..86d87212a 100644 --- a/ts/client/src/scripts/archive/example1-create-liquidation-candidate.ts +++ b/ts/client/src/scripts/archive/example1-create-liquidation-candidate.ts @@ -1,7 +1,6 @@ import { AnchorProvider, Wallet } from '@project-serum/anchor'; import { Connection, Keypair } from '@solana/web3.js'; import fs from 'fs'; -import { AccountSize } from '../../accounts/mangoAccount'; import { MangoClient } from '../../client'; import { MANGO_V4_ID } from '../../constants'; @@ -45,10 +44,7 @@ async function main() { const group = await user1Client.getGroupForAdmin(admin.publicKey, GROUP_NUM); console.log(`Found group ${group.publicKey.toBase58()}`); - const user1MangoAccount = await user1Client.getOrCreateMangoAccount( - group, - user1.publicKey, - ); + const user1MangoAccount = await user1Client.getOrCreateMangoAccount(group); console.log(`...mangoAccount1 ${user1MangoAccount.publicKey}`); @@ -75,10 +71,7 @@ async function main() { ); console.log(`user2 ${user2Wallet.publicKey.toBase58()}`); - const user2MangoAccount = await user2Client.getOrCreateMangoAccount( - group, - user2.publicKey, - ); + const user2MangoAccount = await user2Client.getOrCreateMangoAccount(group); console.log(`...mangoAccount2 ${user2MangoAccount.publicKey}`); /// Increase usdc price temporarily to allow lots of borrows diff --git a/ts/client/src/scripts/archive/example1-flash-loan.ts b/ts/client/src/scripts/archive/example1-flash-loan.ts index 71ceacc1e..a6539e4cf 100644 --- a/ts/client/src/scripts/archive/example1-flash-loan.ts +++ b/ts/client/src/scripts/archive/example1-flash-loan.ts @@ -48,10 +48,7 @@ async function main() { // create + fetch account console.log(`Creating mangoaccount...`); - const mangoAccount = await client.getOrCreateMangoAccount( - group, - user.publicKey, - ); + const mangoAccount = await client.getOrCreateMangoAccount(group); console.log(`...created/found mangoAccount ${mangoAccount.publicKey}`); console.log(mangoAccount.toString()); diff --git a/ts/client/src/scripts/archive/example1-ob.ts b/ts/client/src/scripts/archive/example1-ob.ts index d1d23aeb6..2e351bb9d 100644 --- a/ts/client/src/scripts/archive/example1-ob.ts +++ b/ts/client/src/scripts/archive/example1-ob.ts @@ -42,10 +42,7 @@ async function main() { // create + fetch account console.log(`Creating mangoaccount...`); - const mangoAccount = await client.getOrCreateMangoAccount( - group, - user.publicKey, - ); + const mangoAccount = await client.getOrCreateMangoAccount(group); console.log(`...created/found mangoAccount ${mangoAccount.publicKey}`); // logging serum3 open orders for user diff --git a/ts/client/src/scripts/archive/example1-user-account.ts b/ts/client/src/scripts/archive/example1-user-account.ts index 892d77748..07f513689 100644 --- a/ts/client/src/scripts/archive/example1-user-account.ts +++ b/ts/client/src/scripts/archive/example1-user-account.ts @@ -43,10 +43,7 @@ async function main() { // create + fetch account console.log(`Creating mangoaccount...`); - const mangoAccount = await client.getOrCreateMangoAccount( - group, - user.publicKey, - ); + const mangoAccount = await client.getOrCreateMangoAccount(group); console.log(`...created/found mangoAccount ${mangoAccount.publicKey}`); // log users tokens diff --git a/ts/client/src/scripts/archive/example1-user2.ts b/ts/client/src/scripts/archive/example1-user2.ts index 2ecb82680..a1ae7614b 100644 --- a/ts/client/src/scripts/archive/example1-user2.ts +++ b/ts/client/src/scripts/archive/example1-user2.ts @@ -48,10 +48,7 @@ async function main() { // create + fetch account console.log(`Creating mangoaccount...`); - const mangoAccount = await client.getOrCreateMangoAccount( - group, - user.publicKey, - ); + const mangoAccount = await client.getOrCreateMangoAccount(group); console.log(`...created/found mangoAccount ${mangoAccount.publicKey}`); console.log(mangoAccount.toString()); diff --git a/ts/client/src/scripts/archive/mb-flash-loan.ts b/ts/client/src/scripts/archive/mb-flash-loan.ts index a75322267..79bb32e67 100644 --- a/ts/client/src/scripts/archive/mb-flash-loan.ts +++ b/ts/client/src/scripts/archive/mb-flash-loan.ts @@ -53,10 +53,7 @@ async function main() { // create + fetch account console.log(`Creating mangoaccount...`); - const mangoAccount = await client.getOrCreateMangoAccount( - group, - user.publicKey, - ); + const mangoAccount = await client.getOrCreateMangoAccount(group); console.log(`...created/found mangoAccount ${mangoAccount.publicKey}`); console.log(`start balance \n${mangoAccount.toString(group)}`); diff --git a/ts/client/src/scripts/devnet-user-2.ts b/ts/client/src/scripts/devnet-user-2.ts index 14b532968..0f1e8d170 100644 --- a/ts/client/src/scripts/devnet-user-2.ts +++ b/ts/client/src/scripts/devnet-user-2.ts @@ -71,10 +71,7 @@ async function main() { // create + fetch account console.log(`Creating mangoaccount...`); - const mangoAccount = await client.getOrCreateMangoAccount( - group, - user.publicKey, - ); + const mangoAccount = await client.getOrCreateMangoAccount(group); console.log(`...created/found mangoAccount ${mangoAccount.publicKey}`); console.log(mangoAccount.toString(group)); diff --git a/ts/client/src/scripts/devnet-user.ts b/ts/client/src/scripts/devnet-user.ts index fa58ee709..c379b5651 100644 --- a/ts/client/src/scripts/devnet-user.ts +++ b/ts/client/src/scripts/devnet-user.ts @@ -68,10 +68,7 @@ async function main() { // create + fetch account console.log(`Creating mangoaccount...`); - let mangoAccount = (await client.getOrCreateMangoAccount( - group, - user.publicKey, - ))!; + let mangoAccount = (await client.getOrCreateMangoAccount(group))!; if (!mangoAccount) { throw new Error(`MangoAccount not found for user ${user.publicKey}`); } diff --git a/ts/client/src/scripts/mb-example1-admin.ts b/ts/client/src/scripts/mb-example1-admin.ts index c80f204af..887d37a02 100644 --- a/ts/client/src/scripts/mb-example1-admin.ts +++ b/ts/client/src/scripts/mb-example1-admin.ts @@ -337,10 +337,7 @@ async function createUser(userKeypair: string) { const user = result[2]; console.log(`Creating MangoAccount...`); - const mangoAccount = await client.getOrCreateMangoAccount( - group, - user.publicKey, - ); + const mangoAccount = await client.getOrCreateMangoAccount(group); if (!mangoAccount) { throw new Error(`MangoAccount not found for user ${user.publicKey}`); } diff --git a/ts/client/src/scripts/mb-example1-user.ts b/ts/client/src/scripts/mb-example1-user.ts index 9aa248a20..8cb46c95f 100644 --- a/ts/client/src/scripts/mb-example1-user.ts +++ b/ts/client/src/scripts/mb-example1-user.ts @@ -34,10 +34,7 @@ async function main() { // create + fetch account console.log(`Creating mangoaccount...`); - const mangoAccount = await client.getOrCreateMangoAccount( - group, - user.publicKey, - ); + const mangoAccount = await client.getOrCreateMangoAccount(group); console.log(`...created/found mangoAccount ${mangoAccount.publicKey}`); console.log(mangoAccount.toString(group));