Skip to content

Commit 5a36306

Browse files
committed
Added support for querying blocknumber by timestamp
1 parent d6a10b1 commit 5a36306

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

block.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
package etherscan
99

10+
import "strconv"
11+
1012
// BlockReward gets block and uncle rewards by block number
1113
func (c *Client) BlockReward(blockNum int) (rewards BlockRewards, err error) {
1214
param := M{
@@ -16,3 +18,22 @@ func (c *Client) BlockReward(blockNum int) (rewards BlockRewards, err error) {
1618
err = c.call("block", "getblockreward", param, &rewards)
1719
return
1820
}
21+
22+
// BlockReward gets closest block number by UNIX timestamp
23+
func (c *Client) BlockNumber(timestamp int64, closest string) (blockNumber BlockNumberFromTimestamp, err error) {
24+
var result string
25+
param := M{
26+
"timestamp": strconv.Itoa(int(timestamp)),
27+
"closest": closest,
28+
}
29+
30+
err = c.call("block", "getblocknobytime", param, &result)
31+
32+
if err != nil {
33+
return
34+
}
35+
36+
blockNum, err := strconv.ParseInt(result, 10, 64)
37+
blockNumber.BlockNumber = int(blockNum)
38+
return
39+
}

response.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ type BlockRewards struct {
129129
UncleInclusionReward *BigInt `json:"uncleInclusionReward"`
130130
}
131131

132+
// BlockNumberFromTimestamp holds info from query for block by UNIX timestamp
133+
type BlockNumberFromTimestamp struct {
134+
BlockNumber int
135+
}
136+
132137
// LatestPrice holds info from query for latest ether price
133138
type LatestPrice struct {
134139
ETHBTC float64 `json:"ethbtc,string"`

0 commit comments

Comments
 (0)