From b504552373104ccfc6f668de2e6ba123606791d8 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Thu, 2 Mar 2023 15:59:01 +0330 Subject: [PATCH] Pass pointer to log for unpack --- accounts/abi/bind/lib.go | 12 ++++++------ accounts/abi/bind/template2.go | 5 +++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/accounts/abi/bind/lib.go b/accounts/abi/bind/lib.go index 0b9cc2bf19..6d3ea71264 100644 --- a/accounts/abi/bind/lib.go +++ b/accounts/abi/bind/lib.go @@ -72,7 +72,7 @@ func Transfer2(instance ContractInstance, opts *TransactOpts) (*types.Transactio return c.Transfer(opts) } -func FilterLogs[T any](instance ContractInstance, opts *FilterOpts, eventID common.Hash, unpack func(types.Log) (*T, error), topics ...[]any) (*EventIterator[T], error) { +func FilterLogs[T any](instance ContractInstance, opts *FilterOpts, eventID common.Hash, unpack func(*types.Log) (*T, error), topics ...[]any) (*EventIterator[T], error) { backend := instance.Backend() c := NewBoundContract(instance.Address(), abi.ABI{}, backend, backend, backend) logs, sub, err := c.filterLogs(opts, eventID, topics...) @@ -82,7 +82,7 @@ func FilterLogs[T any](instance ContractInstance, opts *FilterOpts, eventID comm return &EventIterator[T]{unpack: unpack, logs: logs, sub: sub}, nil } -func WatchLogs[T any](instance ContractInstance, opts *WatchOpts, eventID common.Hash, unpack func(types.Log) (*T, error), sink chan<- *T, topics ...[]any) (event.Subscription, error) { +func WatchLogs[T any](instance ContractInstance, opts *WatchOpts, eventID common.Hash, unpack func(*types.Log) (*T, error), sink chan<- *T, topics ...[]any) (event.Subscription, error) { backend := instance.Backend() c := NewBoundContract(instance.Address(), abi.ABI{}, backend, backend, backend) logs, sub, err := c.watchLogs(opts, eventID, topics...) @@ -95,7 +95,7 @@ func WatchLogs[T any](instance ContractInstance, opts *WatchOpts, eventID common select { case log := <-logs: // New log arrived, parse the event and forward to the user - ev, err := unpack(log) + ev, err := unpack(&log) if err != nil { return err } @@ -120,7 +120,7 @@ func WatchLogs[T any](instance ContractInstance, opts *WatchOpts, eventID common type EventIterator[T any] struct { Event *T // Event containing the contract specifics and raw log - unpack func(types.Log) (*T, error) // Unpack function for the event + unpack func(*types.Log) (*T, error) // Unpack function for the event logs chan types.Log // Log channel receiving the found contract events sub ethereum.Subscription // Subscription for errors, completion and termination @@ -140,7 +140,7 @@ func (it *EventIterator[T]) Next() bool { if it.done { select { case log := <-it.logs: - res, err := it.unpack(log) + res, err := it.unpack(&log) if err != nil { it.fail = err return false @@ -155,7 +155,7 @@ func (it *EventIterator[T]) Next() bool { // Iterator still in progress, wait for either a data or an error event select { case log := <-it.logs: - res, err := it.unpack(log) + res, err := it.unpack(&log) if err != nil { it.fail = err return false diff --git a/accounts/abi/bind/template2.go b/accounts/abi/bind/template2.go index c5bd3a71b3..f9e53d65ea 100644 --- a/accounts/abi/bind/template2.go +++ b/accounts/abi/bind/template2.go @@ -133,13 +133,14 @@ var ( // {{$contract.Type}}{{.Normalized.Name}} represents a {{.Normalized.Name}} event raised by the {{$contract.Type}} contract. type {{$contract.Type}}{{.Normalized.Name}} struct { {{range .Normalized.Inputs}} {{capitalise .Name}} {{if .Indexed}}{{bindtopictype .Type $structs}}{{else}}{{bindtype .Type $structs}}{{end}}; {{end}} - Raw types.Log // Blockchain specific contextual infos + Raw *types.Log // Blockchain specific contextual infos } + func (_{{$contract.Type}} *{{$contract.Type}}) {{.Normalized.Name}}EventID() common.Hash { return common.HexToHash("{{.Original.ID}}") } - func (_{{$contract.Type}} *{{$contract.Type}}) Unpack{{.Normalized.Name}}Event(log types.Log) (*{{$contract.Type}}{{.Normalized.Name}}, error) { + func (_{{$contract.Type}} *{{$contract.Type}}) Unpack{{.Normalized.Name}}Event(log *types.Log) (*{{$contract.Type}}{{.Normalized.Name}}, error) { event := "{{.Normalized.Name}}" if log.Topics[0] != _{{$contract.Type}}.abi.Events[event].ID { return nil, errors.New("event signature mismatch")