Skip to content
This repository was archived by the owner on Mar 16, 2022. It is now read-only.

Commit

Permalink
Use TokenType from golibs (#1282)
Browse files Browse the repository at this point in the history
* Use TokenType from golibs

* Remove TokenType
  • Loading branch information
vikmeup authored Nov 24, 2020
1 parent 91e36d7 commit e7a984d
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 130 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
65 changes: 12 additions & 53 deletions pkg/blockatlas/tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package blockatlas

import (
"github.com/trustwallet/golibs/tokentype"
"sort"
"strconv"
"strings"
Expand All @@ -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"
Expand Down Expand Up @@ -67,7 +54,6 @@ type (
// Types of transaction statuses
Direction string
Status string
TokenType string
TransactionType string
KeyType string
KeyTitle string
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
51 changes: 6 additions & 45 deletions pkg/blockatlas/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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,
},
{
Expand Down
3 changes: 2 additions & 1 deletion platform/binance/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package binance

import (
"github.com/trustwallet/golibs/tokentype"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions platform/ethereum/blockbook/model.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package blockbook

import (
"github.com/trustwallet/golibs/tokentype"
"math/big"

"github.com/trustwallet/blockatlas/pkg/blockatlas"
)

type Page struct {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion platform/ethereum/blockbook/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
}
}
3 changes: 2 additions & 1 deletion platform/ethereum/trustray/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
17 changes: 9 additions & 8 deletions platform/ethereum/trustray/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package trustray
import (
"bytes"
"encoding/json"
"github.com/trustwallet/golibs/tokentype"
"testing"

"github.com/trustwallet/blockatlas/pkg/blockatlas"
Expand Down Expand Up @@ -40,7 +41,7 @@ func TestNormalizeToken(t *testing.T) {
Decimals: 18,
TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
Coin: coin.ETH,
Type: blockatlas.TokenTypeERC20,
Type: tokentype.ERC20,
},
},
{"classic etc20",
Expand All @@ -52,7 +53,7 @@ func TestNormalizeToken(t *testing.T) {
Decimals: 18,
TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
Coin: coin.ETC,
Type: blockatlas.TokenTypeETC20,
Type: tokentype.ETC20,
},
},
{"gochain go20",
Expand All @@ -64,7 +65,7 @@ func TestNormalizeToken(t *testing.T) {
Decimals: 18,
TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
Coin: coin.GO,
Type: blockatlas.TokenTypeGO20,
Type: tokentype.GO20,
},
},
{"thudertoken tt20",
Expand All @@ -76,7 +77,7 @@ func TestNormalizeToken(t *testing.T) {
Decimals: 18,
TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
Coin: coin.TT,
Type: blockatlas.TokenTypeTT20,
Type: tokentype.TT20,
},
},
{"wanchain wan20",
Expand All @@ -88,7 +89,7 @@ func TestNormalizeToken(t *testing.T) {
Decimals: 18,
TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
Coin: coin.WAN,
Type: blockatlas.TokenTypeWAN20,
Type: tokentype.WAN20,
},
},
{"poa poa20",
Expand All @@ -100,7 +101,7 @@ func TestNormalizeToken(t *testing.T) {
Decimals: 18,
TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
Coin: coin.POA,
Type: blockatlas.TokenTypePOA20,
Type: tokentype.POA20,
},
},
{"callisto clo20",
Expand All @@ -112,7 +113,7 @@ func TestNormalizeToken(t *testing.T) {
Decimals: 18,
TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
Coin: coin.CLO,
Type: blockatlas.TokenTypeCLO20,
Type: tokentype.CLO20,
},
},
{"unknown",
Expand All @@ -124,7 +125,7 @@ func TestNormalizeToken(t *testing.T) {
Decimals: 18,
TokenID: "0xa14839c9837657EFcDE754EbEAF5cbECDd801B2A",
Coin: 1999,
Type: blockatlas.TokenTypeERC20,
Type: tokentype.ERC20,
},
},
}
Expand Down
Loading

0 comments on commit e7a984d

Please sign in to comment.