Skip to content

Commit

Permalink
add loadAllOpenOrders helper to OpenOrdersIndexer
Browse files Browse the repository at this point in the history
  • Loading branch information
mschneider committed Jun 17, 2024
1 parent db192a2 commit 2270356
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
39 changes: 38 additions & 1 deletion ts/client/src/accounts/openOrdersIndexer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { PublicKey } from '@solana/web3.js';
import { OpenBookV2Client, OpenOrdersIndexerAccount } from '../client';
import {
OpenBookV2Client,
OpenOrdersIndexerAccount,
OpenOrders,
Market,
BookSide,
SideUtils,
} from '..';

export class OpenOrdersIndexer {
constructor(
Expand Down Expand Up @@ -28,4 +35,34 @@ export class OpenOrdersIndexer {
await client.program.account.openOrdersIndexer.fetchNullable(pubkey);
return account && new OpenOrdersIndexer(client, pubkey, account);
}

public async loadAllOpenOrders(): Promise<OpenOrders[]> {
const ooPks = this.account.addresses;
const oos =
await this.client.program.account.openOrdersAccount.fetchMultiple(ooPks);
const marketPks = oos.map((oo) => oo!.market);
const markets = await this.client.program.account.market.fetchMultiple(
marketPks,
);
const bookSidePks = markets.flatMap((m) => [m!.bids, m!.asks]);
const bookSides = await this.client.program.account.bookSide.fetchMultiple(
bookSidePks,
);
return oos.map((oo, i) => {
const mkt = new Market(this.client, marketPks[i], markets[i]!);
mkt.bids = new BookSide(
mkt,
bookSidePks[2 * i],
bookSides[2 * i]!,
SideUtils.Bid,
);
mkt.asks = new BookSide(
mkt,
bookSidePks[2 * i + 1],
bookSides[2 * i + 1]!,
SideUtils.Ask,
);
return new OpenOrders(ooPks[i], oo!, mkt);
});
}
}
10 changes: 9 additions & 1 deletion ts/client/src/test/openOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ async function testLoadOOForMarket(): Promise<void> {
console.log(oo?.toPrettyString());
}

async function testLoadOOForWallet(): Promise<void> {
const client = initOpenbookClient();
const ooi = await OpenOrdersIndexer.loadNullable(client);
const oos = await ooi!.loadAllOpenOrders();
for (const oo of oos) console.log(oo?.toPrettyString());
}

async function testPlaceAndCancelOrder(): Promise<void> {
const client = initOpenbookClient();
const marketPk = new PublicKey(
Expand Down Expand Up @@ -86,6 +93,7 @@ async function testPlaceAndCancelOrderByClientId(): Promise<void> {
}

// testLoadIndexerNonExistent();
void testLoadOOForMarket();
// void testLoadOOForMarket();
void testLoadOOForWallet();
// testPlaceAndCancelOrder();
// testPlaceAndCancelOrderByClientId();

0 comments on commit 2270356

Please sign in to comment.