Store slug field then use for coin link

pull/275/head
Vuong 3 years ago
parent 8b8db3bd13
commit b2cfe9f614
No known key found for this signature in database
GPG Key ID: 9E32EEF440B0A5D4

@ -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)

@ -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

@ -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,
})
}
}

@ -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)
}

@ -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

@ -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

@ -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) {

Loading…
Cancel
Save