This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
coinmarketcap.go
834 lines (698 loc) · 22.1 KB
/
coinmarketcap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
// Package coinmarketcap Coin Market Cap API client for Go
package coinmarketcap
import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
)
// Client the CoinMarketCap client
type Client struct {
proAPIKey string
Cryptocurrency *CryptocurrencyService
Fiat *FiatService
Exchange *ExchangeService
GlobalMetrics *GlobalMetricsService
Tools *ToolsService
common service
}
// Config the client config structure
type Config struct {
ProAPIKey string
}
// CryptocurrencyService ...
type CryptocurrencyService service
// FiatService ...
type FiatService service
// ExchangeService ...
type ExchangeService service
// GlobalMetricsService ...
type GlobalMetricsService service
// ToolsService ...
type ToolsService service
// Status is the status structure
type Status struct {
Timestamp string `json:"timestamp"`
ErrorCode int `json:"error_code"`
ErrorMessage *string `json:"error_message"`
Elapsed int `json:"elapsed"`
CreditCount int `json:"credit_count"`
}
// Response is the response structure
type Response struct {
Status Status `json:"status"`
Data interface{} `json:"data"`
}
// Listing is the listing structure
type Listing struct {
ID float64 `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Slug string `json:"slug"`
CirculatingSupply float64 `json:"circulating_supply"`
TotalSupply float64 `json:"total_supply"`
MaxSupply float64 `json:"max_supply"`
DateAdded string `json:"date_added"`
NumMarketPairs float64 `json:"num_market_pairs"`
CMCRank float64 `json:"cmc_rank"`
LastUpdated string `json:"last_updated"`
Quote map[string]*Quote `json:"quote"`
}
// MapListing is the structure of a map listing
type Platform struct {
ID int `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Slug string `json:"slug"`
TokenAddress string `json:"token_address"`
}
type MapListing struct {
ID float64 `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Slug string `json:"slug"`
IsActive int `json:"is_active"`
FirstHistoricalData string `json:"first_historical_data"`
LastHistoricalData string `json:"last_historical_data"`
Platform Platform `json:"platform"`
}
// FiatMapListing is the structure of a fiat map listing
type FiatMapListing struct {
ID float64 `json:"id"`
Name string `json:"name"`
Sign string `json:"sign"`
Symbol string `json:"symbol"`
}
// ConvertListing is the converted listing structure
type ConvertListing struct {
ID int `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Amount float64 `json:"amount"`
LastUpdated string `json:"last_updated"`
Quote map[string]*ConvertQuote `json:"quote"`
}
// ConvertQuote is the converted listing structure
type ConvertQuote struct {
Price float64 `json:"price"`
LastUpdated string `json:"last_updated"`
}
// QuoteLatest is the quotes structure
type QuoteLatest struct {
ID float64 `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Slug string `json:"slug"`
CirculatingSupply float64 `json:"circulating_supply"`
TotalSupply float64 `json:"total_supply"`
MaxSupply float64 `json:"max_supply"`
DateAdded string `json:"date_added"`
NumMarketPairs float64 `json:"num_market_pairs"`
CMCRank float64 `json:"cmc_rank"`
LastUpdated string `json:"last_updated"`
Quote map[string]*Quote `json:"quote"`
}
// Quote is the quote structure
type Quote struct {
Price float64 `json:"price"`
Volume24H float64 `json:"volume_24h"`
PercentChange1H float64 `json:"percent_change_1h"`
PercentChange24H float64 `json:"percent_change_24h"`
PercentChange7D float64 `json:"percent_change_7d"`
MarketCap float64 `json:"market_cap"`
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"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Category string `json:"category"`
Slug string `json:"slug"`
Logo string `json:"logo"`
Tags []string `json:"tags"`
Urls map[string]interface{} `json:"urls"`
}
// InfoOptions options
type InfoOptions struct {
ID string
Symbol string
Slug string
}
// ListingOptions options
type ListingOptions struct {
Start int
Limit int
Convert string
Sort string
}
// MapOptions options
type MapOptions struct {
ListingStatus string
Start int
Limit int
Symbol string
}
// FiatMapOptions options
type FiatMapOptions struct {
Start int
Limit int
IncludeMetals bool
}
// QuoteOptions options
type QuoteOptions struct {
// Covert suppots multiple currencies command separated. eg. "BRL,USD"
Convert string
// Symbols suppots multiple tickers command separated. eg. "BTC,ETH,XRP"
Symbol string
}
// ConvertOptions options
type ConvertOptions struct {
Amount float64
ID string
Symbol string
Time int
Convert string
ConvertID string
}
// MarketPairOptions options
type MarketPairOptions struct {
ID int
Symbol string
Start int
Limit int
Convert string
}
// service is abstraction for individual endpoint resources
type service struct {
client *Client
}
// SortOptions sort options
var SortOptions sortOptions
type sortOptions struct {
Name string
Symbol string
DateAdded string
MarketCap string
Price string
CirculatingSupply string
TotalSupply string
MaxSupply string
NumMarketPairs string
Volume24H string
PercentChange1H string
PercentChange24H string
PercentChange7D string
}
var (
// ErrTypeAssertion is type assertion error
ErrTypeAssertion = errors.New("type assertion error")
)
var (
siteURL = "https://coinmarketcap.com"
baseURL = "https://pro-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"
)
// NewClient initializes a new client
func NewClient(cfg *Config) *Client {
if cfg == nil {
cfg = new(Config)
}
if cfg.ProAPIKey == "" {
cfg.ProAPIKey = os.Getenv("CMC_PRO_API_KEY")
}
c := &Client{
proAPIKey: cfg.ProAPIKey,
}
c.common.client = c
c.Cryptocurrency = (*CryptocurrencyService)(&c.common)
c.Fiat = (*FiatService)(&c.common)
c.Exchange = (*ExchangeService)(&c.common)
c.GlobalMetrics = (*GlobalMetricsService)(&c.common)
c.Tools = (*ToolsService)(&c.common)
return c
}
// Info returns all static metadata for one or more cryptocurrencies including name, symbol, logo, and its various registered URLs.
func (s *CryptocurrencyService) Info(options *InfoOptions) (map[string]*CryptocurrencyInfo, error) {
var params []string
if options == nil {
options = new(InfoOptions)
}
if options.ID != "" {
params = append(params, fmt.Sprintf("id=%s", options.ID))
}
if options.Symbol != "" {
params = append(params, fmt.Sprintf("symbol=%s", options.Symbol))
}
if options.Slug != "" {
params = append(params, fmt.Sprintf("slug=%s", strings.ToLower(options.Slug)))
}
url := fmt.Sprintf("%s/cryptocurrency/info?%s", baseURL, strings.Join(params, "&"))
body, err := s.client.makeReq(url)
resp := new(Response)
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
var result = make(map[string]*CryptocurrencyInfo)
ifcs, ok := resp.Data.(map[string]interface{})
if !ok {
return nil, ErrTypeAssertion
}
for k, v := range ifcs {
info := new(CryptocurrencyInfo)
b, err := json.Marshal(v)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, info)
if err != nil {
return nil, err
}
result[k] = info
}
return result, nil
}
// LatestListings gets a paginated list of all cryptocurrencies with latest market data. You can configure this call to sort by market cap or another market ranking field. Use the "convert" option to return market values in multiple fiat and cryptocurrency conversions in the same call.
func (s *CryptocurrencyService) LatestListings(options *ListingOptions) ([]*Listing, error) {
var params []string
if options == nil {
options = new(ListingOptions)
}
if options.Start != 0 {
params = append(params, fmt.Sprintf("start=%v", options.Start))
}
if options.Limit != 0 {
params = append(params, fmt.Sprintf("limit=%v", options.Limit))
}
if options.Convert != "" {
params = append(params, fmt.Sprintf("convert=%s", options.Convert))
}
if options.Sort != "" {
params = append(params, fmt.Sprintf("sort=%s", options.Sort))
}
url := fmt.Sprintf("%s/cryptocurrency/listings/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))
}
var listings []*Listing
ifcs, ok := resp.Data.([]interface{})
if !ok {
return nil, ErrTypeAssertion
}
for i := range ifcs {
ifc := ifcs[i]
listing := new(Listing)
b, err := json.Marshal(ifc)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, listing)
if err != nil {
return nil, err
}
listings = append(listings, listing)
}
return listings, nil
}
// Map returns a paginated list of all cryptocurrencies by CoinMarketCap ID.
func (s *CryptocurrencyService) Map(options *MapOptions) ([]*MapListing, error) {
var params []string
if options == nil {
options = new(MapOptions)
}
if options.ListingStatus != "" {
params = append(params, fmt.Sprintf("listing_status=%s", options.ListingStatus))
}
if options.Start != 0 {
params = append(params, fmt.Sprintf("start=%d", options.Start))
}
if options.Limit != 0 {
params = append(params, fmt.Sprintf("limit=%d", options.Limit))
}
if options.Symbol != "" {
params = append(params, fmt.Sprintf("symbol=%s", options.Symbol))
}
url := fmt.Sprintf("%s/cryptocurrency/map?%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))
}
var result []*MapListing
ifcs, ok := resp.Data.(interface{})
if !ok {
return nil, ErrTypeAssertion
}
for _, item := range ifcs.([]interface{}) {
value := new(MapListing)
b, err := json.Marshal(item)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, value)
if err != nil {
return nil, err
}
result = append(result, value)
}
return result, nil
}
// Map returns a paginated list of all cryptocurrencies by CoinMarketCap ID.
func (s *FiatService) Map(options *FiatMapOptions) ([]*FiatMapListing, error) {
var params []string
if options == nil {
options = new(FiatMapOptions)
}
if options.Start != 0 {
params = append(params, fmt.Sprintf("start=%d", options.Start))
}
if options.Limit != 0 {
params = append(params, fmt.Sprintf("limit=%d", options.Limit))
}
if options.IncludeMetals {
params = append(params, "include_metals=true")
}
url := fmt.Sprintf("%s/fiat/map?%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))
}
var result []*FiatMapListing
ifcs, ok := resp.Data.(interface{})
if !ok {
return nil, ErrTypeAssertion
}
for _, item := range ifcs.([]interface{}) {
value := new(FiatMapListing)
b, err := json.Marshal(item)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, value)
if err != nil {
return nil, err
}
result = append(result, value)
}
return result, nil
}
// Exchange ...
type Exchange struct {
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
}
// MarketPairBase ...
type MarketPairBase struct {
CurrencyID int `json:"currency_id"`
CurrencySymbol string `json:"currency_symbol"`
CurrencyType string `json:"currency_type"`
}
// MarketPairQuote ...
type MarketPairQuote struct {
CurrencyID int `json:"currency_id"`
CurrencySymbol string `json:"currency_symbol"`
CurrencyType string `json:"currency_type"`
}
// ExchangeQuote ...
type ExchangeQuote struct {
Price float64 `json:"price"`
Volume24 float64 `json:"volume_24h"`
Volume24HBase float64 `json:"volume_24h_base"` // for 'exchange_reported'
Volume24HQuote float64 `json:"volume_24h_quote"` // for 'exchange_reported'
LastUpdated string `json:"last_updated"`
}
// ExchangeQuotes ...
type ExchangeQuotes map[string]*ExchangeQuote
// ExchangeReported ...
type ExchangeReported struct {
Price float64 `json:"price"`
Volume24HBase float64 `json:"volume_24h_base"`
Volume24HQuote float64 `json:"volume_24h_quote"`
LastUpdated string `json:"last_updated"`
}
// MarketPairs ...
type MarketPairs struct {
ID int `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
NumMarketPairs int `json:"num_market_pairs"`
MarketPairs []*MarketPair `json:"market_pairs"`
}
// MarketPair ...
type MarketPair struct {
Exchange *Exchange
MarketPair string `json:"market_pair"`
MarketPairBase *MarketPairBase `json:"market_pair_base"`
MarketPairQuote *MarketPairQuote `json:"market_pair_quote"`
Quote ExchangeQuotes `json:"quote"`
ExchangeReported *ExchangeReported `json:"exchange_reported"`
}
// LatestMarketPairs Lists all market pairs across all exchanges for the specified cryptocurrency with associated stats. Use the "convert" option to return market values in multiple fiat and cryptocurrency conversions in the same call.
func (s *CryptocurrencyService) LatestMarketPairs(options *MarketPairOptions) (*MarketPairs, error) {
var params []string
if options == nil {
options = new(MarketPairOptions)
}
if options.ID != 0 {
params = append(params, fmt.Sprintf("id=%v", options.ID))
}
if options.Symbol != "" {
params = append(params, fmt.Sprintf("symbol=%s", options.Symbol))
}
if options.Start != 0 {
params = append(params, fmt.Sprintf("start=%v", options.Start))
}
if options.Limit != 0 {
params = append(params, fmt.Sprintf("limit=%v", options.Limit))
}
if options.Convert != "" {
params = append(params, fmt.Sprintf("convert=%s", options.Convert))
}
url := fmt.Sprintf("%s/cryptocurrency/market-pairs/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
}
marketPairs := new(MarketPairs)
b, err := json.Marshal(data)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, marketPairs)
if err != nil {
return nil, err
}
for _, pair := range marketPairs.MarketPairs {
reported, ok := pair.Quote["exchange_reported"]
if ok {
pair.ExchangeReported = &ExchangeReported{
Price: reported.Price,
Volume24HBase: reported.Volume24HBase,
Volume24HQuote: reported.Volume24HQuote,
LastUpdated: reported.LastUpdated,
}
delete(pair.Quote, "exchange_reported")
}
}
return marketPairs, nil
}
// HistoricalOHLCV NOT IMPLEMENTED
func (s *CryptocurrencyService) HistoricalOHLCV() error {
return nil
}
// LatestQuotes gets latest quote for each specified symbol. Use the "convert" option to return market values in multiple fiat and cryptocurrency conversions in the same call.
func (s *CryptocurrencyService) LatestQuotes(options *QuoteOptions) ([]*QuoteLatest, error) {
var params []string
if options == nil {
options = new(QuoteOptions)
}
if options.Symbol != "" {
params = append(params, fmt.Sprintf("symbol=%s", options.Symbol))
}
if options.Convert != "" {
params = append(params, fmt.Sprintf("convert=%s", options.Convert))
}
url := fmt.Sprintf("%s/cryptocurrency/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))
}
var quotesLatest []*QuoteLatest
ifcs, ok := resp.Data.(interface{})
if !ok {
return nil, ErrTypeAssertion
}
for _, coinObj := range ifcs.(map[string]interface{}) {
quoteLatest := new(QuoteLatest)
b, err := json.Marshal(coinObj)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, quoteLatest)
if err != nil {
return nil, err
}
quotesLatest = append(quotesLatest, quoteLatest)
}
return quotesLatest, nil
}
// HistoricalQuotes NOT IMPLEMENTED
func (s *CryptocurrencyService) HistoricalQuotes() error {
return nil
}
// Info NOT IMPLEMENTED
func (s *ExchangeService) Info() error {
return nil
}
// Map NOT IMPLEMENTED
func (s *ExchangeService) Map() error {
return nil
}
// LatestListings NOT IMPLEMENTED
func (s *ExchangeService) LatestListings() error {
return nil
}
// LatestMarketPairs NOT IMPLEMENTED
func (s *ExchangeService) LatestMarketPairs() error {
return nil
}
// LatestQuotes NOT IMPLEMENTED
func (s *ExchangeService) LatestQuotes() error {
return nil
}
// HistoricalQuotes NOT IMPLEMENTED
func (s *ExchangeService) HistoricalQuotes() error {
return nil
}
// 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
func (s *GlobalMetricsService) HistoricalQuotes() error {
return nil
}
// PriceConversion Convert an amount of one currency into multiple cryptocurrencies or fiat currencies at the same time using the latest market averages. Optionally pass a historical timestamp to convert values based on historic averages.
func (s *ToolsService) PriceConversion(options *ConvertOptions) (*ConvertListing, error) {
var params []string
if options == nil {
options = new(ConvertOptions)
}
if options.Amount != 0 {
params = append(params, fmt.Sprintf("amount=%f", options.Amount))
}
if options.ID != "" {
params = append(params, fmt.Sprintf("id=%s", options.ID))
}
if options.Symbol != "" {
params = append(params, fmt.Sprintf("symbol=%s", options.Symbol))
}
if options.Time != 0 {
params = append(params, fmt.Sprintf("time=%d", options.Time))
}
if options.Convert != "" {
params = append(params, fmt.Sprintf("convert=%s", options.Convert))
} else if options.ConvertID != "" {
params = append(params, fmt.Sprintf("convert_id=%s", options.ConvertID))
}
url := fmt.Sprintf("%s/tools/price-conversion?%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))
}
ifc, ok := resp.Data.(interface{})
if !ok {
return nil, ErrTypeAssertion
}
listing := new(ConvertListing)
b, err := json.Marshal(ifc)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, listing)
if err != nil {
return nil, err
}
return listing, nil
}
func init() {
SortOptions = sortOptions{
Name: "name",
Symbol: "symbol",
DateAdded: "date_added",
MarketCap: "market_cap",
Price: "price",
CirculatingSupply: "circulating_supply",
TotalSupply: "total_supply",
MaxSupply: "max_supply",
NumMarketPairs: "num_market_pairs",
Volume24H: "volume_24h",
PercentChange1H: "percent_change_1h",
PercentChange24H: "percent_change_24h",
PercentChange7D: "percent_change_7d",
}
}