Skip to content

Commit bd5579a

Browse files
authored
Fix nil types tmpl (#751)
* add more nil types and fix int types * readd hash type * remove convertToNil
1 parent 19bef00 commit bd5579a

File tree

2 files changed

+10
-27
lines changed

2 files changed

+10
-27
lines changed

accounts/abi/bind/bind.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ func BindHelper(types []string, abis []string, bytecodes []string, fsigs []map[s
310310
"namedtype": namedType[lang],
311311
"capitalise": capitalise,
312312
"decapitalise": decapitalise,
313-
"convertToNil": convertToNil,
314313
"mkList": mkList,
315314
}
316315

@@ -646,24 +645,6 @@ func decapitalise(input string) string {
646645
return strings.ToLower(goForm[:1]) + goForm[1:]
647646
}
648647

649-
// convertToNil converts any type to its proper nil form.
650-
func convertToNil(input abi.Type) string {
651-
switch input.T {
652-
case abi.IntTy, abi.UintTy:
653-
return "big.NewInt(0)"
654-
case abi.StringTy:
655-
return "\"\""
656-
case abi.BoolTy:
657-
return "false"
658-
case abi.AddressTy:
659-
return "common.Address{}"
660-
case abi.HashTy:
661-
return "common.Hash{}"
662-
default:
663-
return "nil"
664-
}
665-
}
666-
667648
// structured checks whether a list of ABI data types has enough information to
668649
// operate through a proper Go struct or if flat returns are needed.
669650
func structured(args abi.Arguments) bool {

accounts/abi/bind/precompilebind/precompile_contract_template.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,22 @@ func Pack{{.Normalized.Name}}(inputStruct {{capitalise .Normalized.Name}}Input)
153153
{{else if len .Normalized.Inputs | eq 1 }}
154154
{{$method := .}}
155155
{{$input := index $method.Normalized.Inputs 0}}
156-
// Unpack{{capitalise .Normalized.Name}}Input attempts to unpack [input] into the {{bindtype $input.Type $structs}} type argument
156+
{{$bindedType := bindtype $input.Type $structs}}
157+
// Unpack{{capitalise .Normalized.Name}}Input attempts to unpack [input] into the {{$bindedType}} type argument
157158
// assumes that [input] does not include selector (omits first 4 func signature bytes)
158-
func Unpack{{capitalise .Normalized.Name}}Input(input []byte)({{bindtype $input.Type $structs}}, error) {
159+
func Unpack{{capitalise .Normalized.Name}}Input(input []byte)({{$bindedType}}, error) {
159160
res, err := {{$contract.Type}}ABI.UnpackInput("{{$method.Original.Name}}", input)
160161
if err != nil {
161-
return {{convertToNil $input.Type}}, err
162+
return *new({{$bindedType}}), err
162163
}
163-
unpacked := *abi.ConvertType(res[0], new({{bindtype $input.Type $structs}})).(*{{bindtype $input.Type $structs}})
164+
unpacked := *abi.ConvertType(res[0], new({{$bindedType}})).(*{{$bindedType}})
164165
return unpacked, nil
165166
}
166167
167-
// Pack{{.Normalized.Name}} packs [{{decapitalise $input.Name}}] of type {{bindtype $input.Type $structs}} into the appropriate arguments for {{$method.Original.Name}}.
168+
// Pack{{.Normalized.Name}} packs [{{decapitalise $input.Name}}] of type {{$bindedType}} into the appropriate arguments for {{$method.Original.Name}}.
168169
// the packed bytes include selector (first 4 func signature bytes).
169170
// This function is mostly used for tests.
170-
func Pack{{$method.Normalized.Name}}( {{decapitalise $input.Name}} {{bindtype $input.Type $structs}},) ([]byte, error) {
171+
func Pack{{$method.Normalized.Name}}( {{decapitalise $input.Name}} {{$bindedType}},) ([]byte, error) {
171172
return {{$contract.Type}}ABI.Pack("{{$method.Original.Name}}", {{decapitalise $input.Name}},)
172173
}
173174
{{else}}
@@ -192,9 +193,10 @@ func Pack{{capitalise .Normalized.Name}}Output (outputStruct {{capitalise .Norma
192193
{{else if len .Normalized.Outputs | eq 1 }}
193194
{{$method := .}}
194195
{{$output := index $method.Normalized.Outputs 0}}
195-
// Pack{{capitalise .Normalized.Name}}Output attempts to pack given {{decapitalise $output.Name}} of type {{bindtype $output.Type $structs}}
196+
{{$bindedType := bindtype $output.Type $structs}}
197+
// Pack{{capitalise .Normalized.Name}}Output attempts to pack given {{decapitalise $output.Name}} of type {{$bindedType}}
196198
// to conform the ABI outputs.
197-
func Pack{{$method.Normalized.Name}}Output ({{decapitalise $output.Name}} {{bindtype $output.Type $structs}}) ([]byte, error) {
199+
func Pack{{$method.Normalized.Name}}Output ({{decapitalise $output.Name}} {{$bindedType}}) ([]byte, error) {
198200
return {{$contract.Type}}ABI.PackOutput("{{$method.Original.Name}}", {{decapitalise $output.Name}})
199201
}
200202
{{end}}

0 commit comments

Comments
 (0)