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