diff --git a/ts/client/src/accounts/bookSide.ts b/ts/client/src/accounts/bookSide.ts index 33d23219..81038492 100644 --- a/ts/client/src/accounts/bookSide.ts +++ b/ts/client/src/accounts/bookSide.ts @@ -32,18 +32,19 @@ 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; @@ -51,8 +52,8 @@ export class BookSide { 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; } @@ -60,7 +61,10 @@ export class BookSide { // 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; } @@ -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)), + ); } } } diff --git a/ts/client/src/accounts/market.ts b/ts/client/src/accounts/market.ts index 179c5b05..4d65cc0a 100644 --- a/ts/client/src/accounts/market.ts +++ b/ts/client/src/accounts/market.ts @@ -102,14 +102,28 @@ export class Market { } public async loadBids(): Promise { - 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 { - 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; } diff --git a/ts/client/src/accounts/openOrders.ts b/ts/client/src/accounts/openOrders.ts index 5dbc1be4..20ad2e82 100644 --- a/ts/client/src/accounts/openOrders.ts +++ b/ts/client/src/accounts/openOrders.ts @@ -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; diff --git a/ts/client/src/test/market.ts b/ts/client/src/test/market.ts index a8b75692..1785a468 100644 --- a/ts/client/src/test/market.ts +++ b/ts/client/src/test/market.ts @@ -70,14 +70,14 @@ async function benchDecodeMarket(): Promise { async function testDecodeMultiple(): Promise { 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)); @@ -147,4 +147,4 @@ async function testMarketLots(): Promise { // void testWatchMarket(); // void testMarketLots(); void benchDecodeMarket(); -// void testDecodeMultiple(); \ No newline at end of file +// void testDecodeMultiple(); diff --git a/ts/client/src/utils/watcher.ts b/ts/client/src/utils/watcher.ts index 6d30791a..dcec4eb6 100644 --- a/ts/client/src/utils/watcher.ts +++ b/ts/client/src/utils/watcher.ts @@ -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;