-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitcoin.go
81 lines (65 loc) · 1.79 KB
/
bitcoin.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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"time"
)
type BitcoinPrice struct {
Data struct {
Amount string `json:"amount"`
Currency string `json:"currency"`
} `json:"data"`
}
func bitcoinEventCallback(position *int, button *int) (f callbackFunc, err error) {
return nil, nil
}
//Don't call this function more than once per second
//or you may end up getting rate limited by coinbase
func bitcoinCallback(position int) error {
//Get the price of btc from 7 days ago
response, err := http.Get("https://api.coinbase.com/v2/prices/spot?currency=USD&date=" + time.Now().Add(-7*24*time.Hour).Format("2006-01-02"))
if err != nil {
return err
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
var dayPrice = BitcoinPrice{}
err = json.Unmarshal(body, &dayPrice)
if err != nil {
return err
}
dayPriceValue, err := strconv.ParseFloat(dayPrice.Data.Amount, 64)
if err != nil {
return err
}
//Get the current price of btc
response, err = http.Get("https://api.coinbase.com/v2/prices/spot?currency=USD")
if err != nil {
return err
}
body, err = ioutil.ReadAll(response.Body)
if err != nil {
return err
}
var currentPrice = BitcoinPrice{}
err = json.Unmarshal(body, ¤tPrice)
if err != nil {
return err
}
currentPriceValue, err := strconv.ParseFloat(currentPrice.Data.Amount, 64)
if err != nil {
return err
}
//Calculate the % change between the two prices
change := strconv.FormatFloat((((currentPriceValue - dayPriceValue) / dayPriceValue) * 100), 'f', 2, 64)
if dayPriceValue > currentPriceValue {
sectionStatus[position] = "BTC/USD: $" + currentPrice.Data.Amount + " " + change + "% 🔻"
} else {
sectionStatus[position] = "BTC/USD: $" + currentPrice.Data.Amount + " +" + change + "% 🚀"
}
return nil
}