add generic call/transact

This commit is contained in:
Jared Wasinger 2024-12-03 13:49:24 +07:00 committed by Felix Lange
parent 28d57002ff
commit a0485167d5
2 changed files with 13 additions and 13 deletions

View File

@ -345,19 +345,23 @@ func (it *EventIterator[T]) Close() error {
// Transact creates and submits a transaction to the bound contract instance // Transact creates and submits a transaction to the bound contract instance
// using the provided abi-encoded input (or nil). // using the provided abi-encoded input (or nil).
func Transact(instance *ContractInstance, opts *bind.TransactOpts, packedInput []byte) (*types.Transaction, error) { func Transact(instance *ContractInstance, opts *bind.TransactOpts, input []byte) (*types.Transaction, error) {
var ( var (
addr = instance.Address addr = instance.Address
backend = instance.Backend backend = instance.Backend
) )
c := bind.NewBoundContract(addr, abi.ABI{}, backend, backend, backend) c := bind.NewBoundContract(addr, abi.ABI{}, backend, backend, backend)
return c.RawTransact(opts, packedInput) return c.RawTransact(opts, input)
} }
// Call performs an eth_call on the given bound contract instance, using the // Call performs an eth_call on the given bound contract instance, using the
// provided abi-encoded input (or nil). // provided abi-encoded input (or nil).
func Call(instance *ContractInstance, opts *bind.CallOpts, packedInput []byte) ([]byte, error) { func Call[T any](instance *ContractInstance, opts *bind.CallOpts, packedInput []byte, unpack func([]byte) (*T, error)) (*T, error) {
backend := instance.Backend backend := instance.Backend
c := bind.NewBoundContract(instance.Address, abi.ABI{}, backend, backend, backend) c := bind.NewBoundContract(instance.Address, abi.ABI{}, backend, backend, backend)
return c.CallRaw(opts, packedInput) packedOutput, err := c.CallRaw(opts, packedInput)
if err != nil {
return nil, err
}
return unpack(packedOutput)
} }

View File

@ -148,21 +148,17 @@ func TestDeploymentLibraries(t *testing.T) {
t.Fatalf("pack function input err: %v\n", doInput) t.Fatalf("pack function input err: %v\n", doInput)
} }
cABI, err := nested_libraries.C1MetaData.GetAbi()
if err != nil {
t.Fatalf("error getting abi object: %v", err)
}
contractAddr := res.Addrs[nested_libraries.C1MetaData.Pattern] contractAddr := res.Addrs[nested_libraries.C1MetaData.Pattern]
boundC := bind.NewBoundContract(contractAddr, *cABI, bindBackend, bindBackend, bindBackend)
callOpts := &bind.CallOpts{ callOpts := &bind.CallOpts{
From: common.Address{}, From: common.Address{},
Context: context.Background(), Context: context.Background(),
} }
callRes, err := boundC.CallRaw(callOpts, doInput)
if err != nil { ctrctInstance := &ContractInstance{
t.Fatalf("err calling contract: %v", err) Address: contractAddr,
Backend: bindBackend,
} }
internalCallCount, err := c.UnpackDo(callRes) internalCallCount, err := Call[big.Int](ctrctInstance, callOpts, doInput, ctrct.UnpackDo)
if err != nil { if err != nil {
t.Fatalf("err unpacking result: %v", err) t.Fatalf("err unpacking result: %v", err)
} }