github.com/0xsequence/ethkit@v1.25.0/ethcoder/events.go (about) 1 package ethcoder 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/0xsequence/ethkit" 8 ) 9 10 // EventTopicHash returns the keccak256 hash of the event signature 11 // 12 // e.g. "Transfer(address indexed from, address indexed to, uint256 value)" 13 // will return 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef 14 func EventTopicHash(event string) (ethkit.Hash, error) { 15 eventSig := parseEventSignature(event) 16 if eventSig == "" { 17 return ethkit.Hash{}, fmt.Errorf("ethcoder: event format is invalid, expecting Method(arg1,arg2,..)") 18 } 19 return Keccak256Hash([]byte(eventSig)), nil 20 } 21 22 func parseEventSignature(event string) string { 23 if !strings.Contains(event, "(") || !strings.Contains(event, ")") { 24 return "" 25 } 26 p := strings.Split(event, "(") 27 if len(p) != 2 { 28 return "" 29 } 30 method := p[0] 31 32 args := strings.TrimSuffix(p[1], ")") 33 if args == "" { 34 return fmt.Sprintf("%s()", method) 35 } 36 37 typs := []string{} 38 p = strings.Split(args, ",") 39 for _, a := range p { 40 typ := strings.Split(strings.TrimSpace(a), " ")[0] 41 typs = append(typs, typ) 42 } 43 44 return fmt.Sprintf("%s(%s)", method, strings.Join(typs, ",")) 45 }