Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
implement global market metrics endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Apr 17, 2019
1 parent 1ab78e6 commit 509be5f
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ go get -u github.com/miguelmota/go-coinmarketcap
| Exchange | /v1/exchange/market-pairs/latest | - |
| Exchange | /v1/exchange/quotes/latest | - |
| Exchange | /v1/exchange/quotes/historical | - |
| Global Metrics | /v1/global-metrics/quotes/latest | - |
| Global Metrics | /v1/global-metrics/quotes/latest | |
| Global Metrics | /v1/global-metrics/quotes/historical | - |
| Tools | /v1/tools/price-conversion ||

Expand Down
86 changes: 83 additions & 3 deletions pro/v1/coinmarketcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ type Quote struct {
LastUpdated string `json:"last_updated"`
}

// MarketMetrics is the market metrics structure
type MarketMetrics struct {
BTCDominance float64 `json:"btc_dominance"`
ETHDominance float64 `json:"eth_dominance"`
ActiveCryptocurrencies float64 `json:"active_cryptocurrencies"`
ActiveMarketPairs float64 `json:"active_market_pairs"`
ActiveExchanges float64 `json:"active_exchanges"`
LastUpdated string `json:"last_updated"`
Quote map[string]*MarketMetricsQuote `json:"quote"`
}

// MarketMetricsQuote is the quote structure
type MarketMetricsQuote struct {
TotalMarketCap float64 `json:"total_market_cap"`
TotalVolume24H float64 `json:"total_volume_24h"`
LastUpdated string `json:"last_updated"`
}

// CryptocurrencyInfo options
type CryptocurrencyInfo struct {
ID float64 `json:"id"`
Expand Down Expand Up @@ -615,9 +633,71 @@ func (s *ExchangeService) HistoricalQuotes() error {
return nil
}

// LatestQuotes NOT IMPLEMENTED
func (s *GlobalMetricsService) LatestQuotes() error {
return nil
/*
{
"data": {
"btc_dominance": 55.5,
"eth_dominance": 10.1,
"active_cryptocurrencies": 2500,
"active_market_pairs": 9910,
"active_exchanges": 600,
"last_updated": 1522523367,
"quote": {
"USD": {
"total_market_cap": 375179000000,
"total_volume_24h": 19918400000,
"last_updated": "2018-08-09T23:02:00.000Z"
}
}
},
"status": {
"timestamp": "2018-06-02T22:51:28.209Z",
"error_code": 0,
"error_message": "",
"elapsed": 10,
"credit_count": 1
}
}
*/

// LatestQuotes Get the latest quote of aggregate market metrics. Use the "convert" option to return market values in multiple fiat and cryptocurrency conversions in the same call.
func (s *GlobalMetricsService) LatestQuotes(options *QuoteOptions) (*MarketMetrics, error) {
var params []string
if options == nil {
options = new(QuoteOptions)
}

if options.Convert != "" {
params = append(params, fmt.Sprintf("convert=%s", options.Convert))
}

url := fmt.Sprintf("%s/global-metrics/quotes/latest?%s", baseURL, strings.Join(params, "&"))

body, err := s.client.makeReq(url)
resp := new(Response)

err = json.Unmarshal(body, &resp)
if err != nil {
return nil, fmt.Errorf("JSON Error: [%s]. Response body: [%s]", err.Error(), string(body))
}

data, ok := resp.Data.(interface{})
if !ok {
return nil, ErrTypeAssertion
}

marketMetrics := new(MarketMetrics)
b, err := json.Marshal(data)
if err != nil {
return nil, err
}

err = json.Unmarshal(b, marketMetrics)
if err != nil {
return nil, err
}

return marketMetrics, nil
}

// HistoricalQuotes NOT IMPLEMENTED
Expand Down
13 changes: 12 additions & 1 deletion pro/v1/coinmarketcap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,18 @@ func TestExchangeHistoricalQuotes(t *testing.T) {
}

func TestGlobalMetricsLatestQuotes(t *testing.T) {
// TODO
metrics, err := client.GlobalMetrics.LatestQuotes(&QuoteOptions{
Convert: "USD",
})
if err != nil {
t.Error(err)
}
if metrics.BTCDominance <= 0 {
t.FailNow()
}
if metrics.Quote["USD"].TotalMarketCap <= 0 {
t.FailNow()
}
}

func TestGlobalMetricsHistoricalQuotes(t *testing.T) {
Expand Down
24 changes: 24 additions & 0 deletions pro/v1/example/market_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
"os"

cmc "github.com/miguelmota/go-coinmarketcap/pro/v1"
)

func main() {
client := cmc.NewClient(&cmc.Config{
ProAPIKey: os.Getenv("CMC_PRO_API_KEY"),
})

metrics, err := client.GlobalMetrics.LatestQuotes(&cmc.QuoteOptions{
Convert: "USD",
})
if err != nil {
panic(err)
}

fmt.Println(metrics.BTCDominance) // 52.3318
fmt.Println(metrics.Quote["USD"].TotalMarketCap) // 1.76486412385549e+11
}

0 comments on commit 509be5f

Please sign in to comment.