2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-18 15:25:31 +00:00
cointop/pkg/api/cmc/cmc.go

72 lines
1.9 KiB
Go
Raw Normal View History

package api
import (
types "github.com/miguelmota/cointop/pkg/api/types"
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
}