/
coinmarketcap.go
123 lines (112 loc) · 3.83 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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"time"
)
const coinInfoURL = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/info"
const coinmarketcapAPIKeyEnvVar = "COINMARKETCAP_API_KEY"
var coinmarketcapAPIKey string
func GetCoinInfoFromCoinMarketCap(symbol string) *Data {
client := &http.Client{}
req, err := http.NewRequest("GET", coinInfoURL, nil)
orPanicf(err, "failed to new request to get coin info from CoinMarketCap\n")
q := url.Values{}
q.Add("symbol", symbol)
req.Header.Set("Accepts", "application/json")
req.Header.Add("X-CMC_PRO_API_KEY", coinmarketcapAPIKey)
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
orPanicf(err, "failed to send request to CoinMarketCap\n")
respBody, err := ioutil.ReadAll(resp.Body)
orPanicf(err, "failed to read body from coin market cap resp")
coinInfoResp := &CoinInfoResp{}
err = json.Unmarshal(respBody, coinInfoResp)
orPanicf(err, "failed to json unmarshal resp body")
for _, data := range coinInfoResp.Data {
return data
}
return nil
}
const PlatformEthereum = "Ethereum"
func GetEthereumAddressFromCoinMarketCapBySymbol(symbol string) string {
data := GetCoinInfoFromCoinMarketCap(symbol)
if data == nil || len(data.ContractAddress) == 0 {
return ""
}
for _, ca := range data.ContractAddress {
if ca == nil {
continue
}
if ca.Platform.Name == PlatformEthereum {
return ca.ContractAddress
}
}
return ""
}
func GetLogoBySymbol(symbol string)string{
data := GetCoinInfoFromCoinMarketCap(symbol)
if data == nil || len(data.ContractAddress) == 0 {
return ""
}
return data.Logo
}
type CoinInfoResp struct {
Status struct {
Timestamp time.Time `json:"timestamp"`
ErrorCode int `json:"error_code"`
ErrorMessage interface{} `json:"error_message"`
Elapsed int `json:"elapsed"`
CreditCount int `json:"credit_count"`
Notice interface{} `json:"notice"`
} `json:"status"`
Data map[string]*Data `json:"data"`
}
type Data struct {
Id int `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Category string `json:"category"`
Description string `json:"description"`
Slug string `json:"slug"`
Logo string `json:"logo"`
Subreddit string `json:"subreddit"`
Notice string `json:"notice"`
Tags []string `json:"tags"`
TagNames []string `json:"tag-names"`
TagGroups []string `json:"tag-groups"`
Urls struct {
Website []string `json:"website"`
Twitter []string `json:"twitter"`
MessageBoard []string `json:"message_board"`
Chat []string `json:"chat"`
Facebook []interface{} `json:"facebook"`
Explorer []string `json:"explorer"`
Reddit []string `json:"reddit"`
TechnicalDoc []string `json:"technical_doc"`
SourceCode []string `json:"source_code"`
Announcement []interface{} `json:"announcement"`
} `json:"urls"`
Platform interface{} `json:"platform"`
DateAdded time.Time `json:"date_added"`
TwitterUsername string `json:"twitter_username"`
IsHidden int `json:"is_hidden"`
DateLaunched interface{} `json:"date_launched"`
ContractAddress []*ContractAddress `json:"contract_address"`
SelfReportedCirculatingSupply interface{} `json:"self_reported_circulating_supply"`
SelfReportedTags []string `json:"self_reported_tags"`
}
type ContractAddress struct {
ContractAddress string `json:"contract_address"`
Platform struct {
Name string `json:"name"`
Coin struct {
Id string `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Slug string `json:"slug"`
} `json:"coin"`
} `json:"platform"`
}