-
Notifications
You must be signed in to change notification settings - Fork 532
xdr: add helper functions for contract events #5780
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
base: master
Are you sure you want to change the base?
xdr: add helper functions for contract events #5780
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 <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)) |
Copilot uses AI. Check for mistakes.
case 0: | ||
return eb.MustV0().Data | ||
default: | ||
panic("unsupported event body version: " + fmt.Sprintf("%d", eb.V)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)) |
Copilot uses AI. Check for mistakes.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)) |
Copilot uses AI. Check for mistakes.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
Copilot uses AI. Check for mistakes.
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