Skip to content

Commit

Permalink
ethcoder: add EventDef#String
Browse files Browse the repository at this point in the history
  • Loading branch information
pkieltyka committed Jul 16, 2024
1 parent a8e9d09 commit 3f61ced
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
9 changes: 8 additions & 1 deletion ethcoder/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ func EventTopicHash(event string) (ethkit.Hash, string, error) {
}

func ValidateEventSig(eventSig string) (bool, error) {
selector, err := abi.ParseSelector(eventSig)
// First parse with eventDef to normalize
eventDef, err := ParseEventDef(eventSig)
if err != nil {
return false, err
}

// Then check against the selector to confirm
selector, err := abi.ParseSelector(eventDef.Sig)
if err != nil {
return false, err
}
Expand Down
26 changes: 23 additions & 3 deletions ethcoder/events_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ type EventDef struct {
NumIndexed int `json:"-"`
}

func (e EventDef) String() string {
if !(len(e.ArgTypes) == len(e.ArgIndexed) && len(e.ArgTypes) == len(e.ArgNames)) {
return "<invalid event definition>"
}
s := ""
for i := range e.ArgTypes {
s += e.ArgTypes[i]
if e.ArgIndexed[i] {
s += " indexed"
}
if e.ArgNames[i] != "" {
s += " " + e.ArgNames[i]
}
if i < len(e.ArgTypes)-1 {
s += ","
}
}
return fmt.Sprintf("%s(%s)", e.Name, s)
}

func ParseEventDef(event string) (EventDef, error) {
eventDef := EventDef{
ArgTypes: []string{},
Expand Down Expand Up @@ -96,9 +116,9 @@ type eventSelectorTree struct {
// ie. "address indexed from, address indexed to, uint256 value".
func parseEventArgs(eventArgs string, iteration int) (eventSelectorTree, error) {
args := strings.TrimSpace(eventArgs)
if iteration == 0 {
args = strings.ReplaceAll(eventArgs, " ", "")
}
// if iteration == 0 {
// args = strings.ReplaceAll(eventArgs, " ", "")
// }

out := eventSelectorTree{}
if args == "" {
Expand Down
8 changes: 6 additions & 2 deletions ethcoder/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestEventTopicHash3(t *testing.T) {
}{
{"NftItemCreated(uint256,uint32,address,bool,uint32,address,address[],uint32[])"},
{"NftItemCreated(uint256 num,uint32 val,address from,bool flag, uint32 param, address from, address[] friends, uint32[] nums )"},
{"NftItemCreated(uint256,uint32,address op,bool,uint32,address,address[],uint32[] nums)"},
{"NftItemCreated(uint256,uint32,address op,bool,uint32,address,address[],uint32[] nums)"},
}

for _, x := range in {
Expand All @@ -92,14 +92,18 @@ func TestEventTopicHash3(t *testing.T) {
}

func TestValidateEventSig(t *testing.T) {
valid, err := ethcoder.ValidateEventSig("Approve(address,address,uint256)")
valid, err := ethcoder.ValidateEventSig("Approve(address indexed,address,uint256)")
require.NoError(t, err)
require.True(t, valid)

valid, err = ethcoder.ValidateEventSig("Approve(address,address,uinBREAKt25627fff)")
require.Error(t, err)
require.False(t, valid)

valid, err = ethcoder.ValidateEventSig("Approve(address indexed, address, uint256 )")
require.NoError(t, err)
require.True(t, valid)

// valid, err = ethcoder.ValidateEventSig("Approve(address,address,uint2ffff)")
// require.Error(t, err)
// require.False(t, valid)
Expand Down

0 comments on commit 3f61ced

Please sign in to comment.