Skip to content

Commit

Permalink
export event names in bindings. remove added APIs Filter/Watch-Logs w…
Browse files Browse the repository at this point in the history
…hich take event ids.
  • Loading branch information
jwasinger committed Dec 16, 2024
1 parent eec95b6 commit a009c60
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 43 deletions.
12 changes: 1 addition & 11 deletions accounts/abi/bind/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,25 +462,15 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i
return signedTx, nil
}

// FilterLogsById filters contract logs for past blocks, returning the necessary
// channels to construct a strongly typed bound iterator on top of them.
func (c *BoundContract) FilterLogsById(opts *FilterOpts, eventID common.Hash, query ...[]interface{}) (<-chan types.Log, event.Subscription, error) {
return c.filterLogs(opts, eventID, query...)
}

// FilterLogs filters contract logs for past blocks, returning the necessary
// channels to construct a strongly typed bound iterator on top of them.
func (c *BoundContract) FilterLogs(opts *FilterOpts, name string, query ...[]interface{}) (chan types.Log, event.Subscription, error) {
return c.filterLogs(opts, c.abi.Events[name].ID, query...)
}

func (c *BoundContract) filterLogs(opts *FilterOpts, eventID common.Hash, query ...[]interface{}) (chan types.Log, event.Subscription, error) {
// Don't crash on a lazy user
if opts == nil {
opts = new(FilterOpts)
}
// Append the event selector to the query parameters and construct the topic set
query = append([][]interface{}{{eventID}}, query...)
query = append([][]interface{}{{c.abi.Events[name].ID}}, query...)
topics, err := abi.MakeTopics(query...)
if err != nil {
return nil, nil, err
Expand Down
4 changes: 1 addition & 3 deletions accounts/abi/bind/source2.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ var (
Raw *types.Log // Blockchain specific contextual infos
}

func {{$contract.Type}}{{.Normalized.Name}}EventID() common.Hash {
return common.HexToHash("{{.Original.ID}}")
}
const {{$contract.Type}}{{.Normalized.Name}}EventName = "{{.Original.Name}}"

func (_{{$contract.Type}} *{{$contract.Type}}) Unpack{{.Normalized.Name}}Event(log *types.Log) (*{{$contract.Type}}{{.Normalized.Name}}, error) {
event := "{{.Original.Name}}"
Expand Down
12 changes: 2 additions & 10 deletions accounts/abi/bind/v2/internal/events/bindings.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions accounts/abi/bind/v2/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ import (
type ContractInstance struct {
Address common.Address
Backend bind.ContractBackend
abi abi.ABI
}

// 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 *bind.FilterOpts, eventID common.Hash, unpack func(*types.Log) (*T, error), topics ...[]any) (*EventIterator[T], error) {
func FilterEvents[T any](instance *ContractInstance, opts *bind.FilterOpts, eventName string, unpack func(*types.Log) (*T, error), topics ...[]any) (*EventIterator[T], error) {
backend := instance.Backend
c := bind.NewBoundContract(instance.Address, abi.ABI{}, backend, backend, backend)
logs, sub, err := c.FilterLogsById(opts, eventID, topics...)
c := bind.NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
logs, sub, err := c.FilterLogs(opts, eventName, topics...)
if err != nil {
return nil, err
}
Expand All @@ -47,10 +48,10 @@ func FilterEvents[T any](instance *ContractInstance, opts *bind.FilterOpts, even
// 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 *bind.WatchOpts, eventID common.Hash, unpack func(*types.Log) (*T, error), sink chan<- *T, topics ...[]any) (event.Subscription, error) {
func WatchEvents[T any](instance *ContractInstance, opts *bind.WatchOpts, eventName string, unpack func(*types.Log) (*T, error), sink chan<- *T, topics ...[]any) (event.Subscription, error) {
backend := instance.Backend
c := bind.NewBoundContract(instance.Address, abi.ABI{}, backend, backend, backend)
logs, sub, err := c.WatchLogsForId(opts, eventID, topics...)
c := bind.NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
logs, sub, err := c.WatchLogs(opts, eventName, topics...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -159,15 +160,15 @@ func Transact(instance *ContractInstance, opts *bind.TransactOpts, input []byte)
addr = instance.Address
backend = instance.Backend
)
c := bind.NewBoundContract(addr, abi.ABI{}, backend, backend, backend)
c := bind.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 (or nil).
func Call[T any](instance *ContractInstance, opts *bind.CallOpts, packedInput []byte, unpack func([]byte) (*T, error)) (*T, error) {
backend := instance.Backend
c := bind.NewBoundContract(instance.Address, abi.ABI{}, backend, backend, backend)
c := bind.NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
packedOutput, err := c.CallRaw(opts, packedInput)
if err != nil {
return nil, err
Expand Down
21 changes: 10 additions & 11 deletions accounts/abi/bind/v2/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,11 @@ func TestEvents(t *testing.T) {
t.Fatalf("error instantiating contract instance: %v", err)
}

boundContract := ContractInstance{
ctrctABI, _ := events.CMetaData.GetAbi()
ctrctInstance := ContractInstance{
res.Addrs[events.CMetaData.Pattern],
backend,
*ctrctABI,
}

newCBasic1Ch := make(chan *events.CBasic1)
Expand All @@ -301,23 +303,19 @@ func TestEvents(t *testing.T) {
Start: nil,
Context: context.Background(),
}
sub1, err := WatchEvents(&boundContract, watchOpts, events.CBasic1EventID(), ctrct.UnpackBasic1Event, newCBasic1Ch)
sub1, err := WatchEvents(&ctrctInstance, watchOpts, events.CBasic1EventName, ctrct.UnpackBasic1Event, newCBasic1Ch)
if err != nil {
t.Fatalf("WatchEvents returned error: %v", err)
}
sub2, err := WatchEvents(&boundContract, watchOpts, events.CBasic2EventID(), ctrct.UnpackBasic2Event, newCBasic2Ch)
sub2, err := WatchEvents(&ctrctInstance, watchOpts, events.CBasic2EventName, ctrct.UnpackBasic2Event, newCBasic2Ch)
if err != nil {
t.Fatalf("WatchEvents returned error: %v", err)
}
defer sub1.Unsubscribe()
defer sub2.Unsubscribe()

crtctInstance := &ContractInstance{
Address: res.Addrs[events.CMetaData.Pattern],
Backend: backend,
}
packedInput, _ := ctrct.PackEmitMulti()
tx, err := Transact(crtctInstance, txAuth, packedInput)
tx, err := Transact(&ctrctInstance, txAuth, packedInput)
if err != nil {
t.Fatalf("failed to send transaction: %v", err)
}
Expand Down Expand Up @@ -356,11 +354,11 @@ done:
Start: 0,
Context: context.Background(),
}
it, err := FilterEvents[events.CBasic1](crtctInstance, filterOpts, events.CBasic1EventID(), ctrct.UnpackBasic1Event)
it, err := FilterEvents[events.CBasic1](&ctrctInstance, filterOpts, events.CBasic1EventName, ctrct.UnpackBasic1Event)
if err != nil {
t.Fatalf("error filtering logs %v\n", err)
}
it2, err := FilterEvents[events.CBasic2](crtctInstance, filterOpts, events.CBasic2EventID(), ctrct.UnpackBasic2Event)
it2, err := FilterEvents[events.CBasic2](&ctrctInstance, filterOpts, events.CBasic2EventName, ctrct.UnpackBasic2Event)
if err != nil {
t.Fatalf("error filtering logs %v\n", err)
}
Expand Down Expand Up @@ -409,7 +407,8 @@ func TestErrors(t *testing.T) {
}
packedInput, _ = ctrct.PackFoo()

instance := ContractInstance{res.Addrs[solc_errors.CMetaData.Pattern], backend}
ctrctABI, _ := solc_errors.CMetaData.GetAbi()
instance := ContractInstance{res.Addrs[solc_errors.CMetaData.Pattern], backend, *ctrctABI}
_, err = Call[struct{}](&instance, opts, packedInput, func(packed []byte) (*struct{}, error) { return nil, nil })
if err == nil {
t.Fatalf("expected call to fail")
Expand Down

0 comments on commit a009c60

Please sign in to comment.