From 96a8a11ac84eaa584cc186b3a6246feb280f452c Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Mon, 4 Jul 2022 19:38:41 +0530 Subject: [PATCH 1/4] Make option args as per the api --- go.mod | 2 +- go.sum | 1 + logs.go | 31 ++++++++++++++++++++++++++++--- logs_e2e_test.go | 7 +++++-- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 3e16f01..5837de6 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/nanmu42/etherscan-api go 1.13 -require github.com/google/go-cmp v0.5.7 // indirect +require github.com/google/go-cmp v0.5.7 diff --git a/go.sum b/go.sum index a365b08..a6ca3a4 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ 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 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/logs.go b/logs.go index 0b21124..206678d 100644 --- a/logs.go +++ b/logs.go @@ -7,15 +7,40 @@ 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) { +import "fmt" + +// GetLogs gets logs that match "topicsOrOps" filter emitted by the specified "address" between the "fromBlock" and "toBlock" +// Leave nil if needed. Atleast on topic is a must. "topicOrOps" is a sequence of topics of 32 bytes and operators either `and` +// or `or`. +func (c *Client) GetLogs(fromBlock, toBlock *int, address *string, topicsOrOps ...string) (logs []Log, err error) { + if len(topicsOrOps)%2 != 1 { + return nil, fmt.Errorf("atleast one topic is required and each topic must have an operator in between") + } + + if len(topicsOrOps) > 7 { + return nil, fmt.Errorf("cannot provide more than 4 topics") + } + param := M{ "fromBlock": fromBlock, "toBlock": toBlock, - "topic0": topic, "address": address, } + // Not all the operators are supported (https://docs.etherscan.io/api-endpoints/logs). + // Only operator in between topics are supported. + for index, topicOrOp := range topicsOrOps { + if index%2 == 0 { + param[fmt.Sprintf("topic%v", index)] = topicOrOp + } else { + if topicOrOp != "and" && topicOrOp != "or" { + return nil, fmt.Errorf("invalid operator") + } + + param[fmt.Sprintf("topic%v_%v_opr", index-1, index)] = topicOrOp + } + } + err = c.call("logs", "getLogs", param, &logs) return } diff --git a/logs_e2e_test.go b/logs_e2e_test.go index d4894ed..78e23ff 100644 --- a/logs_e2e_test.go +++ b/logs_e2e_test.go @@ -8,7 +8,7 @@ import ( func TestClient_GetLogs(t *testing.T) { expectedLogs := []Log{ - Log{ + { Address: "0x33990122638b9132ca29c723bdf037f1a891a70c", Topics: []string{"0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545", "0x72657075746174696f6e00000000000000000000000000000000000000000000", "0x000000000000000000000000d9b2f59f3b5c7b3c67047d2f03c3e8052470be92"}, Data: "0x", @@ -18,7 +18,10 @@ func TestClient_GetLogs(t *testing.T) { }, } - actualLogs, err := api.GetLogs(379224, 379225, "0x33990122638b9132ca29c723bdf037f1a891a70c", "0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545") + from := 379224 + to := 379225 + address := "0x33990122638b9132ca29c723bdf037f1a891a70c" + actualLogs, err := api.GetLogs(&from, &to, &address, "0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545") noError(t, err, "api.GetLogs") From 83797556e833beab3cad6a6b79282355f05ea2fc Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Mon, 4 Jul 2022 19:41:37 +0530 Subject: [PATCH 2/4] Change the module name --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 5837de6..d63cb28 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/nanmu42/etherscan-api +module github.com/pepsighan/etherscan-api go 1.13 From a69e744af497cde6fe3f546d3e2cc4232dacff20 Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Mon, 4 Jul 2022 19:51:35 +0530 Subject: [PATCH 3/4] Compose the params --- logs.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/logs.go b/logs.go index 206678d..543eddc 100644 --- a/logs.go +++ b/logs.go @@ -21,11 +21,10 @@ func (c *Client) GetLogs(fromBlock, toBlock *int, address *string, topicsOrOps . return nil, fmt.Errorf("cannot provide more than 4 topics") } - param := M{ - "fromBlock": fromBlock, - "toBlock": toBlock, - "address": address, - } + param := M{} + compose(param, "fromBlock", fromBlock) + compose(param, "toBlock", toBlock) + compose(param, "address", address) // Not all the operators are supported (https://docs.etherscan.io/api-endpoints/logs). // Only operator in between topics are supported. From b110f8d518a9fcd3f08e4d95ba84423ea2d0b436 Mon Sep 17 00:00:00 2001 From: Sharad Chand Date: Mon, 4 Jul 2022 20:14:27 +0530 Subject: [PATCH 4/4] Fix the indexes for the topic --- logs.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/logs.go b/logs.go index 543eddc..23882da 100644 --- a/logs.go +++ b/logs.go @@ -30,13 +30,13 @@ func (c *Client) GetLogs(fromBlock, toBlock *int, address *string, topicsOrOps . // Only operator in between topics are supported. for index, topicOrOp := range topicsOrOps { if index%2 == 0 { - param[fmt.Sprintf("topic%v", index)] = topicOrOp + param[fmt.Sprintf("topic%v", index/2)] = topicOrOp } else { if topicOrOp != "and" && topicOrOp != "or" { return nil, fmt.Errorf("invalid operator") } - param[fmt.Sprintf("topic%v_%v_opr", index-1, index)] = topicOrOp + param[fmt.Sprintf("topic%v_%v_opr", index/2, index/2+1)] = topicOrOp } }