From 919f09bb8cfd45f4f7865a1e1530a5465ea13146 Mon Sep 17 00:00:00 2001 From: microwavedcola1 <89031858+microwavedcola1@users.noreply.github.com> Date: Thu, 19 Jan 2023 11:31:54 +0100 Subject: [PATCH] fix bug where serum oo where not loaded (#403) * fix bug where serum oo where not loaded Signed-off-by: microwavedcola1 * fixes from review Signed-off-by: microwavedcola1 * fixes from review Signed-off-by: microwavedcola1 Signed-off-by: microwavedcola1 --- ts/client/src/accounts/mangoAccount.ts | 6 +- ts/client/src/accounts/perp.ts | 4 +- ts/client/src/client.ts | 80 ++++++++++++++++++------ ts/client/src/scripts/mm/log-perp-ob.ts | 2 +- ts/client/src/scripts/mm/market-maker.ts | 3 +- ts/client/src/scripts/mm/taker.ts | 2 +- 6 files changed, 71 insertions(+), 26 deletions(-) diff --git a/ts/client/src/accounts/mangoAccount.ts b/ts/client/src/accounts/mangoAccount.ts index 241d58243..d783f199b 100644 --- a/ts/client/src/accounts/mangoAccount.ts +++ b/ts/client/src/accounts/mangoAccount.ts @@ -90,7 +90,7 @@ export class MangoAccount { public async reload(client: MangoClient): Promise { const mangoAccount = await client.getMangoAccount(this); - await mangoAccount.reloadAccountData(client); + await mangoAccount.reloadSerum3OpenOrders(client); Object.assign(this, mangoAccount); return mangoAccount; } @@ -99,12 +99,12 @@ export class MangoAccount { client: MangoClient, ): Promise<{ value: MangoAccount; slot: number }> { const resp = await client.getMangoAccountWithSlot(this.publicKey); - await resp?.value.reloadAccountData(client); + await resp?.value.reloadSerum3OpenOrders(client); Object.assign(this, resp?.value); return { value: resp!.value, slot: resp!.slot }; } - async reloadAccountData(client: MangoClient): Promise { + async reloadSerum3OpenOrders(client: MangoClient): Promise { const serum3Active = this.serum3Active(); const ais = await client.program.provider.connection.getMultipleAccountsInfo( diff --git a/ts/client/src/accounts/perp.ts b/ts/client/src/accounts/perp.ts index 6bfe142c0..464a2db00 100644 --- a/ts/client/src/accounts/perp.ts +++ b/ts/client/src/accounts/perp.ts @@ -431,7 +431,9 @@ export class PerpMarket { direction: 'negative' | 'positive', count = 2, ): Promise<{ account: MangoAccount; settleablePnl: I80F48 }[]> { - let accountsWithSettleablePnl = (await client.getAllMangoAccounts(group)) + let accountsWithSettleablePnl = ( + await client.getAllMangoAccounts(group, true) + ) .filter((acc) => acc.perpPositionExistsForMarket(this)) .map((acc) => { const pp = acc diff --git a/ts/client/src/client.ts b/ts/client/src/client.ts index 340b18f79..8f81cbfb7 100644 --- a/ts/client/src/client.ts +++ b/ts/client/src/client.ts @@ -53,10 +53,10 @@ import { I80F48 } from './numbers/I80F48'; import { FlashLoanType, InterestRateParams, OracleConfigParams } from './types'; import { I64_MAX_BN, + U64_MAX_BN, createAssociatedTokenAccountIdempotentInstruction, getAssociatedTokenAddress, toNative, - U64_MAX_BN, } from './utils'; import { sendTransaction } from './utils/rpc'; @@ -540,16 +540,24 @@ export class MangoClient { // MangoAccount - public async getOrCreateMangoAccount(group: Group): Promise { + public async getOrCreateMangoAccount( + group: Group, + loadSerum3Oo = false, + ): Promise { const clientOwner = (this.program.provider as AnchorProvider).wallet .publicKey; let mangoAccounts = await this.getMangoAccountsForOwner( group, (this.program.provider as AnchorProvider).wallet.publicKey, + loadSerum3Oo, ); if (mangoAccounts.length === 0) { await this.createMangoAccount(group); - mangoAccounts = await this.getMangoAccountsForOwner(group, clientOwner); + mangoAccounts = await this.getMangoAccountsForOwner( + group, + clientOwner, + loadSerum3Oo, + ); } return mangoAccounts.sort((a, b) => a.accountNum - b.accountNum)[0]; } @@ -593,6 +601,7 @@ export class MangoClient { serum3Count?: number, perpCount?: number, perpOoCount?: number, + loadSerum3Oo = false, ): Promise { const accNum = accountNumber ?? 0; await this.createMangoAccount( @@ -608,6 +617,7 @@ export class MangoClient { group, (this.program.provider as AnchorProvider).wallet.publicKey, accNum, + loadSerum3Oo, ); } @@ -668,28 +678,25 @@ export class MangoClient { public async getMangoAccount( mangoAccount: MangoAccount | PublicKey, + loadSerum3Oo = false, ): Promise { const mangoAccountPk = mangoAccount instanceof MangoAccount ? mangoAccount.publicKey : mangoAccount; - return MangoAccount.from( - mangoAccountPk, - await this.program.account.mangoAccount.fetch(mangoAccountPk), - ); - } - - public async getMangoAccountForPublicKey( - mangoAccountPk: PublicKey, - ): Promise { - return MangoAccount.from( + const mangoAccount_ = MangoAccount.from( mangoAccountPk, await this.program.account.mangoAccount.fetch(mangoAccountPk), ); + if (loadSerum3Oo) { + await mangoAccount_?.reloadSerum3OpenOrders(this); + } + return mangoAccount_; } public async getMangoAccountWithSlot( mangoAccountPk: PublicKey, + loadSerum3Oo = false, ): Promise<{ slot: number; value: MangoAccount } | undefined> { const resp = await this.program.provider.connection.getAccountInfoAndContext( @@ -701,6 +708,9 @@ export class MangoClient { resp.value.data, ); const mangoAccount = MangoAccount.from(mangoAccountPk, decodedMangoAccount); + if (loadSerum3Oo) { + await mangoAccount?.reloadSerum3OpenOrders(this); + } return { slot: resp.context.slot, value: mangoAccount }; } @@ -708,8 +718,13 @@ export class MangoClient { group: Group, ownerPk: PublicKey, accountNumber: number, + loadSerum3Oo = false, ): Promise { - const mangoAccounts = await this.getMangoAccountsForOwner(group, ownerPk); + const mangoAccounts = await this.getMangoAccountsForOwner( + group, + ownerPk, + loadSerum3Oo, + ); const foundMangoAccount = mangoAccounts.find( (a) => a.accountNum == accountNumber, ); @@ -720,8 +735,9 @@ export class MangoClient { public async getMangoAccountsForOwner( group: Group, ownerPk: PublicKey, + loadSerum3Oo = false, ): Promise { - return ( + const accounts = ( await this.program.account.mangoAccount.all([ { memcmp: { @@ -739,13 +755,22 @@ export class MangoClient { ).map((pa) => { return MangoAccount.from(pa.publicKey, pa.account); }); + + if (loadSerum3Oo) { + await Promise.all( + accounts.map(async (a) => await a.reloadSerum3OpenOrders(this)), + ); + } + + return accounts; } public async getMangoAccountsForDelegate( group: Group, delegate: PublicKey, + loadSerum3Oo = false, ): Promise { - return ( + const accounts = ( await this.program.account.mangoAccount.all([ { memcmp: { @@ -763,10 +788,21 @@ export class MangoClient { ).map((pa) => { return MangoAccount.from(pa.publicKey, pa.account); }); + + if (loadSerum3Oo) { + await Promise.all( + accounts.map(async (a) => await a.reloadSerum3OpenOrders(this)), + ); + } + + return accounts; } - public async getAllMangoAccounts(group: Group): Promise { - return ( + public async getAllMangoAccounts( + group: Group, + loadSerum3Oo = false, + ): Promise { + const accounts = ( await this.program.account.mangoAccount.all([ { memcmp: { @@ -778,6 +814,14 @@ export class MangoClient { ).map((pa) => { return MangoAccount.from(pa.publicKey, pa.account); }); + + if (loadSerum3Oo) { + await Promise.all( + accounts.map(async (a) => await a.reloadSerum3OpenOrders(this)), + ); + } + + return accounts; } /** diff --git a/ts/client/src/scripts/mm/log-perp-ob.ts b/ts/client/src/scripts/mm/log-perp-ob.ts index 735af496b..8a5da5556 100644 --- a/ts/client/src/scripts/mm/log-perp-ob.ts +++ b/ts/client/src/scripts/mm/log-perp-ob.ts @@ -28,7 +28,7 @@ async function main() { ); // Load mango account - let mangoAccount = await client.getMangoAccountForPublicKey( + let mangoAccount = await client.getMangoAccount( new PublicKey(MANGO_ACCOUNT_PK), ); await mangoAccount.reload(client); diff --git a/ts/client/src/scripts/mm/market-maker.ts b/ts/client/src/scripts/mm/market-maker.ts index cd39d5e67..2be01ae14 100644 --- a/ts/client/src/scripts/mm/market-maker.ts +++ b/ts/client/src/scripts/mm/market-maker.ts @@ -28,7 +28,6 @@ import { makeInitSequenceEnforcerAccountIx, seqEnforcerProgramIds, } from './sequence-enforcer-util'; -import * as defaultParams from './params/default.json'; // Future // * use async nodejs logging @@ -248,7 +247,7 @@ async function fullMarketMaker() { ); // Load mango account - let mangoAccount = await client.getMangoAccountForPublicKey( + let mangoAccount = await client.getMangoAccount( new PublicKey(MANGO_ACCOUNT_PK), ); console.log( diff --git a/ts/client/src/scripts/mm/taker.ts b/ts/client/src/scripts/mm/taker.ts index 20a7cf7a2..ce19c7539 100644 --- a/ts/client/src/scripts/mm/taker.ts +++ b/ts/client/src/scripts/mm/taker.ts @@ -177,7 +177,7 @@ async function main() { ); // Load mango account - let mangoAccount = await client.getMangoAccountForPublicKey( + let mangoAccount = await client.getMangoAccount( new PublicKey(MANGO_ACCOUNT_PK), ); await mangoAccount.reload(client);