Skip to content

Commit 56502a9

Browse files
committed
accounts/abi/bind/v2: support passing nil unpack function in Call
1 parent 1602eb8 commit 56502a9

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

accounts/abi/bind/v2/lib.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package bind
1818

1919
import (
20+
"errors"
21+
2022
"github.com/ethereum/go-ethereum"
2123
"github.com/ethereum/go-ethereum/accounts/abi"
2224
bind1 "github.com/ethereum/go-ethereum/accounts/abi/bind"
@@ -34,7 +36,7 @@ type ContractInstance struct {
3436
abi abi.ABI
3537
}
3638

37-
func NewContractInstance(addr common.Address, backend ContractBackend, abi abi.ABI) ContractInstance {
39+
func NewContractInstance(backend ContractBackend, addr common.Address, abi abi.ABI) ContractInstance {
3840
return ContractInstance{addr, backend, abi}
3941
}
4042

@@ -169,8 +171,11 @@ func Transact(instance ContractInstance, opts *TransactOpts, input []byte) (*typ
169171
return c.RawTransact(opts, input)
170172
}
171173

172-
// Call performs an eth_call on the given bound contract instance, using the
173-
// provided abi-encoded input (or nil).
174+
// Call performs an eth_call on the given bound contract instance, using the provided
175+
// ABI-encoded input.
176+
//
177+
// To call a function that doesn't return any output, pass nil as the unpack function.
178+
// This can be useful if you just want to check that the function doesn't revert.
174179
func Call[T any](instance ContractInstance, opts *CallOpts, packedInput []byte, unpack func([]byte) (T, error)) (T, error) {
175180
var defaultResult T
176181
backend := instance.Backend
@@ -179,6 +184,12 @@ func Call[T any](instance ContractInstance, opts *CallOpts, packedInput []byte,
179184
if err != nil {
180185
return defaultResult, err
181186
}
187+
if unpack == nil {
188+
if len(packedOutput) > 0 {
189+
return defaultResult, errors.New("contract returned data, but no unpack function was given")
190+
}
191+
return defaultResult, nil
192+
}
182193
res, err := unpack(packedOutput)
183194
if err != nil {
184195
return defaultResult, err

0 commit comments

Comments
 (0)