Skip to content

Commit bc67e7d

Browse files
mask-ppjwasinger
andauthored
accounts/abi: generate TryPack* methods for abigen v2 bindings (#31692)
1. Fix the error return format. **todo**: ~~`bindtype` needs more complex logic to fix it.~~ ` if err != nil { return nil, err } if err == nil { return obj, nil } ` 2. ~~Return pointer type object to avoid copying the whole struct content.~~ 3. Give the panic decision to the user. 4. Fix empty line at the end of function. **TODO**: ~~fix some related test cases.~~ --------- Co-authored-by: Jared Wasinger <j-wasinger@hotmail.com>
1 parent 3fb6499 commit bc67e7d

26 files changed

+1186
-197
lines changed

accounts/abi/abigen/bindv2_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func TestBindingV2ConvertedV1Tests(t *testing.T) {
281281
}
282282
// Set this environment variable to regenerate the test outputs.
283283
if os.Getenv("WRITE_TEST_FILES") != "" {
284-
if err := os.WriteFile((fname), []byte(have), 0666); err != nil {
284+
if err := os.WriteFile(fname, []byte(have), 0666); err != nil {
285285
t.Fatalf("err writing expected output to file: %v\n", err)
286286
}
287287
}

accounts/abi/abigen/source2.go.tpl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ var (
9090

9191
{{range .Calls}}
9292
// Pack{{.Normalized.Name}} is the Go binding used to pack the parameters required for calling
93-
// the contract method with ID 0x{{printf "%x" .Original.ID}}.
93+
// the contract method with ID 0x{{printf "%x" .Original.ID}}. This method will panic if any
94+
// invalid/nil inputs are passed.
9495
//
9596
// Solidity: {{.Original.String}}
9697
func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) Pack{{.Normalized.Name}}({{range .Normalized.Inputs}} {{.Name}} {{bindtype .Type $structs}}, {{end}}) []byte {
@@ -101,6 +102,15 @@ var (
101102
return enc
102103
}
103104

105+
// TryPack{{.Normalized.Name}} is the Go binding used to pack the parameters required for calling
106+
// the contract method with ID 0x{{printf "%x" .Original.ID}}. This method will return an error
107+
// if any inputs are invalid/nil.
108+
//
109+
// Solidity: {{.Original.String}}
110+
func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) TryPack{{.Normalized.Name}}({{range .Normalized.Inputs}} {{.Name}} {{bindtype .Type $structs}}, {{end}}) ([]byte, error) {
111+
return {{ decapitalise $contract.Type}}.abi.Pack("{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
112+
}
113+
104114
{{/* Unpack method is needed only when there are return args */}}
105115
{{if .Normalized.Outputs }}
106116
{{ if .Structured }}
@@ -133,8 +143,7 @@ var (
133143
outstruct.{{capitalise .Name}} = *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}})
134144
{{- end }}
135145
{{- end }}
136-
return *outstruct, err
137-
{{else}}
146+
return *outstruct, nil{{else}}
138147
if err != nil {
139148
return {{range $i, $_ := .Normalized.Outputs}}{{if ispointertype .Type}}new({{underlyingbindtype .Type }}), {{else}}*new({{bindtype .Type $structs}}), {{end}}{{end}} err
140149
}
@@ -145,8 +154,8 @@ var (
145154
out{{$i}} := *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}})
146155
{{- end }}
147156
{{- end}}
148-
return {{range $i, $t := .Normalized.Outputs}}out{{$i}}, {{end}} err
149-
{{- end}}
157+
return {{range $i, $t := .Normalized.Outputs}}out{{$i}}, {{end}} nil
158+
{{- end}}
150159
}
151160
{{end}}
152161
{{end}}

accounts/abi/abigen/testdata/v2/callbackparam.go.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ func (c *CallbackParam) Instance(backend bind.ContractBackend, addr common.Addre
5252
}
5353

5454
// PackTest is the Go binding used to pack the parameters required for calling
55-
// the contract method with ID 0xd7a5aba2.
55+
// the contract method with ID 0xd7a5aba2. This method will panic if any
56+
// invalid/nil inputs are passed.
5657
//
5758
// Solidity: function test(function callback) returns()
5859
func (callbackParam *CallbackParam) PackTest(callback [24]byte) []byte {
@@ -62,3 +63,12 @@ func (callbackParam *CallbackParam) PackTest(callback [24]byte) []byte {
6263
}
6364
return enc
6465
}
66+
67+
// TryPackTest is the Go binding used to pack the parameters required for calling
68+
// the contract method with ID 0xd7a5aba2. This method will return an error
69+
// if any inputs are invalid/nil.
70+
//
71+
// Solidity: function test(function callback) returns()
72+
func (callbackParam *CallbackParam) TryPackTest(callback [24]byte) ([]byte, error) {
73+
return callbackParam.abi.Pack("test", callback)
74+
}

accounts/abi/abigen/testdata/v2/crowdsale.go.txt

Lines changed: 95 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ func (crowdsale *Crowdsale) PackConstructor(ifSuccessfulSendTo common.Address, f
6464
}
6565

6666
// PackAmountRaised is the Go binding used to pack the parameters required for calling
67-
// the contract method with ID 0x7b3e5e7b.
67+
// the contract method with ID 0x7b3e5e7b. This method will panic if any
68+
// invalid/nil inputs are passed.
6869
//
6970
// Solidity: function amountRaised() returns(uint256)
7071
func (crowdsale *Crowdsale) PackAmountRaised() []byte {
@@ -75,6 +76,15 @@ func (crowdsale *Crowdsale) PackAmountRaised() []byte {
7576
return enc
7677
}
7778

79+
// TryPackAmountRaised is the Go binding used to pack the parameters required for calling
80+
// the contract method with ID 0x7b3e5e7b. This method will return an error
81+
// if any inputs are invalid/nil.
82+
//
83+
// Solidity: function amountRaised() returns(uint256)
84+
func (crowdsale *Crowdsale) TryPackAmountRaised() ([]byte, error) {
85+
return crowdsale.abi.Pack("amountRaised")
86+
}
87+
7888
// UnpackAmountRaised is the Go binding that unpacks the parameters returned
7989
// from invoking the contract method with ID 0x7b3e5e7b.
8090
//
@@ -85,11 +95,12 @@ func (crowdsale *Crowdsale) UnpackAmountRaised(data []byte) (*big.Int, error) {
8595
return new(big.Int), err
8696
}
8797
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
88-
return out0, err
98+
return out0, nil
8999
}
90100

91101
// PackBeneficiary is the Go binding used to pack the parameters required for calling
92-
// the contract method with ID 0x38af3eed.
102+
// the contract method with ID 0x38af3eed. This method will panic if any
103+
// invalid/nil inputs are passed.
93104
//
94105
// Solidity: function beneficiary() returns(address)
95106
func (crowdsale *Crowdsale) PackBeneficiary() []byte {
@@ -100,6 +111,15 @@ func (crowdsale *Crowdsale) PackBeneficiary() []byte {
100111
return enc
101112
}
102113

114+
// TryPackBeneficiary is the Go binding used to pack the parameters required for calling
115+
// the contract method with ID 0x38af3eed. This method will return an error
116+
// if any inputs are invalid/nil.
117+
//
118+
// Solidity: function beneficiary() returns(address)
119+
func (crowdsale *Crowdsale) TryPackBeneficiary() ([]byte, error) {
120+
return crowdsale.abi.Pack("beneficiary")
121+
}
122+
103123
// UnpackBeneficiary is the Go binding that unpacks the parameters returned
104124
// from invoking the contract method with ID 0x38af3eed.
105125
//
@@ -110,11 +130,12 @@ func (crowdsale *Crowdsale) UnpackBeneficiary(data []byte) (common.Address, erro
110130
return *new(common.Address), err
111131
}
112132
out0 := *abi.ConvertType(out[0], new(common.Address)).(*common.Address)
113-
return out0, err
133+
return out0, nil
114134
}
115135

116136
// PackCheckGoalReached is the Go binding used to pack the parameters required for calling
117-
// the contract method with ID 0x01cb3b20.
137+
// the contract method with ID 0x01cb3b20. This method will panic if any
138+
// invalid/nil inputs are passed.
118139
//
119140
// Solidity: function checkGoalReached() returns()
120141
func (crowdsale *Crowdsale) PackCheckGoalReached() []byte {
@@ -125,8 +146,18 @@ func (crowdsale *Crowdsale) PackCheckGoalReached() []byte {
125146
return enc
126147
}
127148

149+
// TryPackCheckGoalReached is the Go binding used to pack the parameters required for calling
150+
// the contract method with ID 0x01cb3b20. This method will return an error
151+
// if any inputs are invalid/nil.
152+
//
153+
// Solidity: function checkGoalReached() returns()
154+
func (crowdsale *Crowdsale) TryPackCheckGoalReached() ([]byte, error) {
155+
return crowdsale.abi.Pack("checkGoalReached")
156+
}
157+
128158
// PackDeadline is the Go binding used to pack the parameters required for calling
129-
// the contract method with ID 0x29dcb0cf.
159+
// the contract method with ID 0x29dcb0cf. This method will panic if any
160+
// invalid/nil inputs are passed.
130161
//
131162
// Solidity: function deadline() returns(uint256)
132163
func (crowdsale *Crowdsale) PackDeadline() []byte {
@@ -137,6 +168,15 @@ func (crowdsale *Crowdsale) PackDeadline() []byte {
137168
return enc
138169
}
139170

171+
// TryPackDeadline is the Go binding used to pack the parameters required for calling
172+
// the contract method with ID 0x29dcb0cf. This method will return an error
173+
// if any inputs are invalid/nil.
174+
//
175+
// Solidity: function deadline() returns(uint256)
176+
func (crowdsale *Crowdsale) TryPackDeadline() ([]byte, error) {
177+
return crowdsale.abi.Pack("deadline")
178+
}
179+
140180
// UnpackDeadline is the Go binding that unpacks the parameters returned
141181
// from invoking the contract method with ID 0x29dcb0cf.
142182
//
@@ -147,11 +187,12 @@ func (crowdsale *Crowdsale) UnpackDeadline(data []byte) (*big.Int, error) {
147187
return new(big.Int), err
148188
}
149189
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
150-
return out0, err
190+
return out0, nil
151191
}
152192

153193
// PackFunders is the Go binding used to pack the parameters required for calling
154-
// the contract method with ID 0xdc0d3dff.
194+
// the contract method with ID 0xdc0d3dff. This method will panic if any
195+
// invalid/nil inputs are passed.
155196
//
156197
// Solidity: function funders(uint256 ) returns(address addr, uint256 amount)
157198
func (crowdsale *Crowdsale) PackFunders(arg0 *big.Int) []byte {
@@ -162,6 +203,15 @@ func (crowdsale *Crowdsale) PackFunders(arg0 *big.Int) []byte {
162203
return enc
163204
}
164205

206+
// TryPackFunders is the Go binding used to pack the parameters required for calling
207+
// the contract method with ID 0xdc0d3dff. This method will return an error
208+
// if any inputs are invalid/nil.
209+
//
210+
// Solidity: function funders(uint256 ) returns(address addr, uint256 amount)
211+
func (crowdsale *Crowdsale) TryPackFunders(arg0 *big.Int) ([]byte, error) {
212+
return crowdsale.abi.Pack("funders", arg0)
213+
}
214+
165215
// FundersOutput serves as a container for the return parameters of contract
166216
// method Funders.
167217
type FundersOutput struct {
@@ -181,12 +231,12 @@ func (crowdsale *Crowdsale) UnpackFunders(data []byte) (FundersOutput, error) {
181231
}
182232
outstruct.Addr = *abi.ConvertType(out[0], new(common.Address)).(*common.Address)
183233
outstruct.Amount = abi.ConvertType(out[1], new(big.Int)).(*big.Int)
184-
return *outstruct, err
185-
234+
return *outstruct, nil
186235
}
187236

188237
// PackFundingGoal is the Go binding used to pack the parameters required for calling
189-
// the contract method with ID 0x7a3a0e84.
238+
// the contract method with ID 0x7a3a0e84. This method will panic if any
239+
// invalid/nil inputs are passed.
190240
//
191241
// Solidity: function fundingGoal() returns(uint256)
192242
func (crowdsale *Crowdsale) PackFundingGoal() []byte {
@@ -197,6 +247,15 @@ func (crowdsale *Crowdsale) PackFundingGoal() []byte {
197247
return enc
198248
}
199249

250+
// TryPackFundingGoal is the Go binding used to pack the parameters required for calling
251+
// the contract method with ID 0x7a3a0e84. This method will return an error
252+
// if any inputs are invalid/nil.
253+
//
254+
// Solidity: function fundingGoal() returns(uint256)
255+
func (crowdsale *Crowdsale) TryPackFundingGoal() ([]byte, error) {
256+
return crowdsale.abi.Pack("fundingGoal")
257+
}
258+
200259
// UnpackFundingGoal is the Go binding that unpacks the parameters returned
201260
// from invoking the contract method with ID 0x7a3a0e84.
202261
//
@@ -207,11 +266,12 @@ func (crowdsale *Crowdsale) UnpackFundingGoal(data []byte) (*big.Int, error) {
207266
return new(big.Int), err
208267
}
209268
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
210-
return out0, err
269+
return out0, nil
211270
}
212271

213272
// PackPrice is the Go binding used to pack the parameters required for calling
214-
// the contract method with ID 0xa035b1fe.
273+
// the contract method with ID 0xa035b1fe. This method will panic if any
274+
// invalid/nil inputs are passed.
215275
//
216276
// Solidity: function price() returns(uint256)
217277
func (crowdsale *Crowdsale) PackPrice() []byte {
@@ -222,6 +282,15 @@ func (crowdsale *Crowdsale) PackPrice() []byte {
222282
return enc
223283
}
224284

285+
// TryPackPrice is the Go binding used to pack the parameters required for calling
286+
// the contract method with ID 0xa035b1fe. This method will return an error
287+
// if any inputs are invalid/nil.
288+
//
289+
// Solidity: function price() returns(uint256)
290+
func (crowdsale *Crowdsale) TryPackPrice() ([]byte, error) {
291+
return crowdsale.abi.Pack("price")
292+
}
293+
225294
// UnpackPrice is the Go binding that unpacks the parameters returned
226295
// from invoking the contract method with ID 0xa035b1fe.
227296
//
@@ -232,11 +301,12 @@ func (crowdsale *Crowdsale) UnpackPrice(data []byte) (*big.Int, error) {
232301
return new(big.Int), err
233302
}
234303
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
235-
return out0, err
304+
return out0, nil
236305
}
237306

238307
// PackTokenReward is the Go binding used to pack the parameters required for calling
239-
// the contract method with ID 0x6e66f6e9.
308+
// the contract method with ID 0x6e66f6e9. This method will panic if any
309+
// invalid/nil inputs are passed.
240310
//
241311
// Solidity: function tokenReward() returns(address)
242312
func (crowdsale *Crowdsale) PackTokenReward() []byte {
@@ -247,6 +317,15 @@ func (crowdsale *Crowdsale) PackTokenReward() []byte {
247317
return enc
248318
}
249319

320+
// TryPackTokenReward is the Go binding used to pack the parameters required for calling
321+
// the contract method with ID 0x6e66f6e9. This method will return an error
322+
// if any inputs are invalid/nil.
323+
//
324+
// Solidity: function tokenReward() returns(address)
325+
func (crowdsale *Crowdsale) TryPackTokenReward() ([]byte, error) {
326+
return crowdsale.abi.Pack("tokenReward")
327+
}
328+
250329
// UnpackTokenReward is the Go binding that unpacks the parameters returned
251330
// from invoking the contract method with ID 0x6e66f6e9.
252331
//
@@ -257,7 +336,7 @@ func (crowdsale *Crowdsale) UnpackTokenReward(data []byte) (common.Address, erro
257336
return *new(common.Address), err
258337
}
259338
out0 := *abi.ConvertType(out[0], new(common.Address)).(*common.Address)
260-
return out0, err
339+
return out0, nil
261340
}
262341

263342
// CrowdsaleFundTransfer represents a FundTransfer event raised by the Crowdsale contract.

0 commit comments

Comments
 (0)