template.go 32 KB

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