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

Commit

Permalink
coinmarketcap: imp. /v1/cryptocurrency/market-pairs/latest
Browse files Browse the repository at this point in the history
  • Loading branch information
hexoul committed Nov 5, 2018
1 parent efe2820 commit 1fbe731
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
| Cryptocurrency | /v1/cryptocurrency/map | YES |
| Cryptocurrency | /v1/cryptocurrency/listings/latest | YES |
| Cryptocurrency | /v1/cryptocurrency/listings/historical | - |
| Cryptocurrency | /v1/cryptocurrency/market-pairs/latest | - |
| Cryptocurrency | /v1/cryptocurrency/market-pairs/latest | YES |
| Cryptocurrency | /v1/cryptocurrency/ohlcv/latest | - |
| Cryptocurrency | /v1/cryptocurrency/ohlcv/historical | - |
| Cryptocurrency | /v1/cryptocurrency/quotes/latest | YES |
Expand Down
5 changes: 3 additions & 2 deletions coinmarketcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ type Interface interface {
CryptoInfo(options *types.Options) (*types.CryptoInfoMap, error)
CryptoMap(options *types.Options) (*types.CryptoMapList, error)
CryptoListingsLatest(options *types.Options) (*types.CryptoMarketList, error)
CryptoMarketPairsLatest(options *types.Options) (*types.MarketPairs, error)
CryptoMarketQuotesLatest(options *types.Options) (*types.CryptoMarketMap, error)

ExchangeInfo(options *types.Options) (*types.ExchangeInfoMap, error)
ExchangeMap(options *types.Options) (*types.ExchangeMapList, error)
ExchangeListingsLatest(options *types.Options) (*types.ExchangeMarketList, error)
ExchangeMarketPairsLatest(options *types.Options) (*types.ExchangeMarketPairs, error)
ExchangeMarketPairsLatest(options *types.Options) (*types.MarketPairs, error)
ExchangeMarketQuotesLatest(options *types.Options) (*types.ExchangeMarketQuotes, error)
}

Expand Down Expand Up @@ -97,7 +98,7 @@ func (s *Client) getResponse(url string) (*types.Response, []byte, error) {
return nil, nil, err
}
if resp.Status.ErrorCode != 0 {
return nil, nil, fmt.Errorf("%d %s", resp.Status.ErrorCode, *resp.Status.ErrorMessage)
return nil, nil, fmt.Errorf("[%d] %s", resp.Status.ErrorCode, *resp.Status.ErrorMessage)
}
return resp, body, nil
}
23 changes: 23 additions & 0 deletions cryptocurrency.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ func (s *Client) CryptoListingsLatest(options *types.Options) (*types.CryptoMark
return result, nil
}

// CryptoMarketPairsLatest lists all market pairs for the specified cryptocurrency with associated stats.
// arg: id, symbol, start, limit, convert
// src: https://pro-api.coinmarketcap.com/v1/cryptocurrency/market-pairs/latest
// doc: https://pro.coinmarketcap.com/api/v1#operation/getV1CryptocurrencyMarketpairsLatest
func (s *Client) CryptoMarketPairsLatest(options *types.Options) (*types.MarketPairs, error) {
url := fmt.Sprintf("%s/cryptocurrency/market-pairs/latest?%s", baseURL, strings.Join(util.ParseOptions(options), "&"))

resp, _, err := s.getResponse(url)
if err != nil {
return nil, err
}

var result = new(types.MarketPairs)
b, err := json.Marshal(resp.Data)
if err != nil {
return nil, err
}
if err := json.Unmarshal(b, result); err != nil {
return nil, err
}
return result, nil
}

// CryptoMarketQuotesLatest gets the latest market quote for 1 or more cryptocurrencies.
// arg: id, symbol, convert
// src: https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest
Expand Down
12 changes: 12 additions & 0 deletions cryptocurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ func TestCryptoListingsLatest(t *testing.T) {
}
}

func TestCryptoMarketPairsLatest(t *testing.T) {
info, err := GetInstance().CryptoMarketPairsLatest(&types.Options{
Symbol: "BTC",
})
if err != nil {
t.Fatal(err)
}
if info.Name != "Bitcoin" {
t.FailNow()
}
}

func TestCryptoMarketQuotesLatest(t *testing.T) {
quotes, err := GetInstance().CryptoMarketQuotesLatest(&types.Options{
Symbol: "BTC,ETH",
Expand Down
4 changes: 2 additions & 2 deletions exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ func (s *Client) ExchangeListingsLatest(options *types.Options) (*types.Exchange
// arg: id, slug, start, limit, convert
// src: https://pro-api.coinmarketcap.com/v1/exchange/market-pairs/latest
// doc: https://pro.coinmarketcap.com/api/v1#operation/getV1ExchangeMarketpairsLatest
func (s *Client) ExchangeMarketPairsLatest(options *types.Options) (*types.ExchangeMarketPairs, error) {
func (s *Client) ExchangeMarketPairsLatest(options *types.Options) (*types.MarketPairs, error) {
url := fmt.Sprintf("%s/exchange/market-pairs/latest?%s", baseURL, strings.Join(util.ParseOptions(options), "&"))

resp, _, err := s.getResponse(url)
if err != nil {
return nil, err
}

var result = new(types.ExchangeMarketPairs)
var result = new(types.MarketPairs)
b, err := json.Marshal(resp.Data)
if err != nil {
return nil, err
Expand Down
20 changes: 11 additions & 9 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,19 @@ type ExchangeMap struct {
LastHistoricalData string `json:"last_historical_data"`
}

// ExchangeMarketPairs structure
type ExchangeMarketPairs struct {
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
NumMarketPairs int `json:"num_market_pairs"`
MarketPairs []*MarketPairs `json:"market_pairs"`
}

// MarketPairs structure
type MarketPairs struct {
ID int `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Slug string `json:"slug"`
NumMarketPairs int `json:"num_market_pairs"`
MarketPair []*MarketPair `json:"market_pairs"`
}

// MarketPair structure
type MarketPair struct {
ExchangeInfo *ExchangeInfo `json:"exchange"`
MarketPair string `json:"market_pair"`
MarketPairBase *Currency `json:"market_pair_base"`
MarketPairQuote *Currency `json:"market_pair_quote"`
Expand Down

0 comments on commit 1fbe731

Please sign in to comment.