template.go 29 KB

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