Skip to content

Commit 28455ff

Browse files
authored
Merge pull request nanmu42#12 from nanmu42/develop
Workaround for missing tokenDecimal for tokentx calls
2 parents 5adb18a + 8f0b304 commit 28455ff

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

account.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ func (c *Client) InternalTxByAddress(address string, startBlock *int, endBlock *
7979
// contract address and/or from/to address.
8080
//
8181
// leave undesired condition to nil.
82+
//
83+
// Note on a Etherscan bug:
84+
// Some ERC20 contract does not have valid decimals information in Etherscan.
85+
// When that happens, TokenName, TokenSymbol are empty strings,
86+
// and TokenDecimal is 0.
87+
//
88+
// More information can be found at:
89+
// https://github.com/nanmu42/etherscan-api/issues/8
8290
func (c *Client) ERC20Transfers(contractAddress, address *string, startBlock *int, endBlock *int, page int, offset int) (txs []ERC20Transfer, err error) {
8391
param := M{
8492
"page": page,

account_e2e_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func TestClient_ERC20Transfers(t *testing.T) {
7373
const (
7474
wantLen1 = 3
7575
wantLen2 = 458
76+
wantLen3 = 2
7677
)
7778

7879
var a, b = 3273004, 3328071
@@ -92,6 +93,17 @@ func TestClient_ERC20Transfers(t *testing.T) {
9293
if len(txs) != wantLen2 {
9394
t.Errorf("got txs length %v, want %v", len(txs), wantLen2)
9495
}
96+
97+
// some ERC20 contract does not have valid decimals information in Etherscan,
98+
// which brings errors like `json: invalid use of ,string struct tag, trying to unmarshal "" into uint8`
99+
var specialContract = "0x5eac95ad5b287cf44e058dcf694419333b796123"
100+
var specialStartHeight = 6024142
101+
var specialEndHeight = 6485274
102+
txs, err = api.ERC20Transfers(&specialContract, nil, &specialStartHeight, &specialEndHeight, 1, 500)
103+
noError(t, err, "api.ERC20Transfers 2")
104+
if len(txs) != wantLen3 {
105+
t.Errorf("got txs length %v, want %v", len(txs), wantLen3)
106+
}
95107
}
96108

97109
func TestClient_BlocksMinedByAddress(t *testing.T) {

client.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"net/http"
1616
"net/http/httputil"
1717
"net/url"
18+
"time"
1819
)
1920

2021
// Client etherscan API client
@@ -41,7 +42,9 @@ type Client struct {
4142
// please use pre-defined network value
4243
func New(network Network, APIKey string) *Client {
4344
return &Client{
44-
coon: &http.Client{},
45+
coon: &http.Client{
46+
Timeout: 30 * time.Second,
47+
},
4548
network: network,
4649
key: APIKey,
4750
baseURL: fmt.Sprintf(`https://%s.etherscan.io/api?`, network.SubDomain()),
@@ -134,7 +137,12 @@ func (c *Client) call(module, action string, param map[string]interface{}, outco
134137
return
135138
}
136139

137-
err = json.Unmarshal(envelope.Result, outcome)
140+
// workaround for missing tokenDecimal for some tokentx calls
141+
if action == "tokentx" {
142+
err = json.Unmarshal(bytes.Replace(envelope.Result, []byte(`"tokenDecimal":""`), []byte(`"tokenDecimal":"0"`), -1), outcome)
143+
} else {
144+
err = json.Unmarshal(envelope.Result, outcome)
145+
}
138146
if err != nil {
139147
err = wrapErr(err, "json unmarshal outcome")
140148
return

0 commit comments

Comments
 (0)