Skip to content

Commit ff54090

Browse files
committed
Added support for querying log events based on one topic
1 parent a1340f6 commit ff54090

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

logs.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2022 Avi Misra
3+
*
4+
* Use of this work is governed by a MIT License.
5+
* You may find a license copy in project root.
6+
*/
7+
8+
package etherscan
9+
10+
// GetLogs gets logs that match "topic" emitted by the specified "address" between the "fromBlock" and "toBlock"
11+
func (c *Client) GetLogs(fromBlock, toBlock int, address, topic string) (logs []Log, err error) {
12+
param := M{
13+
"fromBlock": fromBlock,
14+
"toBlock": toBlock,
15+
"topic0": topic,
16+
"address": address,
17+
}
18+
19+
err = c.call("logs", "getLogs", param, &logs)
20+
return
21+
}

logs_e2e_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package etherscan
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
)
8+
9+
func TestClient_GetLogs(t *testing.T) {
10+
const ans = `[{"address":"0x33990122638b9132ca29c723bdf037f1a891a70c","topics":[],"data":"0x","blockNumber":"0x5c958","logIndex":"0x","blockHash":"","transactionHash":"0x0b03498648ae2da924f961dda00dc6bb0a8df15519262b7e012b7d67f4bb7e83", "removed":false}]`
11+
expectedLogs := []Log{
12+
Log{
13+
Address: "0x33990122638b9132ca29c723bdf037f1a891a70c",
14+
Topics: []string{"0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545", "0x72657075746174696f6e00000000000000000000000000000000000000000000", "0x000000000000000000000000d9b2f59f3b5c7b3c67047d2f03c3e8052470be92"},
15+
Data: "0x",
16+
BlockNumber: "0x5c958",
17+
TransactionHash: "0x0b03498648ae2da924f961dda00dc6bb0a8df15519262b7e012b7d67f4bb7e83",
18+
LogIndex: "0x",
19+
},
20+
}
21+
22+
actualLogs, err := api.GetLogs(379224, 379225, "0x33990122638b9132ca29c723bdf037f1a891a70c", "0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545")
23+
24+
noError(t, err, "api.GetLogs")
25+
26+
var expected []map[string]interface{}
27+
var actual []map[string]interface{}
28+
29+
equal := cmp.Equal(expectedLogs, actualLogs)
30+
31+
if !equal {
32+
t.Errorf("api.GetLogs not working\n: %s\n", cmp.Diff(expected, actual))
33+
}
34+
}

0 commit comments

Comments
 (0)