Repository navigation
fix(contract): prefer the exact event match in parseEvent - #1900
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Duplicate data-param names can make an event declaration incorrectly match a map while silently dropping an unmatched field, requiring distinct-name counting and a regression test.
1 open issue
What changed in this PR
Tightens contract-event matching in Spec.parseEvent(), which generated clients also use, to skip declarations whose data shape does not fit rather than return partial results.
Changes:
- Checks map key coverage, exact vector length, and void data for single-value declarations with no data parameters.
- Preserves sparse-map omissions for
Option,()andVal, plus dense-map compatibility. - Adds regression tests and documents when parsing now returns
undefined.
| File | Description |
|---|---|
test/unit/spec/event_spec.test.ts |
Adds shape-validation and fallback tests; updates the malformed-map expectation. |
src/contract/spec.ts |
Documents stricter event-data matching. |
src/contract/event_spec.ts |
Adds data-shape checks during candidate matching. |
docs/reference/contracts-client.md |
Updates generated API documentation and source links. |
CHANGELOG.md |
Records the fix and its compatibility impact. |
Review effort: Balanced
|
This removes behavior we intended. Only one of the four rules is a pure bug fix. The missing-key rule is right. The other three rules (undeclared map key, longer vec, non-void data on a single value with no data params) resolve collisions by rejecting the declaration. They also reject it when there is no collision, where the partial parse was the intended result. The CHANGELOG entry says so: data from a newer contract version now returns Both goals can be met. Run the declarations with the strict rules first. If none fits, run them again with the old lenient rules for these three cases, and keep the missing-key rule in both passes. Then:
The second pass runs only when no declaration fits strictly, so the common path should cost the same as this PR. Was the two-pass version you measured this design, or a pre-check pass inside the loop? If we take this, the "returns undefined when no declaration lists every key" test and the |
|
Should we consider sorting the declarations instead of the exact/loose split? Sort by prefix topics, then topic param count, then data param count, biggest first, and take the first one that matches. That way we always try the best fit first and fall back to smaller declarations with the same prefix. It also covers the cases the split misses, like two declarations that differ only in topic params ( |

Spec.parseEvent()and the generated bindings'parseEvent(), which returned an earlier declaration with fields dropped when a later declaration fit the event — e.g.{ amount, memo }parsed asMint { amount }whenMintcame first, and the topics[evt, from, to]parsed asEvt { from }whenEvtcame beforeEvtTo { from, to }parseEventdocs and pinned by a test)main)Option,()orVal— the sparse map drops only void values, so{}no longer parses asMintwith noamount(aValfield can hold void, so it joins the issue'sOptionand())parseEventneeds. Tightening the generated types is a follow-up (Generated event interfaces mark required map fields as optional #1920){ amount, memo, extra }returnsMintand[1, 2, 3]returnsEvtbeforeEvt2; andSpec.scValToNativelets a number through anAddressorStringtype, so a numeric trailing topic can fill a typed topic param of a later declaration (follow-up: Spec.scValToNative passes a number through Address and String types #1919)Mint { amount }andMintMemo { amount, memo: None }emit the same bytes, so a sort would always pickMintMemo, and it would change the winner for events that already match exactlyCloses #1818