Skip to content

Commit

Permalink
Merge pull request #58 from keep-network/template-past-events
Browse files Browse the repository at this point in the history
Adding a template function to retrieve past events

Adds a template to generate functions that retrieve past events that
happened on-chain.
  • Loading branch information
pdyraga authored Nov 16, 2020
2 parents 977a23a + 016d77a commit 8af0579
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tools/generators/ethereum/contract_events.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@ type {{$contract.FullVar}}{{$event.CapsName}}Func func(
{{$event.ParamDeclarations -}}
)

func ({{$contract.ShortVar}} *{{$contract.Class}}) Past{{$event.CapsName}}Events(
startBlock uint64,
endBlock *uint64,
{{$event.IndexedFilterDeclarations -}}
) ([]*abi.{{$contract.AbiClass}}{{$event.CapsName}}, error){
iterator, err := {{$contract.ShortVar}}.contract.Filter{{$event.CapsName}}(
&bind.FilterOpts{
Start: startBlock,
End: endBlock,
},
{{$event.IndexedFilters}}
)
if err != nil {
return nil, fmt.Errorf(
"error retrieving past {{$event.CapsName}} events: [%v]",
err,
)
}

events := make([]*abi.{{$contract.AbiClass}}{{$event.CapsName}}, 0)

for iterator.Next() {
event := iterator.Event
events = append(events, event)
}

return events, nil
}

func ({{$contract.ShortVar}} *{{$contract.Class}}) Watch{{$event.CapsName}}(
success {{$contract.FullVar}}{{$event.CapsName}}Func,
fail func(err error) error,
Expand Down
29 changes: 29 additions & 0 deletions tools/generators/ethereum/contract_events_template_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,35 @@ type {{$contract.FullVar}}{{$event.CapsName}}Func func(
{{$event.ParamDeclarations -}}
)
func ({{$contract.ShortVar}} *{{$contract.Class}}) Past{{$event.CapsName}}Events(
startBlock uint64,
endBlock *uint64,
{{$event.IndexedFilterDeclarations -}}
) ([]*abi.{{$contract.AbiClass}}{{$event.CapsName}}, error){
iterator, err := {{$contract.ShortVar}}.contract.Filter{{$event.CapsName}}(
&bind.FilterOpts{
Start: startBlock,
End: endBlock,
},
{{$event.IndexedFilters}}
)
if err != nil {
return nil, fmt.Errorf(
"error retrieving past {{$event.CapsName}} events: [%v]",
err,
)
}
events := make([]*abi.{{$contract.AbiClass}}{{$event.CapsName}}, 0)
for iterator.Next() {
event := iterator.Event
events = append(events, event)
}
return events, nil
}
func ({{$contract.ShortVar}} *{{$contract.Class}}) Watch{{$event.CapsName}}(
success {{$contract.FullVar}}{{$event.CapsName}}Func,
fail func(err error) error,
Expand Down

0 comments on commit 8af0579

Please sign in to comment.