Skip to content

Commit

Permalink
format TS
Browse files Browse the repository at this point in the history
  • Loading branch information
mschneider committed Jun 17, 2024
1 parent 893fccb commit db192a2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 37 deletions.
42 changes: 27 additions & 15 deletions ts/client/src/accounts/bookSide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,39 @@ export class BookSide {
}

public static decodeAccountfromBuffer(data: Buffer): BookSideAccount {
// TODO: add discriminator parsing & check
// TODO: add discriminator parsing & check
const roots = [
decodeOrderTreeRootStruct(data.subarray(8)),
decodeOrderTreeRootStruct(data.subarray(16))];
decodeOrderTreeRootStruct(data.subarray(16)),
];

// skip reserved
let offset = 56+256;
let offset = 56 + 256;

const orderTreeType = data.readUInt8(offset);
const bumpIndex = data.readUInt32LE(offset+4);
const freeListLen = data.readUInt32LE(offset+8);
const freeListHead = data.readUInt32LE(offset+12);
const bumpIndex = data.readUInt32LE(offset + 4);
const freeListLen = data.readUInt32LE(offset + 8);
const freeListHead = data.readUInt32LE(offset + 12);

// skip more reserved data
offset += 16 + 512;

const nodes: any[] = [];
for (let i = 0; i < 1024; ++i) {
const tag = data.readUInt8(offset);
const nodeData = data.subarray(offset, offset+88);
nodes.push({tag, nodeData});
const nodeData = data.subarray(offset, offset + 88);
nodes.push({ tag, nodeData });
offset += 88;
}

// this result has a slightly different layout than the regular account
// it doesn't include reserved data and it's AnyNodes don't have the field
// data: number[] (excluding the tag prefix byte)
// but nodeData: Buffer (including the tag prefix byte)
const result = { roots, nodes: { orderTreeType, bumpIndex, freeListLen, freeListHead, nodes }};
const result = {
roots,
nodes: { orderTreeType, bumpIndex, freeListLen, freeListHead, nodes },
};

return result as any;
}
Expand Down Expand Up @@ -195,22 +199,30 @@ export class BookSide {
private static LEAF_NODE_TAG = 2;

private toInnerNode(node: AnyNode): InnerNode {
const layout = (this.market.client.program as any)._coder.types.typeLayouts.get('InnerNode');
const layout = (
this.market.client.program as any
)._coder.types.typeLayouts.get('InnerNode');
// need to differentiate between accounts loaded via anchor and decodeAccountfromBuffer
if ( 'nodeData' in node) {
if ('nodeData' in node) {
return layout.decode(node['nodeData']);
} else {
return layout.decode(Buffer.from([BookSide.INNER_NODE_TAG].concat(node.data)));
return layout.decode(
Buffer.from([BookSide.INNER_NODE_TAG].concat(node.data)),
);
}
}

private toLeafNode(node: AnyNode): LeafNode {
const layout = (this.market.client.program as any)._coder.types.typeLayouts.get('LeafNode');
const layout = (
this.market.client.program as any
)._coder.types.typeLayouts.get('LeafNode');
// need to differentiate between accounts loaded via anchor and decodeAccountfromBuffer
if ( 'nodeData' in node) {
if ('nodeData' in node) {
return layout.decode(node['nodeData']);
} else {
return layout.decode(Buffer.from([BookSide.LEAF_NODE_TAG].concat(node.data)));
return layout.decode(
Buffer.from([BookSide.LEAF_NODE_TAG].concat(node.data)),
);
}
}
}
22 changes: 18 additions & 4 deletions ts/client/src/accounts/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,28 @@ export class Market {
}

public async loadBids(): Promise<BookSide> {
const bidsAi = await this.client.connection.getAccountInfo(this.account.bids);
this.bids = new BookSide(this, this.account.bids, BookSide.decodeAccountfromBuffer(bidsAi!.data), SideUtils.Bid);
const bidsAi = await this.client.connection.getAccountInfo(
this.account.bids,
);
this.bids = new BookSide(
this,
this.account.bids,
BookSide.decodeAccountfromBuffer(bidsAi!.data),
SideUtils.Bid,
);
return this.bids;
}

public async loadAsks(): Promise<BookSide> {
const asksAi = await this.client.connection.getAccountInfo(this.account.asks);
this.asks = new BookSide(this, this.account.asks, BookSide.decodeAccountfromBuffer(asksAi!.data), SideUtils.Ask);
const asksAi = await this.client.connection.getAccountInfo(
this.account.asks,
);
this.asks = new BookSide(
this,
this.account.asks,
BookSide.decodeAccountfromBuffer(asksAi!.data),
SideUtils.Ask,
);
return this.asks;
}

Expand Down
21 changes: 10 additions & 11 deletions ts/client/src/accounts/openOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,27 @@ export class OpenOrders {
// Need to reload orderbooks because not all information about orders, like
// size, is stored on the open orders account. Do all fetches together to
// ensure they are synced to the same slot.
const [bidsAi, asksAi, ooAi] = await this.market.client.connection.getMultipleAccountsInfo(
[
this.market.account.bids,
this.market.account.asks,
this.pubkey,
]
);
const [bidsAi, asksAi, ooAi] =
await this.market.client.connection.getMultipleAccountsInfo([
this.market.account.bids,
this.market.account.asks,
this.pubkey,
]);
this.market.bids = new BookSide(
this.market,
this.market.account.bids,
BookSide.decodeAccountfromBuffer(bidsAi!.data),
SideUtils.Bid
SideUtils.Bid,
);
this.market.asks = new BookSide(
this.market,
this.market.account.asks,
BookSide.decodeAccountfromBuffer(asksAi!.data),
SideUtils.Ask
SideUtils.Ask,
);
this.account = this.market.client.program.coder.accounts.decode(
"openOrdersAccount",
ooAi!.data
'openOrdersAccount',
ooAi!.data,
);

return this;
Expand Down
8 changes: 4 additions & 4 deletions ts/client/src/test/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ async function benchDecodeMarket(): Promise<void> {
async function testDecodeMultiple(): Promise<void> {
const client = initReadOnlyOpenbookClient();
const markets = [
new PublicKey("BU3EaRVo9WN44muCBy3mwkCQ4uYQWiuqay1whEmeSXK3"),
new PublicKey("D8BPZXCYvVBkXR5NAoDnuzjFGuF2kFKWyfEUtZbmjRg7"),
new PublicKey('BU3EaRVo9WN44muCBy3mwkCQ4uYQWiuqay1whEmeSXK3'),
new PublicKey('D8BPZXCYvVBkXR5NAoDnuzjFGuF2kFKWyfEUtZbmjRg7'),
];
for (const marketPk of markets) {
const market = await Market.load(client, marketPk);
await market.loadOrderBook();

const mktTag = marketPk.toString().substring(0,6);
const mktTag = marketPk.toString().substring(0, 6);

console.log(mktTag, market.bids?.getL2(300));
console.log(mktTag, market.asks?.getL2(300));
Expand Down Expand Up @@ -147,4 +147,4 @@ async function testMarketLots(): Promise<void> {
// void testWatchMarket();
// void testMarketLots();
void benchDecodeMarket();
// void testDecodeMultiple();
// void testDecodeMultiple();
4 changes: 1 addition & 3 deletions ts/client/src/utils/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ export class Watcher {
this.accountSubs[pubkey.toBase58()] = this.connection.onAccountChange(
pubkey,
(ai) => {
bookSide.account = BookSide.decodeAccountfromBuffer(
ai.data,
);
bookSide.account = BookSide.decodeAccountfromBuffer(ai.data);
},
);
return this;
Expand Down

0 comments on commit db192a2

Please sign in to comment.