From c340cb937103f4edef88a11b7661e6a771f753e5 Mon Sep 17 00:00:00 2001 From: Viktor Radchenko <1641795+vikmeup@users.noreply.github.com> Date: Mon, 1 Mar 2021 18:41:39 -0800 Subject: [PATCH] Add GetImageURL from asset id (#51) * Add GetImageURL from asset id * Update image_test.go * Update image.go --- asset/id_test.go | 3 ++- asset/image.go | 21 ++++++++++++++++++++ asset/image_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 asset/image.go create mode 100644 asset/image_test.go diff --git a/asset/id_test.go b/asset/id_test.go index e31085a..9e70945 100644 --- a/asset/id_test.go +++ b/asset/id_test.go @@ -2,8 +2,9 @@ package asset import ( "errors" - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestParseID(t *testing.T) { diff --git a/asset/image.go b/asset/image.go new file mode 100644 index 0000000..8379285 --- /dev/null +++ b/asset/image.go @@ -0,0 +1,21 @@ +package asset + +import ( + "fmt" + + "github.com/trustwallet/golibs/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 "" +} diff --git a/asset/image_test.go b/asset/image_test.go new file mode 100644 index 0000000..18cafb6 --- /dev/null +++ b/asset/image_test.go @@ -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) + } + }) + } +}