Repository: trustwallet/blockatlas
Pull request #1213 (2020-09-02 02:52:38)

Add BEP20 support
- Expose `GetEthereumTokenTypeByIndex` function, since `TokenTypeBEP20` types already exposed

codecov[bot]: # [Codecov](https://codecov.io/gh/trustwallet/blockatlas/pull/1213?src=pr&el=h1) Report
> Merging [#1213](https://codecov.io/gh/trustwallet/blockatlas/pull/1213?src=pr&el=desc) into [master](https://codecov.io/gh/trustwallet/blockatlas/commit/ce5a721781b6c960ba4d0b94602dfe5da1839c1f?el=desc) will **decrease** coverage by `0%`.
> The diff coverage is `0%`.

[![Impacted file tree graph](https://codecov.io/gh/trustwallet/blockatlas/pull/1213/graphs/tree.svg?width=650&height=150&src=pr&token=jnrjoEQAqv)](https://codecov.io/gh/trustwallet/blockatlas/pull/1213?src=pr&el=tree)

```diff
@@          Coverage Diff           @@
##           master   #1213   +/-   ##
======================================
- Coverage      37%     37%   -1%     
======================================
  Files         175     175           
  Lines        6642    6644    +2     
======================================
  Hits         2484    2484           
- Misses       3970    3972    +2     
  Partials      188     188           
```


| [Impacted Files](https://codecov.io/gh/trustwallet/blockatlas/pull/1213?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/blockatlas/tx.go](https://codecov.io/gh/trustwallet/blockatlas/pull/1213/diff?src=pr&el=tree#diff-cGtnL2Jsb2NrYXRsYXMvdHguZ28=) | `76% <ø> (ø)` | |
| [platform/ethereum/trustray/token.go](https://codecov.io/gh/trustwallet/blockatlas/pull/1213/diff?src=pr&el=tree#diff-cGxhdGZvcm0vZXRoZXJldW0vdHJ1c3RyYXkvdG9rZW4uZ28=) | `69% <0%> (-4%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/trustwallet/blockatlas/pull/1213?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/trustwallet/blockatlas/pull/1213?src=pr&el=footer). Last update [ce5a721...1e101f0](https://codecov.io/gh/trustwallet/blockatlas/pull/1213?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/pkg/blockatlas/tx.go b/pkg/blockatlas/tx.go
index 02b83d8..f8ab42e 100644
--- a/pkg/blockatlas/tx.go
+++ b/pkg/blockatlas/tx.go
@@ -19,6 +19,7 @@ const (
 	TokenTypeERC20 TokenType = "ERC20"
 	TokenTypeBEP2  TokenType = "BEP2"
 	TokenTypeBEP8  TokenType = "BEP8"
+	TokenTypeBEP20 TokenType = "BEP20"
 	TokenTypeTRC10 TokenType = "TRC10"
 	TokenTypeETC20 TokenType = "ETC20"
 	TokenTypePOA20 TokenType = "POA20"
@@ -375,3 +376,28 @@ func InferValue(tx *Tx, direction Direction, addressSet mapset.Set) Amount {
 	}
 	return value
 }
+
+func GetEthereumTokenTypeByIndex(coinIndex uint) TokenType {
+	var tokenType TokenType
+	switch coinIndex {
+	case coin.Ethereum().ID:
+		tokenType = TokenTypeERC20
+	case coin.Classic().ID:
+		tokenType = TokenTypeETC20
+	case coin.Poa().ID:
+		tokenType = TokenTypePOA20
+	case coin.Callisto().ID:
+		tokenType = TokenTypeCLO20
+	case coin.Wanchain().ID:
+		tokenType = TokenTypeWAN20
+	case coin.Thundertoken().ID:
+		tokenType = TokenTypeTT20
+	case coin.Gochain().ID:
+		tokenType = TokenTypeGO20
+	case coin.Bsc().ID:
+		tokenType = TokenTypeBEP20
+	default:
+		tokenType = TokenTypeERC20
+	}
+	return tokenType
+}
diff --git a/platform/ethereum/blockbook/token.go b/platform/ethereum/blockbook/token.go
index 2e8e2b0..a99ff33 100644
--- a/platform/ethereum/blockbook/token.go
+++ b/platform/ethereum/blockbook/token.go
@@ -2,7 +2,6 @@ package blockbook
 
 import (
 	"github.com/trustwallet/blockatlas/pkg/blockatlas"
-	"github.com/trustwallet/blockatlas/platform/ethereum/trustray"
 )
 
 func (c *Client) GetTokenList(address string, coinIndex uint) (blockatlas.TokenPage, error) {
@@ -32,6 +31,6 @@ func NormalizeToken(srcToken *Token, coinIndex uint) blockatlas.Token {
 		TokenID:  srcToken.Contract,
 		Coin:     coinIndex,
 		Decimals: srcToken.Decimals,
-		Type:     trustray.GetTokenTypeByIndex(coinIndex),
+		Type:     blockatlas.GetEthereumTokenTypeByIndex(coinIndex),
 	}
 }
diff --git a/platform/ethereum/trustray/token.go b/platform/ethereum/trustray/token.go
index fe68a92..8ce8d41 100644
--- a/platform/ethereum/trustray/token.go
+++ b/platform/ethereum/trustray/token.go
@@ -1,7 +1,6 @@
 package trustray
 
 import (
-	"github.com/trustwallet/blockatlas/coin"
 	"github.com/trustwallet/blockatlas/pkg/blockatlas"
 )
 
@@ -13,32 +12,9 @@ func (c *Client) GetTokenList(address string, coinIndex uint) (blockatlas.TokenP
 	return NormalizeTokens(account.Docs, coinIndex), nil
 }
 
-func GetTokenTypeByIndex(coinIndex uint) blockatlas.TokenType {
-	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"
-	}
-	return tokenType
-}
-
 // NormalizeToken converts a Ethereum token into the generic model
 func NormalizeToken(srcToken *Contract, coinIndex uint) blockatlas.Token {
-	tokenType := GetTokenTypeByIndex(coinIndex)
+	tokenType := blockatlas.GetEthereumTokenTypeByIndex(coinIndex)
 
 	return blockatlas.Token{
 		Name:     srcToken.Name,


Test patch:
diff --git a/pkg/blockatlas/tx_test.go b/pkg/blockatlas/tx_test.go
index 5b7db6e..738d4ec 100644
--- a/pkg/blockatlas/tx_test.go
+++ b/pkg/blockatlas/tx_test.go
@@ -565,3 +565,43 @@ func TestTx_TokenID(t *testing.T) {
 	assert.True(t, ok3)
 
 }
+
+func TestGetEthereumTokenTypeByIndex(t *testing.T) {
+	type args struct {
+		coinIndex uint
+	}
+	tests := []struct {
+		name string
+		args args
+		want TokenType
+	}{
+		{
+			"Ethereum",
+			args{
+				coinIndex: coin.Ethereum().ID,
+			},
+			TokenTypeERC20,
+		},
+		{
+			"Smart Chain",
+			args{
+				coinIndex: coin.Bsc().ID,
+			},
+			TokenTypeBEP20,
+		},
+		{
+			"Default Name",
+			args{
+				coinIndex: coin.Bitcoin().ID,
+			},
+			TokenTypeERC20,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := GetEthereumTokenTypeByIndex(tt.args.coinIndex); got != tt.want {
+				t.Errorf("GetEthereumTokenTypeByIndex() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}
diff --git a/platform/ethereum/trustray/token_test.go b/platform/ethereum/trustray/token_test.go
index c2d1341..e7c7acc 100644
--- a/platform/ethereum/trustray/token_test.go
+++ b/platform/ethereum/trustray/token_test.go
@@ -115,7 +115,7 @@ func TestNormalizeToken(t *testing.T) {
 				Type:     blockatlas.TokenTypeCLO20,
 			},
 		},
-		{"unkown",
+		{"unknown",
 			tokenSrc,
 			1999,
 			blockatlas.Token{
@@ -124,7 +124,7 @@ func TestNormalizeToken(t *testing.T) {
 				Decimals: 18,
 				TokenID:  "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
 				Coin:     1999,
-				Type:     "unknown",
+				Type:     blockatlas.TokenTypeERC20,
 			},
 		},
 	}