Skip to content

accounts/bind/v2: generate TryPack* methods #31692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion accounts/abi/abigen/bindv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func TestBindingV2ConvertedV1Tests(t *testing.T) {
}
// Set this environment variable to regenerate the test outputs.
if os.Getenv("WRITE_TEST_FILES") != "" {
if err := os.WriteFile((fname), []byte(have), 0666); err != nil {
if err := os.WriteFile(fname, []byte(have), 0666); err != nil {
t.Fatalf("err writing expected output to file: %v\n", err)
}
}
Expand Down
22 changes: 14 additions & 8 deletions accounts/abi/abigen/source2.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var (
{{ end }}

{{range .Calls}}
// Pack{{.Normalized.Name}} is the Go binding used to pack the parameters required for calling
// Pack{{.Normalized.Name}} is the Go binding used to pack the parameters required for calling, will panic for any error.
// the contract method with ID 0x{{printf "%x" .Original.ID}}.
//
// Solidity: {{.Original.String}}
Expand All @@ -101,6 +101,14 @@ var (
return enc
}

// Pack{{.Normalized.Name}} is the Go binding used to pack the parameters required for calling, return error if it failed to pack.
// the contract method with ID 0x{{printf "%x" .Original.ID}}.
//
// Solidity: {{.Original.String}}
func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) TryPack{{.Normalized.Name}}({{range .Normalized.Inputs}} {{.Name}} {{bindtype .Type $structs}}, {{end}}) ([]byte, error) {
return {{ decapitalise $contract.Type}}.abi.Pack("{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}})
}

{{/* Unpack method is needed only when there are return args */}}
{{if .Normalized.Outputs }}
{{ if .Structured }}
Expand All @@ -117,24 +125,23 @@ var (
//
// Solidity: {{.Original.String}}
func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) Unpack{{.Normalized.Name}}(data []byte) (
{{- if .Structured}} {{.Normalized.Name}}Output,{{else}}
{{- if .Structured}} *{{.Normalized.Name}}Output,{{else}}
{{- range .Normalized.Outputs}} {{bindtype .Type $structs}},{{- end }}
{{- end }} error) {
out, err := {{ decapitalise $contract.Type}}.abi.Unpack("{{.Original.Name}}", data)
{{- if .Structured}}
outstruct := new({{.Normalized.Name}}Output)
if err != nil {
return *outstruct, err
return nil, err
}
outstruct := new({{.Normalized.Name}}Output)
{{- range $i, $t := .Normalized.Outputs}}
{{- if ispointertype .Type}}
outstruct.{{capitalise .Name}} = abi.ConvertType(out[{{$i}}], new({{underlyingbindtype .Type }})).({{bindtype .Type $structs}})
{{- else }}
outstruct.{{capitalise .Name}} = *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}})
{{- end }}
{{- end }}
return *outstruct, err
{{else}}
return outstruct, nil{{else}}
if err != nil {
return {{range $i, $_ := .Normalized.Outputs}}{{if ispointertype .Type}}new({{underlyingbindtype .Type }}), {{else}}*new({{bindtype .Type $structs}}), {{end}}{{end}} err
}
Expand All @@ -145,8 +152,7 @@ var (
out{{$i}} := *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}})
{{- end }}
{{- end}}
return {{range $i, $t := .Normalized.Outputs}}out{{$i}}, {{end}} err
{{- end}}
return {{range $i, $t := .Normalized.Outputs}}out{{$i}}, {{end}} nil{{- end}}
}
{{end}}
{{end}}
Expand Down
10 changes: 9 additions & 1 deletion accounts/abi/abigen/testdata/v2/callbackparam.go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *CallbackParam) Instance(backend bind.ContractBackend, addr common.Addre
return bind.NewBoundContract(addr, c.abi, backend, backend, backend)
}

// PackTest is the Go binding used to pack the parameters required for calling
// PackTest is the Go binding used to pack the parameters required for calling, will panic for any error.
// the contract method with ID 0xd7a5aba2.
//
// Solidity: function test(function callback) returns()
Expand All @@ -62,3 +62,11 @@ func (callbackParam *CallbackParam) PackTest(callback [24]byte) []byte {
}
return enc
}

// PackTest is the Go binding used to pack the parameters required for calling, return error if it failed to pack.
// the contract method with ID 0xd7a5aba2.
//
// Solidity: function test(function callback) returns()
func (callbackParam *CallbackParam) TryPackTest(callback [24]byte) ([]byte, error) {
return callbackParam.abi.Pack("test", callback)
}
101 changes: 82 additions & 19 deletions accounts/abi/abigen/testdata/v2/crowdsale.go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (crowdsale *Crowdsale) PackConstructor(ifSuccessfulSendTo common.Address, f
return enc
}

// PackAmountRaised is the Go binding used to pack the parameters required for calling
// PackAmountRaised is the Go binding used to pack the parameters required for calling, will panic for any error.
// the contract method with ID 0x7b3e5e7b.
//
// Solidity: function amountRaised() returns(uint256)
Expand All @@ -75,6 +75,14 @@ func (crowdsale *Crowdsale) PackAmountRaised() []byte {
return enc
}

// PackAmountRaised is the Go binding used to pack the parameters required for calling, return error if it failed to pack.
// the contract method with ID 0x7b3e5e7b.
//
// Solidity: function amountRaised() returns(uint256)
func (crowdsale *Crowdsale) TryPackAmountRaised() ([]byte, error) {
return crowdsale.abi.Pack("amountRaised")
}

// UnpackAmountRaised is the Go binding that unpacks the parameters returned
// from invoking the contract method with ID 0x7b3e5e7b.
//
Expand All @@ -85,10 +93,10 @@ func (crowdsale *Crowdsale) UnpackAmountRaised(data []byte) (*big.Int, error) {
return new(big.Int), err
}
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
return out0, err
return out0, nil
}

// PackBeneficiary is the Go binding used to pack the parameters required for calling
// PackBeneficiary is the Go binding used to pack the parameters required for calling, will panic for any error.
// the contract method with ID 0x38af3eed.
//
// Solidity: function beneficiary() returns(address)
Expand All @@ -100,6 +108,14 @@ func (crowdsale *Crowdsale) PackBeneficiary() []byte {
return enc
}

// PackBeneficiary is the Go binding used to pack the parameters required for calling, return error if it failed to pack.
// the contract method with ID 0x38af3eed.
//
// Solidity: function beneficiary() returns(address)
func (crowdsale *Crowdsale) TryPackBeneficiary() ([]byte, error) {
return crowdsale.abi.Pack("beneficiary")
}

// UnpackBeneficiary is the Go binding that unpacks the parameters returned
// from invoking the contract method with ID 0x38af3eed.
//
Expand All @@ -110,10 +126,10 @@ func (crowdsale *Crowdsale) UnpackBeneficiary(data []byte) (common.Address, erro
return *new(common.Address), err
}
out0 := *abi.ConvertType(out[0], new(common.Address)).(*common.Address)
return out0, err
return out0, nil
}

// PackCheckGoalReached is the Go binding used to pack the parameters required for calling
// PackCheckGoalReached is the Go binding used to pack the parameters required for calling, will panic for any error.
// the contract method with ID 0x01cb3b20.
//
// Solidity: function checkGoalReached() returns()
Expand All @@ -125,7 +141,15 @@ func (crowdsale *Crowdsale) PackCheckGoalReached() []byte {
return enc
}

// PackDeadline is the Go binding used to pack the parameters required for calling
// PackCheckGoalReached is the Go binding used to pack the parameters required for calling, return error if it failed to pack.
// the contract method with ID 0x01cb3b20.
//
// Solidity: function checkGoalReached() returns()
func (crowdsale *Crowdsale) TryPackCheckGoalReached() ([]byte, error) {
return crowdsale.abi.Pack("checkGoalReached")
}

// PackDeadline is the Go binding used to pack the parameters required for calling, will panic for any error.
// the contract method with ID 0x29dcb0cf.
//
// Solidity: function deadline() returns(uint256)
Expand All @@ -137,6 +161,14 @@ func (crowdsale *Crowdsale) PackDeadline() []byte {
return enc
}

// PackDeadline is the Go binding used to pack the parameters required for calling, return error if it failed to pack.
// the contract method with ID 0x29dcb0cf.
//
// Solidity: function deadline() returns(uint256)
func (crowdsale *Crowdsale) TryPackDeadline() ([]byte, error) {
return crowdsale.abi.Pack("deadline")
}

// UnpackDeadline is the Go binding that unpacks the parameters returned
// from invoking the contract method with ID 0x29dcb0cf.
//
Expand All @@ -147,10 +179,10 @@ func (crowdsale *Crowdsale) UnpackDeadline(data []byte) (*big.Int, error) {
return new(big.Int), err
}
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
return out0, err
return out0, nil
}

// PackFunders is the Go binding used to pack the parameters required for calling
// PackFunders is the Go binding used to pack the parameters required for calling, will panic for any error.
// the contract method with ID 0xdc0d3dff.
//
// Solidity: function funders(uint256 ) returns(address addr, uint256 amount)
Expand All @@ -162,6 +194,14 @@ func (crowdsale *Crowdsale) PackFunders(arg0 *big.Int) []byte {
return enc
}

// PackFunders is the Go binding used to pack the parameters required for calling, return error if it failed to pack.
// the contract method with ID 0xdc0d3dff.
//
// Solidity: function funders(uint256 ) returns(address addr, uint256 amount)
func (crowdsale *Crowdsale) TryPackFunders(arg0 *big.Int) ([]byte, error) {
return crowdsale.abi.Pack("funders", arg0)
}

// FundersOutput serves as a container for the return parameters of contract
// method Funders.
type FundersOutput struct {
Expand All @@ -173,19 +213,18 @@ type FundersOutput struct {
// from invoking the contract method with ID 0xdc0d3dff.
//
// Solidity: function funders(uint256 ) returns(address addr, uint256 amount)
func (crowdsale *Crowdsale) UnpackFunders(data []byte) (FundersOutput, error) {
func (crowdsale *Crowdsale) UnpackFunders(data []byte) (*FundersOutput, error) {
out, err := crowdsale.abi.Unpack("funders", data)
outstruct := new(FundersOutput)
if err != nil {
return *outstruct, err
return nil, err
}
outstruct := new(FundersOutput)
outstruct.Addr = *abi.ConvertType(out[0], new(common.Address)).(*common.Address)
outstruct.Amount = abi.ConvertType(out[1], new(big.Int)).(*big.Int)
return *outstruct, err

return outstruct, nil
}

// PackFundingGoal is the Go binding used to pack the parameters required for calling
// PackFundingGoal is the Go binding used to pack the parameters required for calling, will panic for any error.
// the contract method with ID 0x7a3a0e84.
//
// Solidity: function fundingGoal() returns(uint256)
Expand All @@ -197,6 +236,14 @@ func (crowdsale *Crowdsale) PackFundingGoal() []byte {
return enc
}

// PackFundingGoal is the Go binding used to pack the parameters required for calling, return error if it failed to pack.
// the contract method with ID 0x7a3a0e84.
//
// Solidity: function fundingGoal() returns(uint256)
func (crowdsale *Crowdsale) TryPackFundingGoal() ([]byte, error) {
return crowdsale.abi.Pack("fundingGoal")
}

// UnpackFundingGoal is the Go binding that unpacks the parameters returned
// from invoking the contract method with ID 0x7a3a0e84.
//
Expand All @@ -207,10 +254,10 @@ func (crowdsale *Crowdsale) UnpackFundingGoal(data []byte) (*big.Int, error) {
return new(big.Int), err
}
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
return out0, err
return out0, nil
}

// PackPrice is the Go binding used to pack the parameters required for calling
// PackPrice is the Go binding used to pack the parameters required for calling, will panic for any error.
// the contract method with ID 0xa035b1fe.
//
// Solidity: function price() returns(uint256)
Expand All @@ -222,6 +269,14 @@ func (crowdsale *Crowdsale) PackPrice() []byte {
return enc
}

// PackPrice is the Go binding used to pack the parameters required for calling, return error if it failed to pack.
// the contract method with ID 0xa035b1fe.
//
// Solidity: function price() returns(uint256)
func (crowdsale *Crowdsale) TryPackPrice() ([]byte, error) {
return crowdsale.abi.Pack("price")
}

// UnpackPrice is the Go binding that unpacks the parameters returned
// from invoking the contract method with ID 0xa035b1fe.
//
Expand All @@ -232,10 +287,10 @@ func (crowdsale *Crowdsale) UnpackPrice(data []byte) (*big.Int, error) {
return new(big.Int), err
}
out0 := abi.ConvertType(out[0], new(big.Int)).(*big.Int)
return out0, err
return out0, nil
}

// PackTokenReward is the Go binding used to pack the parameters required for calling
// PackTokenReward is the Go binding used to pack the parameters required for calling, will panic for any error.
// the contract method with ID 0x6e66f6e9.
//
// Solidity: function tokenReward() returns(address)
Expand All @@ -247,6 +302,14 @@ func (crowdsale *Crowdsale) PackTokenReward() []byte {
return enc
}

// PackTokenReward is the Go binding used to pack the parameters required for calling, return error if it failed to pack.
// the contract method with ID 0x6e66f6e9.
//
// Solidity: function tokenReward() returns(address)
func (crowdsale *Crowdsale) TryPackTokenReward() ([]byte, error) {
return crowdsale.abi.Pack("tokenReward")
}

// UnpackTokenReward is the Go binding that unpacks the parameters returned
// from invoking the contract method with ID 0x6e66f6e9.
//
Expand All @@ -257,7 +320,7 @@ func (crowdsale *Crowdsale) UnpackTokenReward(data []byte) (common.Address, erro
return *new(common.Address), err
}
out0 := *abi.ConvertType(out[0], new(common.Address)).(*common.Address)
return out0, err
return out0, nil
}

// CrowdsaleFundTransfer represents a FundTransfer event raised by the Crowdsale contract.
Expand Down
Loading