-
Notifications
You must be signed in to change notification settings - Fork 607
fix: EventMetadata class implementation for serialisation #9574
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 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4a05735
fix: EventMetadata class implementation for serialisation
spypsy 4cd64b5
Merge branch 'master' into spy/event-metadata
spypsy 968e766
fix e2e test
spypsy d5401cc
Merge branch 'master' into spy/event-metadata
spypsy acdc2fd
improve getEvents API
spypsy 5c5a9d1
small fixes
spypsy ad380c2
rm unused import
spypsy 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
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,63 @@ | ||
| import { L1EventPayload, type UnencryptedL2Log } from '@aztec/circuit-types'; | ||
| import { type AbiType } from '@aztec/foundation/abi'; | ||
| import { EventSelector, decodeFromAbi } from '@aztec/foundation/abi'; | ||
| import { Fr } from '@aztec/foundation/fields'; | ||
|
|
||
| /** | ||
| * Represents metadata for an event decoder, including all information needed to reconstruct it. | ||
| */ | ||
| export class EventMetadata<T> { | ||
| public readonly decode: (payload: L1EventPayload | UnencryptedL2Log) => T | undefined; | ||
|
|
||
| constructor(public eventSelector: EventSelector, private readonly eventType: AbiType, public fieldNames: string[]) { | ||
| this.decode = EventMetadata.decodeEvent<T>(eventSelector, eventType); | ||
| } | ||
|
|
||
| public static decodeEvent<T>( | ||
| eventSelector: EventSelector, | ||
| eventType: AbiType, | ||
| ): (payload: L1EventPayload | UnencryptedL2Log | undefined) => T | undefined { | ||
| return (payload: L1EventPayload | UnencryptedL2Log | undefined): T | undefined => { | ||
| if (payload === undefined) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (payload instanceof L1EventPayload) { | ||
| if (!eventSelector.equals(payload.eventTypeId)) { | ||
| return undefined; | ||
| } | ||
| return decodeFromAbi([eventType], payload.event.items) as T; | ||
| } else { | ||
| const items = []; | ||
| for (let i = 0; i < payload.data.length; i += 32) { | ||
| items.push(new Fr(payload.data.subarray(i, i + 32))); | ||
| } | ||
|
|
||
| return decodeFromAbi([eventType], items) as T; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Serializes the metadata to a JSON-friendly format | ||
| */ | ||
| public toJSON() { | ||
| return { | ||
| type: 'event_metadata', | ||
| eventSelector: this.eventSelector.toString(), | ||
| eventType: this.eventType, | ||
| fieldNames: this.fieldNames, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Creates an EventMetadata instance from a JSON representation | ||
| */ | ||
| public static fromJSON(json: any): EventMetadata<any> { | ||
| if (json?.type !== 'event_metadata') { | ||
| throw new Error('Invalid event metadata format'); | ||
| } | ||
|
|
||
| return new EventMetadata(EventSelector.fromString(json.eventSelector), json.eventType, json.fieldNames); | ||
| } | ||
| } |
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
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.