Skip to content

Commit

Permalink
Fix getting token type for asset from logo path (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
unanoc authored Dec 14, 2021
1 parent 383c0ba commit 2f4429d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pkg/asset/logo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ func GetTokenFromAssetLogoPath(path string) (tokenID, tokenType string) {
suffix := "/logo.png"

if strings.HasPrefix(path, prefix) && strings.HasSuffix(path, suffix) {
tokenType = string(t)
tokenID = path[len(prefix):(len(path) - len(suffix))]

var ok bool
tokenType, ok = types.GetTokenType(chain.ID, tokenID)
if !ok {
tokenType = string(t)
}

break
}
}

if tokenType == "TRC20" && strings.HasPrefix(tokenID, "10") {
tokenType = "TRC10"
}

return tokenID, tokenType
}
51 changes: 51 additions & 0 deletions pkg/asset/logo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package asset_test

import (
"testing"

"github.com/trustwallet/assets-go-libs/pkg/asset"
)

func Test_GetTokenFromAssetLogoPath(t *testing.T) {
tests := []struct {
name string
path string
resulTokenID string
resultTokenType string
}{
{
name: "Tron (TRC20)",
path: "blockchains/tron/assets/TXBcx59eDVndV5upFQnTR2xdvqFd5reXET/logo.png",
resulTokenID: "TXBcx59eDVndV5upFQnTR2xdvqFd5reXET",
resultTokenType: "TRC20",
},
{
name: "Ethereum (ERC20)",
path: "blockchains/ethereum/assets/0x0a2D9370cF74Da3FD3dF5d764e394Ca8205C50B6/logo.png",
resulTokenID: "0x0a2D9370cF74Da3FD3dF5d764e394Ca8205C50B6",
resultTokenType: "ERC20",
},
{
name: "Binance (BEP2)",
path: "blockchains/binance/assets/AAVE-8FA/logo.png",
resulTokenID: "AAVE-8FA",
resultTokenType: "BEP2",
},
{
name: "Smartchain (BEP20)",
path: "blockchains/smartchain/assets/0x0b3f42481C228F70756DbFA0309d3ddC2a5e0F6a/logo.png",
resulTokenID: "0x0b3f42481C228F70756DbFA0309d3ddC2a5e0F6a",
resultTokenType: "BEP20",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tokenID, tokenType := asset.GetTokenFromAssetLogoPath(tt.path)
if tokenID != tt.resulTokenID || tokenType != tt.resultTokenType {
t.Errorf("got tokenID=%s tokenType=%s, want tokenID=%s tokenType=%s",
tokenID, tokenType, tt.resulTokenID, tt.resultTokenType)
}
})
}
}

0 comments on commit 2f4429d

Please sign in to comment.