Skip to content

Commit

Permalink
refactor(logs): make log filtering easier
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Nov 8, 2024
1 parent 68f01e7 commit a29f41d
Show file tree
Hide file tree
Showing 21 changed files with 169 additions and 379 deletions.
5 changes: 2 additions & 3 deletions accounts/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"math/big"
"testing"

"github.com/darrenvechain/thorgo/events"
"github.com/darrenvechain/thorgo/thorest"
"github.com/darrenvechain/thorgo/txmanager"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -79,7 +78,7 @@ func TestContract_EventCriteria(t *testing.T) {
assert.NoError(t, err)

// fetch events
transfers, err := events.New(thorClient, []thorest.EventCriteria{criteria}).Apply(0, 100)
transfers, err := thorClient.FilterEvents([]thorest.EventCriteria{criteria}, &thorest.LogFilters{})
assert.NoError(t, err)

// decode events
Expand Down Expand Up @@ -111,7 +110,7 @@ func TestContract_UnpackLog(t *testing.T) {
assert.NoError(t, err)

// fetch events
transfers, err := events.New(thorClient, []thorest.EventCriteria{criteria}).Apply(0, 100)
transfers, err := thorClient.FilterEvents([]thorest.EventCriteria{criteria}, nil)
assert.NoError(t, err)
assert.Greater(t, len(transfers), 0)

Expand Down
10 changes: 2 additions & 8 deletions builtins/authority.go

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

3 changes: 2 additions & 1 deletion builtins/authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package builtins
import (
"testing"

"github.com/darrenvechain/thorgo/thorest"
"github.com/stretchr/testify/assert"
)

func TestAuthority(t *testing.T) {
authority, err := NewAuthority(thor)
assert.NoError(t, err)

candidates, err := authority.FilterCandidate(make([]AuthorityCandidateCriteria, 0), nil, nil)
candidates, err := authority.FilterCandidate([]AuthorityCandidateCriteria{}, &thorest.LogFilters{})
assert.NoError(t, err)
assert.NotEmpty(t, candidates)

Expand Down
30 changes: 6 additions & 24 deletions builtins/executor.go

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

10 changes: 2 additions & 8 deletions builtins/params.go

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

20 changes: 4 additions & 16 deletions builtins/vtho.go

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

15 changes: 7 additions & 8 deletions builtins/vtho_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

var thor = thorgo.New("https://mainnet.vechain.org")
var eventLimit = int64(10)
var logFilter = new(thorest.LogFilters).Limit(eventLimit)

func TestEnergy_Name(t *testing.T) {
energy, err := NewVTHO(thor)
Expand Down Expand Up @@ -60,20 +62,18 @@ func TestEnergy_FilterTransfer(t *testing.T) {
energy, err := NewVTHO(thor)
assert.NoError(t, err)

maxEvents := int64(10)
events, err := energy.FilterTransfer(make([]VTHOTransferCriteria, 0), &thorest.FilterOptions{Limit: &maxEvents}, nil)
events, err := energy.FilterTransfer(make([]VTHOTransferCriteria, 0), logFilter)
assert.NoError(t, err)
assert.Equal(t, maxEvents, int64(len(events)))
assert.Equal(t, eventLimit, int64(len(events)))
}

func TestEnergy_FilterApproval(t *testing.T) {
energy, err := NewVTHO(thor)
assert.NoError(t, err)

maxEvents := int64(10)
events, err := energy.FilterApproval(make([]VTHOApprovalCriteria, 0), &thorest.FilterOptions{Limit: &maxEvents}, nil)
events, err := energy.FilterApproval(make([]VTHOApprovalCriteria, 0), logFilter)
assert.NoError(t, err)
assert.Equal(t, maxEvents, int64(len(events)))
assert.Equal(t, eventLimit, int64(len(events)))
}

func TestEnergy_FilterTransfer_WithCriteria(t *testing.T) {
Expand All @@ -82,8 +82,7 @@ func TestEnergy_FilterTransfer_WithCriteria(t *testing.T) {

to := common.HexToAddress("0xbF89016e670595AAa225eEfa0a84B7FB17b8dAC8")

maxEvents := int64(10)
events, err := energy.FilterTransfer([]VTHOTransferCriteria{{To: &to}}, &thorest.FilterOptions{Limit: &maxEvents}, nil)
events, err := energy.FilterTransfer([]VTHOTransferCriteria{{To: &to}}, logFilter)
assert.NoError(t, err)
assert.NotZero(t, len(events))
}
10 changes: 2 additions & 8 deletions cmd/thorgen/source.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ var (
// Filter{{.Normalized.Name}} is a free log retrieval operation binding the contract event 0x{{printf "%x" .Original.ID}}.
//
// Solidity: {{.Original.String}}
func (_{{$contract.Type}} *{{$contract.Type}}) Filter{{.Normalized.Name}}({{ if gt $indexedArgCount 0 }}criteria []{{$contract.Type}}{{.Normalized.Name}}Criteria, {{ end }}opts *thorest.FilterOptions, rang *thorest.FilterRange) ([]{{$contract.Type}}{{.Normalized.Name}}, error) {
func (_{{$contract.Type}} *{{$contract.Type}}) Filter{{.Normalized.Name}}({{ if gt $indexedArgCount 0 }}criteria []{{$contract.Type}}{{.Normalized.Name}}Criteria, {{ end }}filters *thorest.LogFilters) ([]{{$contract.Type}}{{.Normalized.Name}}, error) {
topicHash := _{{$contract.Type}}.contract.ABI.Events["{{.Normalized.Name}}"].ID

{{ if gt $indexedArgCount 0 }}
Expand Down Expand Up @@ -286,13 +286,7 @@ var (
}
{{ end }}

filter := &thorest.EventFilter{
Range: rang,
Options: opts,
Criteria: &criteriaSet,
}

logs, err := _{{$contract.Type}}.thor.Client.FilterEvents(filter)
logs, err := _{{$contract.Type}}.thor.Client.FilterEvents(criteriaSet, filters)
if err != nil {
return nil, err
}
Expand Down
65 changes: 0 additions & 65 deletions events/events.go

This file was deleted.

48 changes: 0 additions & 48 deletions events/events_test.go

This file was deleted.

Loading

0 comments on commit a29f41d

Please sign in to comment.