Skip to content

Commit 860b1d6

Browse files
committed
Remove extra GetLogs() and add pagination option
1 parent 5e32154 commit 860b1d6

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

logs.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,26 @@
77

88
package etherscan
99

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-
}
10+
type Option func(M)
1811

19-
err = c.call("logs", "getLogs", param, &logs)
20-
return
12+
func WithPagination(page, offset int) Option {
13+
return func(m M) {
14+
m["page"] = page
15+
m["offset"] = offset
16+
}
2117
}
2218

23-
func (c *Client) GetLogsWithPagination(fromBlock, toBlock int, address, topic string, page, offset int) (logs []Log, err error) {
19+
// GetLogs gets logs that match "topic" emitted by the specified "address" between the "fromBlock" and "toBlock"
20+
func (c *Client) GetLogs(fromBlock, toBlock int, address, topic string, options ...Option) (logs []Log, err error) {
2421
param := M{
2522
"fromBlock": fromBlock,
2623
"toBlock": toBlock,
2724
"topic0": topic,
2825
"address": address,
29-
"page": page,
30-
"offset": offset,
26+
}
27+
28+
for _, applyOpt := range options {
29+
applyOpt(param)
3130
}
3231

3332
err = c.call("logs", "getLogs", param, &logs)

logs_e2e_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,32 @@ func TestClient_GetLogsWithPagination(t *testing.T) {
8787
errContain string
8888
}{
8989
{
90-
name: "no page or offset",
90+
name: "no page or offset returns all logs",
9191
page: 0, off: 0,
9292
wantSlice: expectedLogs,
9393
},
9494
{
95-
name: "page=1, offset=0",
95+
name: "page=1, offset=0 returns all logs",
9696
page: 1, off: 0,
9797
wantSlice: expectedLogs,
9898
},
9999
{
100-
name: "page=2, offset=0",
100+
name: "page=2, offset=0 returns all logs",
101101
page: 2, off: 0,
102102
wantSlice: expectedLogs,
103103
},
104104
{
105-
name: "page=1, offset=2",
105+
name: "page=1, offset=2 returns first 2 logs of 4",
106106
page: 1, off: 2,
107107
wantSlice: expectedLogs[0:2],
108108
},
109109
{
110-
name: "page=2, offset=2",
110+
name: "page=2, offset=2 returns last 2 logs of 4",
111111
page: 2, off: 2,
112112
wantSlice: expectedLogs[2:4],
113113
},
114114
{
115-
name: "page=5, offset=1",
115+
name: "page=5, offset=1 returns no logs as page is beyond available logs",
116116
page: 5, off: 1,
117117
wantSlice: []Log{},
118118
wantErr: true,
@@ -122,13 +122,12 @@ func TestClient_GetLogsWithPagination(t *testing.T) {
122122

123123
for _, tt := range tests {
124124
t.Run(tt.name, func(t *testing.T) {
125-
got, err := api.GetLogsWithPagination(
125+
got, err := api.GetLogs(
126126
379224,
127127
430000,
128128
"0x33990122638b9132ca29c723bdf037f1a891a70c",
129129
"0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545",
130-
tt.page,
131-
tt.off,
130+
WithPagination(tt.page, tt.off),
132131
)
133132

134133
if tt.wantErr {

0 commit comments

Comments
 (0)