xdr: add helper functions for contract events#5780
xdr: add helper functions for contract events#5780DavidLiedle wants to merge 1 commit intostellar:mainfrom
Conversation
This commit addresses TODOs in the contract events processor by moving frequently-used helper functions to the XDR package where they belong. Changes: - Add GetTopics() and GetData() methods to ContractEventBody - Add ToContractAddress() method to Hash for contract ID conversion - Add Serialize() method to ScVal for consistent serialization - Add SerializeScValArray() for array serialization - Update contract_events.go and contract_data.go to use new XDR methods - Remove duplicate code from processors These changes improve code reusability and maintainability by centralizing XDR-related functionality in the appropriate package. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Pull Request Overview
This PR addresses technical debt by moving XDR-related helper functions from processor packages to the XDR package where they logically belong, improving code organization and reusability.
- Adds helper methods to XDR types for extracting contract event data and serialization
- Moves contract event processing utilities from processors to the XDR package
- Updates processors to use the new centralized XDR methods
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| xdr/scval_helpers.go | Adds ScVal serialization methods moved from processors |
| xdr/hash.go | Adds ToContractAddress() method for converting Hash to contract address |
| xdr/event.go | Adds GetTopics() and GetData() methods to ContractEventBody |
| processors/contract/contract_events.go | Updates to use new XDR methods and removes duplicated functions |
| processors/contract/contract_data.go | Updates to use new ScVal.Serialize() method |
| case 0: | ||
| return eb.MustV0().Topics | ||
| default: | ||
| panic("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) |
There was a problem hiding this comment.
The panic message uses string concatenation with fmt.Sprintf which is inefficient. Consider using fmt.Sprintf directly: panic(fmt.Sprintf("unsupported event body version: %d", eb.V))
| panic("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) | |
| panic(fmt.Sprintf("unsupported event body version: %d", eb.V)) |
| case 0: | ||
| return eb.MustV0().Data | ||
| default: | ||
| panic("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) |
There was a problem hiding this comment.
The panic message uses string concatenation with fmt.Sprintf which is inefficient. Consider using fmt.Sprintf directly: panic(fmt.Sprintf("unsupported event body version: %d", eb.V))
| panic("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) | |
| panic(fmt.Sprintf("unsupported event body version: %d", eb.V)) |
| func (eb ContractEventBody) GetTopics() []ScVal { | ||
| switch eb.V { | ||
| case 0: | ||
| return eb.MustV0().Topics | ||
| default: | ||
| panic("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) | ||
| } | ||
| } | ||
|
|
||
| // GetData extracts the data from a contract event body. | ||
| // This is a helper function to abstract the versioning of ContractEventBody. | ||
| func (eb ContractEventBody) GetData() ScVal { | ||
| switch eb.V { | ||
| case 0: | ||
| return eb.MustV0().Data | ||
| default: | ||
| panic("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) |
There was a problem hiding this comment.
Consider returning an error instead of panicking to allow callers to handle unsupported versions gracefully. Panicking can crash the application and makes error handling more difficult.
| func (eb ContractEventBody) GetTopics() []ScVal { | |
| switch eb.V { | |
| case 0: | |
| return eb.MustV0().Topics | |
| default: | |
| panic("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) | |
| } | |
| } | |
| // GetData extracts the data from a contract event body. | |
| // This is a helper function to abstract the versioning of ContractEventBody. | |
| func (eb ContractEventBody) GetData() ScVal { | |
| switch eb.V { | |
| case 0: | |
| return eb.MustV0().Data | |
| default: | |
| panic("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) | |
| func (eb ContractEventBody) GetTopics() ([]ScVal, error) { | |
| switch eb.V { | |
| case 0: | |
| return eb.MustV0().Topics, nil | |
| default: | |
| return nil, errors.New("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) | |
| } | |
| } | |
| // GetData extracts the data from a contract event body. | |
| // This is a helper function to abstract the versioning of ContractEventBody. | |
| func (eb ContractEventBody) GetData() (ScVal, error) { | |
| switch eb.V { | |
| case 0: | |
| return eb.MustV0().Data, nil | |
| default: | |
| return ScVal{}, errors.New("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) |
| func (eb ContractEventBody) GetTopics() []ScVal { | ||
| switch eb.V { | ||
| case 0: | ||
| return eb.MustV0().Topics | ||
| default: | ||
| panic("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) | ||
| } | ||
| } | ||
|
|
||
| // GetData extracts the data from a contract event body. | ||
| // This is a helper function to abstract the versioning of ContractEventBody. | ||
| func (eb ContractEventBody) GetData() ScVal { | ||
| switch eb.V { | ||
| case 0: | ||
| return eb.MustV0().Data | ||
| default: | ||
| panic("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) |
There was a problem hiding this comment.
Consider returning an error instead of panicking to allow callers to handle unsupported versions gracefully. Panicking can crash the application and makes error handling more difficult.
| func (eb ContractEventBody) GetTopics() []ScVal { | |
| switch eb.V { | |
| case 0: | |
| return eb.MustV0().Topics | |
| default: | |
| panic("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) | |
| } | |
| } | |
| // GetData extracts the data from a contract event body. | |
| // This is a helper function to abstract the versioning of ContractEventBody. | |
| func (eb ContractEventBody) GetData() ScVal { | |
| switch eb.V { | |
| case 0: | |
| return eb.MustV0().Data | |
| default: | |
| panic("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) | |
| func (eb ContractEventBody) GetTopics() ([]ScVal, error) { | |
| switch eb.V { | |
| case 0: | |
| return eb.MustV0().Topics, nil | |
| default: | |
| return nil, fmt.Errorf("unsupported event body version: %d", eb.V) | |
| } | |
| } | |
| // GetData extracts the data from a contract event body. | |
| // This is a helper function to abstract the versioning of ContractEventBody. | |
| func (eb ContractEventBody) GetData() (ScVal, error) { | |
| switch eb.V { | |
| case 0: | |
| return eb.MustV0().Data, nil | |
| default: | |
| return ScVal{}, fmt.Errorf("unsupported event body version: %d", eb.V) |
This commit addresses TODOs in the contract events processor by moving frequently-used helper functions to the XDR package where they belong.
Changes:
These changes improve code reusability and maintainability by centralizing XDR-related functionality in the appropriate package.
🤖 Generated with Claude Code