-
Notifications
You must be signed in to change notification settings - Fork 533
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -21,3 +21,25 @@ func (eb ContractEventBody) String() string { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
func (de DiagnosticEvent) String() string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return fmt.Sprintf("%s, successful call: %t", de.Event, de.InSuccessfulContractCall) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// GetTopics extracts the topics from a contract event body. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// This is a helper function to abstract the versioning of ContractEventBody. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 commentThe 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
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback
Comment on lines
+27
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback
Comment on lines
+27
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package xdr | ||
|
||
import "encoding/base64" | ||
|
||
// SerializeScVal converts an ScVal to two map representations: | ||
// 1. A map with base64-encoded value and type | ||
// 2. A map with human-readable decoded value and type | ||
// This is useful for data export and analytics purposes. | ||
func (scVal ScVal) Serialize() (map[string]string, map[string]string) { | ||
serializedData := map[string]string{} | ||
serializedData["value"] = "n/a" | ||
serializedData["type"] = "n/a" | ||
|
||
serializedDataDecoded := map[string]string{} | ||
serializedDataDecoded["value"] = "n/a" | ||
serializedDataDecoded["type"] = "n/a" | ||
|
||
if scValTypeName, ok := scVal.ArmForSwitch(int32(scVal.Type)); ok { | ||
serializedData["type"] = scValTypeName | ||
serializedDataDecoded["type"] = scValTypeName | ||
if raw, err := scVal.MarshalBinary(); err == nil { | ||
serializedData["value"] = base64.StdEncoding.EncodeToString(raw) | ||
serializedDataDecoded["value"] = scVal.String() | ||
} | ||
} | ||
|
||
return serializedData, serializedDataDecoded | ||
} | ||
|
||
// SerializeScValArray converts an array of ScVal to two arrays of map representations. | ||
// Each ScVal is serialized using the Serialize method. | ||
func SerializeScValArray(scVals []ScVal) ([]map[string]string, []map[string]string) { | ||
data := make([]map[string]string, 0, len(scVals)) | ||
dataDecoded := make([]map[string]string, 0, len(scVals)) | ||
|
||
for _, scVal := range scVals { | ||
serializedData, serializedDataDecoded := scVal.Serialize() | ||
data = append(data, serializedData) | ||
dataDecoded = append(dataDecoded, serializedDataDecoded) | ||
} | ||
|
||
return data, dataDecoded | ||
} |
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))
Copilot uses AI. Check for mistakes.