github.com/niluplatform/go-nilu@v1.7.4-0.20200912082737-a0cb0776d52c/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/NiluPlatform/go-nilu/accounts/abi/bind"
    28  	"github.com/NiluPlatform/go-nilu/common/compiler"
    29  )
    30  
    31  var (
    32  	abiFlag = flag.String("abi", "", "Path to the Ethereum contract ABI json to bind")
    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 != "" {
    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  		contracts, err := compiler.CompileSolidity(*solcFlag, *solFlag)
    85  		if err != nil {
    86  			fmt.Printf("Failed to build Solidity contract: %v\n", err)
    87  			os.Exit(-1)
    88  		}
    89  		// Gather all non-excluded contract for binding
    90  		for name, contract := range contracts {
    91  			if exclude[strings.ToLower(name)] {
    92  				continue
    93  			}
    94  			abi, _ := json.Marshal(contract.Info.AbiDefinition) // Flatten the compiler parse
    95  			abis = append(abis, string(abi))
    96  			bins = append(bins, contract.Code)
    97  
    98  			nameParts := strings.Split(name, ":")
    99  			types = append(types, nameParts[len(nameParts)-1])
   100  		}
   101  	} else {
   102  		// Otherwise load up the ABI, optional bytecode and type name from the parameters
   103  		abi, err := ioutil.ReadFile(*abiFlag)
   104  		if err != nil {
   105  			fmt.Printf("Failed to read input ABI: %v\n", err)
   106  			os.Exit(-1)
   107  		}
   108  		abis = append(abis, string(abi))
   109  
   110  		bin := []byte{}
   111  		if *binFlag != "" {
   112  			if bin, err = ioutil.ReadFile(*binFlag); err != nil {
   113  				fmt.Printf("Failed to read input bytecode: %v\n", err)
   114  				os.Exit(-1)
   115  			}
   116  		}
   117  		bins = append(bins, string(bin))
   118  
   119  		kind := *typFlag
   120  		if kind == "" {
   121  			kind = *pkgFlag
   122  		}
   123  		types = append(types, kind)
   124  	}
   125  	// Generate the contract binding
   126  	code, err := bind.Bind(types, abis, bins, *pkgFlag, lang)
   127  	if err != nil {
   128  		fmt.Printf("Failed to generate ABI binding: %v\n", err)
   129  		os.Exit(-1)
   130  	}
   131  	// Either flush it out to a file or display on the standard output
   132  	if *outFlag == "" {
   133  		fmt.Printf("%s\n", code)
   134  		return
   135  	}
   136  	if err := ioutil.WriteFile(*outFlag, []byte(code), 0600); err != nil {
   137  		fmt.Printf("Failed to write ABI binding: %v\n", err)
   138  		os.Exit(-1)
   139  	}
   140  }