template.go 32 KB

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