diff --git a/accounts/abi/bind/template.go b/accounts/abi/bind/template.go index 8828a55c70..c96f205a30 100644 --- a/accounts/abi/bind/template.go +++ b/accounts/abi/bind/template.go @@ -79,6 +79,13 @@ var tmplSource = map[Lang]string{ LangJava: tmplSourceJava, } +// SetTmplSource supports this func in order to set special template file. +func SetTmplSource(lang Lang, source string) { + if _, ok := tmplSource[lang]; ok { + tmplSource[lang] = source + } +} + // tmplSourceGo is the Go source template that the generated Go contract binding // is based on. const tmplSourceGo = ` diff --git a/cmd/abigen/main.go b/cmd/abigen/main.go index 0c6926f5fe..eb207098c9 100644 --- a/cmd/abigen/main.go +++ b/cmd/abigen/main.go @@ -103,6 +103,10 @@ var ( Name: "contract", Usage: "Name of the contract to generate the bindings for", } + tmplFlag = cli.StringFlag{ + Name: "tmpl", + Usage: "Template file if a user wants to customize", + } ) func init() { @@ -122,6 +126,7 @@ func init() { langFlag, aliasFlag, contractFlag, + tmplFlag, } app.Action = utils.MigrateFlags(abigen) cli.CommandHelpTemplate = flags.OriginCommandHelpTemplate @@ -265,6 +270,15 @@ func abigen(c *cli.Context) error { aliases[match[1]] = match[2] } } + // Set customize template file. + if c.GlobalIsSet(tmplFlag.Name) { + tmplFile := c.GlobalString(tmplFlag.Name) + data, err := os.ReadFile(tmplFile) + if err != nil { + utils.Fatalf("Failed to read template file: %v", err) + } + bind.SetTmplSource(lang, string(data)) + } // Generate the contract binding code, err := bind.Bind(types, abis, bins, sigs, c.GlobalString(pkgFlag.Name), lang, libs, aliases) if err != nil {