main.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. package main
  17. import (
  18. "encoding/json"
  19. "flag"
  20. "fmt"
  21. "io/ioutil"
  22. "os"
  23. "strings"
  24. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  25. "github.com/ethereum/go-ethereum/common/compiler"
  26. )
  27. var (
  28. abiFlag = flag.String("abi", "", "Path to the Ethereum contract ABI json to bind")
  29. binFlag = flag.String("bin", "", "Path to the Ethereum contract bytecode (generate deploy method)")
  30. typFlag = flag.String("type", "", "Go struct name for the binding (default = package name)")
  31. solFlag = flag.String("sol", "", "Path to the Ethereum contract Solidity source to build and bind")
  32. solcFlag = flag.String("solc", "solc", "Solidity compiler to use if source builds are requested")
  33. excFlag = flag.String("exc", "", "Comma separated types to exclude from binding")
  34. pkgFlag = flag.String("pkg", "", "Go package name to generate the binding into")
  35. outFlag = flag.String("out", "", "Output file for the generated binding (default = stdout)")
  36. )
  37. func main() {
  38. // Parse and ensure all needed inputs are specified
  39. flag.Parse()
  40. if *abiFlag == "" && *solFlag == "" {
  41. fmt.Printf("No contract ABI (--abi) or Solidity source (--sol) specified\n")
  42. os.Exit(-1)
  43. } else if (*abiFlag != "" || *binFlag != "" || *typFlag != "") && *solFlag != "" {
  44. fmt.Printf("Contract ABI (--abi), bytecode (--bin) and type (--type) flags are mutually exclusive with the Solidity source (--sol) flag\n")
  45. os.Exit(-1)
  46. }
  47. if *pkgFlag == "" {
  48. fmt.Printf("No destination Go package specified (--pkg)\n")
  49. os.Exit(-1)
  50. }
  51. // If the entire solidity code was specified, build and bind based on that
  52. var (
  53. abis []string
  54. bins []string
  55. types []string
  56. )
  57. if *solFlag != "" {
  58. // Generate the list of types to exclude from binding
  59. exclude := make(map[string]bool)
  60. for _, kind := range strings.Split(*excFlag, ",") {
  61. exclude[strings.ToLower(kind)] = true
  62. }
  63. // Build the Solidity source into bindable components
  64. solc, err := compiler.New(*solcFlag)
  65. if err != nil {
  66. fmt.Printf("Failed to locate Solidity compiler: %v\n", err)
  67. os.Exit(-1)
  68. }
  69. source, err := ioutil.ReadFile(*solFlag)
  70. if err != nil {
  71. fmt.Printf("Failed to read Soldity source code: %v\n", err)
  72. os.Exit(-1)
  73. }
  74. contracts, err := solc.Compile(string(source))
  75. if err != nil {
  76. fmt.Printf("Failed to build Solidity contract: %v\n", err)
  77. os.Exit(-1)
  78. }
  79. // Gather all non-excluded contract for binding
  80. for name, contract := range contracts {
  81. if exclude[strings.ToLower(name)] {
  82. continue
  83. }
  84. abi, _ := json.Marshal(contract.Info.AbiDefinition) // Flatten the compiler parse
  85. abis = append(abis, string(abi))
  86. bins = append(bins, contract.Code)
  87. types = append(types, name)
  88. }
  89. } else {
  90. // Otherwise load up the ABI, optional bytecode and type name from the parameters
  91. abi, err := ioutil.ReadFile(*abiFlag)
  92. if err != nil {
  93. fmt.Printf("Failed to read input ABI: %v\n", err)
  94. os.Exit(-1)
  95. }
  96. abis = append(abis, string(abi))
  97. bin := []byte{}
  98. if *binFlag != "" {
  99. if bin, err = ioutil.ReadFile(*binFlag); err != nil {
  100. fmt.Printf("Failed to read input bytecode: %v\n", err)
  101. os.Exit(-1)
  102. }
  103. }
  104. bins = append(bins, string(bin))
  105. kind := *typFlag
  106. if kind == "" {
  107. kind = *pkgFlag
  108. }
  109. types = append(types, kind)
  110. }
  111. // Generate the contract binding
  112. code, err := bind.Bind(types, abis, bins, *pkgFlag)
  113. if err != nil {
  114. fmt.Printf("Failed to generate ABI binding: %v\n", err)
  115. os.Exit(-1)
  116. }
  117. // Either flush it out to a file or display on the standard output
  118. if *outFlag == "" {
  119. fmt.Printf("%s\n", code)
  120. return
  121. }
  122. if err := ioutil.WriteFile(*outFlag, []byte(code), 0600); err != nil {
  123. fmt.Printf("Failed to write ABI binding: %v\n", err)
  124. os.Exit(-1)
  125. }
  126. }