cmd/abigen: added vyper support in abigen

This commit is contained in:
Kushagra Sharma 2019-02-18 20:16:16 +00:00
parent 49ff08be6d
commit 192c5bfd61

View file

@ -17,149 +17,161 @@
package main package main
import ( import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
"github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common/compiler" "github.com/ethereum/go-ethereum/common/compiler"
) )
var ( var (
abiFlag = flag.String("abi", "", "Path to the Ethereum contract ABI json to bind, - for STDIN") abiFlag = flag.String("abi", "", "Path to the Ethereum contract ABI json to bind, - for STDIN")
binFlag = flag.String("bin", "", "Path to the Ethereum contract bytecode (generate deploy method)") binFlag = flag.String("bin", "", "Path to the Ethereum contract bytecode (generate deploy method)")
typFlag = flag.String("type", "", "Struct name for the binding (default = package name)") typFlag = flag.String("type", "", "Struct name for the binding (default = package name)")
solFlag = flag.String("sol", "", "Path to the Ethereum contract Solidity source to build and bind") solFlag = flag.String("sol", "", "Path to the Ethereum contract Solidity source to build and bind")
solcFlag = flag.String("solc", "solc", "Solidity compiler to use if source builds are requested") solcFlag = flag.String("solc", "solc", "Solidity compiler to use if source builds are requested")
excFlag = flag.String("exc", "", "Comma separated types to exclude from binding") excFlag = flag.String("exc", "", "Comma separated types to exclude from binding")
pkgFlag = flag.String("pkg", "", "Package name to generate the binding into") vyFlag = flag.String("vy", "", "Path to the Ethereum contract Vyper source to build and bind")
outFlag = flag.String("out", "", "Output file for the generated binding (default = stdout)") vyperFlag = flag.String("vyper", "vyper", "Vyper compiler to use if source builds are requested")
langFlag = flag.String("lang", "go", "Destination language for the bindings (go, java, objc)")
pkgFlag = flag.String("pkg", "", "Package name to generate the binding into")
outFlag = flag.String("out", "", "Output file for the generated binding (default = stdout)")
langFlag = flag.String("lang", "go", "Destination language for the bindings (go, java, objc)")
) )
func main() { func main() {
// Parse and ensure all needed inputs are specified // Parse and ensure all needed inputs are specified
flag.Parse() flag.Parse()
if *abiFlag == "" && *solFlag == "" { if *abiFlag == "" && *solFlag == "" && *vyFlag == "" {
fmt.Printf("No contract ABI (--abi) or Solidity source (--sol) specified\n") fmt.Printf("No contract ABI (--abi), Solidity source (--sol), or Vyper source (--vy) specified\n")
os.Exit(-1) os.Exit(-1)
} else if (*abiFlag != "" || *binFlag != "" || *typFlag != "") && *solFlag != "" { } else if (*abiFlag != "" || *binFlag != "" || *typFlag != "") && (*solFlag != "" || *vyFlag != "") {
fmt.Printf("Contract ABI (--abi), bytecode (--bin) and type (--type) flags are mutually exclusive with the Solidity source (--sol) flag\n") fmt.Printf("Contract ABI (--abi), bytecode (--bin) and type (--type) flags are mutually exclusive with the Solidity (--sol) and Vyper (--vy) flags\n")
os.Exit(-1) os.Exit(-1)
} } else if *solFlag != "" && *vyFlag != "" {
if *pkgFlag == "" { fmt.Printf("Solidity (--sol) and Vyper (--vy) flags are mutually exclusive\n")
fmt.Printf("No destination package specified (--pkg)\n") os.Exit(-1)
os.Exit(-1) }
} if *pkgFlag == "" {
var lang bind.Lang fmt.Printf("No destination package specified (--pkg)\n")
switch *langFlag { os.Exit(-1)
case "go": }
lang = bind.LangGo var lang bind.Lang
case "java": switch *langFlag {
lang = bind.LangJava case "go":
case "objc": lang = bind.LangGo
lang = bind.LangObjC case "java":
default: lang = bind.LangJava
fmt.Printf("Unsupported destination language \"%s\" (--lang)\n", *langFlag) case "objc":
os.Exit(-1) lang = bind.LangObjC
} default:
// If the entire solidity code was specified, build and bind based on that fmt.Printf("Unsupported destination language \"%s\" (--lang)\n", *langFlag)
var ( os.Exit(-1)
abis []string }
bins []string // If the entire solidity code was specified, build and bind based on that
types []string var (
) abis []string
if *solFlag != "" || (*abiFlag == "-" && *pkgFlag == "") { bins []string
// Generate the list of types to exclude from binding types []string
exclude := make(map[string]bool) )
for _, kind := range strings.Split(*excFlag, ",") { if *solFlag != "" || *vyFlag != "" || (*abiFlag == "-" && *pkgFlag == "") {
exclude[strings.ToLower(kind)] = true // Generate the list of types to exclude from binding
} exclude := make(map[string]bool)
for _, kind := range strings.Split(*excFlag, ",") {
exclude[strings.ToLower(kind)] = true
}
var contracts map[string]*compiler.Contract var contracts map[string]*compiler.Contract
var err error var err error
if *solFlag != "" { if *solFlag != "" {
contracts, err = compiler.CompileSolidity(*solcFlag, *solFlag) contracts, err = compiler.CompileSolidity(*solcFlag, *solFlag)
if err != nil { if err != nil {
fmt.Printf("Failed to build Solidity contract: %v\n", err) fmt.Printf("Failed to build Solidity contract: %v\n", err)
os.Exit(-1) os.Exit(-1)
} }
} else { } else if *vyFlag != "" {
contracts, err = contractsFromStdin() contracts, err = compiler.CompileVyper(*vyperFlag, *vyFlag)
if err != nil { if err != nil {
fmt.Printf("Failed to read input ABIs from STDIN: %v\n", err) fmt.Printf("Failed to build Vyper contract: %v\n", err)
os.Exit(-1) os.Exit(-1)
} }
} } else {
// Gather all non-excluded contract for binding contracts, err = contractsFromStdin()
for name, contract := range contracts { if err != nil {
if exclude[strings.ToLower(name)] { fmt.Printf("Failed to read input ABIs from STDIN: %v\n", err)
continue os.Exit(-1)
} }
abi, _ := json.Marshal(contract.Info.AbiDefinition) // Flatten the compiler parse }
abis = append(abis, string(abi)) // Gather all non-excluded contract for binding
bins = append(bins, contract.Code) for name, contract := range contracts {
if exclude[strings.ToLower(name)] {
continue
}
abi, _ := json.Marshal(contract.Info.AbiDefinition) // Flatten the compiler parse
abis = append(abis, string(abi))
bins = append(bins, contract.Code)
nameParts := strings.Split(name, ":") nameParts := strings.Split(name, ":")
types = append(types, nameParts[len(nameParts)-1]) types = append(types, nameParts[len(nameParts)-1])
} }
} else { } else {
// Otherwise load up the ABI, optional bytecode and type name from the parameters // Otherwise load up the ABI, optional bytecode and type name from the parameters
var abi []byte var abi []byte
var err error var err error
if *abiFlag == "-" { if *abiFlag == "-" {
abi, err = ioutil.ReadAll(os.Stdin) abi, err = ioutil.ReadAll(os.Stdin)
} else { } else {
abi, err = ioutil.ReadFile(*abiFlag) abi, err = ioutil.ReadFile(*abiFlag)
} }
if err != nil { if err != nil {
fmt.Printf("Failed to read input ABI: %v\n", err) fmt.Printf("Failed to read input ABI: %v\n", err)
os.Exit(-1) os.Exit(-1)
} }
abis = append(abis, string(abi)) abis = append(abis, string(abi))
bin := []byte{} bin := []byte{}
if *binFlag != "" { if *binFlag != "" {
if bin, err = ioutil.ReadFile(*binFlag); err != nil { if bin, err = ioutil.ReadFile(*binFlag); err != nil {
fmt.Printf("Failed to read input bytecode: %v\n", err) fmt.Printf("Failed to read input bytecode: %v\n", err)
os.Exit(-1) os.Exit(-1)
} }
} }
bins = append(bins, string(bin)) bins = append(bins, string(bin))
kind := *typFlag kind := *typFlag
if kind == "" { if kind == "" {
kind = *pkgFlag kind = *pkgFlag
} }
types = append(types, kind) types = append(types, kind)
} }
// Generate the contract binding // Generate the contract binding
code, err := bind.Bind(types, abis, bins, *pkgFlag, lang) code, err := bind.Bind(types, abis, bins, *pkgFlag, lang)
if err != nil { if err != nil {
fmt.Printf("Failed to generate ABI binding: %v\n", err) fmt.Printf("Failed to generate ABI binding: %v\n", err)
os.Exit(-1) os.Exit(-1)
} }
// Either flush it out to a file or display on the standard output // Either flush it out to a file or display on the standard output
if *outFlag == "" { if *outFlag == "" {
fmt.Printf("%s\n", code) fmt.Printf("%s\n", code)
return return
} }
if err := ioutil.WriteFile(*outFlag, []byte(code), 0600); err != nil { if err := ioutil.WriteFile(*outFlag, []byte(code), 0600); err != nil {
fmt.Printf("Failed to write ABI binding: %v\n", err) fmt.Printf("Failed to write ABI binding: %v\n", err)
os.Exit(-1) os.Exit(-1)
} }
} }
func contractsFromStdin() (map[string]*compiler.Contract, error) { func contractsFromStdin() (map[string]*compiler.Contract, error) {
bytes, err := ioutil.ReadAll(os.Stdin) bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return compiler.ParseCombinedJSON(bytes, "", "", "", "") return compiler.ParseCombinedJSON(bytes, "", "", "", "")
} }