-
Notifications
You must be signed in to change notification settings - Fork 38
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
[Events Orderbook] Implement querying the orderbook at the latest block #735
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Would be nice to unit test the clone behavior.
@@ -132,7 +133,10 @@ export class StreamedOrderbook { | |||
* Retrieves the current open orders in the orderbook. | |||
*/ | |||
public getOpenOrders(): IndexedOrder<bigint>[] { | |||
return this.state.getOrders(this.batch) | |||
this.throwOnInvalidState() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this helper a lot!
* Creates a copy of the auction state that can apply events independently | ||
* without modifying the original state. | ||
*/ | ||
public copy(): AuctionState { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! Can we add a unit test that this actually creates a deep copy? It seems to work because bigint seems to be an
immutable type (I think with BN we would have to also copy the data of the maps).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if you think the test is sufficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect, thanks!
This PR implements querying the latest open orderbook.
This is implemented by keeping a separate "confirmed" auction state, built from events that are part of confirmed blocks, and not subject to reorgs, and a "latest" auction state that is built by cloning the "confirmed" state and applying events in between the confirmed block and the latest block.
The "latest" state is implemented to be discardable and rebuilt on every update so that it is not prone to errors stemming from reorgs, nor does complex logic for rolling back events need to be implemented. Unfortunately, the current implementation is slightly inefficient. I created #734 as an issue to capture a potential solution to the problem.
This closes #719
Test Plan
Run the e2e test to ensure the event based orderbook on the latest block matches the onchain queried orderbook: