-
Notifications
You must be signed in to change notification settings - Fork 3
feat: record order events for wallet history #1343
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * ORDER BOOK EVENT HISTORY | ||
| * | ||
| * Creates the ob_order_events table to record individual order events | ||
| * (placement, cancellation, fill, settlement) for wallet history queries. | ||
| * | ||
| * This table is EPHEMERAL on-chain — the indexer syncs events to permanent | ||
| * storage, then trim_order_events() deletes old rows to bound node storage. | ||
| * This follows the same pattern as tn_digest (write → index → trim). | ||
| */ | ||
|
|
||
| -- ============================================================================= | ||
| -- ob_order_events: Individual order event log | ||
| -- ============================================================================= | ||
| CREATE TABLE IF NOT EXISTS ob_order_events ( | ||
| id INT8 PRIMARY KEY, | ||
| tx_hash BYTEA NOT NULL, | ||
| query_id INT NOT NULL, | ||
| participant_id INT NOT NULL, | ||
| event_type TEXT NOT NULL, | ||
| outcome BOOLEAN NOT NULL, | ||
| price INT NOT NULL, | ||
| amount INT8 NOT NULL, | ||
| counterparty_id INT, | ||
| block_height INT8 NOT NULL, | ||
| block_timestamp INT8 NOT NULL, | ||
|
|
||
| FOREIGN KEY (query_id) REFERENCES ob_queries(id), | ||
| FOREIGN KEY (participant_id) REFERENCES ob_participants(id) | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| -- Index for indexer sequential sync (same pattern as ob_net_impacts) | ||
| CREATE INDEX IF NOT EXISTS idx_ob_order_events_id ON ob_order_events(id); | ||
|
|
||
| -- Index for trim operations (delete by block_height) | ||
| CREATE INDEX IF NOT EXISTS idx_ob_order_events_height ON ob_order_events(block_height); | ||
|
|
||
| -- ============================================================================= | ||
| -- ob_record_order_event: Helper to insert a single order event | ||
| -- ============================================================================= | ||
| CREATE OR REPLACE ACTION ob_record_order_event( | ||
| $query_id INT, | ||
| $participant_id INT, | ||
| $event_type TEXT, | ||
| $outcome BOOLEAN, | ||
| $price INT, | ||
| $amount INT8, | ||
| $counterparty_id INT | ||
| ) PRIVATE { | ||
| $next_id INT8; | ||
| for $row in SELECT COALESCE(MAX(id), 0::INT8) + 1 as val FROM ob_order_events { | ||
| $next_id := $row.val; | ||
| } | ||
|
|
||
| INSERT INTO ob_order_events ( | ||
| id, tx_hash, query_id, participant_id, event_type, | ||
| outcome, price, amount, counterparty_id, | ||
| block_height, block_timestamp | ||
| ) VALUES ( | ||
| $next_id, decode(@txid, 'hex'), $query_id, $participant_id, $event_type, | ||
| $outcome, $price, $amount, $counterparty_id, | ||
| @height, @block_timestamp | ||
| ); | ||
| }; | ||
|
|
||
| -- ============================================================================= | ||
| -- trim_order_events: Delete old events after indexer has synced them | ||
| -- ============================================================================= | ||
| /** | ||
| * Called by the tn_digest scheduler (leader-only) to trim old order events. | ||
| * Uses block_height cutoff with a configurable buffer to ensure the indexer | ||
| * has had time to sync before deletion. | ||
| * | ||
| * Parameters: | ||
| * - $preserve_blocks: Number of recent blocks to keep (e.g., 172800 = ~2 days) | ||
| * - $delete_cap: Maximum rows to delete per invocation (prevents large txs) | ||
| * | ||
| * Returns via NOTICE: deleted count, remaining count, has_more flag | ||
| */ | ||
| CREATE OR REPLACE ACTION trim_order_events( | ||
| $preserve_blocks INT8, | ||
| $delete_cap INT | ||
| ) PUBLIC owner { | ||
| $cutoff INT8 := @height - $preserve_blocks; | ||
|
|
||
| -- Don't trim if cutoff is negative (chain is younger than preserve window) | ||
| if $cutoff <= 0 { | ||
| NOTICE('trim_order_events: cutoff<=0, nothing to trim'); | ||
| RETURN; | ||
| } | ||
|
|
||
| $count INT; | ||
| for $row in SELECT count(*)::INT as cnt FROM ob_order_events WHERE block_height < $cutoff { | ||
| $count := $row.cnt; | ||
| } | ||
|
|
||
| if $count = 0 { | ||
| NOTICE('trim_order_events: deleted=0 remaining=0 has_more=false'); | ||
| RETURN; | ||
| } | ||
|
|
||
| $to_delete INT; | ||
| if $count > $delete_cap { | ||
| $to_delete := $delete_cap; | ||
| } else { | ||
| $to_delete := $count; | ||
| } | ||
|
|
||
| DELETE FROM ob_order_events | ||
| WHERE id IN ( | ||
| SELECT id FROM ob_order_events | ||
| WHERE block_height < $cutoff | ||
| ORDER BY id ASC | ||
| LIMIT $to_delete | ||
| ); | ||
|
|
||
| $remaining INT := $count - $to_delete; | ||
| $has_more BOOL := $count > $delete_cap; | ||
| NOTICE('trim_order_events: deleted=' || $to_delete::TEXT | ||
| || ' remaining=' || $remaining::TEXT | ||
| || ' has_more=' || $has_more::TEXT); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.