template.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. Events map[string]*tmplEvent // Contract events accessors
  32. }
  33. // tmplMethod is a wrapper around an abi.Method that contains a few preprocessed
  34. // and cached data fields.
  35. type tmplMethod struct {
  36. Original abi.Method // Original method as parsed by the abi package
  37. Normalized abi.Method // Normalized version of the parsed method (capitalized names, non-anonymous args/returns)
  38. Structured bool // Whether the returns should be accumulated into a struct
  39. }
  40. // tmplEvent is a wrapper around an a
  41. type tmplEvent struct {
  42. Original abi.Event // Original event as parsed by the abi package
  43. Normalized abi.Event // Normalized version of the parsed fields
  44. }
  45. // tmplSource is language to template mapping containing all the supported
  46. // programming languages the package can generate to.
  47. var tmplSource = map[Lang]string{
  48. LangGo: tmplSourceGo,
  49. LangJava: tmplSourceJava,
  50. }
  51. // tmplSourceGo is the Go source template use to generate the contract binding
  52. // based on.
  53. const tmplSourceGo = `
  54. // Code generated - DO NOT EDIT.
  55. // This file is a generated binding and any manual changes will be lost.
  56. package {{.Package}}
  57. import (
  58. "math/big"
  59. "strings"
  60. ethereum "github.com/ethereum/go-ethereum"
  61. "github.com/ethereum/go-ethereum/accounts/abi"
  62. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  63. "github.com/ethereum/go-ethereum/common"
  64. "github.com/ethereum/go-ethereum/core/types"
  65. "github.com/ethereum/go-ethereum/event"
  66. )
  67. // Reference imports to suppress errors if they are not otherwise used.
  68. var (
  69. _ = big.NewInt
  70. _ = strings.NewReader
  71. _ = ethereum.NotFound
  72. _ = abi.U256
  73. _ = bind.Bind
  74. _ = common.Big1
  75. _ = types.BloomLookup
  76. _ = event.NewSubscription
  77. )
  78. {{range $contract := .Contracts}}
  79. // {{.Type}}ABI is the input ABI used to generate the binding from.
  80. const {{.Type}}ABI = "{{.InputABI}}"
  81. {{if .InputBin}}
  82. // {{.Type}}Bin is the compiled bytecode used for deploying new contracts.
  83. const {{.Type}}Bin = ` + "`" + `{{.InputBin}}` + "`" + `
  84. // Deploy{{.Type}} deploys a new Ethereum contract, binding an instance of {{.Type}} to it.
  85. func Deploy{{.Type}}(auth *bind.TransactOpts, backend bind.ContractBackend {{range .Constructor.Inputs}}, {{.Name}} {{bindtype .Type}}{{end}}) (common.Address, *types.Transaction, *{{.Type}}, error) {
  86. parsed, err := abi.JSON(strings.NewReader({{.Type}}ABI))
  87. if err != nil {
  88. return common.Address{}, nil, nil, err
  89. }
  90. address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex({{.Type}}Bin), backend {{range .Constructor.Inputs}}, {{.Name}}{{end}})
  91. if err != nil {
  92. return common.Address{}, nil, nil, err
  93. }
  94. return address, tx, &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract}, {{.Type}}Filterer: {{.Type}}Filterer{contract: contract} }, nil
  95. }
  96. {{end}}
  97. // {{.Type}} is an auto generated Go binding around an Ethereum contract.
  98. type {{.Type}} struct {
  99. {{.Type}}Caller // Read-only binding to the contract
  100. {{.Type}}Transactor // Write-only binding to the contract
  101. {{.Type}}Filterer // Log filterer for contract events
  102. }
  103. // {{.Type}}Caller is an auto generated read-only Go binding around an Ethereum contract.
  104. type {{.Type}}Caller struct {
  105. contract *bind.BoundContract // Generic contract wrapper for the low level calls
  106. }
  107. // {{.Type}}Transactor is an auto generated write-only Go binding around an Ethereum contract.
  108. type {{.Type}}Transactor struct {
  109. contract *bind.BoundContract // Generic contract wrapper for the low level calls
  110. }
  111. // {{.Type}}Filterer is an auto generated log filtering Go binding around an Ethereum contract events.
  112. type {{.Type}}Filterer struct {
  113. contract *bind.BoundContract // Generic contract wrapper for the low level calls
  114. }
  115. // {{.Type}}Session is an auto generated Go binding around an Ethereum contract,
  116. // with pre-set call and transact options.
  117. type {{.Type}}Session struct {
  118. Contract *{{.Type}} // Generic contract binding to set the session for
  119. CallOpts bind.CallOpts // Call options to use throughout this session
  120. TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
  121. }
  122. // {{.Type}}CallerSession is an auto generated read-only Go binding around an Ethereum contract,
  123. // with pre-set call options.
  124. type {{.Type}}CallerSession struct {
  125. Contract *{{.Type}}Caller // Generic contract caller binding to set the session for
  126. CallOpts bind.CallOpts // Call options to use throughout this session
  127. }
  128. // {{.Type}}TransactorSession is an auto generated write-only Go binding around an Ethereum contract,
  129. // with pre-set transact options.
  130. type {{.Type}}TransactorSession struct {
  131. Contract *{{.Type}}Transactor // Generic contract transactor binding to set the session for
  132. TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
  133. }
  134. // {{.Type}}Raw is an auto generated low-level Go binding around an Ethereum contract.
  135. type {{.Type}}Raw struct {
  136. Contract *{{.Type}} // Generic contract binding to access the raw methods on
  137. }
  138. // {{.Type}}CallerRaw is an auto generated low-level read-only Go binding around an Ethereum contract.
  139. type {{.Type}}CallerRaw struct {
  140. Contract *{{.Type}}Caller // Generic read-only contract binding to access the raw methods on
  141. }
  142. // {{.Type}}TransactorRaw is an auto generated low-level write-only Go binding around an Ethereum contract.
  143. type {{.Type}}TransactorRaw struct {
  144. Contract *{{.Type}}Transactor // Generic write-only contract binding to access the raw methods on
  145. }
  146. // New{{.Type}} creates a new instance of {{.Type}}, bound to a specific deployed contract.
  147. func New{{.Type}}(address common.Address, backend bind.ContractBackend) (*{{.Type}}, error) {
  148. contract, err := bind{{.Type}}(address, backend, backend, backend)
  149. if err != nil {
  150. return nil, err
  151. }
  152. return &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract}, {{.Type}}Filterer: {{.Type}}Filterer{contract: contract} }, nil
  153. }
  154. // New{{.Type}}Caller creates a new read-only instance of {{.Type}}, bound to a specific deployed contract.
  155. func New{{.Type}}Caller(address common.Address, caller bind.ContractCaller) (*{{.Type}}Caller, error) {
  156. contract, err := bind{{.Type}}(address, caller, nil, nil)
  157. if err != nil {
  158. return nil, err
  159. }
  160. return &{{.Type}}Caller{contract: contract}, nil
  161. }
  162. // New{{.Type}}Transactor creates a new write-only instance of {{.Type}}, bound to a specific deployed contract.
  163. func New{{.Type}}Transactor(address common.Address, transactor bind.ContractTransactor) (*{{.Type}}Transactor, error) {
  164. contract, err := bind{{.Type}}(address, nil, transactor, nil)
  165. if err != nil {
  166. return nil, err
  167. }
  168. return &{{.Type}}Transactor{contract: contract}, nil
  169. }
  170. // New{{.Type}}Filterer creates a new log filterer instance of {{.Type}}, bound to a specific deployed contract.
  171. func New{{.Type}}Filterer(address common.Address, filterer bind.ContractFilterer) (*{{.Type}}Filterer, error) {
  172. contract, err := bind{{.Type}}(address, nil, nil, filterer)
  173. if err != nil {
  174. return nil, err
  175. }
  176. return &{{.Type}}Filterer{contract: contract}, nil
  177. }
  178. // bind{{.Type}} binds a generic wrapper to an already deployed contract.
  179. func bind{{.Type}}(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) {
  180. parsed, err := abi.JSON(strings.NewReader({{.Type}}ABI))
  181. if err != nil {
  182. return nil, err
  183. }
  184. return bind.NewBoundContract(address, parsed, caller, transactor, filterer), nil
  185. }
  186. // Call invokes the (constant) contract method with params as input values and
  187. // sets the output to result. The result type might be a single field for simple
  188. // returns, a slice of interfaces for anonymous returns and a struct for named
  189. // returns.
  190. func (_{{$contract.Type}} *{{$contract.Type}}Raw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error {
  191. return _{{$contract.Type}}.Contract.{{$contract.Type}}Caller.contract.Call(opts, result, method, params...)
  192. }
  193. // Transfer initiates a plain transaction to move funds to the contract, calling
  194. // its default method if one is available.
  195. func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
  196. return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transfer(opts)
  197. }
  198. // Transact invokes the (paid) contract method with params as input values.
  199. func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
  200. return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transact(opts, method, params...)
  201. }
  202. // Call invokes the (constant) contract method with params as input values and
  203. // sets the output to result. The result type might be a single field for simple
  204. // returns, a slice of interfaces for anonymous returns and a struct for named
  205. // returns.
  206. func (_{{$contract.Type}} *{{$contract.Type}}CallerRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error {
  207. return _{{$contract.Type}}.Contract.contract.Call(opts, result, method, params...)
  208. }
  209. // Transfer initiates a plain transaction to move funds to the contract, calling
  210. // its default method if one is available.
  211. func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
  212. return _{{$contract.Type}}.Contract.contract.Transfer(opts)
  213. }
  214. // Transact invokes the (paid) contract method with params as input values.
  215. func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
  216. return _{{$contract.Type}}.Contract.contract.Transact(opts, method, params...)
  217. }
  218. {{range .Calls}}
  219. // {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.Id}}.
  220. //
  221. // Solidity: {{.Original.String}}
  222. 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) {
  223. {{if .Structured}}ret := new(struct{
  224. {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type}}
  225. {{end}}
  226. }){{else}}var (
  227. {{range $i, $_ := .Normalized.Outputs}}ret{{$i}} = new({{bindtype .Type}})
  228. {{end}}
  229. ){{end}}
  230. out := {{if .Structured}}ret{{else}}{{if eq (len .Normalized.Outputs) 1}}ret0{{else}}&[]interface{}{
  231. {{range $i, $_ := .Normalized.Outputs}}ret{{$i}},
  232. {{end}}
  233. }{{end}}{{end}}
  234. err := _{{$contract.Type}}.contract.Call(opts, out, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
  235. return {{if .Structured}}*ret,{{else}}{{range $i, $_ := .Normalized.Outputs}}*ret{{$i}},{{end}}{{end}} err
  236. }
  237. // {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.Id}}.
  238. //
  239. // Solidity: {{.Original.String}}
  240. 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) {
  241. return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}})
  242. }
  243. // {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.Id}}.
  244. //
  245. // Solidity: {{.Original.String}}
  246. 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) {
  247. return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}})
  248. }
  249. {{end}}
  250. {{range .Transacts}}
  251. // {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.Id}}.
  252. //
  253. // Solidity: {{.Original.String}}
  254. func (_{{$contract.Type}} *{{$contract.Type}}Transactor) {{.Normalized.Name}}(opts *bind.TransactOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type}} {{end}}) (*types.Transaction, error) {
  255. return _{{$contract.Type}}.contract.Transact(opts, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
  256. }
  257. // {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.Id}}.
  258. //
  259. // Solidity: {{.Original.String}}
  260. func (_{{$contract.Type}} *{{$contract.Type}}Session) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type}} {{end}}) (*types.Transaction, error) {
  261. return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}})
  262. }
  263. // {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.Id}}.
  264. //
  265. // Solidity: {{.Original.String}}
  266. func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type}} {{end}}) (*types.Transaction, error) {
  267. return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}})
  268. }
  269. {{end}}
  270. {{range .Events}}
  271. // {{$contract.Type}}{{.Normalized.Name}}Iterator is returned from Filter{{.Normalized.Name}} and is used to iterate over the raw logs and unpacked data for {{.Normalized.Name}} events raised by the {{$contract.Type}} contract.
  272. type {{$contract.Type}}{{.Normalized.Name}}Iterator struct {
  273. Event *{{$contract.Type}}{{.Normalized.Name}} // Event containing the contract specifics and raw log
  274. contract *bind.BoundContract // Generic contract to use for unpacking event data
  275. event string // Event name to use for unpacking event data
  276. logs chan types.Log // Log channel receiving the found contract events
  277. sub ethereum.Subscription // Subscription for errors, completion and termination
  278. done bool // Whether the subscription completed delivering logs
  279. fail error // Occurred error to stop iteration
  280. }
  281. // Next advances the iterator to the subsequent event, returning whether there
  282. // are any more events found. In case of a retrieval or parsing error, false is
  283. // returned and Error() can be queried for the exact failure.
  284. func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Next() bool {
  285. // If the iterator failed, stop iterating
  286. if (it.fail != nil) {
  287. return false
  288. }
  289. // If the iterator completed, deliver directly whatever's available
  290. if (it.done) {
  291. select {
  292. case log := <-it.logs:
  293. it.Event = new({{$contract.Type}}{{.Normalized.Name}})
  294. if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil {
  295. it.fail = err
  296. return false
  297. }
  298. it.Event.Raw = log
  299. return true
  300. default:
  301. return false
  302. }
  303. }
  304. // Iterator still in progress, wait for either a data or an error event
  305. select {
  306. case log := <-it.logs:
  307. it.Event = new({{$contract.Type}}{{.Normalized.Name}})
  308. if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil {
  309. it.fail = err
  310. return false
  311. }
  312. it.Event.Raw = log
  313. return true
  314. case err := <-it.sub.Err():
  315. it.done = true
  316. it.fail = err
  317. return it.Next()
  318. }
  319. }
  320. // Error returns any retrieval or parsing error occurred during filtering.
  321. func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Error() error {
  322. return it.fail
  323. }
  324. // Close terminates the iteration process, releasing any pending underlying
  325. // resources.
  326. func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Close() error {
  327. it.sub.Unsubscribe()
  328. return nil
  329. }
  330. // {{$contract.Type}}{{.Normalized.Name}} represents a {{.Normalized.Name}} event raised by the {{$contract.Type}} contract.
  331. type {{$contract.Type}}{{.Normalized.Name}} struct { {{range .Normalized.Inputs}}
  332. {{capitalise .Name}} {{if .Indexed}}{{bindtopictype .Type}}{{else}}{{bindtype .Type}}{{end}}; {{end}}
  333. Raw types.Log // Blockchain specific contextual infos
  334. }
  335. // Filter{{.Normalized.Name}} is a free log retrieval operation binding the contract event 0x{{printf "%x" .Original.Id}}.
  336. //
  337. // Solidity: {{.Original.String}}
  338. func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Filter{{.Normalized.Name}}(opts *bind.FilterOpts{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}} []{{bindtype .Type}}{{end}}{{end}}) (*{{$contract.Type}}{{.Normalized.Name}}Iterator, error) {
  339. {{range .Normalized.Inputs}}
  340. {{if .Indexed}}var {{.Name}}Rule []interface{}
  341. for _, {{.Name}}Item := range {{.Name}} {
  342. {{.Name}}Rule = append({{.Name}}Rule, {{.Name}}Item)
  343. }{{end}}{{end}}
  344. logs, sub, err := _{{$contract.Type}}.contract.FilterLogs(opts, "{{.Original.Name}}"{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}}Rule{{end}}{{end}})
  345. if err != nil {
  346. return nil, err
  347. }
  348. return &{{$contract.Type}}{{.Normalized.Name}}Iterator{contract: _{{$contract.Type}}.contract, event: "{{.Original.Name}}", logs: logs, sub: sub}, nil
  349. }
  350. // Watch{{.Normalized.Name}} is a free log subscription operation binding the contract event 0x{{printf "%x" .Original.Id}}.
  351. //
  352. // Solidity: {{.Original.String}}
  353. func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Watch{{.Normalized.Name}}(opts *bind.WatchOpts, sink chan<- *{{$contract.Type}}{{.Normalized.Name}}{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}} []{{bindtype .Type}}{{end}}{{end}}) (event.Subscription, error) {
  354. {{range .Normalized.Inputs}}
  355. {{if .Indexed}}var {{.Name}}Rule []interface{}
  356. for _, {{.Name}}Item := range {{.Name}} {
  357. {{.Name}}Rule = append({{.Name}}Rule, {{.Name}}Item)
  358. }{{end}}{{end}}
  359. logs, sub, err := _{{$contract.Type}}.contract.WatchLogs(opts, "{{.Original.Name}}"{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}}Rule{{end}}{{end}})
  360. if err != nil {
  361. return nil, err
  362. }
  363. return event.NewSubscription(func(quit <-chan struct{}) error {
  364. defer sub.Unsubscribe()
  365. for {
  366. select {
  367. case log := <-logs:
  368. // New log arrived, parse the event and forward to the user
  369. event := new({{$contract.Type}}{{.Normalized.Name}})
  370. if err := _{{$contract.Type}}.contract.UnpackLog(event, "{{.Original.Name}}", log); err != nil {
  371. return err
  372. }
  373. event.Raw = log
  374. select {
  375. case sink <- event:
  376. case err := <-sub.Err():
  377. return err
  378. case <-quit:
  379. return nil
  380. }
  381. case err := <-sub.Err():
  382. return err
  383. case <-quit:
  384. return nil
  385. }
  386. }
  387. }), nil
  388. }
  389. {{end}}
  390. {{end}}
  391. `
  392. // tmplSourceJava is the Java source template use to generate the contract binding
  393. // based on.
  394. const tmplSourceJava = `
  395. // This file is an automatically generated Java binding. Do not modify as any
  396. // change will likely be lost upon the next re-generation!
  397. package {{.Package}};
  398. import org.ethereum.geth.*;
  399. {{range $contract := .Contracts}}
  400. public class {{.Type}} {
  401. // ABI is the input ABI used to generate the binding from.
  402. public final static String ABI = "{{.InputABI}}";
  403. {{if .InputBin}}
  404. // BYTECODE is the compiled bytecode used for deploying new contracts.
  405. public final static String BYTECODE = "0x{{.InputBin}}";
  406. // deploy deploys a new Ethereum contract, binding an instance of {{.Type}} to it.
  407. public static {{.Type}} deploy(TransactOpts auth, EthereumClient client{{range .Constructor.Inputs}}, {{bindtype .Type}} {{.Name}}{{end}}) throws Exception {
  408. Interfaces args = Geth.newInterfaces({{(len .Constructor.Inputs)}});
  409. {{range $index, $element := .Constructor.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type) .Type}}({{.Name}});args.set({{$index}},arg{{$index}});
  410. {{end}}
  411. return new {{.Type}}(Geth.deployContract(auth, ABI, Geth.decodeFromHex(BYTECODE), client, args));
  412. }
  413. // Internal constructor used by contract deployment.
  414. private {{.Type}}(BoundContract deployment) {
  415. this.Address = deployment.getAddress();
  416. this.Deployer = deployment.getDeployer();
  417. this.Contract = deployment;
  418. }
  419. {{end}}
  420. // Ethereum address where this contract is located at.
  421. public final Address Address;
  422. // Ethereum transaction in which this contract was deployed (if known!).
  423. public final Transaction Deployer;
  424. // Contract instance bound to a blockchain address.
  425. private final BoundContract Contract;
  426. // Creates a new instance of {{.Type}}, bound to a specific deployed contract.
  427. public {{.Type}}(Address address, EthereumClient client) throws Exception {
  428. this(Geth.bindContract(address, ABI, client));
  429. }
  430. {{range .Calls}}
  431. {{if gt (len .Normalized.Outputs) 1}}
  432. // {{capitalise .Normalized.Name}}Results is the output of a call to {{.Normalized.Name}}.
  433. public class {{capitalise .Normalized.Name}}Results {
  434. {{range $index, $item := .Normalized.Outputs}}public {{bindtype .Type}} {{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}};
  435. {{end}}
  436. }
  437. {{end}}
  438. // {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.Id}}.
  439. //
  440. // Solidity: {{.Original.String}}
  441. 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 {
  442. Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}});
  443. {{range $index, $item := .Normalized.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type) .Type}}({{.Name}});args.set({{$index}},arg{{$index}});
  444. {{end}}
  445. Interfaces results = Geth.newInterfaces({{(len .Normalized.Outputs)}});
  446. {{range $index, $item := .Normalized.Outputs}}Interface result{{$index}} = Geth.newInterface(); result{{$index}}.setDefault{{namedtype (bindtype .Type) .Type}}(); results.set({{$index}}, result{{$index}});
  447. {{end}}
  448. if (opts == null) {
  449. opts = Geth.newCallOpts();
  450. }
  451. this.Contract.call(opts, results, "{{.Original.Name}}", args);
  452. {{if gt (len .Normalized.Outputs) 1}}
  453. {{capitalise .Normalized.Name}}Results result = new {{capitalise .Normalized.Name}}Results();
  454. {{range $index, $item := .Normalized.Outputs}}result.{{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}} = results.get({{$index}}).get{{namedtype (bindtype .Type) .Type}}();
  455. {{end}}
  456. return result;
  457. {{else}}{{range .Normalized.Outputs}}return results.get(0).get{{namedtype (bindtype .Type) .Type}}();{{end}}
  458. {{end}}
  459. }
  460. {{end}}
  461. {{range .Transacts}}
  462. // {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.Id}}.
  463. //
  464. // Solidity: {{.Original.String}}
  465. public Transaction {{.Normalized.Name}}(TransactOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type}} {{.Name}}{{end}}) throws Exception {
  466. Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}});
  467. {{range $index, $item := .Normalized.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type) .Type}}({{.Name}});args.set({{$index}},arg{{$index}});
  468. {{end}}
  469. return this.Contract.transact(opts, "{{.Original.Name}}" , args);
  470. }
  471. {{end}}
  472. }
  473. {{end}}
  474. `