Skip to content

Commit

Permalink
Merge pull request #12 from nanmu42/develop
Browse files Browse the repository at this point in the history
Workaround for missing tokenDecimal for tokentx calls
  • Loading branch information
nanmu42 authored Nov 20, 2018
2 parents 5adb18a + 8f0b304 commit 28455ff
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
8 changes: 8 additions & 0 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ func (c *Client) InternalTxByAddress(address string, startBlock *int, endBlock *
// contract address and/or from/to address.
//
// leave undesired condition to nil.
//
// Note on a Etherscan bug:
// Some ERC20 contract does not have valid decimals information in Etherscan.
// When that happens, TokenName, TokenSymbol are empty strings,
// and TokenDecimal is 0.
//
// More information can be found at:
// https://github.com/nanmu42/etherscan-api/issues/8
func (c *Client) ERC20Transfers(contractAddress, address *string, startBlock *int, endBlock *int, page int, offset int) (txs []ERC20Transfer, err error) {
param := M{
"page": page,
Expand Down
12 changes: 12 additions & 0 deletions account_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestClient_ERC20Transfers(t *testing.T) {
const (
wantLen1 = 3
wantLen2 = 458
wantLen3 = 2
)

var a, b = 3273004, 3328071
Expand All @@ -92,6 +93,17 @@ func TestClient_ERC20Transfers(t *testing.T) {
if len(txs) != wantLen2 {
t.Errorf("got txs length %v, want %v", len(txs), wantLen2)
}

// some ERC20 contract does not have valid decimals information in Etherscan,
// which brings errors like `json: invalid use of ,string struct tag, trying to unmarshal "" into uint8`
var specialContract = "0x5eac95ad5b287cf44e058dcf694419333b796123"
var specialStartHeight = 6024142
var specialEndHeight = 6485274
txs, err = api.ERC20Transfers(&specialContract, nil, &specialStartHeight, &specialEndHeight, 1, 500)
noError(t, err, "api.ERC20Transfers 2")
if len(txs) != wantLen3 {
t.Errorf("got txs length %v, want %v", len(txs), wantLen3)
}
}

func TestClient_BlocksMinedByAddress(t *testing.T) {
Expand Down
12 changes: 10 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"time"
)

// Client etherscan API client
Expand All @@ -41,7 +42,9 @@ type Client struct {
// please use pre-defined network value
func New(network Network, APIKey string) *Client {
return &Client{
coon: &http.Client{},
coon: &http.Client{
Timeout: 30 * time.Second,
},
network: network,
key: APIKey,
baseURL: fmt.Sprintf(`https://%s.etherscan.io/api?`, network.SubDomain()),
Expand Down Expand Up @@ -134,7 +137,12 @@ func (c *Client) call(module, action string, param map[string]interface{}, outco
return
}

err = json.Unmarshal(envelope.Result, outcome)
// workaround for missing tokenDecimal for some tokentx calls
if action == "tokentx" {
err = json.Unmarshal(bytes.Replace(envelope.Result, []byte(`"tokenDecimal":""`), []byte(`"tokenDecimal":"0"`), -1), outcome)
} else {
err = json.Unmarshal(envelope.Result, outcome)
}
if err != nil {
err = wrapErr(err, "json unmarshal outcome")
return
Expand Down

0 comments on commit 28455ff

Please sign in to comment.