accounts/abi/bind/v2: remove ContractInstance

It's equivalent to BoundContract.
This commit is contained in:
Felix Lange 2025-01-27 23:55:26 +01:00
parent c9da576739
commit 7c5c0bbbf3
7 changed files with 19 additions and 44 deletions

View file

@ -52,7 +52,7 @@ func NewDB() *DB {
// Instance creates a wrapper for a deployed contract instance at the given address.
// Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc.
func (c *DB) Instance(backend bind.ContractBackend, addr common.Address) bind.ContractInstance {
func (c *DB) Instance(backend bind.ContractBackend, addr common.Address) *bind.BoundContract {
return bind.NewContractInstance(backend, addr, c.abi)
}

View file

@ -51,7 +51,7 @@ func NewC() *C {
// Instance creates a wrapper for a deployed contract instance at the given address.
// Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc.
func (c *C) Instance(backend bind.ContractBackend, addr common.Address) bind.ContractInstance {
func (c *C) Instance(backend bind.ContractBackend, addr common.Address) *bind.BoundContract {
return bind.NewContractInstance(backend, addr, c.abi)
}

View file

@ -49,7 +49,7 @@ func NewC1() *C1 {
// Instance creates a wrapper for a deployed contract instance at the given address.
// Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc.
func (c *C1) Instance(backend bind.ContractBackend, addr common.Address) bind.ContractInstance {
func (c *C1) Instance(backend bind.ContractBackend, addr common.Address) *bind.BoundContract {
return bind.NewContractInstance(backend, addr, c.abi)
}
@ -112,7 +112,7 @@ func NewC2() *C2 {
// Instance creates a wrapper for a deployed contract instance at the given address.
// Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc.
func (c *C2) Instance(backend bind.ContractBackend, addr common.Address) bind.ContractInstance {
func (c *C2) Instance(backend bind.ContractBackend, addr common.Address) *bind.BoundContract {
return bind.NewContractInstance(backend, addr, c.abi)
}
@ -171,7 +171,7 @@ func NewL1() *L1 {
// Instance creates a wrapper for a deployed contract instance at the given address.
// Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc.
func (c *L1) Instance(backend bind.ContractBackend, addr common.Address) bind.ContractInstance {
func (c *L1) Instance(backend bind.ContractBackend, addr common.Address) *bind.BoundContract {
return bind.NewContractInstance(backend, addr, c.abi)
}
@ -225,7 +225,7 @@ func NewL2() *L2 {
// Instance creates a wrapper for a deployed contract instance at the given address.
// Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc.
func (c *L2) Instance(backend bind.ContractBackend, addr common.Address) bind.ContractInstance {
func (c *L2) Instance(backend bind.ContractBackend, addr common.Address) *bind.BoundContract {
return bind.NewContractInstance(backend, addr, c.abi)
}
@ -279,7 +279,7 @@ func NewL2b() *L2b {
// Instance creates a wrapper for a deployed contract instance at the given address.
// Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc.
func (c *L2b) Instance(backend bind.ContractBackend, addr common.Address) bind.ContractInstance {
func (c *L2b) Instance(backend bind.ContractBackend, addr common.Address) *bind.BoundContract {
return bind.NewContractInstance(backend, addr, c.abi)
}
@ -330,7 +330,7 @@ func NewL3() *L3 {
// Instance creates a wrapper for a deployed contract instance at the given address.
// Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc.
func (c *L3) Instance(backend bind.ContractBackend, addr common.Address) bind.ContractInstance {
func (c *L3) Instance(backend bind.ContractBackend, addr common.Address) *bind.BoundContract {
return bind.NewContractInstance(backend, addr, c.abi)
}
@ -385,7 +385,7 @@ func NewL4() *L4 {
// Instance creates a wrapper for a deployed contract instance at the given address.
// Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc.
func (c *L4) Instance(backend bind.ContractBackend, addr common.Address) bind.ContractInstance {
func (c *L4) Instance(backend bind.ContractBackend, addr common.Address) *bind.BoundContract {
return bind.NewContractInstance(backend, addr, c.abi)
}
@ -439,7 +439,7 @@ func NewL4b() *L4b {
// Instance creates a wrapper for a deployed contract instance at the given address.
// Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc.
func (c *L4b) Instance(backend bind.ContractBackend, addr common.Address) bind.ContractInstance {
func (c *L4b) Instance(backend bind.ContractBackend, addr common.Address) *bind.BoundContract {
return bind.NewContractInstance(backend, addr, c.abi)
}

View file

@ -45,7 +45,7 @@ func NewC() *C {
// Instance creates a wrapper for a deployed contract instance at the given address.
// Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc.
func (c *C) Instance(backend bind.ContractBackend, addr common.Address) bind.ContractInstance {
func (c *C) Instance(backend bind.ContractBackend, addr common.Address) *bind.BoundContract {
return bind.NewContractInstance(backend, addr, c.abi)
}
@ -151,7 +151,7 @@ func NewC2() *C2 {
// Instance creates a wrapper for a deployed contract instance at the given address.
// Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc.
func (c *C2) Instance(backend bind.ContractBackend, addr common.Address) bind.ContractInstance {
func (c *C2) Instance(backend bind.ContractBackend, addr common.Address) *bind.BoundContract {
return bind.NewContractInstance(backend, addr, c.abi)
}

View file

@ -45,7 +45,7 @@ func NewMyContract() *MyContract {
// Instance creates a wrapper for a deployed contract instance at the given address.
// Use this to create the instance object passed to abigen v2 library functions Call, Transact, etc.
func (c *MyContract) Instance(backend bind.ContractBackend, addr common.Address) bind.ContractInstance {
func (c *MyContract) Instance(backend bind.ContractBackend, addr common.Address) *bind.BoundContract {
return bind.NewContractInstance(backend, addr, c.abi)
}

View file

@ -27,22 +27,12 @@ import (
"github.com/ethereum/go-ethereum/event"
)
// ContractInstance represents a contract deployed on-chain that can be interacted with (filter for past logs, watch
// for new logs, call, transact).
type ContractInstance struct {
Address common.Address
Backend ContractBackend
abi abi.ABI
}
func NewContractInstance(backend ContractBackend, addr common.Address, abi abi.ABI) ContractInstance {
return ContractInstance{addr, backend, abi}
func NewContractInstance(backend ContractBackend, addr common.Address, abi abi.ABI) *BoundContract {
return NewBoundContract(addr, abi, backend, backend, backend)
}
// FilterEvents returns an EventIterator instance for filtering historical events based on the event id and a block range.
func FilterEvents[T any](instance ContractInstance, opts *FilterOpts, eventName string, unpack func(*types.Log) (*T, error), topics ...[]any) (*EventIterator[T], error) {
backend := instance.Backend
c := NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
func FilterEvents[T any](c *BoundContract, opts *FilterOpts, eventName string, unpack func(*types.Log) (*T, error), topics ...[]any) (*EventIterator[T], error) {
logs, sub, err := c.FilterLogs(opts, eventName, topics...)
if err != nil {
return nil, err
@ -54,9 +44,7 @@ func FilterEvents[T any](instance ContractInstance, opts *FilterOpts, eventName
// contract to be intercepted, unpacked, and forwarded to sink. If
// unpack returns an error, the returned subscription is closed with the
// error.
func WatchEvents[T any](instance ContractInstance, opts *WatchOpts, eventName string, unpack func(*types.Log) (*T, error), sink chan<- *T, topics ...[]any) (event.Subscription, error) {
backend := instance.Backend
c := NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
func WatchEvents[T any](c *BoundContract, opts *WatchOpts, eventName string, unpack func(*types.Log) (*T, error), sink chan<- *T, topics ...[]any) (event.Subscription, error) {
logs, sub, err := c.WatchLogs(opts, eventName, topics...)
if err != nil {
return nil, err
@ -159,26 +147,13 @@ func (it *EventIterator[T]) Close() error {
return nil
}
// Transact creates and submits a transaction to the bound contract instance
// using the provided abi-encoded input (or nil).
func Transact(instance ContractInstance, opts *TransactOpts, input []byte) (*types.Transaction, error) {
var (
addr = instance.Address
backend = instance.Backend
)
c := NewBoundContract(addr, instance.abi, backend, backend, backend)
return c.RawTransact(opts, input)
}
// Call performs an eth_call on the given bound contract instance, using the provided
// ABI-encoded input.
//
// To call a function that doesn't return any output, pass nil as the unpack function.
// This can be useful if you just want to check that the function doesn't revert.
func Call[T any](instance ContractInstance, opts *CallOpts, packedInput []byte, unpack func([]byte) (T, error)) (T, error) {
func Call[T any](c *BoundContract, opts *CallOpts, packedInput []byte, unpack func([]byte) (T, error)) (T, error) {
var defaultResult T
backend := instance.Backend
c := NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
packedOutput, err := c.CallRaw(opts, packedInput)
if err != nil {
return defaultResult, err

View file

@ -237,7 +237,7 @@ func TestEvents(t *testing.T) {
defer sub2.Unsubscribe()
packedInput := c.PackEmitMulti()
tx, err := bind.Transact(instance, txAuth, packedInput)
tx, err := instance.RawTransact(txAuth, packedInput)
if err != nil {
t.Fatalf("failed to send transaction: %v", err)
}