use getMultipleAccounts instead of gPA for serum oo
This commit is contained in:
parent
1e22b639f6
commit
3015f73724
|
@ -56,6 +56,7 @@ import {
|
|||
} from './instruction';
|
||||
import { Aggregator } from './schema';
|
||||
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
|
||||
import { getMultipleAccounts } from '.';
|
||||
|
||||
export const tokenToDecimals = {
|
||||
"BTC": 4,
|
||||
|
@ -1381,37 +1382,32 @@ export class MangoClient {
|
|||
marginAccountsFilters.push(...filters);
|
||||
}
|
||||
|
||||
const marginAccountsProms = getFilteredProgramAccounts(connection, programId, marginAccountsFilters)
|
||||
const marginAccounts = await getFilteredProgramAccounts(connection, programId, marginAccountsFilters)
|
||||
.then((accounts) => (
|
||||
accounts.map(({ publicKey, accountInfo }) =>
|
||||
new MarginAccount(publicKey, MarginAccountLayout.decode(accountInfo == null ? undefined : accountInfo.data))
|
||||
)
|
||||
))
|
||||
|
||||
const ordersFilters = [
|
||||
{
|
||||
memcmp: {
|
||||
offset: OpenOrders.getLayout(mangoGroup.dexProgramId).offsetOf('owner'),
|
||||
bytes: mangoGroup.signerKey.toBase58()
|
||||
}
|
||||
},
|
||||
{
|
||||
dataSize: OpenOrders.getLayout(mangoGroup.dexProgramId).span
|
||||
}
|
||||
]
|
||||
|
||||
const openOrdersProms = getFilteredProgramAccounts(connection, mangoGroup.dexProgramId, ordersFilters)
|
||||
.then(
|
||||
(accounts) => (
|
||||
accounts.map(
|
||||
( { publicKey, accountInfo } ) =>
|
||||
OpenOrders.fromAccountInfo(publicKey, accountInfo, mangoGroup.dexProgramId)
|
||||
)
|
||||
)
|
||||
)
|
||||
const openOrderPks = marginAccounts
|
||||
.map((ma) => ma.openOrders.filter((pk) => !pk.equals(zeroKey)))
|
||||
.flat();
|
||||
|
||||
const openOrderAccountInfos = await getMultipleAccounts(
|
||||
connection,
|
||||
openOrderPks,
|
||||
);
|
||||
|
||||
const openOrders = openOrderAccountInfos.map(
|
||||
({ publicKey, accountInfo }) =>
|
||||
OpenOrders.fromAccountInfo(
|
||||
publicKey,
|
||||
accountInfo,
|
||||
mangoGroup.dexProgramId,
|
||||
),
|
||||
);
|
||||
|
||||
const marginAccounts = await marginAccountsProms
|
||||
const openOrders = await openOrdersProms
|
||||
const pkToOpenOrdersAccount = {}
|
||||
openOrders.forEach(
|
||||
(openOrdersAccount) => (
|
||||
|
|
27
src/utils.ts
27
src/utils.ts
|
@ -278,19 +278,36 @@ export function parseTokenAccount(
|
|||
export async function getMultipleAccounts(
|
||||
connection: Connection,
|
||||
publicKeys: PublicKey[],
|
||||
commitment?: Commitment
|
||||
): Promise<{ publicKey: PublicKey; accountInfo: AccountInfo<Buffer> }[]> {
|
||||
const publickKeyStrs = publicKeys.map((pk) => (pk.toBase58()));
|
||||
commitment?: Commitment,
|
||||
): Promise<
|
||||
{
|
||||
publicKey: PublicKey;
|
||||
context: { slot: number };
|
||||
accountInfo: AccountInfo<Buffer>;
|
||||
}[]
|
||||
> {
|
||||
const len = publicKeys.length;
|
||||
if (len > 100) {
|
||||
const mid = Math.floor(publicKeys.length / 2);
|
||||
return Promise.all([
|
||||
getMultipleAccounts(connection, publicKeys.slice(0, mid), commitment),
|
||||
getMultipleAccounts(connection, publicKeys.slice(mid, len), commitment),
|
||||
]).then((a) => a[0].concat(a[1]));
|
||||
}
|
||||
const publicKeyStrs = publicKeys.map((pk) => pk.toBase58());
|
||||
// load connection commitment as a default
|
||||
commitment ||= connection.commitment;
|
||||
|
||||
const args = commitment ? [publickKeyStrs, {commitment}] : [publickKeyStrs];
|
||||
const args = commitment ? [publicKeyStrs, { commitment }] : [publicKeyStrs];
|
||||
// @ts-ignore
|
||||
const resp = await connection._rpcRequest('getMultipleAccounts', args);
|
||||
if (resp.error) {
|
||||
throw new Error(resp.error.message);
|
||||
}
|
||||
return resp.result.value.map(
|
||||
({ data, executable, lamports, owner } , i) => ({
|
||||
({ data, executable, lamports, owner }, i: number) => ({
|
||||
publicKey: publicKeys[i],
|
||||
context: resp.result.context,
|
||||
accountInfo: {
|
||||
data: Buffer.from(data[0], 'base64'),
|
||||
executable,
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
"extends": "@tsconfig/node10/tsconfig.json",
|
||||
|
||||
"compilerOptions": {
|
||||
"target": "es6",
|
||||
"lib": ["es2019"],
|
||||
"outDir": "./lib",
|
||||
"allowJs": true,
|
||||
"checkJs": true,
|
||||
|
@ -13,5 +15,10 @@
|
|||
"jsx": "react"
|
||||
},
|
||||
"include": ["./tests/**/*", "./src/**/*"],
|
||||
"exclude": ["./tests/**/*.test.js", "./src/**/*.test.js", "node_modules", "**/node_modules"]
|
||||
"exclude": [
|
||||
"./tests/**/*.test.js",
|
||||
"./src/**/*.test.js",
|
||||
"node_modules",
|
||||
"**/node_modules"
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue