github.com/MaynardMiner/ethereumprogpow@v1.8.23/cmd/abigen/main.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum 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-ethereum 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-ethereum. 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/ethereumprogpow/ethereumprogpow/accounts/abi/bind"
    28  	"github.com/ethereumprogpow/ethereumprogpow/common/compiler"
    29  )
    30  
    31  var (
    32  	abiFlag = flag.String("abi", "", "Path to the Ethereum contract ABI json to bind, - for STDIN")
    33  	binFlag = flag.String("bin", "", "Path to the Ethereum 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 Ethereum 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 == "-" && *pkgFlag == "") {
    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  		var abi []byte
   115  		var err error
   116  		if *abiFlag == "-" {
   117  			abi, err = ioutil.ReadAll(os.Stdin)
   118  		} else {
   119  			abi, err = ioutil.ReadFile(*abiFlag)
   120  		}
   121  		if err != nil {
   122  			fmt.Printf("Failed to read input ABI: %v\n", err)
   123  			os.Exit(-1)
   124  		}
   125  		abis = append(abis, string(abi))
   126  
   127  		bin := []byte{}
   128  		if *binFlag != "" {
   129  			if bin, err = ioutil.ReadFile(*binFlag); err != nil {
   130  				fmt.Printf("Failed to read input bytecode: %v\n", err)
   131  				os.Exit(-1)
   132  			}
   133  		}
   134  		bins = append(bins, string(bin))
   135  
   136  		kind := *typFlag
   137  		if kind == "" {
   138  			kind = *pkgFlag
   139  		}
   140  		types = append(types, kind)
   141  	}
   142  	// Generate the contract binding
   143  	code, err := bind.Bind(types, abis, bins, *pkgFlag, lang)
   144  	if err != nil {
   145  		fmt.Printf("Failed to generate ABI binding: %v\n", err)
   146  		os.Exit(-1)
   147  	}
   148  	// Either flush it out to a file or display on the standard output
   149  	if *outFlag == "" {
   150  		fmt.Printf("%s\n", code)
   151  		return
   152  	}
   153  	if err := ioutil.WriteFile(*outFlag, []byte(code), 0600); err != nil {
   154  		fmt.Printf("Failed to write ABI binding: %v\n", err)
   155  		os.Exit(-1)
   156  	}
   157  }
   158  
   159  func contractsFromStdin() (map[string]*compiler.Contract, error) {
   160  	bytes, err := ioutil.ReadAll(os.Stdin)
   161  	if err != nil {
   162  		return nil, err
   163  	}
   164  	return compiler.ParseCombinedJSON(bytes, "", "", "", "")
   165  }