diff --git a/cointop/list.go b/cointop/list.go index ff66e6a..a663c40 100644 --- a/cointop/list.go +++ b/cointop/list.go @@ -8,8 +8,10 @@ import ( log "github.com/sirupsen/logrus" ) -var coinslock sync.Mutex -var updatecoinsmux sync.Mutex +var ( + coinslock sync.Mutex + updatecoinsmux sync.Mutex +) // UpdateCoins updates coins view func (ct *Cointop) UpdateCoins() error { @@ -110,6 +112,7 @@ func (ct *Cointop) processCoins(coins []types.Coin) { PercentChange30D: v.PercentChange30D, PercentChange1Y: v.PercentChange1Y, LastUpdated: v.LastUpdated, + Slug: v.Slug, }) if ilast != nil { last, _ := ilast.(*Coin) diff --git a/cointop/table.go b/cointop/table.go index bfdf8ab..d5f44a4 100644 --- a/cointop/table.go +++ b/cointop/table.go @@ -197,7 +197,7 @@ func (ct *Cointop) RowLink() string { return "" } - return ct.api.CoinLink(coin.Name) + return ct.api.CoinLink(coin.Slug) } // RowLinkShort returns a shortened version of the row url link diff --git a/pkg/api/impl/coingecko/coingecko.go b/pkg/api/impl/coingecko/coingecko.go index 5f548b3..ac9cb66 100644 --- a/pkg/api/impl/coingecko/coingecko.go +++ b/pkg/api/impl/coingecko/coingecko.go @@ -267,15 +267,13 @@ func (s *Service) Price(name string, convert string) (float64, error) { return 0, ErrNotFound } -// CoinLink returns the URL link for the coin -func (s *Service) CoinLink(name string) string { - ID := s.coinNameToID(name) - return fmt.Sprintf("https://www.coingecko.com/en/coins/%s", ID) +func (s *Service) CoinLink(slug string) string { + // slug is API ID of coin + return fmt.Sprintf("https://www.coingecko.com/en/coins/%s", slug) } // SupportedCurrencies returns a list of supported currencies func (s *Service) SupportedCurrencies() []string { - // keep these in alphabetical order return []string{ "AED", @@ -462,6 +460,7 @@ func (s *Service) getPaginatedCoinData(convert string, offset int, names []strin PercentChange1Y: util.FormatPercentChange(percentChange1Y), Volume24H: util.FormatVolume(item.TotalVolume), LastUpdated: util.FormatLastUpdated(item.LastUpdated), + Slug: item.ID, }) } } diff --git a/pkg/api/impl/coinmarketcap/coinmarketcap.go b/pkg/api/impl/coinmarketcap/coinmarketcap.go index 71a7b73..5d8ee75 100644 --- a/pkg/api/impl/coinmarketcap/coinmarketcap.go +++ b/pkg/api/impl/coinmarketcap/coinmarketcap.go @@ -90,6 +90,7 @@ func (s *Service) getPaginatedCoinData(convert string, offset int) ([]apitypes.C PercentChange7D: util.FormatPercentChange(quote.PercentChange7D), Volume24H: util.FormatVolume(v.Quote[convert].Volume24H), LastUpdated: util.FormatLastUpdated(v.LastUpdated), + Slug: util.FormatSlug(v.Slug), }) } return ret, nil @@ -297,7 +298,6 @@ func (s *Service) GetGlobalMarketData(convert string) (apitypes.GlobalMarketData market, err := s.client.GlobalMetrics.LatestQuotes(&cmc.QuoteOptions{ Convert: convert, }) - if err != nil { return ret, err } @@ -332,8 +332,7 @@ func (s *Service) Price(name string, convert string) (float64, error) { } // CoinLink returns the URL link for the coin -func (s *Service) CoinLink(name string) string { - slug := util.NameToSlug(name) +func (s *Service) CoinLink(slug string) string { return fmt.Sprintf("https://coinmarketcap.com/currencies/%s", slug) } diff --git a/pkg/api/interface.go b/pkg/api/interface.go index 55ffcf2..206b549 100644 --- a/pkg/api/interface.go +++ b/pkg/api/interface.go @@ -13,7 +13,7 @@ type Interface interface { GetGlobalMarketData(convert string) (types.GlobalMarketData, error) GetCoinData(name string, convert string) (types.Coin, error) GetCoinDataBatch(names []string, convert string) ([]types.Coin, error) - CoinLink(name string) string + CoinLink(slug string) string SupportedCurrencies() []string Price(name string, convert string) (float64, error) GetExchangeRate(convertFrom, convertTo string, cached bool) (float64, error) // I don't love this caching diff --git a/pkg/api/types/types.go b/pkg/api/types/types.go index 3e4bf37..a5fb5f7 100644 --- a/pkg/api/types/types.go +++ b/pkg/api/types/types.go @@ -17,6 +17,8 @@ type Coin struct { PercentChange30D float64 `json:"percentChange30D"` PercentChange1Y float64 `json:"percentChange1Y"` LastUpdated string `json:"lastUpdated"` + // Slug uses to access the coin's info web page + Slug string `json:"slug"` } // GlobalMarketData struct diff --git a/pkg/api/util/util.go b/pkg/api/util/util.go index ffb52db..f0af4d7 100644 --- a/pkg/api/util/util.go +++ b/pkg/api/util/util.go @@ -29,6 +29,10 @@ func FormatName(name string) string { return name } +func FormatSlug(slug string) string { + return slug +} + // FormatRank formats the rank value func FormatRank(rank interface{}) int { switch v := rank.(type) {