2018-03-31 08:18:53 +00:00
|
|
|
package api
|
2018-03-30 18:34:45 +00:00
|
|
|
|
|
|
|
import (
|
2018-03-31 08:18:53 +00:00
|
|
|
types "github.com/miguelmota/cointop/pkg/api/types"
|
2018-03-30 18:34:45 +00:00
|
|
|
cmc "github.com/miguelmota/go-coinmarketcap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Service service
|
|
|
|
type Service struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// New new service
|
|
|
|
func New() *Service {
|
|
|
|
return &Service{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllCoinData gets all coin data
|
|
|
|
func (s *Service) GetAllCoinData(limit int) (map[string]types.Coin, error) {
|
|
|
|
ret := make(map[string]types.Coin)
|
|
|
|
coins, err := cmc.GetAllCoinData(int(limit))
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
for _, v := range coins {
|
|
|
|
ret[v.ID] = types.Coin{
|
|
|
|
ID: v.ID,
|
|
|
|
Name: v.Name,
|
|
|
|
Symbol: v.Symbol,
|
|
|
|
Rank: v.Rank,
|
|
|
|
PriceUSD: v.PriceUSD,
|
|
|
|
PriceBTC: v.PriceBTC,
|
|
|
|
USD24HVolume: v.USD24HVolume,
|
|
|
|
MarketCapUSD: v.MarketCapUSD,
|
|
|
|
AvailableSupply: v.AvailableSupply,
|
|
|
|
TotalSupply: v.TotalSupply,
|
|
|
|
PercentChange1H: v.PercentChange1H,
|
|
|
|
PercentChange24H: v.PercentChange24H,
|
|
|
|
PercentChange7D: v.PercentChange7D,
|
|
|
|
LastUpdated: v.LastUpdated,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCoinGraphData gets coin graph data
|
|
|
|
func (s *Service) GetCoinGraphData(coin string, start int64, end int64) (types.CoinGraph, error) {
|
|
|
|
ret := types.CoinGraph{}
|
|
|
|
graphData, err := cmc.GetCoinGraphData(coin, start, end)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.MarketCapByAvailableSupply = graphData.MarketCapByAvailableSupply
|
|
|
|
ret.PriceBTC = graphData.PriceBTC
|
|
|
|
ret.PriceUSD = graphData.PriceUSD
|
|
|
|
ret.VolumeUSD = graphData.VolumeUSD
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGlobalMarketGraphData gets global market graph data
|
|
|
|
func (s *Service) GetGlobalMarketGraphData(start int64, end int64) (types.MarketGraph, error) {
|
|
|
|
ret := types.MarketGraph{}
|
|
|
|
graphData, err := cmc.GetGlobalMarketGraphData(start, end)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.MarketCapByAvailableSupply = graphData.MarketCapByAvailableSupply
|
|
|
|
ret.VolumeUSD = graphData.VolumeUSD
|
|
|
|
return ret, nil
|
|
|
|
}
|