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