Skip to content

Commit

Permalink
[dev] token-related method
Browse files Browse the repository at this point in the history
  • Loading branch information
nanmu42 committed Aug 6, 2018
1 parent e1a4c85 commit c0affeb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions account.go
Expand Up @@ -113,3 +113,15 @@ func (c *Client) UnclesMinedByAddress(address string, page int, offset int) (min
err = c.call("account", "getminedblocks", param, &mined)
return
}

// TokenBalance get erc20-token account balance of address for contractAddress
func (c *Client) TokenBalance(contractAddress, address string) (balance *BigInt, err error) {
param := M{
"contractaddress": contractAddress,
"address": address,
"tag": "latest",
}

err = c.call("account", "tokenbalance", param, &balance)
return
}
9 changes: 9 additions & 0 deletions account_e2e_test.go
Expand Up @@ -112,3 +112,12 @@ func TestClient_UnclesMinedByAddress(t *testing.T) {
t.Errorf("got txs length %v, want %v", len(blocks), wantLen)
}
}

func TestClient_TokenBalance(t *testing.T) {
balance, err := api.TokenBalance("0x57d90b64a1a57749b0f932f1a3395792e12e7055", "0xe04f27eb70e025b78871a2ad7eabe85e61212761")
noError(t, err, "api.TokenBalance")

if balance.Int().Cmp(big.NewInt(0)) != 1 {
t.Errorf("api.TokenBalance not working, got balance %s", balance.Int().String())
}
}
10 changes: 10 additions & 0 deletions stat.go
Expand Up @@ -18,3 +18,13 @@ func (c *Client) EtherLatestPrice() (price LatestPrice, err error) {
err = c.call("stats", "ethprice", nil, &price)
return
}

// TokenTotalSupply gets total supply of token on specified contract address
func (c *Client) TokenTotalSupply(contractAddress string) (totalSupply *BigInt, err error) {
param := M{
"contractaddress": contractAddress,
}

err = c.call("stats", "tokensupply", param, &totalSupply)
return
}
9 changes: 9 additions & 0 deletions stat_e2e_test.go
Expand Up @@ -38,3 +38,12 @@ func TestClient_EtherLatestPrice(t *testing.T) {
t.Errorf("ETHUSDTimestamp is zero")
}
}

func TestClient_TokenTotalSupply(t *testing.T) {
totalSupply, err := api.TokenTotalSupply("0x57d90b64a1a57749b0f932f1a3395792e12e7055")
noError(t, err, "api.TokenTotalSupply")

if totalSupply.Int().Cmp(big.NewInt(100)) != 1 {
t.Errorf("api.TokenTotalSupply not working, totalSupply is %s", totalSupply.Int().String())
}
}

0 comments on commit c0affeb

Please sign in to comment.