feat(abigen): add --contract flag to specify a given contract (#334)

* add contract flag

* Use GlobalXX

* Update cmd/abigen/main.go

Co-authored-by: Péter Garamvölgyi <th307q@gmail.com>

* Update cmd/abigen/main.go

Co-authored-by: Péter Garamvölgyi <th307q@gmail.com>

---------

Co-authored-by: Péter Garamvölgyi <th307q@gmail.com>
Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
This commit is contained in:
maskpp 2023-05-22 22:44:05 +08:00 committed by GitHub
parent 2f7085715b
commit 0c182f13bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 <solFilePath>:<type>
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