Skip to content

Commit 3a9f979

Browse files
committed
[dev] stats module
1 parent 14906ce commit 3a9f979

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

response.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,11 @@ type BlockRewards struct {
128128
} `json:"uncles"`
129129
UncleInclusionReward *BigInt `json:"uncleInclusionReward"`
130130
}
131+
132+
// LatestPrice holds info from query for latest ether price
133+
type LatestPrice struct {
134+
ETHBTC float64 `json:"ethbtc,string"`
135+
ETHBTCTimestamp Time `json:"ethbtc_timestamp"`
136+
ETHUSD float64 `json:"ethusd,string"`
137+
ETHUSDTimestamp Time `json:"ethusd_timestamp"`
138+
}

stat.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2018 LI Zhennan
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+
// EtherTotalSupply gets total supply of ether
11+
func (c *Client) EtherTotalSupply() (totalSupply *BigInt, err error) {
12+
err = c.call("stats", "ethsupply", nil, &totalSupply)
13+
return
14+
}
15+
16+
// EtherLatestPrice gets the latest ether price, in BTC and USD
17+
func (c *Client) EtherLatestPrice() (price LatestPrice, err error) {
18+
err = c.call("stats", "ethprice", nil, &price)
19+
return
20+
}

stat_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2018 LI Zhennan
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+
import (
11+
"math/big"
12+
"testing"
13+
)
14+
15+
func TestClient_EtherTotalSupply(t *testing.T) {
16+
totalSupply, err := api.EtherTotalSupply()
17+
noError(t, err, "api.EtherTotalSupply")
18+
19+
if totalSupply.Int().Cmp(big.NewInt(100)) != 1 {
20+
t.Errorf("api.EtherTotalSupply not working, totalSupply is %s", totalSupply.Int().String())
21+
}
22+
}
23+
24+
func TestClient_EtherLatestPrice(t *testing.T) {
25+
latest, err := api.EtherLatestPrice()
26+
noError(t, err, "api.EtherLatestPrice")
27+
28+
if latest.ETHBTC == 0 {
29+
t.Errorf("ETHBTC got 0")
30+
}
31+
if latest.ETHBTCTimestamp.Time().IsZero() {
32+
t.Errorf("ETHBTCTimestamp is zero")
33+
}
34+
if latest.ETHUSD == 0 {
35+
t.Errorf("ETHUSD got 0")
36+
}
37+
if latest.ETHUSDTimestamp.Time().IsZero() {
38+
t.Errorf("ETHUSDTimestamp is zero")
39+
}
40+
}

0 commit comments

Comments
 (0)