From e7a984d2262651d195e60ab6195f32a6fc75f5ff Mon Sep 17 00:00:00 2001 From: Viktor Radchenko <1641795+vikmeup@users.noreply.github.com> Date: Mon, 23 Nov 2020 17:10:51 -0800 Subject: [PATCH] Use TokenType from golibs (#1282) * Use TokenType from golibs * Remove TokenType --- go.mod | 2 +- go.sum | 4 +- pkg/blockatlas/tx.go | 65 +++++------------------- pkg/blockatlas/tx_test.go | 51 +++---------------- platform/binance/model.go | 3 +- platform/ethereum/blockbook/model.go | 15 +++--- platform/ethereum/blockbook/token.go | 3 +- platform/ethereum/trustray/token.go | 3 +- platform/ethereum/trustray/token_test.go | 17 ++++--- platform/tron/token.go | 5 +- platform/tron/transaction.go | 11 ++-- platform/tron/transaction_test.go | 7 +-- 12 files changed, 56 insertions(+), 130 deletions(-) diff --git a/go.mod b/go.mod index c5073f559..4a0142d0f 100644 --- a/go.mod +++ b/go.mod @@ -36,7 +36,7 @@ require ( github.com/stretchr/testify v1.6.1 github.com/swaggo/gin-swagger v1.2.0 github.com/swaggo/swag v1.6.7 - github.com/trustwallet/golibs v0.0.13 + github.com/trustwallet/golibs v0.0.15 go.elastic.co/apm v1.8.0 go.elastic.co/apm/module/apmgin v1.8.0 go.elastic.co/apm/module/apmhttp v1.8.0 diff --git a/go.sum b/go.sum index 38876f0f7..7f2ef139c 100644 --- a/go.sum +++ b/go.sum @@ -461,8 +461,8 @@ github.com/swaggo/swag v1.5.1/go.mod h1:1Bl9F/ZBpVWh22nY0zmYyASPO1lI/zIwRDrpZU+t github.com/swaggo/swag v1.6.7 h1:e8GC2xDllJZr3omJkm9YfmK0Y56+rMO3cg0JBKNz09s= github.com/swaggo/swag v1.6.7/go.mod h1:xDhTyuFIujYiN3DKWC/H/83xcfHp+UE/IzWWampG7Zc= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= -github.com/trustwallet/golibs v0.0.13 h1:25XKd76pQr4i6UNoyJYGtrogpN9/YzNNwYzXoxh3h68= -github.com/trustwallet/golibs v0.0.13/go.mod h1:j+Tip4aaW3mu+81S1a+UJ8fSnKzLeJyBueApnCXXRMg= +github.com/trustwallet/golibs v0.0.15 h1:f85eXy/WsmoTSI3cYim4qpTxucW0KYHC4RDgkr1vvJo= +github.com/trustwallet/golibs v0.0.15/go.mod h1:j+Tip4aaW3mu+81S1a+UJ8fSnKzLeJyBueApnCXXRMg= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/ugorji/go v1.1.5-pre/go.mod h1:FwP/aQVg39TXzItUBMwnWp9T9gPQnXw4Poh4/oBQZ/0= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= diff --git a/pkg/blockatlas/tx.go b/pkg/blockatlas/tx.go index 3c909bceb..40a8c4960 100644 --- a/pkg/blockatlas/tx.go +++ b/pkg/blockatlas/tx.go @@ -1,6 +1,7 @@ package blockatlas import ( + "github.com/trustwallet/golibs/tokentype" "sort" "strconv" "strings" @@ -21,20 +22,6 @@ const ( DirectionIncoming Direction = "incoming" DirectionSelf Direction = "yourself" - TokenTypeERC20 TokenType = "ERC20" - TokenTypeBEP2 TokenType = "BEP2" - TokenTypeBEP8 TokenType = "BEP8" - TokenTypeBEP20 TokenType = "BEP20" - TokenTypeTRC10 TokenType = "TRC10" - TokenTypeETC20 TokenType = "ETC20" - TokenTypePOA20 TokenType = "POA20" - TokenTypeTRC20 TokenType = "TRC20" - TokenTypeTRC21 TokenType = "TRC21" - TokenTypeCLO20 TokenType = "CLO20" - TokenTypeGO20 TokenType = "G020" - TokenTypeWAN20 TokenType = "WAN20" - TokenTypeTT20 TokenType = "TT20" - TxTransfer TransactionType = "transfer" TxNativeTokenTransfer TransactionType = "native_token_transfer" TxTokenTransfer TransactionType = "token_transfer" @@ -67,7 +54,6 @@ type ( // Types of transaction statuses Direction string Status string - TokenType string TransactionType string KeyType string KeyTitle string @@ -206,12 +192,12 @@ type ( // Token describes the non-native tokens. // Examples: ERC-20, TRC-20, BEP-2 Token struct { - Name string `json:"name"` - Symbol string `json:"symbol"` - Decimals uint `json:"decimals"` - TokenID string `json:"token_id"` - Coin uint `json:"coin"` - Type TokenType `json:"type"` + Name string `json:"name"` + Symbol string `json:"symbol"` + Decimals uint `json:"decimals"` + TokenID string `json:"token_id"` + Coin uint `json:"coin"` + Type tokentype.Type `json:"type"` } Txs []Tx @@ -435,33 +421,6 @@ 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.Tomochain().ID: - tokenType = TokenTypeTRC21 - case coin.Bsc().ID, coin.Smartchain().ID: - tokenType = TokenTypeBEP20 - default: - tokenType = TokenTypeERC20 - } - return tokenType -} - func (t Tx) AssetModel() (models.Asset, bool) { var a models.Asset switch t.Meta.(type) { @@ -538,17 +497,17 @@ func (t Tx) AssetModel() (models.Asset, bool) { func GetTokenType(c uint, tokenID string) (string, bool) { switch c { case coin.Ethereum().ID: - return string(TokenTypeERC20), true + return string(tokentype.ERC20), true case coin.Tron().ID: _, err := strconv.Atoi(tokenID) if err != nil { - return string(TokenTypeTRC20), true + return string(tokentype.TRC20), true } - return string(TokenTypeTRC10), true + return string(tokentype.TRC10), true case coin.Smartchain().ID: - return string(TokenTypeBEP20), true + return string(tokentype.BEP20), true case coin.Binance().ID: - return string(TokenTypeBEP2), true + return string(tokentype.BEP2), true default: return "", false } diff --git a/pkg/blockatlas/tx_test.go b/pkg/blockatlas/tx_test.go index 842542adc..4fd724425 100644 --- a/pkg/blockatlas/tx_test.go +++ b/pkg/blockatlas/tx_test.go @@ -5,6 +5,7 @@ import ( mapset "github.com/deckarep/golang-set" "github.com/stretchr/testify/assert" "github.com/trustwallet/golibs/coin" + "github.com/trustwallet/golibs/tokentype" "reflect" "sort" "testing" @@ -568,46 +569,6 @@ func TestTx_TokenID(t *testing.T) { } -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.Smartchain().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) - } - }) - } -} - func TestTokenType(t *testing.T) { type testStruct struct { Name string @@ -621,35 +582,35 @@ func TestTokenType(t *testing.T) { Name: "Tron TRC20", ID: coin.Tron().ID, TokenID: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", - WantedType: string(TokenTypeTRC20), + WantedType: string(tokentype.TRC20), WantedOk: true, }, { Name: "Tron TRC10", ID: coin.Tron().ID, TokenID: "1002000", - WantedType: string(TokenTypeTRC10), + WantedType: string(tokentype.TRC10), WantedOk: true, }, { Name: "Ethereum ERC20", ID: coin.Ethereum().ID, TokenID: "dai", - WantedType: string(TokenTypeERC20), + WantedType: string(tokentype.ERC20), WantedOk: true, }, { Name: "Binance BEP20", ID: coin.Smartchain().ID, TokenID: "busd", - WantedType: string(TokenTypeBEP20), + WantedType: string(tokentype.BEP20), WantedOk: true, }, { Name: "Binance BEP10", ID: coin.Binance().ID, TokenID: "busd", - WantedType: string(TokenTypeBEP2), + WantedType: string(tokentype.BEP2), WantedOk: true, }, { diff --git a/platform/binance/model.go b/platform/binance/model.go index c928f805f..f8c3026bf 100644 --- a/platform/binance/model.go +++ b/platform/binance/model.go @@ -1,6 +1,7 @@ package binance import ( + "github.com/trustwallet/golibs/tokentype" "strconv" "strings" "time" @@ -274,7 +275,7 @@ func normalizeToken(srcToken TokenBalance, tokens Tokens) (blockatlas.Token, boo TokenID: token.Symbol, Coin: coin.Binance().ID, Decimals: coin.Binance().Decimals, - Type: blockatlas.TokenTypeBEP2, + Type: tokentype.BEP2, } return result, true diff --git a/platform/ethereum/blockbook/model.go b/platform/ethereum/blockbook/model.go index 415b5294e..f7645ee45 100644 --- a/platform/ethereum/blockbook/model.go +++ b/platform/ethereum/blockbook/model.go @@ -1,9 +1,8 @@ package blockbook import ( + "github.com/trustwallet/golibs/tokentype" "math/big" - - "github.com/trustwallet/blockatlas/pkg/blockatlas" ) type Page struct { @@ -58,12 +57,12 @@ type TokenTransfer struct { // Token contains info about tokens held by an address type Token struct { - Balance string `json:"balance,omitempty"` - Contract string `json:"contract"` - Decimals uint `json:"decimals"` - Name string `json:"name"` - Symbol string `json:"symbol"` - Type blockatlas.TokenType `json:"type"` + Balance string `json:"balance,omitempty"` + Contract string `json:"contract"` + Decimals uint `json:"decimals"` + Name string `json:"name"` + Symbol string `json:"symbol"` + Type tokentype.Type `json:"type"` } // EthereumSpecific contains ethereum specific transaction data diff --git a/platform/ethereum/blockbook/token.go b/platform/ethereum/blockbook/token.go index a99ff339f..5f72ce196 100644 --- a/platform/ethereum/blockbook/token.go +++ b/platform/ethereum/blockbook/token.go @@ -2,6 +2,7 @@ package blockbook import ( "github.com/trustwallet/blockatlas/pkg/blockatlas" + "github.com/trustwallet/golibs/tokentype" ) func (c *Client) GetTokenList(address string, coinIndex uint) (blockatlas.TokenPage, error) { @@ -31,6 +32,6 @@ func NormalizeToken(srcToken *Token, coinIndex uint) blockatlas.Token { TokenID: srcToken.Contract, Coin: coinIndex, Decimals: srcToken.Decimals, - Type: blockatlas.GetEthereumTokenTypeByIndex(coinIndex), + Type: tokentype.GetEthereumTokenTypeByIndex(coinIndex), } } diff --git a/platform/ethereum/trustray/token.go b/platform/ethereum/trustray/token.go index 8ce8d4165..bcd382abb 100644 --- a/platform/ethereum/trustray/token.go +++ b/platform/ethereum/trustray/token.go @@ -2,6 +2,7 @@ package trustray import ( "github.com/trustwallet/blockatlas/pkg/blockatlas" + "github.com/trustwallet/golibs/tokentype" ) func (c *Client) GetTokenList(address string, coinIndex uint) (blockatlas.TokenPage, error) { @@ -14,7 +15,7 @@ func (c *Client) GetTokenList(address string, coinIndex uint) (blockatlas.TokenP // NormalizeToken converts a Ethereum token into the generic model func NormalizeToken(srcToken *Contract, coinIndex uint) blockatlas.Token { - tokenType := blockatlas.GetEthereumTokenTypeByIndex(coinIndex) + tokenType := tokentype.GetEthereumTokenTypeByIndex(coinIndex) return blockatlas.Token{ Name: srcToken.Name, diff --git a/platform/ethereum/trustray/token_test.go b/platform/ethereum/trustray/token_test.go index ba5db892c..6bdff93a3 100644 --- a/platform/ethereum/trustray/token_test.go +++ b/platform/ethereum/trustray/token_test.go @@ -3,6 +3,7 @@ package trustray import ( "bytes" "encoding/json" + "github.com/trustwallet/golibs/tokentype" "testing" "github.com/trustwallet/blockatlas/pkg/blockatlas" @@ -40,7 +41,7 @@ func TestNormalizeToken(t *testing.T) { Decimals: 18, TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A", Coin: coin.ETH, - Type: blockatlas.TokenTypeERC20, + Type: tokentype.ERC20, }, }, {"classic etc20", @@ -52,7 +53,7 @@ func TestNormalizeToken(t *testing.T) { Decimals: 18, TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A", Coin: coin.ETC, - Type: blockatlas.TokenTypeETC20, + Type: tokentype.ETC20, }, }, {"gochain go20", @@ -64,7 +65,7 @@ func TestNormalizeToken(t *testing.T) { Decimals: 18, TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A", Coin: coin.GO, - Type: blockatlas.TokenTypeGO20, + Type: tokentype.GO20, }, }, {"thudertoken tt20", @@ -76,7 +77,7 @@ func TestNormalizeToken(t *testing.T) { Decimals: 18, TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A", Coin: coin.TT, - Type: blockatlas.TokenTypeTT20, + Type: tokentype.TT20, }, }, {"wanchain wan20", @@ -88,7 +89,7 @@ func TestNormalizeToken(t *testing.T) { Decimals: 18, TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A", Coin: coin.WAN, - Type: blockatlas.TokenTypeWAN20, + Type: tokentype.WAN20, }, }, {"poa poa20", @@ -100,7 +101,7 @@ func TestNormalizeToken(t *testing.T) { Decimals: 18, TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A", Coin: coin.POA, - Type: blockatlas.TokenTypePOA20, + Type: tokentype.POA20, }, }, {"callisto clo20", @@ -112,7 +113,7 @@ func TestNormalizeToken(t *testing.T) { Decimals: 18, TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A", Coin: coin.CLO, - Type: blockatlas.TokenTypeCLO20, + Type: tokentype.CLO20, }, }, {"unknown", @@ -124,7 +125,7 @@ func TestNormalizeToken(t *testing.T) { Decimals: 18, TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A", Coin: 1999, - Type: blockatlas.TokenTypeERC20, + Type: tokentype.ERC20, }, }, } diff --git a/platform/tron/token.go b/platform/tron/token.go index 1bd9322dc..31d9c5133 100644 --- a/platform/tron/token.go +++ b/platform/tron/token.go @@ -4,6 +4,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/trustwallet/blockatlas/pkg/blockatlas" "github.com/trustwallet/golibs/coin" + "github.com/trustwallet/golibs/tokentype" "strings" "sync" "time" @@ -41,7 +42,7 @@ func (p *Platform) GetTokenListByAddress(address string) (blockatlas.TokenPage, Decimals: uint(t.Decimals), TokenID: t.ContractAddress, Coin: coin.Tron().ID, - Type: blockatlas.TokenTypeTRC20, + Type: tokentype.TRC20, }) } @@ -84,6 +85,6 @@ func NormalizeToken(info AssetInfo) blockatlas.Token { TokenID: info.ID, Coin: coin.TRX, Decimals: info.Decimals, - Type: blockatlas.TokenTypeTRC10, + Type: tokentype.TRC10, } } diff --git a/platform/tron/transaction.go b/platform/tron/transaction.go index 2509f9f6f..2f7847311 100644 --- a/platform/tron/transaction.go +++ b/platform/tron/transaction.go @@ -6,6 +6,7 @@ import ( "github.com/trustwallet/blockatlas/pkg/address" "github.com/trustwallet/blockatlas/pkg/blockatlas" "github.com/trustwallet/golibs/coin" + "github.com/trustwallet/golibs/tokentype" "strconv" "strings" ) @@ -38,13 +39,13 @@ func (p *Platform) GetTokenTxsByAddress(address, token string) (blockatlas.TxPag tokenType := getTokenType(token) switch tokenType { - case blockatlas.TokenTypeTRC10: + case tokentype.TRC10: txs, err := p.fetchTransactionsForTRC10Tokens(address, token) if err != nil { return nil, err } return txs, nil - case blockatlas.TokenTypeTRC20: + case tokentype.TRC20: trc20Transactions, err := p.client.fetchTRC20Transactions(address) if err != nil { return nil, err @@ -55,12 +56,12 @@ func (p *Platform) GetTokenTxsByAddress(address, token string) (blockatlas.TxPag } } -func getTokenType(token string) blockatlas.TokenType { +func getTokenType(token string) tokentype.Type { _, err := strconv.Atoi(token) if err != nil { - return blockatlas.TokenTypeTRC20 + return tokentype.TRC20 } else { - return blockatlas.TokenTypeTRC10 + return tokentype.TRC10 } } diff --git a/platform/tron/transaction_test.go b/platform/tron/transaction_test.go index 52ad1bd11..843df2abd 100644 --- a/platform/tron/transaction_test.go +++ b/platform/tron/transaction_test.go @@ -5,6 +5,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/trustwallet/blockatlas/pkg/blockatlas" "github.com/trustwallet/golibs/coin" + "github.com/trustwallet/golibs/tokentype" "net/http/httptest" "testing" ) @@ -165,10 +166,10 @@ func Test_getTokenType(t *testing.T) { tests := []struct { name string token string - want blockatlas.TokenType + want tokentype.Type }{ - {"default trc20", "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", blockatlas.TokenTypeTRC20}, - {"default trc10", "1002001", blockatlas.TokenTypeTRC10}, + {"default trc20", "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", tokentype.TRC20}, + {"default trc10", "1002001", tokentype.TRC10}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {