Skip to content

Commit 731ebb1

Browse files
authored
Merge pull request nanmu42#68 from avislash/add-eth_getLogs-support
Add eth get logs support
2 parents 0158ce5 + 4f153f7 commit 731ebb1

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/nanmu42/etherscan-api
22

33
go 1.13
4+
5+
require github.com/google/go-cmp v0.5.7 // indirect

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
2+
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
3+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
expectedLogs := []Log{
11+
Log{
12+
Address: "0x33990122638b9132ca29c723bdf037f1a891a70c",
13+
Topics: []string{"0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545", "0x72657075746174696f6e00000000000000000000000000000000000000000000", "0x000000000000000000000000d9b2f59f3b5c7b3c67047d2f03c3e8052470be92"},
14+
Data: "0x",
15+
BlockNumber: "0x5c958",
16+
TransactionHash: "0x0b03498648ae2da924f961dda00dc6bb0a8df15519262b7e012b7d67f4bb7e83",
17+
LogIndex: "0x",
18+
},
19+
}
20+
21+
actualLogs, err := api.GetLogs(379224, 379225, "0x33990122638b9132ca29c723bdf037f1a891a70c", "0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545")
22+
23+
noError(t, err, "api.GetLogs")
24+
25+
equal := cmp.Equal(expectedLogs, actualLogs)
26+
27+
if !equal {
28+
t.Errorf("api.GetLogs not working\n: %s\n", cmp.Diff(expectedLogs, actualLogs))
29+
}
30+
}

response.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,15 @@ type LatestPrice struct {
159159
ETHUSD float64 `json:"ethusd,string"`
160160
ETHUSDTimestamp Time `json:"ethusd_timestamp"`
161161
}
162+
163+
type Log struct {
164+
Address string `json:"address"`
165+
Topics []string `json:"topics"`
166+
Data string `json:"data"`
167+
BlockNumber string `json:"blockNumber"`
168+
TransactionHash string `json:"transactionHash"`
169+
BlockHash string `json:"blockHash"`
170+
LogIndex string `json:"logIndex"`
171+
Removed bool `json:"removed"`
172+
}
173+

0 commit comments

Comments
 (0)