Skip to content

Commit

Permalink
Merge pull request #1 from trustwallet/vikmeup/asset-image
Browse files Browse the repository at this point in the history
Add asset image
  • Loading branch information
vikmeup authored Aug 19, 2021
2 parents 629475c + ef72e75 commit 1b58524
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
21 changes: 21 additions & 0 deletions asset/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package asset

import (
"fmt"

"github.com/trustwallet/go-primitives/coin"
)

func GetImageURL(endpoint, asset string) string {
coinId, tokenId, err := ParseID(asset)
if err != nil {
return ""
}
if c, ok := coin.Coins[coinId]; ok {
if len(tokenId) > 0 {
return fmt.Sprintf("%s/blockchains/%s/assets/%s/logo.png", endpoint, c.Handle, tokenId)
}
return fmt.Sprintf("%s/blockchains/%s/info/logo.png", endpoint, c.Handle)
}
return ""
}
47 changes: 47 additions & 0 deletions asset/image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package asset

import "testing"

func TestGetImageURL(t *testing.T) {
type args struct {
endpoint string
asset string
}
tests := []struct {
name string
args args
want string
}{
{
"Test coin",
args{
endpoint: "https://assets.com",
asset: "c60",
},
"https://assets.com/blockchains/ethereum/info/logo.png",
},
{
"Test coin",
args{
endpoint: "https://assets.com",
asset: "c60_t123",
},
"https://assets.com/blockchains/ethereum/assets/123/logo.png",
},
{
"Test invalid coin",
args{
endpoint: "https://assets.com",
asset: "c123",
},
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetImageURL(tt.args.endpoint, tt.args.asset); got != tt.want {
t.Errorf("GetImageURL() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 1b58524

Please sign in to comment.