forked from stellar-deprecated/kelp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmcFeed.go
70 lines (60 loc) · 1.4 KB
/
cmcFeed.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
package plugins
import (
"net/http"
"strconv"
"time"
"github.com/stellar/kelp/api"
"github.com/stellar/kelp/support/utils"
)
/*
example JSON returned by coinmarketcap
[
{
"id": "stellar",
"name": "Stellar Lumens",
"symbol": "XLM",
"rank": "27",
"price_usd": "0.0220156",
"price_btc": "0.00000527",
"24h_volume_usd": "27604200.0",
"market_cap_usd": "245811502.0",
"available_supply": "11165332853.0",
"total_supply": "103195955318",
"percent_change_1h": "0.67",
"percent_change_24h": "10.76",
"percent_change_7d": "23.69",
"last_updated": "1503513850"
}
]
*/
// for getting data out of coinmarketcap
type cmcAPIReturn struct {
Price string `json:"price_usd"`
}
// cmcFeed represents the feed for CoinmarketCap
type cmcFeed struct {
url string
client http.Client
}
// ensure that it implements PriceFeed
var _ api.PriceFeed = &cmcFeed{}
// newCMCFeed creates a new CMC Feed from a URL
func newCMCFeed(url string) *cmcFeed {
m := new(cmcFeed)
m.url = url
m.client = http.Client{Timeout: 10 * time.Second}
return m
}
// GetPrice impl
func (c *cmcFeed) GetPrice() (float64, error) {
var retA []cmcAPIReturn
err := utils.GetJSON(c.client, c.url, &retA)
if err != nil {
return 0, err
}
pA, err := strconv.ParseFloat(retA[0].Price, 64)
if err != nil {
return 0, err
}
return pA, nil
}