Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Serum] Populate seqNum on Event and implement loadFillsSince #195

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 17 additions & 1 deletion packages/serum/src/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
TransactionInstruction,
TransactionSignature,
} from '@solana/web3.js';
import { decodeEventQueue, decodeRequestQueue } from './queue';
import {
decodeEventQueue,
decodeEventsSince,
decodeRequestQueue,
} from './queue';
import { Buffer } from 'buffer';
import { getFeeTier, supportsSrmFeeDiscounts } from './fees';
import {
Expand Down Expand Up @@ -1111,6 +1115,18 @@ export class Market {
await connection.getAccountInfo(this._decoded.eventQueue),
);
const events = decodeEventQueue(data, limit);
return this.parseFillEvents(events);
}

async loadFillsSince(connection: Connection, lastSeqNum: number) {
const { data } = throwIfNull(
await connection.getAccountInfo(this._decoded.eventQueue),
);
const events = decodeEventsSince(data, lastSeqNum);
return this.parseFillEvents(events);
}

parseFillEvents(events) {
return events
.filter(
(event) => event.eventFlags.fill && event.nativeQuantityPaid.gtn(0),
Expand Down
6 changes: 6 additions & 0 deletions packages/serum/src/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ export function decodeEventQueue(buffer: Buffer, history?: number): Event[] {
if (!header.accountFlags.initialized || !header.accountFlags.eventQueue) {
throw new Error('Invalid events queue');
}
const modulo32Uint = 0x100000000;
const startSeqNum = header.seqNum + header.count - nodes.length;
for (const [i, node] of nodes.entries()) {
// seqNum itself may have u32 overflow in this span of `nodes`
node.seqNum = (startSeqNum + i + modulo32Uint) % modulo32Uint;
}
return nodes;
}

Expand Down