Skip to content

Commit

Permalink
Add topics to event filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Oct 2, 2024
1 parent 4ff8ea7 commit 79b098c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions services/execdb/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ type EventFilter struct {
// Address is the address of the contract that generated the event.
// If nil then no filter is applied
Address *[]byte

// Topics are the topics of the event.
// If nil then no filter is applied.
Topics [][]byte
}

// TransactionBalanceChangeFilter defines a filter for fetching transaction balance changes.
Expand Down
9 changes: 9 additions & 0 deletions services/execdb/postgresql/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ FROM t_events`)
queryVals = append(queryVals, *filter.To)
queryBuilder.WriteString(fmt.Sprintf(`
%s f_block_height <= $%d`, wherestr, len(queryVals)))
wherestr = " AND"
}

if len(filter.Topics) > 0 {
queryVals = append(queryVals, filter.Topics)
queryBuilder.WriteString(fmt.Sprintf(`
%s f_topics && $%d IS TRUE`, wherestr, len(queryVals)))
//nolint
wherestr = " AND"
}

switch filter.Order {
Expand Down
20 changes: 20 additions & 0 deletions services/execdb/postgresql/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ func hashPtr(input string) *[]byte {
return &res
}

func hash(input string) []byte {
res, err := hex.DecodeString(strings.TrimPrefix(input, "0x"))
if err != nil {
panic(err)
}

return res
}

func TestEvents(t *testing.T) {
ctx := context.Background()
s, err := postgresql.New(ctx,
Expand Down Expand Up @@ -90,6 +99,17 @@ func TestEvents(t *testing.T) {
},
events: `[{"TransactionHash":"WJ6BebauwoVbUX44QURYdMEzu36dxFPZrqXH309XsfY=","BlockHeight":12690581,"Index":133,"Address":"wCqqObIj/o0KDlxPJ+rZCDx1bMI=","Topics":["4f/8xJI9BLVZ9NKai/xs2gTrWw08RgdRwkAsXFzJEJw=","AAAAAAAAAAAAAAAAAAAAAAAAfxUL1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dWzCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="},{"TransactionHash":"WJ6BebauwoVbUX44QURYdMEzu36dxFPZrqXH309XsfY=","BlockHeight":12690581,"Index":135,"Address":"wCqqObIj/o0KDlxPJ+rZCDx1bMI=","Topics":["3fJSrRviyJtpwrBo/DeNqpUrp/FjxKEWKPVaTfUjs+8=","AAAAAAAAAAAAAAAAAAAAAAAAfxUL1vVMQKNNfD1en1Y=","AAAAAAAAAAAAAAAAYXRvhnPuocUUaWvFI8qZH/WGTnc="],"Data":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABotlX2fLCfBk="},{"TransactionHash":"WJ6BebauwoVbUX44QURYdMEzu36dxFPZrqXH309XsfY=","BlockHeight":12690581,"Index":136,"Address":"axdUdOiQlMRNqYuVTu3qxJUnHQ8=","Topics":["3fJSrRviyJtpwrBo/DeNqpUrp/FjxKEWKPVaTfUjs+8=","AAAAAAAAAAAAAAAAYXRvhnPuocUUaWvFI8qZH/WGTnc=","AAAAAAAAAAAAAAAAAAAAAAAAfxUL1vVMQKNNfD1en1Y="],"Data":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAz5dzgiony/3Lo="},{"TransactionHash":"WJ6BebauwoVbUX44QURYdMEzu36dxFPZrqXH309XsfY=","BlockHeight":12690581,"Index":137,"Address":"axdUdOiQlMRNqYuVTu3qxJUnHQ8=","Topics":["3fJSrRviyJtpwrBo/DeNqpUrp/FjxKEWKPVaTfUjs+8=","AAAAAAAAAAAAAAAAAAAAAAAAfxUL1vVMQKNNfD1en1Y=","AAAAAAAAAAAAAAAAw9A+TwQf1M04jFSe4qKanlB1iC8="],"Data":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAz5dzgionz/+PI="},{"TransactionHash":"WJ6BebauwoVbUX44QURYdMEzu36dxFPZrqXH309XsfY=","BlockHeight":12690581,"Index":138,"Address":"wCqqObIj/o0KDlxPJ+rZCDx1bMI=","Topics":["3fJSrRviyJtpwrBo/DeNqpUrp/FjxKEWKPVaTfUjs+8=","AAAAAAAAAAAAAAAAw9A+TwQf1M04jFSe4qKanlB1iC8=","AAAAAAAAAAAAAAAAAAAAAAAAfxUL1vVMQKNNfD1en1Y="],"Data":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABoxq1uv/WbVg="},{"TransactionHash":"WJ6BebauwoVbUX44QURYdMEzu36dxFPZrqXH309XsfY=","BlockHeight":12690581,"Index":139,"Address":"w9A+TwQf1M04jFSe4qKanlB1iC8=","Topics":["HEEempbgcSQcLyH3cmsXronjyrTHi+UOBisDqf/7utE="],"Data":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAUvAD6bCOsY8KMU4AAAAAAAAAAAAAAAAAAAAAAAAAAAAACn18ilgKn8fgwQ=="},{"TransactionHash":"WJ6BebauwoVbUX44QURYdMEzu36dxFPZrqXH309XsfY=","BlockHeight":12690581,"Index":140,"Address":"w9A+TwQf1M04jFSe4qKanlB1iC8=","Topics":["14rZX6RsmUtlUdDahfwnX+YTzjdlf7jV49EwhAFZ2CI=","AAAAAAAAAAAAAAAAAAAAAAAAfxUL1vVMQKNNfD1en1Y=","AAAAAAAAAAAAAAAAAAAAAAAAfxUL1vVMQKNNfD1en1Y="],"Data":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAz5dzgionz/+PIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABoxq1uv/WbVg="},{"TransactionHash":"WJ6BebauwoVbUX44QURYdMEzu36dxFPZrqXH309XsfY=","BlockHeight":12690581,"Index":141,"Address":"wCqqObIj/o0KDlxPJ+rZCDx1bMI=","Topics":["f89TLBXwptsL1tDgOL6nHTDYCMfZjLO/cmipW/UIG2U=","AAAAAAAAAAAAAAAAAAAAAAAAfxUL1vVMQKNNfD1en1Y="],"Data":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABoxq1uv/WbVg="}]`,
},
{
name: "Topics",
filter: &execdb.EventFilter{
Order: execdb.OrderEarliest,
Limit: 1,
Topics: [][]byte{
hash("0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae"),
},
},
events: `[{"TransactionHash":"8A+NE0Y7I20qTBvJZUlW0NY5y+5tAaiag+9KO+ZHOuY=","BlockHeight":3676978,"Index":8,"Address":"MUFZJl3Y27MQZC+Y9QwGYXPBJZs=","Topics":["zgRX/nNzH4JMwnI3YWkjUSjBGLSdNEgXQXxtEI0VXoI=","k83rcIt1RdxmjrkoAXYWnRwzz9jtbwRpCgvMiKk/xK4=","AAAEJbRGLhlGC+20vM/PFtJwl174gvA4Mb89QPc0I1U="],"Data":"AAAAAAAAAAAAAAAACQTawzR+pH0gjz/WdALQOaO5mFk="}]`,
},
}

for _, test := range tests {
Expand Down

0 comments on commit 79b098c

Please sign in to comment.