From 5674ad4fba034ef1397098a88c3c64f443da94f2 Mon Sep 17 00:00:00 2001 From: Shiming Date: Fri, 11 Jun 2021 11:58:12 +0800 Subject: [PATCH 1/3] fix: uint support & method extract issue https://github.com/Alethio/web3-multicall-go/issues/8 https://github.com/Alethio/web3-multicall-go/issues/9 --- multicall/viewcall.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/multicall/viewcall.go b/multicall/viewcall.go index 8a74603..2dfc86d 100644 --- a/multicall/viewcall.go +++ b/multicall/viewcall.go @@ -61,7 +61,10 @@ func (call ViewCall) returnTypes() []string { rawArgs = strings.Replace(rawArgs, ")", "", -1) args := strings.Split(rawArgs, ",") for index, arg := range args { - args[index] = strings.Trim(arg, " ") + if strings.TrimSpace(arg) == "uint"{ + arg = "uint256" + } + args[index] = strings.Trim(arg, " ") } return args } @@ -84,7 +87,7 @@ func (call ViewCall) callData() ([]byte, error) { } func (call ViewCall) methodCallData() ([]byte, error) { - methodParts := strings.Split(call.method, ")(") + methodParts := strings.Split(strings.TrimSpace(call.Method), ")(") var method string if len(methodParts) > 1 { method = fmt.Sprintf("%s)", methodParts[0]) @@ -104,6 +107,9 @@ func (call ViewCall) argsCallData() ([]byte, error) { arguments := make(abi.Arguments, len(call.arguments)) for index, argTypeStr := range argTypes { + if strings.TrimSpace(argTypeStr) == "uint"{ + argTypeStr = "uint256" + } argType, err := abi.NewType(argTypeStr, "", nil) if err != nil { return nil, err From 10a84c4bb697dca1bf81dda6bc4c67bc2bba6464 Mon Sep 17 00:00:00 2001 From: Shiming Date: Fri, 11 Jun 2021 12:25:48 +0800 Subject: [PATCH 2/3] fix: replace all the space in method extract if there's a space between `)(` for example `balanceOf(address) (uint256)`, after this fix, method can be extract correctly, otherwise error. --- multicall/viewcall.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/multicall/viewcall.go b/multicall/viewcall.go index 2dfc86d..0b8a813 100644 --- a/multicall/viewcall.go +++ b/multicall/viewcall.go @@ -87,7 +87,7 @@ func (call ViewCall) callData() ([]byte, error) { } func (call ViewCall) methodCallData() ([]byte, error) { - methodParts := strings.Split(strings.TrimSpace(call.Method), ")(") + methodParts := strings.Split(strings.ReplaceAll(call.Method, " ", ""), ")(") var method string if len(methodParts) > 1 { method = fmt.Sprintf("%s)", methodParts[0]) From 4de41d59901f9848879a2e2111e5c1abe5ff0edf Mon Sep 17 00:00:00 2001 From: Shiming Date: Fri, 11 Jun 2021 16:57:59 +0800 Subject: [PATCH 3/3] fix: uint to uint256 for method hash --- multicall/viewcall.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/multicall/viewcall.go b/multicall/viewcall.go index 0b8a813..5ef24f5 100644 --- a/multicall/viewcall.go +++ b/multicall/viewcall.go @@ -40,6 +40,7 @@ func (call ViewCall) Validate() error { var insideParens = regexp.MustCompile("\\(.*?\\)") var numericArg = regexp.MustCompile("u?int(256)|(8)") +var uintMatch = regexp.MustCompile("\\buint\\b") func (call ViewCall) argumentTypes() []string { rawArgs := insideParens.FindAllString(call.method, -1)[0] @@ -61,10 +62,10 @@ func (call ViewCall) returnTypes() []string { rawArgs = strings.Replace(rawArgs, ")", "", -1) args := strings.Split(rawArgs, ",") for index, arg := range args { - if strings.TrimSpace(arg) == "uint"{ - arg = "uint256" - } - args[index] = strings.Trim(arg, " ") + if strings.TrimSpace(arg) == "uint" { + arg = "uint256" + } + args[index] = strings.Trim(arg, " ") } return args } @@ -87,7 +88,7 @@ func (call ViewCall) callData() ([]byte, error) { } func (call ViewCall) methodCallData() ([]byte, error) { - methodParts := strings.Split(strings.ReplaceAll(call.Method, " ", ""), ")(") + methodParts := strings.Split(uintMatch.ReplaceAllString(strings.ReplaceAll(call.method, " ", ""), "uint256"), ")(") var method string if len(methodParts) > 1 { method = fmt.Sprintf("%s)", methodParts[0]) @@ -107,7 +108,7 @@ func (call ViewCall) argsCallData() ([]byte, error) { arguments := make(abi.Arguments, len(call.arguments)) for index, argTypeStr := range argTypes { - if strings.TrimSpace(argTypeStr) == "uint"{ + if strings.TrimSpace(argTypeStr) == "uint" { argTypeStr = "uint256" } argType, err := abi.NewType(argTypeStr, "", nil)