Fix: limit_order_fill → swap_events linkage uses MIN(id) (GitLab #287 followup) #316
Labels
No labels
agent:fix_bugfix
agent:fix_conflicts
agent:fix_security
agent:gap_analysis
agent:implement
agent:implement
agent:implement
agent:open_issues
agent:ready
agent:research
agent:security_audit
agent:verify
architecture
backend
blocker:hybrid
blocker:launch
blocker:limit-orders
blocker:v2
block:log_only
block:security
bug
ci
contracts
correctness
deploy
dev
devops
docs
documentation
duplicate
e2e
enhancement
epic
feature
frontend
functional-completion
gas
good first issue
governance
help wanted
high-risk
hooks
hybrid
indexer
infra
infrastructure
integrators
invalid
launch-blocker
limit-orders
localnet
localterra
low priority
missing-implementation
needs-design
ops
performance
priority
high
priority
medium
product
qa
QA
question
ready
ready
research
scripts
security
security-hardening
smartcontracts
tech-debt
testing
ux
UX
v2
verification
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
code/cl8y-dex-terraclassic#316
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Current codebase
GitLab #287 added
swap_indextoswap_events— multiple swaps per(tx_hash, pair_id)now stored with unique(tx_hash, pair_id, swap_index)(migration20260605000000_swap_events_per_tx_pair_swap_index.sql). Parser assignsswap_indexper pair in wasm walk order (parser.rs).Limit fill linkage still broken:
process_limit_order_fillcalls:swap_id_for_tx_pair(indexer/src/db/queries/limit_order_fills.rs):This always links fills to the first swap row (MIN internal id), not the swap that produced the fill when a tx has multiple swaps on the same pair (router revisit, batch).
limit_order_fillwasm events do not currently emitswap_index.Follow-up: GitLab #287.
Why this is needed
Incorrect
swap_event_idbreaks:book_input,expired_parks_*)Constraints / guardrails
swap_indexattr not available.swap_index(orswap_event_index) tolimit_order_fill_eventattrs — coordinate wasm + indexer + backward compat.fill_existsdedup key(tx_hash, pair_id, order_id).Relevant files
indexer/src/db/queries/limit_order_fills.rsindexer/src/indexer/parser.rs(process_limit_order_fill,parse_swaps)indexer/src/db/queries/swap_events.rssmartcontracts/contracts/pair/src/orderbook.rs(limit_order_fill_event)indexer/migrations/20260605000000_swap_events_per_tx_pair_swap_index.sqldocs/integrators-hybrid-volume.mdRecommended direction
swap_indexonlimit_order_fillevents during hybrid execute for durable LCD replay.swap_id_for_tx_pairwithswap_id_for_tx_pair_index(tx, pair, swap_index).Acceptance criteria
swap_event_idpoints to correctswap_eventsrow.Test plan (all paths)
Attack / abuse / hack vectors
Verification criteria
JOIN swap_events ON swap_event_id = swap_events.idmatchesswap_indexordering.cargo testindexer suite green.marked as related to #287
mentioned in merge request !774
Took this — it's the MIN(id) linkage I flagged on #287. MR !774.
process_limit_order_fill was resolving the parent swap with swap_id_for_tx_pair = SELECT id ... ORDER BY id ASC LIMIT 1, so every fill linked to the FIRST swap on the pair. Two swaps on one pair in a tx (router revisit / batch) -> the 2nd swap's fills mis-linked to the 1st.
Fills don't carry swap_index on-chain, so I derive it from the deterministic parser walk: maker fills are emitted before their swap action in the same execute (the pair adds book_fill_events, then the swap attribute), so a fill belongs to the upcoming swap on its pair. parse_limit_order_fills now keeps a per-pair swap counter that mirrors parse_swaps' one-swap-per-event detection exactly (so the ordinals line up with the persisted swap_events.swap_index), and tags each fill. process_limit_order_fill then links via swap_id_for_tx_pair_index(tx, pair, swap_index) on the unique (tx_hash, pair_id, swap_index) key from #287, not MIN(id).
No schema change (the link is the existing swap_event_id FK). Single-swap txs unchanged (index 0). Historical multi-swap-same-pair rows can't be backfilled (fills don't store an ordinal) — single-swap history was already correct, so only historical multi-swap would need a re-index; flagging that rather than shipping a migration that can't actually fix it.
Tests: parse_limit_order_fills_assigns_swap_index_per_pair_swap (two swaps one pair -> fills carry 0/1/1) + limit_fill_swap_linkage.rs integration (each swap_index resolves to its own swap_events row). Verified green.
Separate heads-up while I was in here: tests/limit_order_parked_lifecycle.rs::park_event_then_claim_updates_db_and_api_filters is RED on main (fails with my changes stashed too). Its wasm_park_tx/wasm_claim_tx fixtures still use the unreserved contract_address key, but #285 (
e951e61) scoped lifecycle events to the reserved _contract_address only — so the park event never matches and the order stays active. Test-only fixture staleness from the #285 merge; I'll put up a small fix for it. @PlasticDigitsmentioned in merge request !775
mentioned in issue #285
mentioned in commit
8c591a47a4mentioned in merge request !780
mentioned in issue #287
mentioned in merge request !783
mentioned in commit
a121e8a7fdmentioned in merge request !804
Implementation verification — #316
Status
The indexer fix was already merged to
maininf1caac4(MRqa/316-fill-swap-linkage). This pass adds the remaining integrator documentation and re-verifies all acceptance criteria.MR: !804 — docs: fill↔swap linkage by swap_index for integrators (#316)
What changed (code — already on
main)swap_id_for_tx_pair(ORDER BY id ASC LIMIT 1) replaced byswap_id_for_tx_pair_index(tx, pair, swap_index)keyed on(tx_hash, pair_id, swap_index).parse_limit_order_fillsassigns per-pairswap_indexfrom parser walk order (fills precede theirswapaction).process_limit_order_filllinks via the fill'sswap_index.What changed (this branch — docs only)
docs/integrators-hybrid-volume.md— Fill ↔ swap linkage sectionskills/AGENTS_INTEGRATOR_HYBRID_VOLUME.md— cross-link + test commandsAcceptance checklist
swap_event_id→ correctswap_eventsrowcd indexer && cargo test --test limit_fill_swap_linkage -- --test-threads=1swap_index0)cd indexer && cargo test parse_limit_order_fills_assigns_swap_index --libindexer/tests/limit_fill_swap_linkage.rsdocs/integrators-hybrid-volume.md§ Fill ↔ swap linkage (+indexer-invariants.mdlimit fill rows row)Verification commands (third parties)
Follow-ups (not blockers)
swap_indexonlimit_order_fillwasm events for durable LCD replay.mentioned in commit
22fcfdf310Verification — #316
Independent QA pass on branch
cursor/gitlab-issue-verification-b68c(includes merged indexer fix MR !774f1caac4and docs MR !804).Acceptance criteria
swap_event_id→ correctswap_eventsrowcd indexer && cargo test --test limit_fill_swap_linkage -- --test-threads=1—fill_links_to_its_own_swap_not_the_first_on_the_pairresolvesswap_index0 → first row,swap_index1 → second row (not MIN(id))swap_index0)cd indexer && cargo test parse_limit_order_fills_assigns_swap_index --lib+parse_limit_order_fills_twenty_makers_merged_before_transferasserts all fills carryswap_index == 0indexer/tests/limit_fill_swap_linkage.rspresent and greendocs/integrators-hybrid-volume.md§ Fill ↔ swap linkage (swap_event_id);skills/AGENTS_INTEGRATOR_HYBRID_VOLUME.md;docs/indexer-invariants.mdlimit fill rows rowVerification criteria (issue body)
limit_fill_swap_linkage.rsswap_index(notORDER BY id)swap_id_for_tx_pair_indexquery on(tx_hash, pair_id, swap_index)— integration testcd indexer && cargo test --lib→ 118 passed; integration test aboveImplementation summary (already on
main)swap_id_for_tx_pair(ORDER BY id ASC LIMIT 1) replaced byswap_id_for_tx_pair_index(tx, pair, swap_index).parse_limit_order_fillsassigns per-pairswap_indexfrom parser walk order (fills precede theirswapaction).process_limit_order_filllinks via the fill'sswap_index.No additional repo changes required from this verification pass.
mentioned in issue #331
marked as related to #331
mentioned in merge request !817