Skip to content

Conversation

DavidLiedle
Copy link

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

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]>
@Copilot Copilot AI review requested due to automatic review settings August 9, 2025 21:13
Copy link

@Copilot Copilot AI left a 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))
Copy link
Preview

Copilot AI Aug 9, 2025

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))

Suggested change
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))
Copy link
Preview

Copilot AI Aug 9, 2025

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))

Suggested change
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.

Comment on lines +27 to +43
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))
Copy link
Preview

Copilot AI Aug 9, 2025

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.

Suggested change
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.

Comment on lines +27 to +43
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))
Copy link
Preview

Copilot AI Aug 9, 2025

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.

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant