fix bug where serum oo where not loaded (#403)

* fix bug where serum oo where not loaded

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

* fixes from review

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

* fixes from review

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2023-01-19 11:31:54 +01:00 committed by GitHub
parent 44f599ca62
commit 919f09bb8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 26 deletions

View File

@ -90,7 +90,7 @@ export class MangoAccount {
public async reload(client: MangoClient): Promise<MangoAccount> { public async reload(client: MangoClient): Promise<MangoAccount> {
const mangoAccount = await client.getMangoAccount(this); const mangoAccount = await client.getMangoAccount(this);
await mangoAccount.reloadAccountData(client); await mangoAccount.reloadSerum3OpenOrders(client);
Object.assign(this, mangoAccount); Object.assign(this, mangoAccount);
return mangoAccount; return mangoAccount;
} }
@ -99,12 +99,12 @@ export class MangoAccount {
client: MangoClient, client: MangoClient,
): Promise<{ value: MangoAccount; slot: number }> { ): Promise<{ value: MangoAccount; slot: number }> {
const resp = await client.getMangoAccountWithSlot(this.publicKey); const resp = await client.getMangoAccountWithSlot(this.publicKey);
await resp?.value.reloadAccountData(client); await resp?.value.reloadSerum3OpenOrders(client);
Object.assign(this, resp?.value); Object.assign(this, resp?.value);
return { value: resp!.value, slot: resp!.slot }; return { value: resp!.value, slot: resp!.slot };
} }
async reloadAccountData(client: MangoClient): Promise<MangoAccount> { async reloadSerum3OpenOrders(client: MangoClient): Promise<MangoAccount> {
const serum3Active = this.serum3Active(); const serum3Active = this.serum3Active();
const ais = const ais =
await client.program.provider.connection.getMultipleAccountsInfo( await client.program.provider.connection.getMultipleAccountsInfo(

View File

@ -431,7 +431,9 @@ export class PerpMarket {
direction: 'negative' | 'positive', direction: 'negative' | 'positive',
count = 2, count = 2,
): Promise<{ account: MangoAccount; settleablePnl: I80F48 }[]> { ): Promise<{ account: MangoAccount; settleablePnl: I80F48 }[]> {
let accountsWithSettleablePnl = (await client.getAllMangoAccounts(group)) let accountsWithSettleablePnl = (
await client.getAllMangoAccounts(group, true)
)
.filter((acc) => acc.perpPositionExistsForMarket(this)) .filter((acc) => acc.perpPositionExistsForMarket(this))
.map((acc) => { .map((acc) => {
const pp = acc const pp = acc

View File

@ -53,10 +53,10 @@ import { I80F48 } from './numbers/I80F48';
import { FlashLoanType, InterestRateParams, OracleConfigParams } from './types'; import { FlashLoanType, InterestRateParams, OracleConfigParams } from './types';
import { import {
I64_MAX_BN, I64_MAX_BN,
U64_MAX_BN,
createAssociatedTokenAccountIdempotentInstruction, createAssociatedTokenAccountIdempotentInstruction,
getAssociatedTokenAddress, getAssociatedTokenAddress,
toNative, toNative,
U64_MAX_BN,
} from './utils'; } from './utils';
import { sendTransaction } from './utils/rpc'; import { sendTransaction } from './utils/rpc';
@ -540,16 +540,24 @@ export class MangoClient {
// MangoAccount // MangoAccount
public async getOrCreateMangoAccount(group: Group): Promise<MangoAccount> { public async getOrCreateMangoAccount(
group: Group,
loadSerum3Oo = false,
): Promise<MangoAccount> {
const clientOwner = (this.program.provider as AnchorProvider).wallet const clientOwner = (this.program.provider as AnchorProvider).wallet
.publicKey; .publicKey;
let mangoAccounts = await this.getMangoAccountsForOwner( let mangoAccounts = await this.getMangoAccountsForOwner(
group, group,
(this.program.provider as AnchorProvider).wallet.publicKey, (this.program.provider as AnchorProvider).wallet.publicKey,
loadSerum3Oo,
); );
if (mangoAccounts.length === 0) { if (mangoAccounts.length === 0) {
await this.createMangoAccount(group); 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]; return mangoAccounts.sort((a, b) => a.accountNum - b.accountNum)[0];
} }
@ -593,6 +601,7 @@ export class MangoClient {
serum3Count?: number, serum3Count?: number,
perpCount?: number, perpCount?: number,
perpOoCount?: number, perpOoCount?: number,
loadSerum3Oo = false,
): Promise<MangoAccount | undefined> { ): Promise<MangoAccount | undefined> {
const accNum = accountNumber ?? 0; const accNum = accountNumber ?? 0;
await this.createMangoAccount( await this.createMangoAccount(
@ -608,6 +617,7 @@ export class MangoClient {
group, group,
(this.program.provider as AnchorProvider).wallet.publicKey, (this.program.provider as AnchorProvider).wallet.publicKey,
accNum, accNum,
loadSerum3Oo,
); );
} }
@ -668,28 +678,25 @@ export class MangoClient {
public async getMangoAccount( public async getMangoAccount(
mangoAccount: MangoAccount | PublicKey, mangoAccount: MangoAccount | PublicKey,
loadSerum3Oo = false,
): Promise<MangoAccount> { ): Promise<MangoAccount> {
const mangoAccountPk = const mangoAccountPk =
mangoAccount instanceof MangoAccount mangoAccount instanceof MangoAccount
? mangoAccount.publicKey ? mangoAccount.publicKey
: mangoAccount; : mangoAccount;
return MangoAccount.from( const mangoAccount_ = MangoAccount.from(
mangoAccountPk,
await this.program.account.mangoAccount.fetch(mangoAccountPk),
);
}
public async getMangoAccountForPublicKey(
mangoAccountPk: PublicKey,
): Promise<MangoAccount> {
return MangoAccount.from(
mangoAccountPk, mangoAccountPk,
await this.program.account.mangoAccount.fetch(mangoAccountPk), await this.program.account.mangoAccount.fetch(mangoAccountPk),
); );
if (loadSerum3Oo) {
await mangoAccount_?.reloadSerum3OpenOrders(this);
}
return mangoAccount_;
} }
public async getMangoAccountWithSlot( public async getMangoAccountWithSlot(
mangoAccountPk: PublicKey, mangoAccountPk: PublicKey,
loadSerum3Oo = false,
): Promise<{ slot: number; value: MangoAccount } | undefined> { ): Promise<{ slot: number; value: MangoAccount } | undefined> {
const resp = const resp =
await this.program.provider.connection.getAccountInfoAndContext( await this.program.provider.connection.getAccountInfoAndContext(
@ -701,6 +708,9 @@ export class MangoClient {
resp.value.data, resp.value.data,
); );
const mangoAccount = MangoAccount.from(mangoAccountPk, decodedMangoAccount); const mangoAccount = MangoAccount.from(mangoAccountPk, decodedMangoAccount);
if (loadSerum3Oo) {
await mangoAccount?.reloadSerum3OpenOrders(this);
}
return { slot: resp.context.slot, value: mangoAccount }; return { slot: resp.context.slot, value: mangoAccount };
} }
@ -708,8 +718,13 @@ export class MangoClient {
group: Group, group: Group,
ownerPk: PublicKey, ownerPk: PublicKey,
accountNumber: number, accountNumber: number,
loadSerum3Oo = false,
): Promise<MangoAccount | undefined> { ): Promise<MangoAccount | undefined> {
const mangoAccounts = await this.getMangoAccountsForOwner(group, ownerPk); const mangoAccounts = await this.getMangoAccountsForOwner(
group,
ownerPk,
loadSerum3Oo,
);
const foundMangoAccount = mangoAccounts.find( const foundMangoAccount = mangoAccounts.find(
(a) => a.accountNum == accountNumber, (a) => a.accountNum == accountNumber,
); );
@ -720,8 +735,9 @@ export class MangoClient {
public async getMangoAccountsForOwner( public async getMangoAccountsForOwner(
group: Group, group: Group,
ownerPk: PublicKey, ownerPk: PublicKey,
loadSerum3Oo = false,
): Promise<MangoAccount[]> { ): Promise<MangoAccount[]> {
return ( const accounts = (
await this.program.account.mangoAccount.all([ await this.program.account.mangoAccount.all([
{ {
memcmp: { memcmp: {
@ -739,13 +755,22 @@ export class MangoClient {
).map((pa) => { ).map((pa) => {
return MangoAccount.from(pa.publicKey, pa.account); 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( public async getMangoAccountsForDelegate(
group: Group, group: Group,
delegate: PublicKey, delegate: PublicKey,
loadSerum3Oo = false,
): Promise<MangoAccount[]> { ): Promise<MangoAccount[]> {
return ( const accounts = (
await this.program.account.mangoAccount.all([ await this.program.account.mangoAccount.all([
{ {
memcmp: { memcmp: {
@ -763,10 +788,21 @@ export class MangoClient {
).map((pa) => { ).map((pa) => {
return MangoAccount.from(pa.publicKey, pa.account); 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<MangoAccount[]> { public async getAllMangoAccounts(
return ( group: Group,
loadSerum3Oo = false,
): Promise<MangoAccount[]> {
const accounts = (
await this.program.account.mangoAccount.all([ await this.program.account.mangoAccount.all([
{ {
memcmp: { memcmp: {
@ -778,6 +814,14 @@ export class MangoClient {
).map((pa) => { ).map((pa) => {
return MangoAccount.from(pa.publicKey, pa.account); return MangoAccount.from(pa.publicKey, pa.account);
}); });
if (loadSerum3Oo) {
await Promise.all(
accounts.map(async (a) => await a.reloadSerum3OpenOrders(this)),
);
}
return accounts;
} }
/** /**

View File

@ -28,7 +28,7 @@ async function main() {
); );
// Load mango account // Load mango account
let mangoAccount = await client.getMangoAccountForPublicKey( let mangoAccount = await client.getMangoAccount(
new PublicKey(MANGO_ACCOUNT_PK), new PublicKey(MANGO_ACCOUNT_PK),
); );
await mangoAccount.reload(client); await mangoAccount.reload(client);

View File

@ -28,7 +28,6 @@ import {
makeInitSequenceEnforcerAccountIx, makeInitSequenceEnforcerAccountIx,
seqEnforcerProgramIds, seqEnforcerProgramIds,
} from './sequence-enforcer-util'; } from './sequence-enforcer-util';
import * as defaultParams from './params/default.json';
// Future // Future
// * use async nodejs logging // * use async nodejs logging
@ -248,7 +247,7 @@ async function fullMarketMaker() {
); );
// Load mango account // Load mango account
let mangoAccount = await client.getMangoAccountForPublicKey( let mangoAccount = await client.getMangoAccount(
new PublicKey(MANGO_ACCOUNT_PK), new PublicKey(MANGO_ACCOUNT_PK),
); );
console.log( console.log(

View File

@ -177,7 +177,7 @@ async function main() {
); );
// Load mango account // Load mango account
let mangoAccount = await client.getMangoAccountForPublicKey( let mangoAccount = await client.getMangoAccount(
new PublicKey(MANGO_ACCOUNT_PK), new PublicKey(MANGO_ACCOUNT_PK),
); );
await mangoAccount.reload(client); await mangoAccount.reload(client);