Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3164,6 +3164,67 @@ export class MangoClient {
return this.getAllMangoAccounts(mangoGroup, filters, includeOpenOrders);
}

/**
* Get multiple MangoAccounts given a list of mango account public keys
*/
async getMultipleMangoAccounts(
mangoGroup: MangoGroup,
mangoAccountPks: PublicKey[],
includeOpenOrders = true,
): Promise<MangoAccount[]> {

const mangoAccounts = await getMultipleAccounts(
this.connection,
mangoAccountPks,
).then((accounts) =>
accounts.map(({ publicKey, accountInfo }) => {
return new MangoAccount(
publicKey,
MangoAccountLayout.decode(
accountInfo == null ? undefined : accountInfo.data,
),
);
}),
);

if (includeOpenOrders) {
const openOrderPks = mangoAccounts
.map((ma) => ma.spotOpenOrders.filter((pk) => !pk.equals(zeroKey)))
.flat();

const openOrderAccountInfos = await getMultipleAccounts(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a limit of how many open orders you can request at once using gMA, i think it's 100 or 200. could you add here a loop that processes the requests in reasonable batch sizes?

this.connection,
openOrderPks,
);

const openOrders = openOrderAccountInfos.map(
({ publicKey, accountInfo }) =>
OpenOrders.fromAccountInfo(
publicKey,
accountInfo,
mangoGroup.dexProgramId,
),
);

const pkToOpenOrdersAccount = {};
openOrders.forEach((openOrdersAccount) => {
pkToOpenOrdersAccount[openOrdersAccount.publicKey.toBase58()] =
openOrdersAccount;
});

for (const ma of mangoAccounts) {
for (let i = 0; i < ma.spotOpenOrders.length; i++) {
if (ma.spotOpenOrders[i].toBase58() in pkToOpenOrdersAccount) {
ma.spotOpenOrdersAccounts[i] =
pkToOpenOrdersAccount[ma.spotOpenOrders[i].toBase58()];
}
}
}
}

return mangoAccounts;
}

async getAllMangoAccounts(
mangoGroup: MangoGroup,
filters?: any[],
Expand Down