Skip to content

Add eth get logs support #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/nanmu42/etherscan-api

go 1.13

require github.com/google/go-cmp v0.5.7 // indirect
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
21 changes: 21 additions & 0 deletions logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2022 Avi Misra
*
* Use of this work is governed by a MIT License.
* You may find a license copy in project root.
*/

package etherscan

// GetLogs gets logs that match "topic" emitted by the specified "address" between the "fromBlock" and "toBlock"
func (c *Client) GetLogs(fromBlock, toBlock int, address, topic string) (logs []Log, err error) {
param := M{
"fromBlock": fromBlock,
"toBlock": toBlock,
"topic0": topic,
"address": address,
}

err = c.call("logs", "getLogs", param, &logs)
return
}
30 changes: 30 additions & 0 deletions logs_e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package etherscan

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestClient_GetLogs(t *testing.T) {
expectedLogs := []Log{
Log{
Address: "0x33990122638b9132ca29c723bdf037f1a891a70c",
Topics: []string{"0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545", "0x72657075746174696f6e00000000000000000000000000000000000000000000", "0x000000000000000000000000d9b2f59f3b5c7b3c67047d2f03c3e8052470be92"},
Data: "0x",
BlockNumber: "0x5c958",
TransactionHash: "0x0b03498648ae2da924f961dda00dc6bb0a8df15519262b7e012b7d67f4bb7e83",
LogIndex: "0x",
},
}

actualLogs, err := api.GetLogs(379224, 379225, "0x33990122638b9132ca29c723bdf037f1a891a70c", "0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545")

noError(t, err, "api.GetLogs")

equal := cmp.Equal(expectedLogs, actualLogs)

if !equal {
t.Errorf("api.GetLogs not working\n: %s\n", cmp.Diff(expectedLogs, actualLogs))
}
}
12 changes: 12 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,15 @@ type LatestPrice struct {
ETHUSD float64 `json:"ethusd,string"`
ETHUSDTimestamp Time `json:"ethusd_timestamp"`
}

type Log struct {
Address string `json:"address"`
Topics []string `json:"topics"`
Data string `json:"data"`
BlockNumber string `json:"blockNumber"`
TransactionHash string `json:"transactionHash"`
BlockHash string `json:"blockHash"`
LogIndex string `json:"logIndex"`
Removed bool `json:"removed"`
}