diff --git a/cointop/coin.go b/cointop/coin.go index 861f7cb..8d2526e 100644 --- a/cointop/coin.go +++ b/cointop/coin.go @@ -1,6 +1,10 @@ package cointop -import log "github.com/sirupsen/logrus" +import ( + "errors" + + log "github.com/sirupsen/logrus" +) // Coin is the row structure type Coin struct { @@ -92,3 +96,42 @@ func (ct *Cointop) CoinByID(id string) *Coin { } return nil } + +// UpdateCoin updates coin info after fetching from API +func (ct *Cointop) UpdateCoin(coin *Coin) error { + log.Debug("UpdateCoin()") + v, err := ct.api.GetCoinData(coin.Name, ct.State.currencyConversion) + if err != nil { + log.Debugf("UpdateCoin() could not fetch coin data %s", coin.Name) + return err + } + + k, found := ct.State.allCoinsSlugMap.Load(coin.Name) + if !found { + log.Debugf("UpdateCoin() could not found coin %s in the slug map", coin.Name) + return errors.New("could not find coin index in allCoinsSlugMap") + } + + coin = &Coin{ + ID: v.ID, + Name: v.Name, + Symbol: v.Symbol, + Rank: v.Rank, + Price: v.Price, + Volume24H: v.Volume24H, + MarketCap: v.MarketCap, + AvailableSupply: v.AvailableSupply, + TotalSupply: v.TotalSupply, + PercentChange1H: v.PercentChange1H, + PercentChange24H: v.PercentChange24H, + PercentChange7D: v.PercentChange7D, + PercentChange30D: v.PercentChange30D, + PercentChange1Y: v.PercentChange1Y, + LastUpdated: v.LastUpdated, + Slug: v.Slug, + } + + ct.State.allCoinsSlugMap.Store(k, coin) + + return nil +} diff --git a/cointop/list.go b/cointop/list.go index a663c40..12081d5 100644 --- a/cointop/list.go +++ b/cointop/list.go @@ -165,6 +165,7 @@ func (ct *Cointop) processCoins(coins []types.Coin) { c.PercentChange1Y = cm.PercentChange1Y c.LastUpdated = cm.LastUpdated c.Favorite = cm.Favorite + c.Slug = cm.Slug } } diff --git a/cointop/table.go b/cointop/table.go index d5f44a4..b8477d6 100644 --- a/cointop/table.go +++ b/cointop/table.go @@ -197,6 +197,15 @@ func (ct *Cointop) RowLink() string { return "" } + // TODO: Can remove this one after some releases + // because it is a way to force old client refresh coin to have a slug + if coin.Slug == "" { + if err := ct.UpdateCoin(coin); err != nil { + log.Debugf("RowLink() Update coin got err %s", err.Error()) + return "" + } + } + return ct.api.CoinLink(coin.Slug) }