2019-07-22 09:17:27 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2016-04-14 16:18:24 +00:00
|
|
|
// This file is part of go-ethereum.
|
2016-03-16 10:48:33 +00:00
|
|
|
//
|
2016-04-14 16:18:24 +00: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 10:48:33 +00:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2016-04-14 16:18:24 +00:00
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
2016-03-16 10:48:33 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-04-14 16:18:24 +00:00
|
|
|
// GNU General Public License for more details.
|
2016-03-16 10:48:33 +00:00
|
|
|
//
|
2016-04-14 16:18:24 +00: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 10:48:33 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-03-21 12:34:49 +00:00
|
|
|
"encoding/json"
|
2016-03-16 10:48:33 +00:00
|
|
|
"fmt"
|
2022-04-23 14:43:00 +00:00
|
|
|
"io"
|
2016-03-16 10:48:33 +00:00
|
|
|
"os"
|
2019-12-16 12:37:15 +00:00
|
|
|
"path/filepath"
|
2019-11-14 07:26:10 +00:00
|
|
|
"regexp"
|
2016-03-26 09:43:09 +00:00
|
|
|
"strings"
|
2016-03-16 10:48:33 +00:00
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/accounts/abi"
|
|
|
|
"github.com/ledgerwatch/erigon/accounts/abi/bind"
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/utils"
|
|
|
|
"github.com/ledgerwatch/erigon/common/compiler"
|
|
|
|
"github.com/ledgerwatch/erigon/crypto"
|
|
|
|
"github.com/ledgerwatch/erigon/internal/flags"
|
2021-06-11 08:34:37 +00:00
|
|
|
"github.com/ledgerwatch/erigon/params"
|
2021-07-29 10:23:23 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2020-08-19 11:46:20 +00:00
|
|
|
"github.com/urfave/cli"
|
2016-03-16 10:48:33 +00:00
|
|
|
)
|
|
|
|
|
2019-07-08 12:59:07 +00:00
|
|
|
var (
|
|
|
|
app *cli.App
|
2019-03-18 12:29:26 +00:00
|
|
|
|
2019-07-08 12:59:07 +00:00
|
|
|
// Flags needed by abigen
|
|
|
|
abiFlag = cli.StringFlag{
|
|
|
|
Name: "abi",
|
|
|
|
Usage: "Path to the Ethereum contract ABI json to bind, - for STDIN",
|
|
|
|
}
|
|
|
|
binFlag = cli.StringFlag{
|
|
|
|
Name: "bin",
|
|
|
|
Usage: "Path to the Ethereum contract bytecode (generate deploy method)",
|
|
|
|
}
|
|
|
|
typeFlag = cli.StringFlag{
|
|
|
|
Name: "type",
|
|
|
|
Usage: "Struct name for the binding (default = package name)",
|
|
|
|
}
|
|
|
|
jsonFlag = cli.StringFlag{
|
|
|
|
Name: "combined-json",
|
|
|
|
Usage: "Path to the combined-json file generated by compiler",
|
|
|
|
}
|
|
|
|
solFlag = cli.StringFlag{
|
|
|
|
Name: "sol",
|
|
|
|
Usage: "Path to the Ethereum contract Solidity source to build and bind",
|
|
|
|
}
|
|
|
|
solcFlag = cli.StringFlag{
|
|
|
|
Name: "solc",
|
|
|
|
Usage: "Solidity compiler to use if source builds are requested",
|
|
|
|
Value: "solc",
|
|
|
|
}
|
|
|
|
vyFlag = cli.StringFlag{
|
|
|
|
Name: "vy",
|
|
|
|
Usage: "Path to the Ethereum contract Vyper source to build and bind",
|
|
|
|
}
|
|
|
|
vyperFlag = cli.StringFlag{
|
|
|
|
Name: "vyper",
|
|
|
|
Usage: "Vyper compiler to use if source builds are requested",
|
|
|
|
Value: "vyper",
|
|
|
|
}
|
|
|
|
excFlag = cli.StringFlag{
|
|
|
|
Name: "exc",
|
|
|
|
Usage: "Comma separated types to exclude from binding",
|
|
|
|
}
|
|
|
|
pkgFlag = cli.StringFlag{
|
|
|
|
Name: "pkg",
|
|
|
|
Usage: "Package name to generate the binding into",
|
|
|
|
}
|
|
|
|
outFlag = cli.StringFlag{
|
|
|
|
Name: "out",
|
|
|
|
Usage: "Output file for the generated binding (default = stdout)",
|
|
|
|
}
|
|
|
|
langFlag = cli.StringFlag{
|
|
|
|
Name: "lang",
|
|
|
|
Usage: "Destination language for the bindings (go, java, objc)",
|
|
|
|
Value: "go",
|
|
|
|
}
|
2019-11-14 07:26:10 +00:00
|
|
|
aliasFlag = cli.StringFlag{
|
|
|
|
Name: "alias",
|
2020-12-12 16:36:32 +00:00
|
|
|
Usage: "Comma separated aliases for function and event renaming, e.g. original1=alias1, original2=alias2",
|
2019-11-14 07:26:10 +00:00
|
|
|
}
|
2016-03-16 10:48:33 +00:00
|
|
|
)
|
|
|
|
|
2019-07-08 12:59:07 +00:00
|
|
|
func init() {
|
2021-06-11 08:34:37 +00:00
|
|
|
app = flags.NewApp(params.GitCommit, "", "ethereum checkpoint helper tool")
|
2019-07-08 12:59:07 +00:00
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
abiFlag,
|
|
|
|
binFlag,
|
|
|
|
typeFlag,
|
|
|
|
jsonFlag,
|
|
|
|
solFlag,
|
|
|
|
solcFlag,
|
|
|
|
vyFlag,
|
|
|
|
vyperFlag,
|
|
|
|
excFlag,
|
|
|
|
pkgFlag,
|
|
|
|
outFlag,
|
|
|
|
langFlag,
|
2019-11-14 07:26:10 +00:00
|
|
|
aliasFlag,
|
2019-07-08 12:59:07 +00:00
|
|
|
}
|
2021-03-26 18:05:42 +00:00
|
|
|
app.Action = abigen
|
2019-07-08 12:59:07 +00:00
|
|
|
}
|
2016-03-16 10:48:33 +00:00
|
|
|
|
2019-07-08 12:59:07 +00:00
|
|
|
func abigen(c *cli.Context) error {
|
|
|
|
utils.CheckExclusive(c, abiFlag, jsonFlag, solFlag, vyFlag) // Only one source can be selected.
|
|
|
|
if c.GlobalString(pkgFlag.Name) == "" {
|
|
|
|
utils.Fatalf("No destination package specified (--pkg)")
|
2016-09-05 16:07:57 +00:00
|
|
|
}
|
|
|
|
var lang bind.Lang
|
2019-07-08 12:59:07 +00:00
|
|
|
switch c.GlobalString(langFlag.Name) {
|
2016-09-05 16:07:57 +00:00
|
|
|
case "go":
|
|
|
|
lang = bind.LangGo
|
|
|
|
case "java":
|
|
|
|
lang = bind.LangJava
|
2019-07-03 10:17:43 +00:00
|
|
|
case "objc":
|
|
|
|
lang = bind.LangObjC
|
2019-07-08 12:59:07 +00:00
|
|
|
utils.Fatalf("Objc binding generation is uncompleted")
|
2016-09-05 16:07:57 +00:00
|
|
|
default:
|
2019-07-08 12:59:07 +00:00
|
|
|
utils.Fatalf("Unsupported destination language \"%s\" (--lang)", c.GlobalString(langFlag.Name))
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
2016-03-21 12:34:49 +00:00
|
|
|
// If the entire solidity code was specified, build and bind based on that
|
|
|
|
var (
|
2019-11-14 07:26:10 +00:00
|
|
|
abis []string
|
|
|
|
bins []string
|
|
|
|
types []string
|
|
|
|
sigs []map[string]string
|
|
|
|
libs = make(map[string]string)
|
|
|
|
aliases = make(map[string]string)
|
2016-03-21 12:34:49 +00:00
|
|
|
)
|
2019-07-08 12:59:07 +00:00
|
|
|
if c.GlobalString(abiFlag.Name) != "" {
|
|
|
|
// Load up the ABI, optional bytecode and type name from the parameters
|
|
|
|
var (
|
2022-08-04 13:23:00 +00:00
|
|
|
abiBytes []byte
|
|
|
|
err error
|
2019-07-08 12:59:07 +00:00
|
|
|
)
|
|
|
|
input := c.GlobalString(abiFlag.Name)
|
|
|
|
if input == "-" {
|
2022-08-04 13:23:00 +00:00
|
|
|
abiBytes, err = io.ReadAll(os.Stdin)
|
2019-07-08 12:59:07 +00:00
|
|
|
} else {
|
2022-08-04 13:23:00 +00:00
|
|
|
abiBytes, err = os.ReadFile(input)
|
2019-07-08 12:59:07 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Failed to read input ABI: %v", err)
|
|
|
|
}
|
2022-08-04 13:23:00 +00:00
|
|
|
abis = append(abis, string(abiBytes))
|
2019-07-08 12:59:07 +00:00
|
|
|
|
|
|
|
var bin []byte
|
|
|
|
if binFile := c.GlobalString(binFlag.Name); binFile != "" {
|
2022-04-23 14:43:00 +00:00
|
|
|
if bin, err = os.ReadFile(binFile); err != nil {
|
2019-07-08 12:59:07 +00: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))
|
|
|
|
|
|
|
|
kind := c.GlobalString(typeFlag.Name)
|
|
|
|
if kind == "" {
|
|
|
|
kind = c.GlobalString(pkgFlag.Name)
|
|
|
|
}
|
|
|
|
types = append(types, kind)
|
|
|
|
} else {
|
2016-03-26 09:43:09 +00:00
|
|
|
// Generate the list of types to exclude from binding
|
|
|
|
exclude := make(map[string]bool)
|
2019-07-08 12:59:07 +00:00
|
|
|
for _, kind := range strings.Split(c.GlobalString(excFlag.Name), ",") {
|
2016-03-26 09:43:09 +00:00
|
|
|
exclude[strings.ToLower(kind)] = true
|
|
|
|
}
|
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 10:22:02 +00:00
|
|
|
var err error
|
2019-07-08 12:59:07 +00:00
|
|
|
var contracts map[string]*compiler.Contract
|
2019-03-18 12:29:26 +00:00
|
|
|
|
|
|
|
switch {
|
2019-07-08 12:59:07 +00:00
|
|
|
case c.GlobalIsSet(solFlag.Name):
|
|
|
|
contracts, err = compiler.CompileSolidity(c.GlobalString(solcFlag.Name), c.GlobalString(solFlag.Name))
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Failed to build Solidity contract: %v", err)
|
|
|
|
}
|
|
|
|
case c.GlobalIsSet(vyFlag.Name):
|
2019-12-16 12:37:15 +00:00
|
|
|
output, err := compiler.CompileVyper(c.GlobalString(vyperFlag.Name), c.GlobalString(vyFlag.Name))
|
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 10:22:02 +00:00
|
|
|
if err != nil {
|
2019-07-08 12:59:07 +00:00
|
|
|
utils.Fatalf("Failed to build Vyper contract: %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 10:22:02 +00:00
|
|
|
}
|
2019-12-16 12:37:15 +00:00
|
|
|
contracts = make(map[string]*compiler.Contract)
|
|
|
|
for n, contract := range output {
|
|
|
|
name := n
|
|
|
|
// Sanitize the combined json names to match the
|
|
|
|
// format expected by solidity.
|
|
|
|
if !strings.Contains(n, ":") {
|
|
|
|
// Remove extra path components
|
|
|
|
name = abi.ToCamelCase(strings.TrimSuffix(filepath.Base(name), ".vy"))
|
|
|
|
}
|
|
|
|
contracts[name] = contract
|
|
|
|
}
|
|
|
|
|
2019-07-08 12:59:07 +00:00
|
|
|
case c.GlobalIsSet(jsonFlag.Name):
|
2022-04-23 14:43:00 +00:00
|
|
|
jsonOutput, err := os.ReadFile(c.GlobalString(jsonFlag.Name))
|
2019-03-18 12:29:26 +00:00
|
|
|
if err != nil {
|
2019-07-08 12:59:07 +00:00
|
|
|
utils.Fatalf("Failed to read combined-json from compiler: %v", err)
|
2019-03-18 12:29:26 +00:00
|
|
|
}
|
2019-07-08 12:59:07 +00: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 10:22:02 +00:00
|
|
|
if err != nil {
|
2019-07-08 12:59:07 +00: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 10:22:02 +00:00
|
|
|
}
|
2016-03-21 12:34:49 +00:00
|
|
|
}
|
2016-03-26 09:43:09 +00:00
|
|
|
// Gather all non-excluded contract for binding
|
2016-03-21 12:34:49 +00:00
|
|
|
for name, contract := range contracts {
|
2016-03-26 09:43:09 +00:00
|
|
|
if exclude[strings.ToLower(name)] {
|
|
|
|
continue
|
|
|
|
}
|
2019-03-18 12:29:26 +00:00
|
|
|
abi, err := json.Marshal(contract.Info.AbiDefinition) // Flatten the compiler parse
|
|
|
|
if err != nil {
|
2019-07-08 12:59:07 +00:00
|
|
|
utils.Fatalf("Failed to parse ABIs from compiler output: %v", err)
|
2019-03-18 12:29:26 +00:00
|
|
|
}
|
2016-03-21 12:34:49 +00:00
|
|
|
abis = append(abis, string(abi))
|
|
|
|
bins = append(bins, contract.Code)
|
2019-07-02 07:52:58 +00:00
|
|
|
sigs = append(sigs, contract.Hashes)
|
2017-02-06 17:16:56 +00:00
|
|
|
nameParts := strings.Split(name, ":")
|
|
|
|
types = append(types, nameParts[len(nameParts)-1])
|
2019-07-08 08:27:05 +00:00
|
|
|
|
|
|
|
libPattern := crypto.Keccak256Hash([]byte(name)).String()[2:36]
|
|
|
|
libs[libPattern] = nameParts[len(nameParts)-1]
|
2016-03-21 12:34:49 +00:00
|
|
|
}
|
2016-03-17 17:27:37 +00:00
|
|
|
}
|
2019-11-14 07:26:10 +00:00
|
|
|
// Extract all aliases from the flags
|
|
|
|
if c.GlobalIsSet(aliasFlag.Name) {
|
|
|
|
// We support multi-versions for aliasing
|
|
|
|
// e.g.
|
|
|
|
// foo=bar,foo2=bar2
|
|
|
|
// foo:bar,foo2:bar2
|
|
|
|
re := regexp.MustCompile(`(?:(\w+)[:=](\w+))`)
|
|
|
|
submatches := re.FindAllStringSubmatch(c.GlobalString(aliasFlag.Name), -1)
|
|
|
|
for _, match := range submatches {
|
|
|
|
aliases[match[1]] = match[2]
|
|
|
|
}
|
|
|
|
}
|
2016-03-17 17:27:37 +00:00
|
|
|
// Generate the contract binding
|
2019-11-14 07:26:10 +00:00
|
|
|
code, err := bind.Bind(types, abis, bins, sigs, c.GlobalString(pkgFlag.Name), lang, libs, aliases)
|
2016-03-16 10:48:33 +00:00
|
|
|
if err != nil {
|
2019-07-08 12:59:07 +00:00
|
|
|
utils.Fatalf("Failed to generate ABI binding: %v", err)
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
|
|
|
// Either flush it out to a file or display on the standard output
|
2019-07-08 12:59:07 +00:00
|
|
|
if !c.GlobalIsSet(outFlag.Name) {
|
2016-03-16 10:48:33 +00:00
|
|
|
fmt.Printf("%s\n", code)
|
2019-07-08 12:59:07 +00:00
|
|
|
return nil
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
2022-04-23 14:43:00 +00:00
|
|
|
if err := os.WriteFile(c.GlobalString(outFlag.Name), []byte(code), 0600); err != nil {
|
2019-07-08 12:59:07 +00:00
|
|
|
utils.Fatalf("Failed to write ABI binding: %v", err)
|
2016-03-16 10:48:33 +00:00
|
|
|
}
|
2019-07-08 12:59:07 +00:00
|
|
|
return nil
|
2016-03-16 10:48:33 +00: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 10:22:02 +00:00
|
|
|
|
2019-07-08 12:59:07 +00:00
|
|
|
func main() {
|
2021-07-29 10:23:23 +00:00
|
|
|
log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StderrHandler))
|
2019-07-08 12:59:07 +00: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 10:22:02 +00:00
|
|
|
}
|
|
|
|
}
|