optimise getAllMangoAccounts

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2023-07-04 09:26:59 +02:00
parent 5ea81fc0d5
commit 1ffa65d01b
5 changed files with 70 additions and 7 deletions

View File

@ -60,6 +60,7 @@ async function settlePnl(
const candidates = await perpMarket.getSettlePnlCandidates(
client,
group,
undefined,
'negative',
);
if (candidates.length === 0) {
@ -83,6 +84,7 @@ async function settlePnl(
const candidates = await perpMarket.getSettlePnlCandidates(
client,
group,
undefined,
'positive',
);
if (candidates.length === 0) {

View File

@ -141,6 +141,22 @@ export class MangoAccount {
return this;
}
loadSerum3OpenOrders(serum3OosMapByOo: Map<string, OpenOrders>): void {
const serum3Active = this.serum3Active();
if (!serum3Active.length) return;
this.serum3OosMapByMarketIndex = new Map(
Array.from(
serum3Active.map((mangoOo) => {
const oo = serum3OosMapByOo.get(mangoOo.openOrders.toBase58());
if (!oo) {
throw new Error(`Undefined open orders for ${mangoOo.openOrders}`);
}
return [mangoOo.marketIndex, oo];
}),
),
);
}
public isDelegate(client: MangoClient): boolean {
return this.delegate.equals(
(client.program.provider as AnchorProvider).wallet.publicKey,

View File

@ -471,11 +471,12 @@ export class PerpMarket {
public async getSettlePnlCandidates(
client: MangoClient,
group: Group,
direction: 'negative' | 'positive',
accounts?: MangoAccount[],
direction: 'negative' | 'positive' = 'positive',
count = 2,
): Promise<{ account: MangoAccount; settleablePnl: I80F48 }[]> {
let accountsWithSettleablePnl = (
await client.getAllMangoAccounts(group, true)
accounts ?? (await client.getAllMangoAccounts(group, true))
)
.filter((acc) => acc.perpPositionExistsForMarket(this))
.map((acc) => {

View File

@ -5,11 +5,13 @@ import {
Provider,
Wallet,
} from '@coral-xyz/anchor';
import { OpenOrders } from '@project-serum/serum';
import {
createCloseAccountInstruction,
createInitializeAccount3Instruction,
} from '@solana/spl-token';
import {
AccountInfo,
AccountMeta,
AddressLookupTableAccount,
Cluster,
@ -26,6 +28,7 @@ import {
TransactionSignature,
} from '@solana/web3.js';
import bs58 from 'bs58';
import chunk from 'lodash/chunk';
import cloneDeep from 'lodash/cloneDeep';
import uniq from 'lodash/uniq';
import { Bank, MintInfo, TokenIndex } from './accounts/bank';
@ -951,8 +954,43 @@ export class MangoClient {
});
if (loadSerum3Oo) {
await Promise.all(
accounts.map(async (a) => await a.reloadSerum3OpenOrders(this)),
const ooPks = accounts
.map((a) => a.serum3Active().map((serum3) => serum3.openOrders))
.flat();
const ais: AccountInfo<Buffer>[] = (
await Promise.all(
chunk(ooPks, 100).map(
async (ooPksChunk) =>
await this.program.provider.connection.getMultipleAccountsInfo(
ooPksChunk,
),
),
)
).flat();
if (ooPks.length != ais.length) {
throw new Error(`Error in fetch all open orders accounts!`);
}
const serum3OosMapByOo = new Map(
Array.from(
ais.map((ai, i) => {
if (ai == null) {
throw new Error(`Undefined AI for open orders ${ooPks[i]}!`);
}
const oo = OpenOrders.fromAccountInfo(
ooPks[i],
ai,
OPENBOOK_PROGRAM_ID[this.cluster],
);
return [ooPks[i].toBase58(), oo];
}),
),
);
accounts.forEach(
async (a) => await a.loadSerum3OpenOrders(serum3OosMapByOo),
);
}

View File

@ -1,6 +1,6 @@
import { PublicKey } from '@solana/web3.js';
import { Group } from './accounts/group';
import { PerpPosition } from './accounts/mangoAccount';
import { MangoAccount, PerpPosition } from './accounts/mangoAccount';
import { PerpMarket } from './accounts/perp';
import { MangoClient } from './client';
import { I80F48 } from './numbers/I80F48';
@ -14,9 +14,12 @@ import { I80F48 } from './numbers/I80F48';
export async function getLargestPerpPositions(
client: MangoClient,
group: Group,
accounts?: MangoAccount[],
perpMarket?: PerpMarket,
): Promise<{ mangoAccount: PublicKey; perpPosition: PerpPosition }[]> {
const accounts = await client.getAllMangoAccounts(group, true);
if (!accounts) {
accounts = await client.getAllMangoAccounts(group, true);
}
let allPps = accounts
.map((a) => {
@ -56,11 +59,14 @@ export async function getLargestPerpPositions(
export async function getClosestToLiquidationPerpPositions(
client: MangoClient,
group: Group,
accounts?: MangoAccount[],
filterByNotionalValueUi = 10,
): Promise<
{ mangoAccount: PublicKey; perpPosition: PerpPosition; pct: I80F48 }[]
> {
const accounts = await client.getAllMangoAccounts(group, true);
if (!accounts) {
accounts = await client.getAllMangoAccounts(group, true);
}
const accountsMap = new Map(accounts.map((a) => [a.publicKey.toBase58(), a]));
let allPps: any = accounts