-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblockchain.go
173 lines (150 loc) · 3.95 KB
/
blockchain.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
package common
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"sort"
"strconv"
"time"
"github.com/antonholmquist/jason"
humanize "github.com/dustin/go-humanize"
"github.com/guptarohit/asciigraph"
chart "github.com/wcharczuk/go-chart"
)
type Elem struct {
t time.Time
v float64
}
func getCoindeskData() []Elem {
res, err := http.Get("https://api.coindesk.com/v1/bpi/historical/close.json")
if err != nil {
log.Printf("[ERROR] coinbase: cannot fetch JSON: %v", err)
return nil
}
defer res.Body.Close()
v, err := jason.NewObjectFromReader(res.Body)
if err != nil {
log.Printf("[ERROR] coinbase: cannot unmarshal bitcoin JSON: %v", err)
return nil
}
bpi, err := v.GetObject("bpi")
if err != nil {
log.Printf("[ERROR] coinbase: cannot decode JSON API")
return nil
}
var elems []Elem
for jt, jv := range bpi.Map() {
t, err := time.Parse("2006-01-02", jt)
if err != nil {
continue
}
v, err := jv.Float64()
if err != nil {
continue
}
elems = append(elems, Elem{t, v})
}
sort.Slice(elems, func(i, j int) bool {
return elems[i].t.Before(elems[j].t)
})
return elems
}
// Return a graph in PNG format representing the 1-month BTC-USD exchange
func GetBitcoinGraph() []byte {
elems := getCoindeskData()
if elems == nil {
return nil
}
var times []time.Time
var values []float64
for _, e := range elems {
times = append(times, e.t)
values = append(values, e.v)
}
graph := chart.Chart{
Width: 360,
Height: 180,
XAxis: chart.XAxis{
Style: chart.Style{
Show: true,
},
},
YAxis: chart.YAxis{
Style: chart.Style{
Show: true,
},
},
Series: []chart.Series{
chart.TimeSeries{
XValues: times,
YValues: values,
},
},
}
var buf bytes.Buffer
graph.Render(chart.PNG, &buf)
return buf.Bytes()
}
func GetBitcoinAsciiGraph(width, height int) string {
elems := getCoindeskData()
if elems == nil {
return ""
}
var values []float64
for _, e := range elems {
values = append(values, e.v)
}
return asciigraph.Plot(values, asciigraph.Width(width), asciigraph.Height(height))
}
type BlockchainNerdInfo struct {
Name string
Value string
}
func httpGetString(url string, outerr *error) string {
res, err := http.Get(url)
if err != nil {
*outerr = err
return ""
}
var buf bytes.Buffer
io.Copy(&buf, res.Body)
res.Body.Close()
return buf.String()
}
func satoshis(s string) string {
v, err := strconv.ParseInt(s, 10, 64)
if err == nil {
return fmt.Sprintf("%.8f", float64(v)/float64(100000000))
}
return s
}
func seconds(s string) string {
v, err := strconv.ParseFloat(s, 64)
if err == nil {
return fmt.Sprintf("%v", time.Duration(v*1000000000))
}
return s
}
func human(s string) string {
v, err := strconv.ParseFloat(s, 64)
if err == nil {
return fmt.Sprintf("%v", humanize.Comma(int64(v)))
}
return s
}
func GetBlockchainNerdInfos() (infos []BlockchainNerdInfo, err error) {
return []BlockchainNerdInfo{
{Name: "Current BTC price (USD)", Value: "$" + httpGetString("https://blockchain.info/q/24hrprice", &err)},
{Name: "Market cap (USD)", Value: "$" + human(httpGetString("https://blockchain.info/q/marketcap", &err))},
{Name: "Global hash rate (GigaHash)", Value: human(httpGetString("https://blockchain.info/q/hashrate", &err))},
{Name: "Current difficulty target", Value: human(httpGetString("https://blockchain.info/q/getdifficulty", &err))},
{Name: "Current block height", Value: httpGetString("https://blockchain.info/q/getblockcount", &err)},
{Name: "Latest hash", Value: httpGetString("https://blockchain.info/q/latesthash", &err)},
{Name: "Current block reward", Value: satoshis(httpGetString("https://blockchain.info/q/bcperblock", &err))},
{Name: "Total bitcoins", Value: satoshis(httpGetString("https://blockchain.info/q/totalbc", &err))},
{Name: "Probability of mining", Value: httpGetString("https://blockchain.info/q/probability", &err)},
{Name: "ETA until next block", Value: seconds(httpGetString("https://blockchain.info/q/eta", &err))},
}, err
}