Skip to content

Commit c0affeb

Browse files
committed
[dev] token-related method
1 parent e1a4c85 commit c0affeb

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

account.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,15 @@ func (c *Client) UnclesMinedByAddress(address string, page int, offset int) (min
113113
err = c.call("account", "getminedblocks", param, &mined)
114114
return
115115
}
116+
117+
// TokenBalance get erc20-token account balance of address for contractAddress
118+
func (c *Client) TokenBalance(contractAddress, address string) (balance *BigInt, err error) {
119+
param := M{
120+
"contractaddress": contractAddress,
121+
"address": address,
122+
"tag": "latest",
123+
}
124+
125+
err = c.call("account", "tokenbalance", param, &balance)
126+
return
127+
}

account_e2e_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,12 @@ func TestClient_UnclesMinedByAddress(t *testing.T) {
112112
t.Errorf("got txs length %v, want %v", len(blocks), wantLen)
113113
}
114114
}
115+
116+
func TestClient_TokenBalance(t *testing.T) {
117+
balance, err := api.TokenBalance("0x57d90b64a1a57749b0f932f1a3395792e12e7055", "0xe04f27eb70e025b78871a2ad7eabe85e61212761")
118+
noError(t, err, "api.TokenBalance")
119+
120+
if balance.Int().Cmp(big.NewInt(0)) != 1 {
121+
t.Errorf("api.TokenBalance not working, got balance %s", balance.Int().String())
122+
}
123+
}

stat.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,13 @@ func (c *Client) EtherLatestPrice() (price LatestPrice, err error) {
1818
err = c.call("stats", "ethprice", nil, &price)
1919
return
2020
}
21+
22+
// TokenTotalSupply gets total supply of token on specified contract address
23+
func (c *Client) TokenTotalSupply(contractAddress string) (totalSupply *BigInt, err error) {
24+
param := M{
25+
"contractaddress": contractAddress,
26+
}
27+
28+
err = c.call("stats", "tokensupply", param, &totalSupply)
29+
return
30+
}

stat_e2e_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,12 @@ func TestClient_EtherLatestPrice(t *testing.T) {
3838
t.Errorf("ETHUSDTimestamp is zero")
3939
}
4040
}
41+
42+
func TestClient_TokenTotalSupply(t *testing.T) {
43+
totalSupply, err := api.TokenTotalSupply("0x57d90b64a1a57749b0f932f1a3395792e12e7055")
44+
noError(t, err, "api.TokenTotalSupply")
45+
46+
if totalSupply.Int().Cmp(big.NewInt(100)) != 1 {
47+
t.Errorf("api.TokenTotalSupply not working, totalSupply is %s", totalSupply.Int().String())
48+
}
49+
}

0 commit comments

Comments
 (0)