2019-07-22 04:17:27 -05:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2016-04-14 11:18:24 -05:00
|
|
|
// This file is part of go-ethereum.
|
2016-03-16 05:48:33 -05:00
|
|
|
//
|
2016-04-14 11:18:24 -05:00
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2016-03-16 05:48:33 -05:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2016-04-14 11:18:24 -05:00
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
2016-03-16 05:48:33 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-04-14 11:18:24 -05:00
|
|
|
// GNU General Public License for more details.
|
2016-03-16 05:48:33 -05:00
|
|
|
//
|
2016-04-14 11:18:24 -05:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-03-16 05:48:33 -05:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-03-21 07:34:49 -05:00
|
|
|
"encoding/json"
|
2016-03-16 05:48:33 -05:00
|
|
|
"fmt"
|
2022-05-16 04:59:35 -05:00
|
|
|
"io"
|
2016-03-16 05:48:33 -05:00
|
|
|
"os"
|
2019-11-14 01:26:10 -06:00
|
|
|
"regexp"
|
2016-03-26 04:43:09 -05:00
|
|
|
"strings"
|
2016-03-16 05:48:33 -05:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
2019-07-08 07:59:07 -05:00
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
2016-03-21 07:34:49 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common/compiler"
|
2019-07-08 03:27:05 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-07-14 03:35:32 -05:00
|
|
|
"github.com/ethereum/go-ethereum/internal/flags"
|
2019-07-08 07:59:07 -05:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2022-06-27 11:22:36 -05:00
|
|
|
"github.com/urfave/cli/v2"
|
2016-03-16 05:48:33 -05:00
|
|
|
)
|
|
|
|
|
2022-06-27 11:22:36 -05:00
|
|
|
var (
|
2019-07-08 07:59:07 -05:00
|
|
|
// Flags needed by abigen
|
2022-06-27 11:22:36 -05:00
|
|
|
abiFlag = &cli.StringFlag{
|
2019-07-08 07:59:07 -05:00
|
|
|
Name: "abi",
|
|
|
|
Usage: "Path to the Ethereum contract ABI json to bind, - for STDIN",
|
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
binFlag = &cli.StringFlag{
|
2019-07-08 07:59:07 -05:00
|
|
|
Name: "bin",
|
|
|
|
Usage: "Path to the Ethereum contract bytecode (generate deploy method)",
|
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
typeFlag = &cli.StringFlag{
|
2019-07-08 07:59:07 -05:00
|
|
|
Name: "type",
|
|
|
|
Usage: "Struct name for the binding (default = package name)",
|
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
jsonFlag = &cli.StringFlag{
|
2019-07-08 07:59:07 -05:00
|
|
|
Name: "combined-json",
|
2022-05-26 02:26:37 -05:00
|
|
|
Usage: "Path to the combined-json file generated by compiler, - for STDIN",
|
2019-07-08 07:59:07 -05:00
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
excFlag = &cli.StringFlag{
|
2019-07-08 07:59:07 -05:00
|
|
|
Name: "exc",
|
|
|
|
Usage: "Comma separated types to exclude from binding",
|
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
pkgFlag = &cli.StringFlag{
|
2019-07-08 07:59:07 -05:00
|
|
|
Name: "pkg",
|
|
|
|
Usage: "Package name to generate the binding into",
|
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
outFlag = &cli.StringFlag{
|
2019-07-08 07:59:07 -05:00
|
|
|
Name: "out",
|
|
|
|
Usage: "Output file for the generated binding (default = stdout)",
|
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
langFlag = &cli.StringFlag{
|
2019-07-08 07:59:07 -05:00
|
|
|
Name: "lang",
|
2023-02-03 04:26:29 -06:00
|
|
|
Usage: "Destination language for the bindings (go)",
|
2019-07-08 07:59:07 -05:00
|
|
|
Value: "go",
|
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
aliasFlag = &cli.StringFlag{
|
2019-11-14 01:26:10 -06:00
|
|
|
Name: "alias",
|
2020-12-12 10:36:32 -06:00
|
|
|
Usage: "Comma separated aliases for function and event renaming, e.g. original1=alias1, original2=alias2",
|
2019-11-14 01:26:10 -06:00
|
|
|
}
|
2016-03-16 05:48:33 -05:00
|
|
|
)
|
|
|
|
|
2022-09-23 07:08:25 -05:00
|
|
|
var app = flags.NewApp("Ethereum ABI wrapper code generator")
|
|
|
|
|
2019-07-08 07:59:07 -05:00
|
|
|
func init() {
|
2022-06-27 11:22:36 -05:00
|
|
|
app.Name = "abigen"
|
2019-07-08 07:59:07 -05:00
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
abiFlag,
|
|
|
|
binFlag,
|
|
|
|
typeFlag,
|
|
|
|
jsonFlag,
|
|
|
|
excFlag,
|
|
|
|
pkgFlag,
|
|
|
|
outFlag,
|
|
|
|
langFlag,
|
2019-11-14 01:26:10 -06:00
|
|
|
aliasFlag,
|
2019-07-08 07:59:07 -05:00
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
app.Action = abigen
|
2019-07-08 07:59:07 -05:00
|
|
|
}
|
2016-03-16 05:48:33 -05:00
|
|
|
|
2019-07-08 07:59:07 -05:00
|
|
|
func abigen(c *cli.Context) error {
|
2022-05-23 07:29:23 -05:00
|
|
|
utils.CheckExclusive(c, abiFlag, jsonFlag) // Only one source can be selected.
|
2022-06-27 11:22:36 -05:00
|
|
|
|
|
|
|
if c.String(pkgFlag.Name) == "" {
|
2019-07-08 07:59:07 -05:00
|
|
|
utils.Fatalf("No destination package specified (--pkg)")
|
2016-09-05 11:07:57 -05:00
|
|
|
}
|
|
|
|
var lang bind.Lang
|
2022-06-27 11:22:36 -05:00
|
|
|
switch c.String(langFlag.Name) {
|
2016-09-05 11:07:57 -05:00
|
|
|
case "go":
|
|
|
|
lang = bind.LangGo
|
|
|
|
default:
|
2022-06-27 11:22:36 -05:00
|
|
|
utils.Fatalf("Unsupported destination language \"%s\" (--lang)", c.String(langFlag.Name))
|
2016-03-16 05:48:33 -05:00
|
|
|
}
|
2016-03-21 07:34:49 -05:00
|
|
|
// If the entire solidity code was specified, build and bind based on that
|
|
|
|
var (
|
2019-11-14 01:26:10 -06:00
|
|
|
abis []string
|
|
|
|
bins []string
|
|
|
|
types []string
|
|
|
|
sigs []map[string]string
|
|
|
|
libs = make(map[string]string)
|
|
|
|
aliases = make(map[string]string)
|
2016-03-21 07:34:49 -05:00
|
|
|
)
|
2022-06-27 11:22:36 -05:00
|
|
|
if c.String(abiFlag.Name) != "" {
|
2019-07-08 07:59:07 -05:00
|
|
|
// Load up the ABI, optional bytecode and type name from the parameters
|
|
|
|
var (
|
|
|
|
abi []byte
|
|
|
|
err error
|
|
|
|
)
|
2022-06-27 11:22:36 -05:00
|
|
|
input := c.String(abiFlag.Name)
|
2019-07-08 07:59:07 -05:00
|
|
|
if input == "-" {
|
2022-05-16 04:59:35 -05:00
|
|
|
abi, err = io.ReadAll(os.Stdin)
|
2019-07-08 07:59:07 -05:00
|
|
|
} else {
|
2022-05-16 04:59:35 -05:00
|
|
|
abi, err = os.ReadFile(input)
|
2019-07-08 07:59:07 -05:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Failed to read input ABI: %v", err)
|
|
|
|
}
|
|
|
|
abis = append(abis, string(abi))
|
|
|
|
|
|
|
|
var bin []byte
|
2022-06-27 11:22:36 -05:00
|
|
|
if binFile := c.String(binFlag.Name); binFile != "" {
|
2022-05-16 04:59:35 -05:00
|
|
|
if bin, err = os.ReadFile(binFile); err != nil {
|
2019-07-08 07:59:07 -05:00
|
|
|
utils.Fatalf("Failed to read input bytecode: %v", err)
|
|
|
|
}
|
|
|
|
if strings.Contains(string(bin), "//") {
|
|
|
|
utils.Fatalf("Contract has additional library references, please use other mode(e.g. --combined-json) to catch library infos")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bins = append(bins, string(bin))
|
|
|
|
|
2022-06-27 11:22:36 -05:00
|
|
|
kind := c.String(typeFlag.Name)
|
2019-07-08 07:59:07 -05:00
|
|
|
if kind == "" {
|
2022-06-27 11:22:36 -05:00
|
|
|
kind = c.String(pkgFlag.Name)
|
2019-07-08 07:59:07 -05:00
|
|
|
}
|
|
|
|
types = append(types, kind)
|
|
|
|
} else {
|
2016-03-26 04:43:09 -05:00
|
|
|
// Generate the list of types to exclude from binding
|
2022-09-23 12:04:02 -05:00
|
|
|
var exclude *nameFilter
|
|
|
|
if c.IsSet(excFlag.Name) {
|
|
|
|
var err error
|
|
|
|
if exclude, err = newNameFilter(strings.Split(c.String(excFlag.Name), ",")...); err != nil {
|
|
|
|
utils.Fatalf("Failed to parse excludes: %v", err)
|
|
|
|
}
|
2016-03-26 04:43:09 -05:00
|
|
|
}
|
2019-07-08 07:59:07 -05:00
|
|
|
var contracts map[string]*compiler.Contract
|
2019-03-18 07:29:26 -05:00
|
|
|
|
2022-06-27 11:22:36 -05:00
|
|
|
if c.IsSet(jsonFlag.Name) {
|
2022-05-26 02:26:37 -05:00
|
|
|
var (
|
2022-06-27 11:22:36 -05:00
|
|
|
input = c.String(jsonFlag.Name)
|
2022-05-26 02:26:37 -05:00
|
|
|
jsonOutput []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if input == "-" {
|
|
|
|
jsonOutput, err = io.ReadAll(os.Stdin)
|
|
|
|
} else {
|
|
|
|
jsonOutput, err = os.ReadFile(input)
|
|
|
|
}
|
2019-03-18 07:29:26 -05:00
|
|
|
if err != nil {
|
2022-05-26 02:26:37 -05:00
|
|
|
utils.Fatalf("Failed to read combined-json: %v", err)
|
2019-03-18 07:29:26 -05:00
|
|
|
}
|
2019-07-08 07:59:07 -05:00
|
|
|
contracts, err = compiler.ParseCombinedJSON(jsonOutput, "", "", "", "")
|
cmd/abigen: support for reading solc output from stdin (#16683)
Allow the --abi flag to be given - to indicate that it should read the
ABI information from standard input. It expects to read the solc output
with the --combined-json flag providing bin, abi, userdoc, devdoc, and
metadata, and works very similarly to the internal invocation of solc,
except it allows external invocation of solc.
This facilitates integration with more complex solc invocations, such
as invocations that require path remapping or --allow-paths tweaks.
Simple usage example:
solc --combined-json bin,abi,userdoc,devdoc,metadata *.sol | abigen --abi -
2018-06-05 05:22:02 -05:00
|
|
|
if err != nil {
|
2019-07-08 07:59:07 -05:00
|
|
|
utils.Fatalf("Failed to read contract information from json output: %v", err)
|
cmd/abigen: support for reading solc output from stdin (#16683)
Allow the --abi flag to be given - to indicate that it should read the
ABI information from standard input. It expects to read the solc output
with the --combined-json flag providing bin, abi, userdoc, devdoc, and
metadata, and works very similarly to the internal invocation of solc,
except it allows external invocation of solc.
This facilitates integration with more complex solc invocations, such
as invocations that require path remapping or --allow-paths tweaks.
Simple usage example:
solc --combined-json bin,abi,userdoc,devdoc,metadata *.sol | abigen --abi -
2018-06-05 05:22:02 -05:00
|
|
|
}
|
2016-03-21 07:34:49 -05:00
|
|
|
}
|
2016-03-26 04:43:09 -05:00
|
|
|
// Gather all non-excluded contract for binding
|
2016-03-21 07:34:49 -05:00
|
|
|
for name, contract := range contracts {
|
2022-09-23 12:04:02 -05:00
|
|
|
// fully qualified name is of the form <solFilePath>:<type>
|
|
|
|
nameParts := strings.Split(name, ":")
|
|
|
|
typeName := nameParts[len(nameParts)-1]
|
|
|
|
if exclude != nil && exclude.Matches(name) {
|
|
|
|
fmt.Fprintf(os.Stderr, "excluding: %v\n", name)
|
2016-03-26 04:43:09 -05:00
|
|
|
continue
|
|
|
|
}
|
2019-03-18 07:29:26 -05:00
|
|
|
abi, err := json.Marshal(contract.Info.AbiDefinition) // Flatten the compiler parse
|
|
|
|
if err != nil {
|
2019-07-08 07:59:07 -05:00
|
|
|
utils.Fatalf("Failed to parse ABIs from compiler output: %v", err)
|
2019-03-18 07:29:26 -05:00
|
|
|
}
|
2016-03-21 07:34:49 -05:00
|
|
|
abis = append(abis, string(abi))
|
|
|
|
bins = append(bins, contract.Code)
|
2019-07-02 02:52:58 -05:00
|
|
|
sigs = append(sigs, contract.Hashes)
|
2022-09-23 12:04:02 -05:00
|
|
|
types = append(types, typeName)
|
2019-07-08 03:27:05 -05:00
|
|
|
|
2022-06-07 01:38:54 -05:00
|
|
|
// Derive the library placeholder which is a 34 character prefix of the
|
|
|
|
// hex encoding of the keccak256 hash of the fully qualified library name.
|
|
|
|
// Note that the fully qualified library name is the path of its source
|
|
|
|
// file and the library name separated by ":".
|
|
|
|
libPattern := crypto.Keccak256Hash([]byte(name)).String()[2:36] // the first 2 chars are 0x
|
2022-09-23 12:04:02 -05:00
|
|
|
libs[libPattern] = typeName
|
2016-03-21 07:34:49 -05:00
|
|
|
}
|
2016-03-17 12:27:37 -05:00
|
|
|
}
|
2019-11-14 01:26:10 -06:00
|
|
|
// Extract all aliases from the flags
|
2022-06-27 11:22:36 -05:00
|
|
|
if c.IsSet(aliasFlag.Name) {
|
2019-11-14 01:26:10 -06:00
|
|
|
// We support multi-versions for aliasing
|
|
|
|
// e.g.
|
|
|
|
// foo=bar,foo2=bar2
|
|
|
|
// foo:bar,foo2:bar2
|
|
|
|
re := regexp.MustCompile(`(?:(\w+)[:=](\w+))`)
|
2022-06-27 11:22:36 -05:00
|
|
|
submatches := re.FindAllStringSubmatch(c.String(aliasFlag.Name), -1)
|
2019-11-14 01:26:10 -06:00
|
|
|
for _, match := range submatches {
|
|
|
|
aliases[match[1]] = match[2]
|
|
|
|
}
|
|
|
|
}
|
2016-03-17 12:27:37 -05:00
|
|
|
// Generate the contract binding
|
2022-06-27 11:22:36 -05:00
|
|
|
code, err := bind.Bind(types, abis, bins, sigs, c.String(pkgFlag.Name), lang, libs, aliases)
|
2016-03-16 05:48:33 -05:00
|
|
|
if err != nil {
|
2019-07-08 07:59:07 -05:00
|
|
|
utils.Fatalf("Failed to generate ABI binding: %v", err)
|
2016-03-16 05:48:33 -05:00
|
|
|
}
|
|
|
|
// Either flush it out to a file or display on the standard output
|
2022-06-27 11:22:36 -05:00
|
|
|
if !c.IsSet(outFlag.Name) {
|
2016-03-16 05:48:33 -05:00
|
|
|
fmt.Printf("%s\n", code)
|
2019-07-08 07:59:07 -05:00
|
|
|
return nil
|
2016-03-16 05:48:33 -05:00
|
|
|
}
|
2022-06-27 11:22:36 -05:00
|
|
|
if err := os.WriteFile(c.String(outFlag.Name), []byte(code), 0600); err != nil {
|
2019-07-08 07:59:07 -05:00
|
|
|
utils.Fatalf("Failed to write ABI binding: %v", err)
|
2016-03-16 05:48:33 -05:00
|
|
|
}
|
2019-07-08 07:59:07 -05:00
|
|
|
return nil
|
2016-03-16 05:48:33 -05:00
|
|
|
}
|
cmd/abigen: support for reading solc output from stdin (#16683)
Allow the --abi flag to be given - to indicate that it should read the
ABI information from standard input. It expects to read the solc output
with the --combined-json flag providing bin, abi, userdoc, devdoc, and
metadata, and works very similarly to the internal invocation of solc,
except it allows external invocation of solc.
This facilitates integration with more complex solc invocations, such
as invocations that require path remapping or --allow-paths tweaks.
Simple usage example:
solc --combined-json bin,abi,userdoc,devdoc,metadata *.sol | abigen --abi -
2018-06-05 05:22:02 -05:00
|
|
|
|
2019-07-08 07:59:07 -05:00
|
|
|
func main() {
|
2023-11-29 01:33:50 -06:00
|
|
|
log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelInfo, true)))
|
2019-07-08 07:59:07 -05:00
|
|
|
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
cmd/abigen: support for reading solc output from stdin (#16683)
Allow the --abi flag to be given - to indicate that it should read the
ABI information from standard input. It expects to read the solc output
with the --combined-json flag providing bin, abi, userdoc, devdoc, and
metadata, and works very similarly to the internal invocation of solc,
except it allows external invocation of solc.
This facilitates integration with more complex solc invocations, such
as invocations that require path remapping or --allow-paths tweaks.
Simple usage example:
solc --combined-json bin,abi,userdoc,devdoc,metadata *.sol | abigen --abi -
2018-06-05 05:22:02 -05:00
|
|
|
}
|
|
|
|
}
|