From 4be870a2e8a5233d4e1dc171f7e502282ca7be73 Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Mon, 26 Mar 2018 23:27:31 -0700 Subject: [PATCH] add altcoin markets graph data --- coinmarketcap.go | 27 ++++++++++++++++++++++----- coinmarketcap_test.go | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/coinmarketcap.go b/coinmarketcap.go index d28c9bd..ffe2f5e 100644 --- a/coinmarketcap.go +++ b/coinmarketcap.go @@ -13,9 +13,10 @@ import ( ) var ( - baseURL = "https://api.coinmarketcap.com/v1" - graphURL = "https://graphs2.coinmarketcap.com/currencies" - marketGraphURL = "https://graphs2.coinmarketcap.com/global/marketcap-total" + baseURL = "https://api.coinmarketcap.com/v1" + coinGraphURL = "https://graphs2.coinmarketcap.com/currencies" + globalMarketGraphURL = "https://graphs2.coinmarketcap.com/global/marketcap-total" + altcoinMarketGraphURL = "https://graphs2.coinmarketcap.com/global/marketcap-altcoin" ) // GetMarketData get information about the global market data of the cryptocurrencies @@ -35,7 +36,23 @@ func GetMarketData() (GlobalMarketData, error) { // GetMarketGraphData get graph data points of global market func GetMarketGraphData(start int64, end int64) (MarketGraph, error) { - url := fmt.Sprintf("%s/%d/%d", marketGraphURL, start*1000, end*1000) + url := fmt.Sprintf("%s/%d/%d", globalMarketGraphURL, start*1000, end*1000) + resp, err := makeReq(url) + if err != nil { + return MarketGraph{}, err + } + var data MarketGraph + err = json.Unmarshal(resp, &data) + if err != nil { + return MarketGraph{}, err + } + + return data, nil +} + +// GetAltcoinMarketGraphData get graph data points of altcoin market +func GetAltcoinMarketGraphData(start int64, end int64) (MarketGraph, error) { + url := fmt.Sprintf("%s/%d/%d", altcoinMarketGraphURL, start*1000, end*1000) resp, err := makeReq(url) if err != nil { return MarketGraph{}, err @@ -92,7 +109,7 @@ func GetAllCoinData(limit int) (map[string]Coin, error) { // GetCoinGraphData get graph data points for a crypto currency func GetCoinGraphData(coin string, start int64, end int64) (CoinGraph, error) { - url := fmt.Sprintf("%s/%s/%d/%d", graphURL, coin, start*1000, end*1000) + url := fmt.Sprintf("%s/%s/%d/%d", coinGraphURL, coin, start*1000, end*1000) resp, err := makeReq(url) if err != nil { return CoinGraph{}, err diff --git a/coinmarketcap_test.go b/coinmarketcap_test.go index 0b087f2..5baad8e 100644 --- a/coinmarketcap_test.go +++ b/coinmarketcap_test.go @@ -50,6 +50,25 @@ func TestGetMarketGraphData(t *testing.T) { } } +func TestGetAltcoinMarketGraphData(t *testing.T) { + var threeMonths int64 = (60 * 60 * 24 * 90) + end := time.Now().Unix() + start := end - threeMonths + + graph, err := GetAltcoinMarketGraphData(start, end) + if err != nil { + t.FailNow() + } + + if graph.MarketCapByAvailableSupply[0][0] == 0 { + t.FailNow() + } + + if graph.VolumeUSD[0][0] == 0 { + t.FailNow() + } +} + func TestGetCoinData(t *testing.T) { coin, err := GetCoinData("ethereum") if err != nil {