github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/accounts/abi/bind/template.go (about) 1 package bind 2 3 import "github.com/neatio-net/neatio/chain/accounts/abi" 4 5 type tmplData struct { 6 Package string 7 Contracts map[string]*tmplContract 8 Libraries map[string]string 9 Structs map[string]*tmplStruct 10 } 11 12 type tmplContract struct { 13 Type string 14 InputABI string 15 InputBin string 16 FuncSigs map[string]string 17 Constructor abi.Method 18 Calls map[string]*tmplMethod 19 Transacts map[string]*tmplMethod 20 Events map[string]*tmplEvent 21 Libraries map[string]string 22 Library bool 23 } 24 25 type tmplMethod struct { 26 Original abi.Method 27 Normalized abi.Method 28 Structured bool 29 } 30 31 type tmplEvent struct { 32 Original abi.Event 33 Normalized abi.Event 34 } 35 36 type tmplField struct { 37 Type string 38 Name string 39 SolKind abi.Type 40 } 41 42 type tmplStruct struct { 43 Name string 44 Fields []*tmplField 45 } 46 47 var tmplSource = map[Lang]string{ 48 LangGo: tmplSourceGo, 49 LangJava: tmplSourceJava, 50 } 51 52 const tmplSourceGo = ` 53 54 55 56 package {{.Package}} 57 58 import ( 59 "math/big" 60 "strings" 61 62 ethereum "github.com/neatio-net/neatio" 63 "github.com/neatio-net/neatio/chain/accounts/abi" 64 "github.com/neatio-net/neatio/chain/accounts/abi/bind" 65 "github.com/neatio-net/neatio/utilities/common" 66 "github.com/neatio-net/neatio/chain/core/types" 67 "github.com/neatio-net/neatio/utilities/event" 68 ) 69 70 71 var ( 72 _ = big.NewInt 73 _ = strings.NewReader 74 _ = neatio.NotFound 75 _ = abi.U256 76 _ = bind.Bind 77 _ = common.Big1 78 _ = types.BloomLookup 79 _ = event.NewSubscription 80 ) 81 82 {{$structs := .Structs}} 83 {{range $structs}} 84 85 type {{.Name}} struct { 86 {{range $field := .Fields}} 87 {{$field.Name}} {{$field.Type}}{{end}} 88 } 89 {{end}} 90 91 {{range $contract := .Contracts}} 92 93 const {{.Type}}ABI = "{{.InputABI}}" 94 95 {{if $contract.FuncSigs}} 96 97 var {{.Type}}FuncSigs = map[string]string{ 98 {{range $strsig, $binsig := .FuncSigs}}"{{$binsig}}": "{{$strsig}}", 99 {{end}} 100 } 101 {{end}} 102 103 {{if .InputBin}} 104 105 var {{.Type}}Bin = "0x{{.InputBin}}" 106 107 108 func Deploy{{.Type}}(auth *bind.TransactOpts, backend bind.ContractBackend {{range .Constructor.Inputs}}, {{.Name}} {{bindtype .Type $structs}}{{end}}) (common.Address, *types.Transaction, *{{.Type}}, error) { 109 parsed, err := abi.JSON(strings.NewReader({{.Type}}ABI)) 110 if err != nil { 111 return common.Address{}, nil, nil, err 112 } 113 {{range $pattern, $name := .Libraries}} 114 {{decapitalise $name}}Addr, _, _, _ := Deploy{{capitalise $name}}(auth, backend) 115 {{$contract.Type}}Bin = strings.Replace({{$contract.Type}}Bin, "__${{$pattern}}$__", {{decapitalise $name}}Addr.String()[2:], -1) 116 {{end}} 117 address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex({{.Type}}Bin), backend {{range .Constructor.Inputs}}, {{.Name}}{{end}}) 118 if err != nil { 119 return common.Address{}, nil, nil, err 120 } 121 return address, tx, &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract}, {{.Type}}Filterer: {{.Type}}Filterer{contract: contract} }, nil 122 } 123 {{end}} 124 125 126 type {{.Type}} struct { 127 {{.Type}}Caller 128 {{.Type}}Transactor 129 {{.Type}}Filterer 130 } 131 132 133 type {{.Type}}Caller struct { 134 contract *bind.BoundContract 135 } 136 137 138 type {{.Type}}Transactor struct { 139 contract *bind.BoundContract 140 } 141 142 143 type {{.Type}}Filterer struct { 144 contract *bind.BoundContract 145 } 146 147 148 149 type {{.Type}}Session struct { 150 Contract *{{.Type}} 151 CallOpts bind.CallOpts 152 TransactOpts bind.TransactOpts 153 } 154 155 156 157 type {{.Type}}CallerSession struct { 158 Contract *{{.Type}}Caller 159 CallOpts bind.CallOpts 160 } 161 162 163 164 type {{.Type}}TransactorSession struct { 165 Contract *{{.Type}}Transactor 166 TransactOpts bind.TransactOpts 167 } 168 169 170 type {{.Type}}Raw struct { 171 Contract *{{.Type}} 172 } 173 174 175 type {{.Type}}CallerRaw struct { 176 Contract *{{.Type}}Caller 177 } 178 179 180 type {{.Type}}TransactorRaw struct { 181 Contract *{{.Type}}Transactor 182 } 183 184 185 func New{{.Type}}(address common.Address, backend bind.ContractBackend) (*{{.Type}}, error) { 186 contract, err := bind{{.Type}}(address, backend, backend, backend) 187 if err != nil { 188 return nil, err 189 } 190 return &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract}, {{.Type}}Filterer: {{.Type}}Filterer{contract: contract} }, nil 191 } 192 193 194 func New{{.Type}}Caller(address common.Address, caller bind.ContractCaller) (*{{.Type}}Caller, error) { 195 contract, err := bind{{.Type}}(address, caller, nil, nil) 196 if err != nil { 197 return nil, err 198 } 199 return &{{.Type}}Caller{contract: contract}, nil 200 } 201 202 203 func New{{.Type}}Transactor(address common.Address, transactor bind.ContractTransactor) (*{{.Type}}Transactor, error) { 204 contract, err := bind{{.Type}}(address, nil, transactor, nil) 205 if err != nil { 206 return nil, err 207 } 208 return &{{.Type}}Transactor{contract: contract}, nil 209 } 210 211 212 func New{{.Type}}Filterer(address common.Address, filterer bind.ContractFilterer) (*{{.Type}}Filterer, error) { 213 contract, err := bind{{.Type}}(address, nil, nil, filterer) 214 if err != nil { 215 return nil, err 216 } 217 return &{{.Type}}Filterer{contract: contract}, nil 218 } 219 220 221 func bind{{.Type}}(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) { 222 parsed, err := abi.JSON(strings.NewReader({{.Type}}ABI)) 223 if err != nil { 224 return nil, err 225 } 226 return bind.NewBoundContract(address, parsed, caller, transactor, filterer), nil 227 } 228 229 230 231 232 233 func (_{{$contract.Type}} *{{$contract.Type}}Raw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error { 234 return _{{$contract.Type}}.Contract.{{$contract.Type}}Caller.contract.Call(opts, result, method, params...) 235 } 236 237 238 239 func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { 240 return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transfer(opts) 241 } 242 243 244 func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { 245 return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transact(opts, method, params...) 246 } 247 248 249 250 251 252 func (_{{$contract.Type}} *{{$contract.Type}}CallerRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error { 253 return _{{$contract.Type}}.Contract.contract.Call(opts, result, method, params...) 254 } 255 256 257 258 func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { 259 return _{{$contract.Type}}.Contract.contract.Transfer(opts) 260 } 261 262 263 func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { 264 return _{{$contract.Type}}.Contract.contract.Transact(opts, method, params...) 265 } 266 267 {{range .Calls}} 268 269 270 271 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) { 272 {{if .Structured}}ret := new(struct{ 273 {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}} 274 {{end}} 275 }){{else}}var ( 276 {{range $i, $_ := .Normalized.Outputs}}ret{{$i}} = new({{bindtype .Type $structs}}) 277 {{end}} 278 ){{end}} 279 out := {{if .Structured}}ret{{else}}{{if eq (len .Normalized.Outputs) 1}}ret0{{else}}&[]interface{}{ 280 {{range $i, $_ := .Normalized.Outputs}}ret{{$i}}, 281 {{end}} 282 }{{end}}{{end}} 283 err := _{{$contract.Type}}.contract.Call(opts, out, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}}) 284 return {{if .Structured}}*ret,{{else}}{{range $i, $_ := .Normalized.Outputs}}*ret{{$i}},{{end}}{{end}} err 285 } 286 287 288 289 290 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) { 291 return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}}) 292 } 293 294 295 296 297 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) { 298 return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}}) 299 } 300 {{end}} 301 302 {{range .Transacts}} 303 304 305 306 func (_{{$contract.Type}} *{{$contract.Type}}Transactor) {{.Normalized.Name}}(opts *bind.TransactOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type $structs}} {{end}}) (*types.Transaction, error) { 307 return _{{$contract.Type}}.contract.Transact(opts, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}}) 308 } 309 310 311 312 313 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) { 314 return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}}) 315 } 316 317 318 319 320 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) { 321 return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}}) 322 } 323 {{end}} 324 325 {{range .Events}} 326 327 type {{$contract.Type}}{{.Normalized.Name}}Iterator struct { 328 Event *{{$contract.Type}}{{.Normalized.Name}} 329 330 contract *bind.BoundContract 331 event string 332 333 logs chan types.Log 334 sub neatio.Subscription 335 done bool 336 fail error 337 } 338 339 340 341 func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Next() bool { 342 343 if (it.fail != nil) { 344 return false 345 } 346 347 if (it.done) { 348 select { 349 case log := <-it.logs: 350 it.Event = new({{$contract.Type}}{{.Normalized.Name}}) 351 if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { 352 it.fail = err 353 return false 354 } 355 it.Event.Raw = log 356 return true 357 358 default: 359 return false 360 } 361 } 362 363 select { 364 case log := <-it.logs: 365 it.Event = new({{$contract.Type}}{{.Normalized.Name}}) 366 if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { 367 it.fail = err 368 return false 369 } 370 it.Event.Raw = log 371 return true 372 373 case err := <-it.sub.Err(): 374 it.done = true 375 it.fail = err 376 return it.Next() 377 } 378 } 379 380 func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Error() error { 381 return it.fail 382 } 383 384 385 func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Close() error { 386 it.sub.Unsubscribe() 387 return nil 388 } 389 390 391 type {{$contract.Type}}{{.Normalized.Name}} struct { {{range .Normalized.Inputs}} 392 {{capitalise .Name}} {{if .Indexed}}{{bindtopictype .Type $structs}}{{else}}{{bindtype .Type $structs}}{{end}}; {{end}} 393 Raw types.Log 394 } 395 396 397 398 399 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) { 400 {{range .Normalized.Inputs}} 401 {{if .Indexed}}var {{.Name}}Rule []interface{} 402 for _, {{.Name}}Item := range {{.Name}} { 403 {{.Name}}Rule = append({{.Name}}Rule, {{.Name}}Item) 404 }{{end}}{{end}} 405 406 logs, sub, err := _{{$contract.Type}}.contract.FilterLogs(opts, "{{.Original.Name}}"{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}}Rule{{end}}{{end}}) 407 if err != nil { 408 return nil, err 409 } 410 return &{{$contract.Type}}{{.Normalized.Name}}Iterator{contract: _{{$contract.Type}}.contract, event: "{{.Original.Name}}", logs: logs, sub: sub}, nil 411 } 412 413 414 415 416 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) { 417 {{range .Normalized.Inputs}} 418 {{if .Indexed}}var {{.Name}}Rule []interface{} 419 for _, {{.Name}}Item := range {{.Name}} { 420 {{.Name}}Rule = append({{.Name}}Rule, {{.Name}}Item) 421 }{{end}}{{end}} 422 423 logs, sub, err := _{{$contract.Type}}.contract.WatchLogs(opts, "{{.Original.Name}}"{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}}Rule{{end}}{{end}}) 424 if err != nil { 425 return nil, err 426 } 427 return event.NewSubscription(func(quit <-chan struct{}) error { 428 defer sub.Unsubscribe() 429 for { 430 select { 431 case log := <-logs: 432 433 event := new({{$contract.Type}}{{.Normalized.Name}}) 434 if err := _{{$contract.Type}}.contract.UnpackLog(event, "{{.Original.Name}}", log); err != nil { 435 return err 436 } 437 event.Raw = log 438 439 select { 440 case sink <- event: 441 case err := <-sub.Err(): 442 return err 443 case <-quit: 444 return nil 445 } 446 case err := <-sub.Err(): 447 return err 448 case <-quit: 449 return nil 450 } 451 } 452 }), nil 453 } 454 455 456 457 458 func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Parse{{.Normalized.Name}}(log types.Log) (*{{$contract.Type}}{{.Normalized.Name}}, error) { 459 event := new({{$contract.Type}}{{.Normalized.Name}}) 460 if err := _{{$contract.Type}}.contract.UnpackLog(event, "{{.Original.Name}}", log); err != nil { 461 return nil, err 462 } 463 return event, nil 464 } 465 466 {{end}} 467 {{end}} 468 ` 469 470 const tmplSourceJava = ` 471 472 473 474 package {{.Package}}; 475 476 import org.neatio.neatio.*; 477 import java.util.*; 478 479 {{$structs := .Structs}} 480 {{range $contract := .Contracts}} 481 {{if not .Library}}public {{end}}class {{.Type}} { 482 483 public final static String ABI = "{{.InputABI}}"; 484 {{if $contract.FuncSigs}} 485 486 public final static Map<String, String> {{.Type}}FuncSigs; 487 static { 488 Hashtable<String, String> temp = new Hashtable<String, String>(); 489 {{range $strsig, $binsig := .FuncSigs}}temp.put("{{$binsig}}", "{{$strsig}}"); 490 {{end}} 491 {{.Type}}FuncSigs = Collections.unmodifiableMap(temp); 492 } 493 {{end}} 494 {{if .InputBin}} 495 496 public final static String BYTECODE = "0x{{.InputBin}}"; 497 498 499 public static {{.Type}} deploy(TransactOpts auth, EthereumClient client{{range .Constructor.Inputs}}, {{bindtype .Type $structs}} {{.Name}}{{end}}) throws Exception { 500 Interfaces args = Geth.newInterfaces({{(len .Constructor.Inputs)}}); 501 String bytecode = BYTECODE; 502 {{if .Libraries}} 503 504 505 {{range $pattern, $name := .Libraries}} 506 {{capitalise $name}} {{decapitalise $name}}Inst = {{capitalise $name}}.deploy(auth, client); 507 bytecode = bytecode.replace("__${{$pattern}}$__", {{decapitalise $name}}Inst.Address.getHex().substring(2)); 508 {{end}} 509 {{end}} 510 {{range $index, $element := .Constructor.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type $structs) .Type}}({{.Name}});args.set({{$index}},arg{{$index}}); 511 {{end}} 512 return new {{.Type}}(Geth.deployContract(auth, ABI, Geth.decodeFromHex(bytecode), client, args)); 513 } 514 515 516 private {{.Type}}(BoundContract deployment) { 517 this.Address = deployment.getAddress(); 518 this.Deployer = deployment.getDeployer(); 519 this.Contract = deployment; 520 } 521 {{end}} 522 523 524 public final Address Address; 525 526 527 public final Transaction Deployer; 528 529 530 private final BoundContract Contract; 531 532 533 public {{.Type}}(Address address, EthereumClient client) throws Exception { 534 this(Geth.bindContract(address, ABI, client)); 535 } 536 537 {{range .Calls}} 538 {{if gt (len .Normalized.Outputs) 1}} 539 540 public class {{capitalise .Normalized.Name}}Results { 541 {{range $index, $item := .Normalized.Outputs}}public {{bindtype .Type $structs}} {{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}}; 542 {{end}} 543 } 544 {{end}} 545 546 547 548 549 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 { 550 Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}}); 551 {{range $index, $item := .Normalized.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type $structs) .Type}}({{.Name}});args.set({{$index}},arg{{$index}}); 552 {{end}} 553 554 Interfaces results = Geth.newInterfaces({{(len .Normalized.Outputs)}}); 555 {{range $index, $item := .Normalized.Outputs}}Interface result{{$index}} = Geth.newInterface(); result{{$index}}.setDefault{{namedtype (bindtype .Type $structs) .Type}}(); results.set({{$index}}, result{{$index}}); 556 {{end}} 557 558 if (opts == null) { 559 opts = Geth.newCallOpts(); 560 } 561 this.Contract.call(opts, results, "{{.Original.Name}}", args); 562 {{if gt (len .Normalized.Outputs) 1}} 563 {{capitalise .Normalized.Name}}Results result = new {{capitalise .Normalized.Name}}Results(); 564 {{range $index, $item := .Normalized.Outputs}}result.{{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}} = results.get({{$index}}).get{{namedtype (bindtype .Type $structs) .Type}}(); 565 {{end}} 566 return result; 567 {{else}}{{range .Normalized.Outputs}}return results.get(0).get{{namedtype (bindtype .Type $structs) .Type}}();{{end}} 568 {{end}} 569 } 570 {{end}} 571 572 {{range .Transacts}} 573 574 575 576 public Transaction {{.Normalized.Name}}(TransactOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type $structs}} {{.Name}}{{end}}) throws Exception { 577 Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}}); 578 {{range $index, $item := .Normalized.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type $structs) .Type}}({{.Name}});args.set({{$index}},arg{{$index}}); 579 {{end}} 580 return this.Contract.transact(opts, "{{.Original.Name}}" , args); 581 } 582 {{end}} 583 } 584 {{end}} 585 `