template.go 32 KB

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