Repository: trustwallet/blockatlas
Pull request #983 (2020-03-28 01:12:42)

Fix issue with hardcoded token type for any ETH like platform


Patch:
diff --git a/platform/ethereum/token.go b/platform/ethereum/token.go
index 78b15f8..dbf88c1 100644
--- a/platform/ethereum/token.go
+++ b/platform/ethereum/token.go
@@ -1,6 +1,7 @@
 package ethereum
 
 import (
+	"github.com/trustwallet/blockatlas/coin"
 	"github.com/trustwallet/blockatlas/pkg/blockatlas"
 )
 
@@ -14,13 +15,33 @@ func (p *Platform) GetTokenListByAddress(address string) (blockatlas.TokenPage,
 
 // NormalizeToken converts a Ethereum token into the generic model
 func NormalizeToken(srcToken *Contract, coinIndex uint) (t blockatlas.Token, ok bool) {
+	var tokenType blockatlas.TokenType
+	switch coinIndex {
+	case coin.Ethereum().ID:
+		tokenType = blockatlas.TokenTypeERC20
+	case coin.Classic().ID:
+		tokenType = blockatlas.TokenTypeETC20
+	case coin.Poa().ID:
+		tokenType = blockatlas.TokenTypePOA20
+	case coin.Callisto().ID:
+		tokenType = blockatlas.TokenTypeCLO20
+	case coin.Wanchain().ID:
+		tokenType = blockatlas.TokenTypeWAN20
+	case coin.Thundertoken().ID:
+		tokenType = blockatlas.TokenTypeTT20
+	case coin.Gochain().ID:
+		tokenType = blockatlas.TokenTypeGO20
+	default:
+		tokenType = "unknown"
+	}
+
 	t = blockatlas.Token{
 		Name:     srcToken.Name,
 		Symbol:   srcToken.Symbol,
 		TokenID:  srcToken.Address,
 		Coin:     coinIndex,
 		Decimals: srcToken.Decimals,
-		Type:     blockatlas.TokenTypeERC20,
+		Type:     tokenType,
 	}
 
 	return t, true


Test patch:
diff --git a/platform/ethereum/token_test.go b/platform/ethereum/token_test.go
index a2e9fdd..ab7b88f 100644
--- a/platform/ethereum/token_test.go
+++ b/platform/ethereum/token_test.go
@@ -16,27 +16,128 @@ const tokenSrc = `
 	"symbol": "FUS"
 }`
 
-var tokenDst = blockatlas.Token{
-	Name:     "FusChain",
-	Symbol:   "FUS",
-	Decimals: 18,
-	TokenID:  "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
-	Coin:     coin.ETH,
-	Type:     blockatlas.TokenTypeERC20,
-}
-
 type testToken struct {
 	name        string
 	apiResponse string
 	expected    *blockatlas.Token
+	coin        int
 }
 
 func TestNormalizeToken(t *testing.T) {
-	testNormalizeToken(t, &testToken{
-		name:        "token",
-		apiResponse: tokenSrc,
-		expected:    &tokenDst,
-	})
+	var tests = []struct {
+		name     string
+		tokenRaw string
+		coin     int
+		want     blockatlas.Token
+	}{
+		{
+			"ethereum erc20",
+			tokenSrc,
+			coin.ETH,
+			blockatlas.Token{
+				Name:     "FusChain",
+				Symbol:   "FUS",
+				Decimals: 18,
+				TokenID:  "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
+				Coin:     coin.ETH,
+				Type:     blockatlas.TokenTypeERC20,
+			},
+		},
+		{"classic etc20",
+			tokenSrc,
+			coin.ETC,
+			blockatlas.Token{
+				Name:     "FusChain",
+				Symbol:   "FUS",
+				Decimals: 18,
+				TokenID:  "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
+				Coin:     coin.ETC,
+				Type:     blockatlas.TokenTypeETC20,
+			},
+		},
+		{"gochain go20",
+			tokenSrc,
+			coin.GO,
+			blockatlas.Token{
+				Name:     "FusChain",
+				Symbol:   "FUS",
+				Decimals: 18,
+				TokenID:  "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
+				Coin:     coin.GO,
+				Type:     blockatlas.TokenTypeGO20,
+			},
+		},
+		{"thudertoken tt20",
+			tokenSrc,
+			coin.TT,
+			blockatlas.Token{
+				Name:     "FusChain",
+				Symbol:   "FUS",
+				Decimals: 18,
+				TokenID:  "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
+				Coin:     coin.TT,
+				Type:     blockatlas.TokenTypeTT20,
+			},
+		},
+		{"wanchain wan20",
+			tokenSrc,
+			coin.WAN,
+			blockatlas.Token{
+				Name:     "FusChain",
+				Symbol:   "FUS",
+				Decimals: 18,
+				TokenID:  "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
+				Coin:     coin.WAN,
+				Type:     blockatlas.TokenTypeWAN20,
+			},
+		},
+		{"poa poa20",
+			tokenSrc,
+			coin.POA,
+			blockatlas.Token{
+				Name:     "FusChain",
+				Symbol:   "FUS",
+				Decimals: 18,
+				TokenID:  "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
+				Coin:     coin.POA,
+				Type:     blockatlas.TokenTypePOA20,
+			},
+		},
+		{"callisto clo20",
+			tokenSrc,
+			coin.CLO,
+			blockatlas.Token{
+				Name:     "FusChain",
+				Symbol:   "FUS",
+				Decimals: 18,
+				TokenID:  "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
+				Coin:     coin.CLO,
+				Type:     blockatlas.TokenTypeCLO20,
+			},
+		},
+		{"unkown",
+			tokenSrc,
+			1999,
+			blockatlas.Token{
+				Name:     "FusChain",
+				Symbol:   "FUS",
+				Decimals: 18,
+				TokenID:  "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
+				Coin:     1999,
+				Type:     "unknown",
+			},
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			testNormalizeToken(t, &testToken{
+				apiResponse: tt.tokenRaw,
+				expected:    &tt.want,
+				coin:        tt.coin,
+			})
+		})
+	}
+
 }
 
 func testNormalizeToken(t *testing.T, _test *testToken) {
@@ -46,7 +147,7 @@ func testNormalizeToken(t *testing.T, _test *testToken) {
 		t.Error(err)
 		return
 	}
-	tk, ok := NormalizeToken(&token, coin.ETH)
+	tk, ok := NormalizeToken(&token, uint(_test.coin))
 	if !ok {
 		t.Errorf("token: token could not be normalized")
 	}