|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/ethereum/go-ethereum/accounts/abi" |
| 9 | +) |
| 10 | + |
| 11 | +func ConstructFunctionABI(signature string) (*abi.Method, error) { |
| 12 | + regex := regexp.MustCompile(`^(\w+)\((.*)\)$`) |
| 13 | + matches := regex.FindStringSubmatch(strings.TrimSpace(signature)) |
| 14 | + if len(matches) != 3 { |
| 15 | + return nil, fmt.Errorf("invalid event signature format") |
| 16 | + } |
| 17 | + |
| 18 | + functionName := matches[1] |
| 19 | + params := matches[2] |
| 20 | + |
| 21 | + inputs, err := parseParamsToAbiArguments(params) |
| 22 | + if err != nil { |
| 23 | + return nil, fmt.Errorf("failed to parse params to abi arguments '%s': %v", params, err) |
| 24 | + } |
| 25 | + |
| 26 | + function := abi.NewMethod(functionName, functionName, abi.Function, "", false, false, inputs, nil) |
| 27 | + |
| 28 | + return &function, nil |
| 29 | +} |
| 30 | + |
| 31 | +func parseParamsToAbiArguments(params string) (abi.Arguments, error) { |
| 32 | + paramList := splitParams(strings.TrimSpace(params)) |
| 33 | + var inputs abi.Arguments |
| 34 | + for idx, param := range paramList { |
| 35 | + arg, err := parseParamToAbiArgument(param, fmt.Sprintf("%d", idx)) |
| 36 | + if err != nil { |
| 37 | + return nil, fmt.Errorf("failed to parse param to arg '%s': %v", param, err) |
| 38 | + } |
| 39 | + inputs = append(inputs, *arg) |
| 40 | + } |
| 41 | + return inputs, nil |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Splits a string of parameters into a list of parameters |
| 46 | + */ |
| 47 | +func splitParams(params string) []string { |
| 48 | + var result []string |
| 49 | + depth := 0 |
| 50 | + current := "" |
| 51 | + for _, r := range params { |
| 52 | + switch r { |
| 53 | + case ',': |
| 54 | + if depth == 0 { |
| 55 | + result = append(result, strings.TrimSpace(current)) |
| 56 | + current = "" |
| 57 | + continue |
| 58 | + } |
| 59 | + case '(': |
| 60 | + depth++ |
| 61 | + case ')': |
| 62 | + depth-- |
| 63 | + } |
| 64 | + current += string(r) |
| 65 | + } |
| 66 | + if strings.TrimSpace(current) != "" { |
| 67 | + result = append(result, strings.TrimSpace(current)) |
| 68 | + } |
| 69 | + return result |
| 70 | +} |
| 71 | + |
| 72 | +func parseParamToAbiArgument(param string, fallbackName string) (*abi.Argument, error) { |
| 73 | + argName, paramType, err := getArgNameAndType(param, fallbackName) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("failed to get arg name and type '%s': %v", param, err) |
| 76 | + } |
| 77 | + if isTuple(paramType) { |
| 78 | + argType, err := marshalTupleParamToArgumentType(paramType) |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("failed to marshal tuple: %v", err) |
| 81 | + } |
| 82 | + return &abi.Argument{ |
| 83 | + Name: argName, |
| 84 | + Type: argType, |
| 85 | + }, nil |
| 86 | + } else { |
| 87 | + argType, err := abi.NewType(paramType, paramType, nil) |
| 88 | + if err != nil { |
| 89 | + return nil, fmt.Errorf("failed to parse type '%s': %v", paramType, err) |
| 90 | + } |
| 91 | + return &abi.Argument{ |
| 92 | + Name: argName, |
| 93 | + Type: argType, |
| 94 | + }, nil |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func getArgNameAndType(param string, fallbackName string) (name string, paramType string, err error) { |
| 99 | + if isTuple(param) { |
| 100 | + lastParenIndex := strings.LastIndex(param, ")") |
| 101 | + if lastParenIndex == -1 { |
| 102 | + return "", "", fmt.Errorf("invalid tuple format") |
| 103 | + } |
| 104 | + if len(param)-1 == lastParenIndex { |
| 105 | + return fallbackName, param, nil |
| 106 | + } |
| 107 | + paramsEndIdx := lastParenIndex + 1 |
| 108 | + if strings.HasPrefix(param[paramsEndIdx:], "[]") { |
| 109 | + paramsEndIdx = lastParenIndex + 3 |
| 110 | + } |
| 111 | + return strings.TrimSpace(param[paramsEndIdx:]), param[:paramsEndIdx], nil |
| 112 | + } else { |
| 113 | + tokens := strings.Fields(param) |
| 114 | + if len(tokens) == 1 { |
| 115 | + return fallbackName, strings.TrimSpace(tokens[0]), nil |
| 116 | + } |
| 117 | + return strings.TrimSpace(tokens[len(tokens)-1]), strings.Join(tokens[:len(tokens)-1], " "), nil |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func isTuple(param string) bool { |
| 122 | + return strings.HasPrefix(param, "(") |
| 123 | +} |
| 124 | + |
| 125 | +func marshalTupleParamToArgumentType(paramType string) (abi.Type, error) { |
| 126 | + typ := "tuple" |
| 127 | + isSlice := strings.HasSuffix(paramType, "[]") |
| 128 | + strippedParamType := strings.TrimPrefix(paramType, "(") |
| 129 | + if isSlice { |
| 130 | + strippedParamType = strings.TrimSuffix(strippedParamType, "[]") |
| 131 | + typ = "tuple[]" |
| 132 | + } |
| 133 | + strippedParamType = strings.TrimSuffix(strippedParamType, ")") |
| 134 | + components, err := marshalParamArguments(strippedParamType) |
| 135 | + if err != nil { |
| 136 | + return abi.Type{}, fmt.Errorf("failed to marshal tuple: %v", err) |
| 137 | + } |
| 138 | + return abi.NewType(typ, typ, components) |
| 139 | +} |
| 140 | + |
| 141 | +func marshalParamArguments(param string) ([]abi.ArgumentMarshaling, error) { |
| 142 | + paramList := splitParams(param) |
| 143 | + components := []abi.ArgumentMarshaling{} |
| 144 | + for idx, param := range paramList { |
| 145 | + argName, paramType, err := getArgNameAndType(param, fmt.Sprintf("field%d", idx)) |
| 146 | + if err != nil { |
| 147 | + return nil, fmt.Errorf("failed to get arg name and type '%s': %v", param, err) |
| 148 | + } |
| 149 | + if isTuple(paramType) { |
| 150 | + subComponents, err := marshalParamArguments(paramType[1 : len(paramType)-1]) |
| 151 | + if err != nil { |
| 152 | + return nil, fmt.Errorf("failed to marshal tuple: %v", err) |
| 153 | + } |
| 154 | + components = append(components, abi.ArgumentMarshaling{ |
| 155 | + Type: "tuple", |
| 156 | + Name: argName, |
| 157 | + Components: subComponents, |
| 158 | + }) |
| 159 | + } else { |
| 160 | + components = append(components, abi.ArgumentMarshaling{ |
| 161 | + Type: paramType, |
| 162 | + Name: argName, |
| 163 | + }) |
| 164 | + } |
| 165 | + } |
| 166 | + return components, nil |
| 167 | +} |
0 commit comments