github.com/aaa256/atlantis@v0.0.0-20210707112435-42ee889287a2/cmd/abigen/main.go (about)

     1  // Copyright 2016 The go-athereum Authors
     2  // This file is part of go-athereum.
     3  //
     4  // go-athereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-athereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-athereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"encoding/json"
    21  	"flag"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"os"
    25  	"strings"
    26  
    27  	"github.com/athereum/go-athereum/accounts/abi/bind"
    28  	"github.com/athereum/go-athereum/common/compiler"
    29  )
    30  
    31  var (
    32  	abiFlag = flag.String("abi", "", "Path to the Atlantis contract ABI json to bind, - for STDIN")
    33  	binFlag = flag.String("bin", "", "Path to the Atlantis contract bytecode (generate deploy method)")
    34  	typFlag = flag.String("type", "", "Struct name for the binding (default = package name)")
    35  
    36  	solFlag  = flag.String("sol", "", "Path to the Atlantis contract Solidity source to build and bind")
    37  	solcFlag = flag.String("solc", "solc", "Solidity compiler to use if source builds are requested")
    38  	excFlag  = flag.String("exc", "", "Comma separated types to exclude from binding")
    39  
    40  	pkgFlag  = flag.String("pkg", "", "Package name to generate the binding into")
    41  	outFlag  = flag.String("out", "", "Output file for the generated binding (default = stdout)")
    42  	langFlag = flag.String("lang", "go", "Destination language for the bindings (go, java, objc)")
    43  )
    44  
    45  func main() {
    46  	// Parse and ensure all needed inputs are specified
    47  	flag.Parse()
    48  
    49  	if *abiFlag == "" && *solFlag == "" {
    50  		fmt.Printf("No contract ABI (--abi) or Solidity source (--sol) specified\n")
    51  		os.Exit(-1)
    52  	} else if (*abiFlag != "" || *binFlag != "" || *typFlag != "") && *solFlag != "" {
    53  		fmt.Printf("Contract ABI (--abi), bytecode (--bin) and type (--type) flags are mutually exclusive with the Solidity source (--sol) flag\n")
    54  		os.Exit(-1)
    55  	}
    56  	if *pkgFlag == "" {
    57  		fmt.Printf("No destination package specified (--pkg)\n")
    58  		os.Exit(-1)
    59  	}
    60  	var lang bind.Lang
    61  	switch *langFlag {
    62  	case "go":
    63  		lang = bind.LangGo
    64  	case "java":
    65  		lang = bind.LangJava
    66  	case "objc":
    67  		lang = bind.LangObjC
    68  	default:
    69  		fmt.Printf("Unsupported destination language \"%s\" (--lang)\n", *langFlag)
    70  		os.Exit(-1)
    71  	}
    72  	// If the entire solidity code was specified, build and bind based on that
    73  	var (
    74  		abis  []string
    75  		bins  []string
    76  		types []string
    77  	)
    78  	if *solFlag != "" || *abiFlag == "-" {
    79  		// Generate the list of types to exclude from binding
    80  		exclude := make(map[string]bool)
    81  		for _, kind := range strings.Split(*excFlag, ",") {
    82  			exclude[strings.ToLower(kind)] = true
    83  		}
    84  
    85  		var contracts map[string]*compiler.Contract
    86  		var err error
    87  		if *solFlag != "" {
    88  			contracts, err = compiler.CompileSolidity(*solcFlag, *solFlag)
    89  			if err != nil {
    90  				fmt.Printf("Failed to build Solidity contract: %v\n", err)
    91  				os.Exit(-1)
    92  			}
    93  		} else {
    94  			contracts, err = contractsFromStdin()
    95  			if err != nil {
    96  				fmt.Printf("Failed to read input ABIs from STDIN: %v\n", err)
    97  				os.Exit(-1)
    98  			}
    99  		}
   100  		// Gather all non-excluded contract for binding
   101  		for name, contract := range contracts {
   102  			if exclude[strings.ToLower(name)] {
   103  				continue
   104  			}
   105  			abi, _ := json.Marshal(contract.Info.AbiDefinition) // Flatten the compiler parse
   106  			abis = append(abis, string(abi))
   107  			bins = append(bins, contract.Code)
   108  
   109  			nameParts := strings.Split(name, ":")
   110  			types = append(types, nameParts[len(nameParts)-1])
   111  		}
   112  	} else {
   113  		// Otherwise load up the ABI, optional bytecode and type name from the parameters
   114  		abi, err := ioutil.ReadFile(*abiFlag)
   115  		if err != nil {
   116  			fmt.Printf("Failed to read input ABI: %v\n", err)
   117  			os.Exit(-1)
   118  		}
   119  		abis = append(abis, string(abi))
   120  
   121  		bin := []byte{}
   122  		if *binFlag != "" {
   123  			if bin, err = ioutil.ReadFile(*binFlag); err != nil {
   124  				fmt.Printf("Failed to read input bytecode: %v\n", err)
   125  				os.Exit(-1)
   126  			}
   127  		}
   128  		bins = append(bins, string(bin))
   129  
   130  		kind := *typFlag
   131  		if kind == "" {
   132  			kind = *pkgFlag
   133  		}
   134  		types = append(types, kind)
   135  	}
   136  	// Generate the contract binding
   137  	code, err := bind.Bind(types, abis, bins, *pkgFlag, lang)
   138  	if err != nil {
   139  		fmt.Printf("Failed to generate ABI binding: %v\n", err)
   140  		os.Exit(-1)
   141  	}
   142  	// Either flush it out to a file or display on the standard output
   143  	if *outFlag == "" {
   144  		fmt.Printf("%s\n", code)
   145  		return
   146  	}
   147  	if err := ioutil.WriteFile(*outFlag, []byte(code), 0600); err != nil {
   148  		fmt.Printf("Failed to write ABI binding: %v\n", err)
   149  		os.Exit(-1)
   150  	}
   151  }
   152  
   153  func contractsFromStdin() (map[string]*compiler.Contract, error) {
   154  	bytes, err := ioutil.ReadAll(os.Stdin)
   155  	if err != nil {
   156  		return nil, err
   157  	}
   158  
   159  	return compiler.ParseCombinedJSON(bytes, "", "", "", "")
   160  }