diff --git a/ethcoder/events.go b/ethcoder/events.go index c034d67..c65929e 100644 --- a/ethcoder/events.go +++ b/ethcoder/events.go @@ -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 } diff --git a/ethcoder/events_parser.go b/ethcoder/events_parser.go index 36f29c5..9525713 100644 --- a/ethcoder/events_parser.go +++ b/ethcoder/events_parser.go @@ -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 "" + } + 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{}, @@ -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 == "" { diff --git a/ethcoder/events_test.go b/ethcoder/events_test.go index 3c9d7a9..9358d26 100644 --- a/ethcoder/events_test.go +++ b/ethcoder/events_test.go @@ -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 { @@ -92,7 +92,7 @@ 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) @@ -100,6 +100,10 @@ func TestValidateEventSig(t *testing.T) { 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)