|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
package api
|
|
|
|
|
package coinmarketcap
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
@ -12,6 +12,7 @@ import (
|
|
|
|
|
|
|
|
|
|
// Service service
|
|
|
|
|
type Service struct {
|
|
|
|
|
lock sync.RWMutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New new service
|
|
|
|
@ -38,41 +39,18 @@ func getLimitedCoinData(convert string, offset int) (map[string]apitypes.Coin, e
|
|
|
|
|
Rank: v.Rank,
|
|
|
|
|
AvailableSupply: v.CirculatingSupply,
|
|
|
|
|
TotalSupply: v.TotalSupply,
|
|
|
|
|
MarketCapUSD: v.Quotes[convert].MarketCap,
|
|
|
|
|
PriceUSD: v.Quotes[convert].Price,
|
|
|
|
|
MarketCap: v.Quotes[convert].MarketCap,
|
|
|
|
|
Price: v.Quotes[convert].Price,
|
|
|
|
|
PercentChange1H: v.Quotes[convert].PercentChange1H,
|
|
|
|
|
PercentChange24H: v.Quotes[convert].PercentChange24H,
|
|
|
|
|
PercentChange7D: v.Quotes[convert].PercentChange7D,
|
|
|
|
|
USD24HVolume: v.Quotes[convert].Volume24H,
|
|
|
|
|
PriceBTC: 0,
|
|
|
|
|
Volume24H: v.Quotes[convert].Volume24H,
|
|
|
|
|
LastUpdated: strconv.Itoa(v.LastUpdated),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetAllCoinDataV2 gets all coin data
|
|
|
|
|
// V1 is currently better for fetching all the coins at once
|
|
|
|
|
func (s *Service) GetAllCoinDataV2(convert string) (map[string]apitypes.Coin, error) {
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
ret := make(map[string]apitypes.Coin)
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func(j int) {
|
|
|
|
|
coins, err := getLimitedCoinData(convert, j)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for k, v := range coins {
|
|
|
|
|
ret[k] = v
|
|
|
|
|
}
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
}(i)
|
|
|
|
|
}
|
|
|
|
|
wg.Wait()
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetAllCoinData get all coin data
|
|
|
|
|
func (s *Service) GetAllCoinData(convert string) (map[string]apitypes.Coin, error) {
|
|
|
|
|
ret := make(map[string]apitypes.Coin)
|
|
|
|
@ -81,9 +59,11 @@ func (s *Service) GetAllCoinData(convert string) (map[string]apitypes.Coin, erro
|
|
|
|
|
return ret, err
|
|
|
|
|
}
|
|
|
|
|
for _, v := range coins {
|
|
|
|
|
priceraw := v.Quotes[convert].Price
|
|
|
|
|
pricestr := fmt.Sprintf("%.2f", priceraw)
|
|
|
|
|
price, _ := strconv.ParseFloat(pricestr, 64)
|
|
|
|
|
price := v.Quotes[convert].Price
|
|
|
|
|
if convert != "USD" {
|
|
|
|
|
pricestr := fmt.Sprintf("%.2f", price)
|
|
|
|
|
price, _ = strconv.ParseFloat(pricestr, 64)
|
|
|
|
|
}
|
|
|
|
|
ret[v.Symbol] = apitypes.Coin{
|
|
|
|
|
ID: strings.ToLower(v.Name),
|
|
|
|
|
Name: v.Name,
|
|
|
|
@ -91,19 +71,42 @@ func (s *Service) GetAllCoinData(convert string) (map[string]apitypes.Coin, erro
|
|
|
|
|
Rank: v.Rank,
|
|
|
|
|
AvailableSupply: v.AvailableSupply,
|
|
|
|
|
TotalSupply: v.TotalSupply,
|
|
|
|
|
MarketCapUSD: v.Quotes[convert].MarketCap,
|
|
|
|
|
PriceUSD: price,
|
|
|
|
|
MarketCap: v.Quotes[convert].MarketCap,
|
|
|
|
|
Price: price,
|
|
|
|
|
PercentChange1H: v.PercentChange1H,
|
|
|
|
|
PercentChange24H: v.PercentChange24H,
|
|
|
|
|
PercentChange7D: v.PercentChange7D,
|
|
|
|
|
USD24HVolume: v.Quotes[convert].Volume24H,
|
|
|
|
|
PriceBTC: 0,
|
|
|
|
|
Volume24H: v.Quotes[convert].Volume24H,
|
|
|
|
|
LastUpdated: strconv.Itoa(v.LastUpdated),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetAllCoinDataV2 gets all coin data
|
|
|
|
|
// V1 is currently better for fetching all the coins at once
|
|
|
|
|
func (s *Service) GetAllCoinDataV2(convert string) (map[string]apitypes.Coin, error) {
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
ret := make(map[string]apitypes.Coin)
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func(j int) {
|
|
|
|
|
s.lock.RLock()
|
|
|
|
|
defer s.lock.RUnlock()
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
coins, err := getLimitedCoinData(convert, j)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for k, v := range coins {
|
|
|
|
|
ret[k] = v
|
|
|
|
|
}
|
|
|
|
|
}(i)
|
|
|
|
|
}
|
|
|
|
|
wg.Wait()
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetCoinGraphData gets coin graph data
|
|
|
|
|
func (s *Service) GetCoinGraphData(coin string, start int64, end int64) (apitypes.CoinGraph, error) {
|
|
|
|
|
ret := apitypes.CoinGraph{}
|