Skip to content

Commit 3c8032c

Browse files
committed
[fix] workaround for missing tokenDecimal for tokentx calls
Note on a Etherscan bug: Some ERC20 contract does not have valid decimals information in Etherscan. When that happen TokenName, TokenSymbol are empty strings, and TokenDecimal is 0. More information can be found at: nanmu42#8
1 parent 5adb18a commit 3c8032c

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
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 happen 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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ func (c *Client) call(module, action string, param map[string]interface{}, outco
134134
return
135135
}
136136

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

0 commit comments

Comments
 (0)