From 0c182f13bddffb6327adb7511b547436fd80c578 Mon Sep 17 00:00:00 2001 From: maskpp Date: Mon, 22 May 2023 22:44:05 +0800 Subject: [PATCH] feat(abigen): add `--contract` flag to specify a given contract (#334) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add contract flag * Use GlobalXX * Update cmd/abigen/main.go Co-authored-by: Péter Garamvölgyi * Update cmd/abigen/main.go Co-authored-by: Péter Garamvölgyi --------- Co-authored-by: Péter Garamvölgyi Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> --- cmd/abigen/main.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/cmd/abigen/main.go b/cmd/abigen/main.go index 24b3f12bc9..0c6926f5fe 100644 --- a/cmd/abigen/main.go +++ b/cmd/abigen/main.go @@ -99,6 +99,10 @@ var ( Name: "alias", Usage: "Comma separated aliases for function and event renaming, e.g. original1=alias1, original2=alias2", } + contractFlag = cli.StringFlag{ + Name: "contract", + Usage: "Name of the contract to generate the bindings for", + } ) func init() { @@ -117,6 +121,7 @@ func init() { outFlag, langFlag, aliasFlag, + contractFlag, } app.Action = utils.MigrateFlags(abigen) cli.CommandHelpTemplate = flags.OriginCommandHelpTemplate @@ -225,6 +230,13 @@ func abigen(c *cli.Context) error { } // Gather all non-excluded contract for binding for name, contract := range contracts { + // The fully qualified name is of the form : + nameParts := strings.Split(name, ":") + typeName := nameParts[len(nameParts)-1] + // If a contract name is provided then ignore all other contracts + if c.GlobalIsSet(contractFlag.Name) && c.GlobalString(contractFlag.Name) != typeName { + continue + } if exclude[strings.ToLower(name)] { continue } @@ -235,11 +247,10 @@ func abigen(c *cli.Context) error { abis = append(abis, string(abi)) bins = append(bins, contract.Code) sigs = append(sigs, contract.Hashes) - nameParts := strings.Split(name, ":") - types = append(types, nameParts[len(nameParts)-1]) + types = append(types, typeName) libPattern := crypto.Keccak256Hash([]byte(name)).String()[2:36] - libs[libPattern] = nameParts[len(nameParts)-1] + libs[libPattern] = typeName } } // Extract all aliases from the flags