Skip to content

Commit 5e32154

Browse files
committed
Add GetLogsWithPagination
1 parent 0d2eec1 commit 5e32154

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

logs.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,17 @@ func (c *Client) GetLogs(fromBlock, toBlock int, address, topic string) (logs []
1919
err = c.call("logs", "getLogs", param, &logs)
2020
return
2121
}
22+
23+
func (c *Client) GetLogsWithPagination(fromBlock, toBlock int, address, topic string, page, offset int) (logs []Log, err error) {
24+
param := M{
25+
"fromBlock": fromBlock,
26+
"toBlock": toBlock,
27+
"topic0": topic,
28+
"address": address,
29+
"page": page,
30+
"offset": offset,
31+
}
32+
33+
err = c.call("logs", "getLogs", param, &logs)
34+
return
35+
}

logs_e2e_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package etherscan
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/google/go-cmp/cmp"
@@ -29,3 +30,126 @@ func TestClient_GetLogs(t *testing.T) {
2930
t.Errorf("api.GetLogs not working\n: %s\n", cmp.Diff(expectedLogs, actualLogs))
3031
}
3132
}
33+
34+
func TestClient_GetLogsWithPagination(t *testing.T) {
35+
expectedLogs := []Log{
36+
{
37+
Address: "0x33990122638b9132ca29c723bdf037f1a891a70c",
38+
Topics: []string{"0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545",
39+
"0x72657075746174696f6e00000000000000000000000000000000000000000000",
40+
"0x000000000000000000000000d9b2f59f3b5c7b3c67047d2f03c3e8052470be92"},
41+
Data: "0x",
42+
BlockNumber: "0x5c958",
43+
BlockHash: "0xe32a9cac27f823b18454e8d69437d2af41a1b81179c6af2601f1040a72ad444b",
44+
TransactionHash: "0x0b03498648ae2da924f961dda00dc6bb0a8df15519262b7e012b7d67f4bb7e83",
45+
LogIndex: "0x",
46+
},
47+
{
48+
Address: "0x33990122638b9132ca29c723bdf037f1a891a70c",
49+
Topics: []string{"0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545",
50+
"0x6c6f747465727900000000000000000000000000000000000000000000000000",
51+
"0x0000000000000000000000001f6cc3f7c927e1196c03ac49c5aff0d39c9d103d"},
52+
Data: "0x",
53+
BlockNumber: "0x5c965",
54+
BlockHash: "0x46e257ddbafca078402d0c49ad31c79514995667132c45eddbb8ca7153b6871e",
55+
TransactionHash: "0x8c72ea19b48947c4339077bd9c9c09a780dfbdb1cafe68db4d29cdf2754adc11",
56+
LogIndex: "0x",
57+
},
58+
{
59+
Address: "0x33990122638b9132ca29c723bdf037f1a891a70c",
60+
Topics: []string{"0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545",
61+
"0x657870616e736500000000000000000000000000000000000000000000000000",
62+
"0x000000000000000000000000d7586825a3177b1c6ef341ccb18361cfbc62dd0c"},
63+
Data: "0x",
64+
BlockNumber: "0x6664c",
65+
BlockHash: "0xe8e5f93f338348b17df97b4a6723ce4ee899eb375003c8c6c74255325915a1ed",
66+
TransactionHash: "0xf9c4f7843dc1f9bf6d248ebe0033b2c51398255eb8973f4af4bae2c3f9313a78",
67+
LogIndex: "0x",
68+
},
69+
{
70+
Address: "0x33990122638b9132ca29c723bdf037f1a891a70c",
71+
Topics: []string{"0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545",
72+
"0x726f6f7473746f636b0000000000000000000000000000000000000000000000",
73+
"0x000000000000000000000000697c0123cd103cf4c3446b271e0970f109eae78c"},
74+
Data: "0x",
75+
BlockNumber: "0x66650",
76+
BlockHash: "0x49dd65b6e2f97b294d18ec97e26b349b8215f26964be1f26e0639f79995b1a11",
77+
TransactionHash: "0xb190139d14140cf98035c5b78fe3b2629db2787ef234258633278985fa99a13a",
78+
LogIndex: "0x",
79+
},
80+
}
81+
82+
tests := []struct {
83+
name string
84+
page, off int
85+
wantSlice []Log
86+
wantErr bool
87+
errContain string
88+
}{
89+
{
90+
name: "no page or offset",
91+
page: 0, off: 0,
92+
wantSlice: expectedLogs,
93+
},
94+
{
95+
name: "page=1, offset=0",
96+
page: 1, off: 0,
97+
wantSlice: expectedLogs,
98+
},
99+
{
100+
name: "page=2, offset=0",
101+
page: 2, off: 0,
102+
wantSlice: expectedLogs,
103+
},
104+
{
105+
name: "page=1, offset=2",
106+
page: 1, off: 2,
107+
wantSlice: expectedLogs[0:2],
108+
},
109+
{
110+
name: "page=2, offset=2",
111+
page: 2, off: 2,
112+
wantSlice: expectedLogs[2:4],
113+
},
114+
{
115+
name: "page=5, offset=1",
116+
page: 5, off: 1,
117+
wantSlice: []Log{},
118+
wantErr: true,
119+
errContain: "No records found",
120+
},
121+
}
122+
123+
for _, tt := range tests {
124+
t.Run(tt.name, func(t *testing.T) {
125+
got, err := api.GetLogsWithPagination(
126+
379224,
127+
430000,
128+
"0x33990122638b9132ca29c723bdf037f1a891a70c",
129+
"0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545",
130+
tt.page,
131+
tt.off,
132+
)
133+
134+
if tt.wantErr {
135+
if err == nil {
136+
t.Fatal("expected error, got nil")
137+
}
138+
if !strings.Contains(err.Error(), tt.errContain) {
139+
t.Fatalf("expected error to contain %q, got %v", tt.errContain, err)
140+
}
141+
if len(got) != 0 {
142+
t.Errorf("expected no logs on error, but got %d entries", len(got))
143+
}
144+
return
145+
}
146+
147+
if err != nil {
148+
t.Fatalf("unexpected error: %v", err)
149+
}
150+
if diff := cmp.Diff(tt.wantSlice, got); diff != "" {
151+
t.Errorf("logs mismatch (-want +got):\n%s", diff)
152+
}
153+
})
154+
}
155+
}

0 commit comments

Comments
 (0)