-
Notifications
You must be signed in to change notification settings - Fork 0
/
mxc.go
57 lines (45 loc) · 1005 Bytes
/
mxc.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
type MxcResponse struct {
Data []MxcInfo `json:"data"`
}
type MxcInfo struct {
Last string `json:"last"`
}
func getMxcRate() (float64, error) {
url := fmt.Sprintf("https://www.mxc.com/open/api/v2/market/ticker?symbol=%s_USDT", strings.ToUpper(MxcCurrency))
client := http.Client{Timeout: time.Second * 2}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return 0, err
}
req.Header.Set("User-Agent", "cosmos-interacter")
res, err := client.Do(req)
if err != nil {
return 0, err
}
if res.Body != nil {
defer res.Body.Close()
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return 0, err
}
response := MxcResponse{}
jsonErr := json.Unmarshal(body, &response)
if jsonErr != nil {
return 0, err
}
if len(response.Data) == 0 {
return 0, fmt.Errorf("empty response from Mxc")
}
return strconv.ParseFloat(response.Data[0].Last, 64)
}