template.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser 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. // The go-ethereum library 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 Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package bind
  17. import "github.com/ethereum/go-ethereum/accounts/abi"
  18. // tmplData is the data structure required to fill the binding template.
  19. type tmplData struct {
  20. Package string // Name of the package to place the generated file in
  21. Contracts map[string]*tmplContract // List of contracts to generate into this file
  22. }
  23. // tmplContract contains the data needed to generate an individual contract binding.
  24. type tmplContract struct {
  25. Type string // Type name of the main contract binding
  26. InputABI string // JSON ABI used as the input to generate the binding from
  27. InputBin string // Optional EVM bytecode used to denetare deploy code from
  28. Constructor abi.Method // Contract constructor for deploy parametrization
  29. Calls map[string]*tmplMethod // Contract calls that only read state data
  30. Transacts map[string]*tmplMethod // Contract calls that write state data
  31. }
  32. // tmplMethod is a wrapper around an abi.Method that contains a few preprocessed
  33. // and cached data fields.
  34. type tmplMethod struct {
  35. Original abi.Method // Original method as parsed by the abi package
  36. Normalized abi.Method // Normalized version of the parsed method (capitalized names, non-anonymous args/returns)
  37. Structured bool // Whether the returns should be accumulated into a contract
  38. }
  39. // tmplSource is language to template mapping containing all the supported
  40. // programming languages the package can generate to.
  41. var tmplSource = map[Lang]string{
  42. LangGo: tmplSourceGo,
  43. LangJava: tmplSourceJava,
  44. }
  45. // tmplSourceGo is the Go source template use to generate the contract binding
  46. // based on.
  47. const tmplSourceGo = `
  48. // This file is an automatically generated Go binding. Do not modify as any
  49. // change will likely be lost upon the next re-generation!
  50. package {{.Package}}
  51. {{range $contract := .Contracts}}
  52. // {{.Type}}ABI is the input ABI used to generate the binding from.
  53. const {{.Type}}ABI = "{{.InputABI}}"
  54. {{if .InputBin}}
  55. // {{.Type}}Bin is the compiled bytecode used for deploying new contracts.
  56. const {{.Type}}Bin = ` + "`" + `{{.InputBin}}` + "`" + `
  57. // Deploy{{.Type}} deploys a new Ethereum contract, binding an instance of {{.Type}} to it.
  58. func Deploy{{.Type}}(auth *bind.TransactOpts, backend bind.ContractBackend {{range .Constructor.Inputs}}, {{.Name}} {{bindtype .Type}}{{end}}) (common.Address, *types.Transaction, *{{.Type}}, error) {
  59. parsed, err := abi.JSON(strings.NewReader({{.Type}}ABI))
  60. if err != nil {
  61. return common.Address{}, nil, nil, err
  62. }
  63. address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex({{.Type}}Bin), backend {{range .Constructor.Inputs}}, {{.Name}}{{end}})
  64. if err != nil {
  65. return common.Address{}, nil, nil, err
  66. }
  67. return address, tx, &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract} }, nil
  68. }
  69. {{end}}
  70. // {{.Type}} is an auto generated Go binding around an Ethereum contract.
  71. type {{.Type}} struct {
  72. {{.Type}}Caller // Read-only binding to the contract
  73. {{.Type}}Transactor // Write-only binding to the contract
  74. }
  75. // {{.Type}}Caller is an auto generated read-only Go binding around an Ethereum contract.
  76. type {{.Type}}Caller struct {
  77. contract *bind.BoundContract // Generic contract wrapper for the low level calls
  78. }
  79. // {{.Type}}Transactor is an auto generated write-only Go binding around an Ethereum contract.
  80. type {{.Type}}Transactor struct {
  81. contract *bind.BoundContract // Generic contract wrapper for the low level calls
  82. }
  83. // {{.Type}}Session is an auto generated Go binding around an Ethereum contract,
  84. // with pre-set call and transact options.
  85. type {{.Type}}Session struct {
  86. Contract *{{.Type}} // Generic contract binding to set the session for
  87. CallOpts bind.CallOpts // Call options to use throughout this session
  88. TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
  89. }
  90. // {{.Type}}CallerSession is an auto generated read-only Go binding around an Ethereum contract,
  91. // with pre-set call options.
  92. type {{.Type}}CallerSession struct {
  93. Contract *{{.Type}}Caller // Generic contract caller binding to set the session for
  94. CallOpts bind.CallOpts // Call options to use throughout this session
  95. }
  96. // {{.Type}}TransactorSession is an auto generated write-only Go binding around an Ethereum contract,
  97. // with pre-set transact options.
  98. type {{.Type}}TransactorSession struct {
  99. Contract *{{.Type}}Transactor // Generic contract transactor binding to set the session for
  100. TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
  101. }
  102. // {{.Type}}Raw is an auto generated low-level Go binding around an Ethereum contract.
  103. type {{.Type}}Raw struct {
  104. Contract *{{.Type}} // Generic contract binding to access the raw methods on
  105. }
  106. // {{.Type}}CallerRaw is an auto generated low-level read-only Go binding around an Ethereum contract.
  107. type {{.Type}}CallerRaw struct {
  108. Contract *{{.Type}}Caller // Generic read-only contract binding to access the raw methods on
  109. }
  110. // {{.Type}}TransactorRaw is an auto generated low-level write-only Go binding around an Ethereum contract.
  111. type {{.Type}}TransactorRaw struct {
  112. Contract *{{.Type}}Transactor // Generic write-only contract binding to access the raw methods on
  113. }
  114. // New{{.Type}} creates a new instance of {{.Type}}, bound to a specific deployed contract.
  115. func New{{.Type}}(address common.Address, backend bind.ContractBackend) (*{{.Type}}, error) {
  116. contract, err := bind{{.Type}}(address, backend, backend)
  117. if err != nil {
  118. return nil, err
  119. }
  120. return &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract} }, nil
  121. }
  122. // New{{.Type}}Caller creates a new read-only instance of {{.Type}}, bound to a specific deployed contract.
  123. func New{{.Type}}Caller(address common.Address, caller bind.ContractCaller) (*{{.Type}}Caller, error) {
  124. contract, err := bind{{.Type}}(address, caller, nil)
  125. if err != nil {
  126. return nil, err
  127. }
  128. return &{{.Type}}Caller{contract: contract}, nil
  129. }
  130. // New{{.Type}}Transactor creates a new write-only instance of {{.Type}}, bound to a specific deployed contract.
  131. func New{{.Type}}Transactor(address common.Address, transactor bind.ContractTransactor) (*{{.Type}}Transactor, error) {
  132. contract, err := bind{{.Type}}(address, nil, transactor)
  133. if err != nil {
  134. return nil, err
  135. }
  136. return &{{.Type}}Transactor{contract: contract}, nil
  137. }
  138. // bind{{.Type}} binds a generic wrapper to an already deployed contract.
  139. func bind{{.Type}}(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor) (*bind.BoundContract, error) {
  140. parsed, err := abi.JSON(strings.NewReader({{.Type}}ABI))
  141. if err != nil {
  142. return nil, err
  143. }
  144. return bind.NewBoundContract(address, parsed, caller, transactor), nil
  145. }
  146. // Call invokes the (constant) contract method with params as input values and
  147. // sets the output to result. The result type might be a single field for simple
  148. // returns, a slice of interfaces for anonymous returns and a struct for named
  149. // returns.
  150. func (_{{$contract.Type}} *{{$contract.Type}}Raw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error {
  151. return _{{$contract.Type}}.Contract.{{$contract.Type}}Caller.contract.Call(opts, result, method, params...)
  152. }
  153. // Transfer initiates a plain transaction to move funds to the contract, calling
  154. // its default method if one is available.
  155. func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
  156. return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transfer(opts)
  157. }
  158. // Transact invokes the (paid) contract method with params as input values.
  159. func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
  160. return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transact(opts, method, params...)
  161. }
  162. // Call invokes the (constant) contract method with params as input values and
  163. // sets the output to result. The result type might be a single field for simple
  164. // returns, a slice of interfaces for anonymous returns and a struct for named
  165. // returns.
  166. func (_{{$contract.Type}} *{{$contract.Type}}CallerRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error {
  167. return _{{$contract.Type}}.Contract.contract.Call(opts, result, method, params...)
  168. }
  169. // Transfer initiates a plain transaction to move funds to the contract, calling
  170. // its default method if one is available.
  171. func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
  172. return _{{$contract.Type}}.Contract.contract.Transfer(opts)
  173. }
  174. // Transact invokes the (paid) contract method with params as input values.
  175. func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
  176. return _{{$contract.Type}}.Contract.contract.Transact(opts, method, params...)
  177. }
  178. {{range .Calls}}
  179. // {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.Id}}.
  180. //
  181. // Solidity: {{.Original.String}}
  182. func (_{{$contract.Type}} *{{$contract.Type}}Caller) {{.Normalized.Name}}(opts *bind.CallOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type}};{{end}} },{{else}}{{range .Normalized.Outputs}}{{bindtype .Type}},{{end}}{{end}} error) {
  183. {{if .Structured}}ret := new(struct{
  184. {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type}}
  185. {{end}}
  186. }){{else}}var (
  187. {{range $i, $_ := .Normalized.Outputs}}ret{{$i}} = new({{bindtype .Type}})
  188. {{end}}
  189. ){{end}}
  190. out := {{if .Structured}}ret{{else}}{{if eq (len .Normalized.Outputs) 1}}ret0{{else}}&[]interface{}{
  191. {{range $i, $_ := .Normalized.Outputs}}ret{{$i}},
  192. {{end}}
  193. }{{end}}{{end}}
  194. err := _{{$contract.Type}}.contract.Call(opts, out, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
  195. return {{if .Structured}}*ret,{{else}}{{range $i, $_ := .Normalized.Outputs}}*ret{{$i}},{{end}}{{end}} err
  196. }
  197. // {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.Id}}.
  198. //
  199. // Solidity: {{.Original.String}}
  200. func (_{{$contract.Type}} *{{$contract.Type}}Session) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type}};{{end}} }, {{else}} {{range .Normalized.Outputs}}{{bindtype .Type}},{{end}} {{end}} error) {
  201. return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}})
  202. }
  203. // {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.Id}}.
  204. //
  205. // Solidity: {{.Original.String}}
  206. func (_{{$contract.Type}} *{{$contract.Type}}CallerSession) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type}};{{end}} }, {{else}} {{range .Normalized.Outputs}}{{bindtype .Type}},{{end}} {{end}} error) {
  207. return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}})
  208. }
  209. {{end}}
  210. {{range .Transacts}}
  211. // {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.Id}}.
  212. //
  213. // Solidity: {{.Original.String}}
  214. func (_{{$contract.Type}} *{{$contract.Type}}Transactor) {{.Normalized.Name}}(opts *bind.TransactOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type}} {{end}}) (*types.Transaction, error) {
  215. return _{{$contract.Type}}.contract.Transact(opts, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
  216. }
  217. // {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.Id}}.
  218. //
  219. // Solidity: {{.Original.String}}
  220. func (_{{$contract.Type}} *{{$contract.Type}}Session) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type}} {{end}}) (*types.Transaction, error) {
  221. return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}})
  222. }
  223. // {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.Id}}.
  224. //
  225. // Solidity: {{.Original.String}}
  226. func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type}} {{end}}) (*types.Transaction, error) {
  227. return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}})
  228. }
  229. {{end}}
  230. {{end}}
  231. `
  232. // tmplSourceJava is the Java source template use to generate the contract binding
  233. // based on.
  234. const tmplSourceJava = `
  235. // This file is an automatically generated Java binding. Do not modify as any
  236. // change will likely be lost upon the next re-generation!
  237. package {{.Package}};
  238. import org.ethereum.geth.*;
  239. import org.ethereum.geth.internal.*;
  240. {{range $contract := .Contracts}}
  241. public class {{.Type}} {
  242. // ABI is the input ABI used to generate the binding from.
  243. public final static String ABI = "{{.InputABI}}";
  244. {{if .InputBin}}
  245. // Bytecode is the compiled bytecode used for deploying new contracts.
  246. public final static byte[] Bytecode = "{{.InputBin}}".getBytes();
  247. // deploy deploys a new Ethereum contract, binding an instance of {{.Type}} to it.
  248. public static {{.Type}} deploy(TransactOpts auth, EthereumClient client{{range .Constructor.Inputs}}, {{bindtype .Type}} {{.Name}}{{end}}) throws Exception {
  249. Interfaces args = Geth.newInterfaces({{(len .Constructor.Inputs)}});
  250. {{range $index, $element := .Constructor.Inputs}}
  251. args.set({{$index}}, Geth.newInterface()); args.get({{$index}}).set{{namedtype (bindtype .Type) .Type}}({{.Name}});
  252. {{end}}
  253. return new {{.Type}}(Geth.deployContract(auth, ABI, Bytecode, client, args));
  254. }
  255. // Internal constructor used by contract deployment.
  256. private {{.Type}}(BoundContract deployment) {
  257. this.Address = deployment.getAddress();
  258. this.Deployer = deployment.getDeployer();
  259. this.Contract = deployment;
  260. }
  261. {{end}}
  262. // Ethereum address where this contract is located at.
  263. public final Address Address;
  264. // Ethereum transaction in which this contract was deployed (if known!).
  265. public final Transaction Deployer;
  266. // Contract instance bound to a blockchain address.
  267. private final BoundContract Contract;
  268. // Creates a new instance of {{.Type}}, bound to a specific deployed contract.
  269. public {{.Type}}(Address address, EthereumClient client) throws Exception {
  270. this(Geth.bindContract(address, ABI, client));
  271. }
  272. {{range .Calls}}
  273. {{if gt (len .Normalized.Outputs) 1}}
  274. // {{capitalise .Normalized.Name}}Results is the output of a call to {{.Normalized.Name}}.
  275. public class {{capitalise .Normalized.Name}}Results {
  276. {{range $index, $item := .Normalized.Outputs}}public {{bindtype .Type}} {{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}};
  277. {{end}}
  278. }
  279. {{end}}
  280. // {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.Id}}.
  281. //
  282. // Solidity: {{.Original.String}}
  283. public {{if gt (len .Normalized.Outputs) 1}}{{capitalise .Normalized.Name}}Results{{else}}{{range .Normalized.Outputs}}{{bindtype .Type}}{{end}}{{end}} {{.Normalized.Name}}(CallOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type}} {{.Name}}{{end}}) throws Exception {
  284. Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}});
  285. {{range $index, $item := .Normalized.Inputs}}args.set({{$index}}, Geth.newInterface()); args.get({{$index}}).set{{namedtype (bindtype .Type) .Type}}({{.Name}});
  286. {{end}}
  287. Interfaces results = Geth.newInterfaces({{(len .Normalized.Outputs)}});
  288. {{range $index, $item := .Normalized.Outputs}}Interface result{{$index}} = Geth.newInterface(); result{{$index}}.setDefault{{namedtype (bindtype .Type) .Type}}(); results.set({{$index}}, result{{$index}});
  289. {{end}}
  290. if (opts == null) {
  291. opts = Geth.newCallOpts();
  292. }
  293. this.Contract.call(opts, results, "{{.Original.Name}}", args);
  294. {{if gt (len .Normalized.Outputs) 1}}
  295. {{capitalise .Normalized.Name}}Results result = new {{capitalise .Normalized.Name}}Results();
  296. {{range $index, $item := .Normalized.Outputs}}result.{{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}} = results.get({{$index}}).get{{namedtype (bindtype .Type) .Type}}();
  297. {{end}}
  298. return result;
  299. {{else}}{{range .Normalized.Outputs}}return results.get(0).get{{namedtype (bindtype .Type) .Type}}();{{end}}
  300. {{end}}
  301. }
  302. {{end}}
  303. {{range .Transacts}}
  304. // {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.Id}}.
  305. //
  306. // Solidity: {{.Original.String}}
  307. public Transaction {{.Normalized.Name}}(TransactOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type}} {{.Name}}{{end}}) throws Exception {
  308. Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}});
  309. {{range $index, $item := .Normalized.Inputs}}args.set({{$index}}, Geth.newInterface()); args.get({{$index}}).set{{namedtype (bindtype .Type) .Type}}({{.Name}});
  310. {{end}}
  311. return this.Contract.transact(opts, "{{.Original.Name}}" , args);
  312. }
  313. {{end}}
  314. }
  315. {{end}}
  316. `