Pass pointer to log for unpack

This commit is contained in:
Sina Mahmoodi 2023-03-02 15:59:01 +03:30 committed by Jared Wasinger
parent bb63ef3a21
commit 8f756ec576
2 changed files with 9 additions and 8 deletions

View File

@ -72,7 +72,7 @@ func Transfer2(instance ContractInstance, opts *TransactOpts) (*types.Transactio
return c.Transfer(opts) 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() backend := instance.Backend()
c := NewBoundContract(instance.Address(), abi.ABI{}, backend, backend, backend) c := NewBoundContract(instance.Address(), abi.ABI{}, backend, backend, backend)
logs, sub, err := c.filterLogs(opts, eventID, topics...) 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 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() backend := instance.Backend()
c := NewBoundContract(instance.Address(), abi.ABI{}, backend, backend, backend) c := NewBoundContract(instance.Address(), abi.ABI{}, backend, backend, backend)
logs, sub, err := c.watchLogs(opts, eventID, topics...) logs, sub, err := c.watchLogs(opts, eventID, topics...)
@ -95,7 +95,7 @@ func WatchLogs[T any](instance ContractInstance, opts *WatchOpts, eventID common
select { select {
case log := <-logs: case log := <-logs:
// New log arrived, parse the event and forward to the user // New log arrived, parse the event and forward to the user
ev, err := unpack(log) ev, err := unpack(&log)
if err != nil { if err != nil {
return err return err
} }
@ -120,7 +120,7 @@ func WatchLogs[T any](instance ContractInstance, opts *WatchOpts, eventID common
type EventIterator[T any] struct { type EventIterator[T any] struct {
Event *T // Event containing the contract specifics and raw log 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 logs chan types.Log // Log channel receiving the found contract events
sub ethereum.Subscription // Subscription for errors, completion and termination sub ethereum.Subscription // Subscription for errors, completion and termination
@ -140,7 +140,7 @@ func (it *EventIterator[T]) Next() bool {
if it.done { if it.done {
select { select {
case log := <-it.logs: case log := <-it.logs:
res, err := it.unpack(log) res, err := it.unpack(&log)
if err != nil { if err != nil {
it.fail = err it.fail = err
return false 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 // Iterator still in progress, wait for either a data or an error event
select { select {
case log := <-it.logs: case log := <-it.logs:
res, err := it.unpack(log) res, err := it.unpack(&log)
if err != nil { if err != nil {
it.fail = err it.fail = err
return false return false

View File

@ -133,13 +133,14 @@ var (
// {{$contract.Type}}{{.Normalized.Name}} represents a {{.Normalized.Name}} event raised by the {{$contract.Type}} contract. // {{$contract.Type}}{{.Normalized.Name}} represents a {{.Normalized.Name}} event raised by the {{$contract.Type}} contract.
type {{$contract.Type}}{{.Normalized.Name}} struct { {{range .Normalized.Inputs}} type {{$contract.Type}}{{.Normalized.Name}} struct { {{range .Normalized.Inputs}}
{{capitalise .Name}} {{if .Indexed}}{{bindtopictype .Type $structs}}{{else}}{{bindtype .Type $structs}}{{end}}; {{end}} {{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 { func (_{{$contract.Type}} *{{$contract.Type}}) {{.Normalized.Name}}EventID() common.Hash {
return common.HexToHash("{{.Original.ID}}") 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}}" event := "{{.Normalized.Name}}"
if log.Topics[0] != _{{$contract.Type}}.abi.Events[event].ID { if log.Topics[0] != _{{$contract.Type}}.abi.Events[event].ID {
return nil, errors.New("event signature mismatch") return nil, errors.New("event signature mismatch")