Skip to content

Commit b8ea904

Browse files
accounts/abi: accounts/abi/bind: Move topics to abi package (#21057)
* accounts/abi/bind: added test cases for waitDeployed * accounts/abi/bind: added test case for boundContract * accounts/abi/bind: removed unnecessary resolve methods * accounts/abi: moved topics from /bind to /abi * accounts/abi/bind: cleaned up format... functions * accounts/abi: improved log message * accounts/abi: added type tests * accounts/abi/bind: remove superfluous template methods
1 parent 7b7e592 commit b8ea904

File tree

10 files changed

+230
-142
lines changed

10 files changed

+230
-142
lines changed

accounts/abi/argument.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error) {
275275
retval := make([]interface{}, 0, len(nonIndexedArgs))
276276
virtualArgs := 0
277277
for index, arg := range nonIndexedArgs {
278-
marshalledValue, err := ToGoType((index+virtualArgs)*32, arg.Type, data)
278+
marshalledValue, err := toGoType((index+virtualArgs)*32, arg.Type, data)
279279
if arg.Type.T == ArrayTy && !isDynamicType(arg.Type) {
280280
// If we have a static array, like [3]uint256, these are coded as
281281
// just like uint256,uint256,uint256.
@@ -312,7 +312,7 @@ func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
312312
// Make sure arguments match up and pack them
313313
abiArgs := arguments
314314
if len(args) != len(abiArgs) {
315-
return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(abiArgs))
315+
return nil, fmt.Errorf("argument count mismatch: got %d for %d", len(args), len(abiArgs))
316316
}
317317
// variable input is the output appended at the end of packed
318318
// output. This is used for strings and bytes types input.

accounts/abi/bind/base.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (c *BoundContract) FilterLogs(opts *FilterOpts, name string, query ...[]int
266266
// Append the event selector to the query parameters and construct the topic set
267267
query = append([][]interface{}{{c.abi.Events[name].ID}}, query...)
268268

269-
topics, err := makeTopics(query...)
269+
topics, err := abi.MakeTopics(query...)
270270
if err != nil {
271271
return nil, nil, err
272272
}
@@ -315,7 +315,7 @@ func (c *BoundContract) WatchLogs(opts *WatchOpts, name string, query ...[]inter
315315
// Append the event selector to the query parameters and construct the topic set
316316
query = append([][]interface{}{{c.abi.Events[name].ID}}, query...)
317317

318-
topics, err := makeTopics(query...)
318+
topics, err := abi.MakeTopics(query...)
319319
if err != nil {
320320
return nil, nil, err
321321
}
@@ -349,7 +349,7 @@ func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log)
349349
indexed = append(indexed, arg)
350350
}
351351
}
352-
return parseTopics(out, indexed, log.Topics[1:])
352+
return abi.ParseTopics(out, indexed, log.Topics[1:])
353353
}
354354

355355
// UnpackLogIntoMap unpacks a retrieved log into the provided map.
@@ -365,7 +365,7 @@ func (c *BoundContract) UnpackLogIntoMap(out map[string]interface{}, event strin
365365
indexed = append(indexed, arg)
366366
}
367367
}
368-
return parseTopicsIntoMap(out, indexed, log.Topics[1:])
368+
return abi.ParseTopicsIntoMap(out, indexed, log.Topics[1:])
369369
}
370370

371371
// ensureContext is a helper method to ensure a context is not nil, even if the

accounts/abi/bind/base_test.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ import (
3434
)
3535

3636
type mockCaller struct {
37-
codeAtBlockNumber *big.Int
38-
callContractBlockNumber *big.Int
37+
codeAtBlockNumber *big.Int
38+
callContractBlockNumber *big.Int
39+
pendingCodeAtCalled bool
40+
pendingCallContractCalled bool
3941
}
4042

4143
func (mc *mockCaller) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
@@ -47,6 +49,16 @@ func (mc *mockCaller) CallContract(ctx context.Context, call ethereum.CallMsg, b
4749
mc.callContractBlockNumber = blockNumber
4850
return nil, nil
4951
}
52+
53+
func (mc *mockCaller) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) {
54+
mc.pendingCodeAtCalled = true
55+
return nil, nil
56+
}
57+
58+
func (mc *mockCaller) PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error) {
59+
mc.pendingCallContractCalled = true
60+
return nil, nil
61+
}
5062
func TestPassingBlockNumber(t *testing.T) {
5163

5264
mc := &mockCaller{}
@@ -82,6 +94,16 @@ func TestPassingBlockNumber(t *testing.T) {
8294
if mc.codeAtBlockNumber != nil {
8395
t.Fatalf("CodeAt() was passed a block number when it should not have been")
8496
}
97+
98+
bc.Call(&bind.CallOpts{BlockNumber: blockNumber, Pending: true}, &ret, "something")
99+
100+
if !mc.pendingCallContractCalled {
101+
t.Fatalf("CallContract() was not passed the block number")
102+
}
103+
104+
if !mc.pendingCodeAtCalled {
105+
t.Fatalf("CodeAt() was not passed the block number")
106+
}
85107
}
86108

87109
const hexData = "0x000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158"

accounts/abi/bind/bind.go

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
220220
"bindtype": bindType[lang],
221221
"bindtopictype": bindTopicType[lang],
222222
"namedtype": namedType[lang],
223-
"formatmethod": formatMethod,
224-
"formatevent": formatEvent,
225223
"capitalise": capitalise,
226224
"decapitalise": decapitalise,
227225
}
@@ -537,9 +535,7 @@ var methodNormalizer = map[Lang]func(string) string{
537535
}
538536

539537
// capitalise makes a camel-case string which starts with an upper case character.
540-
func capitalise(input string) string {
541-
return abi.ToCamelCase(input)
542-
}
538+
var capitalise = abi.ToCamelCase
543539

544540
// decapitalise makes a camel-case string which starts with a lower case character.
545541
func decapitalise(input string) string {
@@ -588,74 +584,3 @@ func hasStruct(t abi.Type) bool {
588584
return false
589585
}
590586
}
591-
592-
// resolveArgName converts a raw argument representation into a user friendly format.
593-
func resolveArgName(arg abi.Argument, structs map[string]*tmplStruct) string {
594-
var (
595-
prefix string
596-
embedded string
597-
typ = &arg.Type
598-
)
599-
loop:
600-
for {
601-
switch typ.T {
602-
case abi.SliceTy:
603-
prefix += "[]"
604-
case abi.ArrayTy:
605-
prefix += fmt.Sprintf("[%d]", typ.Size)
606-
default:
607-
embedded = typ.TupleRawName + typ.String()
608-
break loop
609-
}
610-
typ = typ.Elem
611-
}
612-
if s, exist := structs[embedded]; exist {
613-
return prefix + s.Name
614-
} else {
615-
return arg.Type.String()
616-
}
617-
}
618-
619-
// formatMethod transforms raw method representation into a user friendly one.
620-
func formatMethod(method abi.Method, structs map[string]*tmplStruct) string {
621-
inputs := make([]string, len(method.Inputs))
622-
for i, input := range method.Inputs {
623-
inputs[i] = fmt.Sprintf("%v %v", resolveArgName(input, structs), input.Name)
624-
}
625-
outputs := make([]string, len(method.Outputs))
626-
for i, output := range method.Outputs {
627-
outputs[i] = resolveArgName(output, structs)
628-
if len(output.Name) > 0 {
629-
outputs[i] += fmt.Sprintf(" %v", output.Name)
630-
}
631-
}
632-
// Extract meaningful state mutability of solidity method.
633-
// If it's default value, never print it.
634-
state := method.StateMutability
635-
if state == "nonpayable" {
636-
state = ""
637-
}
638-
if state != "" {
639-
state = state + " "
640-
}
641-
identity := fmt.Sprintf("function %v", method.RawName)
642-
if method.Type == abi.Fallback {
643-
identity = "fallback"
644-
} else if method.Type == abi.Receive {
645-
identity = "receive"
646-
}
647-
return fmt.Sprintf("%s(%v) %sreturns(%v)", identity, strings.Join(inputs, ", "), state, strings.Join(outputs, ", "))
648-
}
649-
650-
// formatEvent transforms raw event representation into a user friendly one.
651-
func formatEvent(event abi.Event, structs map[string]*tmplStruct) string {
652-
inputs := make([]string, len(event.Inputs))
653-
for i, input := range event.Inputs {
654-
if input.Indexed {
655-
inputs[i] = fmt.Sprintf("%v indexed %v", resolveArgName(input, structs), input.Name)
656-
} else {
657-
inputs[i] = fmt.Sprintf("%v %v", resolveArgName(input, structs), input.Name)
658-
}
659-
}
660-
return fmt.Sprintf("event %v(%v)", event.RawName, strings.Join(inputs, ", "))
661-
}

accounts/abi/bind/template.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ var (
297297
{{range .Calls}}
298298
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
299299
//
300-
// Solidity: {{formatmethod .Original $structs}}
300+
// Solidity: {{.Original.String}}
301301
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) {
302302
{{if .Structured}}ret := new(struct{
303303
{{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}}
@@ -316,14 +316,14 @@ var (
316316
317317
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
318318
//
319-
// Solidity: {{formatmethod .Original $structs}}
319+
// Solidity: {{.Original.String}}
320320
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) {
321321
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}})
322322
}
323323
324324
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
325325
//
326-
// Solidity: {{formatmethod .Original $structs}}
326+
// Solidity: {{.Original.String}}
327327
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) {
328328
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}})
329329
}
@@ -332,21 +332,21 @@ var (
332332
{{range .Transacts}}
333333
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
334334
//
335-
// Solidity: {{formatmethod .Original $structs}}
335+
// Solidity: {{.Original.String}}
336336
func (_{{$contract.Type}} *{{$contract.Type}}Transactor) {{.Normalized.Name}}(opts *bind.TransactOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type $structs}} {{end}}) (*types.Transaction, error) {
337337
return _{{$contract.Type}}.contract.Transact(opts, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
338338
}
339339
340340
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
341341
//
342-
// Solidity: {{formatmethod .Original $structs}}
342+
// Solidity: {{.Original.String}}
343343
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) {
344344
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}})
345345
}
346346
347347
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
348348
//
349-
// Solidity: {{formatmethod .Original $structs}}
349+
// Solidity: {{.Original.String}}
350350
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) {
351351
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}})
352352
}
@@ -355,21 +355,21 @@ var (
355355
{{if .Fallback}}
356356
// Fallback is a paid mutator transaction binding the contract fallback function.
357357
//
358-
// Solidity: {{formatmethod .Fallback.Original $structs}}
358+
// Solidity: {{.Fallback.Original.String}}
359359
func (_{{$contract.Type}} *{{$contract.Type}}Transactor) Fallback(opts *bind.TransactOpts, calldata []byte) (*types.Transaction, error) {
360360
return _{{$contract.Type}}.contract.RawTransact(opts, calldata)
361361
}
362362
363363
// Fallback is a paid mutator transaction binding the contract fallback function.
364364
//
365-
// Solidity: {{formatmethod .Fallback.Original $structs}}
365+
// Solidity: {{.Fallback.Original.String}}
366366
func (_{{$contract.Type}} *{{$contract.Type}}Session) Fallback(calldata []byte) (*types.Transaction, error) {
367367
return _{{$contract.Type}}.Contract.Fallback(&_{{$contract.Type}}.TransactOpts, calldata)
368368
}
369369
370370
// Fallback is a paid mutator transaction binding the contract fallback function.
371371
//
372-
// Solidity: {{formatmethod .Fallback.Original $structs}}
372+
// Solidity: {{.Fallback.Original.String}}
373373
func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) Fallback(calldata []byte) (*types.Transaction, error) {
374374
return _{{$contract.Type}}.Contract.Fallback(&_{{$contract.Type}}.TransactOpts, calldata)
375375
}
@@ -378,21 +378,21 @@ var (
378378
{{if .Receive}}
379379
// Receive is a paid mutator transaction binding the contract receive function.
380380
//
381-
// Solidity: {{formatmethod .Receive.Original $structs}}
381+
// Solidity: {{.Receive.Original.String}}
382382
func (_{{$contract.Type}} *{{$contract.Type}}Transactor) Receive(opts *bind.TransactOpts) (*types.Transaction, error) {
383383
return _{{$contract.Type}}.contract.RawTransact(opts, nil) // calldata is disallowed for receive function
384384
}
385385
386386
// Receive is a paid mutator transaction binding the contract receive function.
387387
//
388-
// Solidity: {{formatmethod .Receive.Original $structs}}
388+
// Solidity: {{.Receive.Original.String}}
389389
func (_{{$contract.Type}} *{{$contract.Type}}Session) Receive() (*types.Transaction, error) {
390390
return _{{$contract.Type}}.Contract.Receive(&_{{$contract.Type}}.TransactOpts)
391391
}
392392
393393
// Receive is a paid mutator transaction binding the contract receive function.
394394
//
395-
// Solidity: {{formatmethod .Receive.Original $structs}}
395+
// Solidity: {{.Receive.Original.String}}
396396
func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) Receive() (*types.Transaction, error) {
397397
return _{{$contract.Type}}.Contract.Receive(&_{{$contract.Type}}.TransactOpts)
398398
}
@@ -471,7 +471,7 @@ var (
471471
472472
// Filter{{.Normalized.Name}} is a free log retrieval operation binding the contract event 0x{{printf "%x" .Original.ID}}.
473473
//
474-
// Solidity: {{formatevent .Original $structs}}
474+
// Solidity: {{.Original.String}}
475475
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) {
476476
{{range .Normalized.Inputs}}
477477
{{if .Indexed}}var {{.Name}}Rule []interface{}
@@ -488,7 +488,7 @@ var (
488488
489489
// Watch{{.Normalized.Name}} is a free log subscription operation binding the contract event 0x{{printf "%x" .Original.ID}}.
490490
//
491-
// Solidity: {{formatevent .Original $structs}}
491+
// Solidity: {{.Original.String}}
492492
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) {
493493
{{range .Normalized.Inputs}}
494494
{{if .Indexed}}var {{.Name}}Rule []interface{}
@@ -530,7 +530,7 @@ var (
530530
531531
// Parse{{.Normalized.Name}} is a log parse operation binding the contract event 0x{{printf "%x" .Original.ID}}.
532532
//
533-
// Solidity: {{formatevent .Original $structs}}
533+
// Solidity: {{.Original.String}}
534534
func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Parse{{.Normalized.Name}}(log types.Log) (*{{$contract.Type}}{{.Normalized.Name}}, error) {
535535
event := new({{$contract.Type}}{{.Normalized.Name}})
536536
if err := _{{$contract.Type}}.contract.UnpackLog(event, "{{.Original.Name}}", log); err != nil {
@@ -662,7 +662,7 @@ import java.util.*;
662662
{{if .Fallback}}
663663
// Fallback is a paid mutator transaction binding the contract fallback function.
664664
//
665-
// Solidity: {{formatmethod .Fallback.Original $structs}}
665+
// Solidity: {{.Fallback.Original.String}}
666666
public Transaction Fallback(TransactOpts opts, byte[] calldata) throws Exception {
667667
return this.Contract.rawTransact(opts, calldata);
668668
}
@@ -671,7 +671,7 @@ import java.util.*;
671671
{{if .Receive}}
672672
// Receive is a paid mutator transaction binding the contract receive function.
673673
//
674-
// Solidity: {{formatmethod .Receive.Original $structs}}
674+
// Solidity: {{.Receive.Original.String}}
675675
public Transaction Receive(TransactOpts opts) throws Exception {
676676
return this.Contract.rawTransact(opts, null);
677677
}

accounts/abi/bind/util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package bind
1818

1919
import (
2020
"context"
21-
"fmt"
21+
"errors"
2222
"time"
2323

2424
"github.com/ethereum/go-ethereum/common"
@@ -56,14 +56,14 @@ func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*ty
5656
// contract address when it is mined. It stops waiting when ctx is canceled.
5757
func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) {
5858
if tx.To() != nil {
59-
return common.Address{}, fmt.Errorf("tx is not contract creation")
59+
return common.Address{}, errors.New("tx is not contract creation")
6060
}
6161
receipt, err := WaitMined(ctx, b, tx)
6262
if err != nil {
6363
return common.Address{}, err
6464
}
6565
if receipt.ContractAddress == (common.Address{}) {
66-
return common.Address{}, fmt.Errorf("zero address")
66+
return common.Address{}, errors.New("zero address")
6767
}
6868
// Check that code has indeed been deployed at the address.
6969
// This matters on pre-Homestead chains: OOG in the constructor

0 commit comments

Comments
 (0)