Skip to content

Commit 47784b9

Browse files
committed
accounts/abi/bind/v2: infer event name from type
1 parent c452fb1 commit 47784b9

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

accounts/abi/bind/v2/internal/contracts/db/bindings.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

accounts/abi/bind/v2/internal/contracts/events/bindings.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

accounts/abi/bind/v2/lib.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,28 @@ func NewContractInstance(backend ContractBackend, addr common.Address, abi abi.A
3131
return NewBoundContract(addr, abi, backend, backend, backend)
3232
}
3333

34+
// ContractEvent is a type constraint for ABI event types.
35+
type ContractEvent interface {
36+
ContractEventName() string
37+
}
38+
3439
// FilterEvents returns an EventIterator instance for filtering historical events based on the event id and a block range.
35-
func FilterEvents[T any](c *BoundContract, opts *FilterOpts, eventName string, unpack func(*types.Log) (*T, error), topics ...[]any) (*EventIterator[T], error) {
36-
logs, sub, err := c.FilterLogs(opts, eventName, topics...)
40+
func FilterEvents[Ev ContractEvent](c *BoundContract, opts *FilterOpts, unpack func(*types.Log) (*Ev, error), topics ...[]any) (*EventIterator[Ev], error) {
41+
var e Ev
42+
logs, sub, err := c.FilterLogs(opts, e.ContractEventName(), topics...)
3743
if err != nil {
3844
return nil, err
3945
}
40-
return &EventIterator[T]{unpack: unpack, logs: logs, sub: sub}, nil
46+
return &EventIterator[Ev]{unpack: unpack, logs: logs, sub: sub}, nil
4147
}
4248

4349
// WatchEvents causes logs emitted with a given event id from a specified
4450
// contract to be intercepted, unpacked, and forwarded to sink. If
4551
// unpack returns an error, the returned subscription is closed with the
4652
// error.
47-
func WatchEvents[T any](c *BoundContract, opts *WatchOpts, eventName string, unpack func(*types.Log) (*T, error), sink chan<- *T, topics ...[]any) (event.Subscription, error) {
48-
logs, sub, err := c.WatchLogs(opts, eventName, topics...)
53+
func WatchEvents[Ev ContractEvent](c *BoundContract, opts *WatchOpts, unpack func(*types.Log) (*Ev, error), sink chan<- *Ev, topics ...[]any) (event.Subscription, error) {
54+
var e Ev
55+
logs, sub, err := c.WatchLogs(opts, e.ContractEventName(), topics...)
4956
if err != nil {
5057
return nil, err
5158
}

accounts/abi/bind/v2/lib_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,11 @@ func TestEvents(t *testing.T) {
233233
newCBasic1Ch := make(chan *events.CBasic1)
234234
newCBasic2Ch := make(chan *events.CBasic2)
235235
watchOpts := &bind.WatchOpts{}
236-
sub1, err := bind.WatchEvents(instance, watchOpts, events.CBasic1EventName, c.UnpackBasic1Event, newCBasic1Ch)
236+
sub1, err := bind.WatchEvents(instance, watchOpts, c.UnpackBasic1Event, newCBasic1Ch)
237237
if err != nil {
238238
t.Fatalf("WatchEvents returned error: %v", err)
239239
}
240-
sub2, err := bind.WatchEvents(instance, watchOpts, events.CBasic2EventName, c.UnpackBasic2Event, newCBasic2Ch)
240+
sub2, err := bind.WatchEvents(instance, watchOpts, c.UnpackBasic2Event, newCBasic2Ch)
241241
if err != nil {
242242
t.Fatalf("WatchEvents returned error: %v", err)
243243
}
@@ -284,11 +284,11 @@ done:
284284
Start: 0,
285285
Context: context.Background(),
286286
}
287-
it, err := bind.FilterEvents(instance, filterOpts, events.CBasic1EventName, c.UnpackBasic1Event)
287+
it, err := bind.FilterEvents(instance, filterOpts, c.UnpackBasic1Event)
288288
if err != nil {
289289
t.Fatalf("error filtering logs %v\n", err)
290290
}
291-
it2, err := bind.FilterEvents(instance, filterOpts, events.CBasic2EventName, c.UnpackBasic2Event)
291+
it2, err := bind.FilterEvents(instance, filterOpts, c.UnpackBasic2Event)
292292
if err != nil {
293293
t.Fatalf("error filtering logs %v\n", err)
294294
}

0 commit comments

Comments
 (0)