-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
140 lines (120 loc) · 3.2 KB
/
crypto.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
package logger
import (
"encoding/json"
"fmt"
logr "github.com/504dev/logr-go-client"
humanize "github.com/dustin/go-humanize"
"github.com/fatih/color"
"io"
"net/http"
"time"
)
type BinancePrice struct {
Price float64 `json:"lastPrice,string"`
Volume float64 `json:"quoteVolume,string"`
}
type HitbtcPrice struct {
Price float64 `json:"last,string"`
Volume float64 `json:"volume_quote,string"`
}
type BitfinexPrice struct {
Price float64
Volume float64
}
func (b *BitfinexPrice) UnmarshalJSON(buf []byte) error {
tmp := []interface{}{nil, nil, nil, nil, nil, nil, &b.Price, &b.Volume}
if err := json.Unmarshal(buf, &tmp); err != nil {
return err
}
b.Volume *= b.Price
return nil
}
func crypto(conf *logr.Config) {
l, _ := conf.NewLogger("crypto.log")
l.Body = "[{version}, pid={pid}] {message}"
for {
l.Info("")
time.Sleep(30 * time.Second)
l.Info("**************************************************")
totals := make(map[string]float64)
for _, base := range [3]string{"BTC", "ETH", "LTC"} {
l.Info("")
sym := base + "_USDT"
bin, hit, bit := BinancePrice{}, HitbtcPrice{}, BitfinexPrice{}
var err error
err = request(&bin, fmt.Sprintf("https://api.binance.com/api/v3/ticker/24hr?symbol=%vUSDT", base))
if err != nil {
Logger.Error("Demo crypto.log binance: %v", err)
continue
}
err = request(&hit, fmt.Sprintf("https://api.hitbtc.com/api/3/public/ticker/%vUSDT", base))
if err != nil {
Logger.Error("Demo crypto.log hitbtc: %v", err)
continue
}
err = request(&bit, fmt.Sprintf("https://api-pub.bitfinex.com/v2/ticker/t%vUSD", base))
if err != nil {
Logger.Error("Demo crypto.log bitfinex: %v", err)
continue
}
binP, hitP, bitP := bin.Price, hit.Price, bit.Price
binV, hitV, bitV := bin.Volume, hit.Volume, bit.Volume
l.Touch(fmt.Sprintf("price:%v", sym)).Avg(hitP).Avg(binP).Avg(bitP)
l.Avg(fmt.Sprintf("volume:%v", sym), hitV+binV+bitV)
bold := color.New(color.Bold).SprintFunc()
l.Info(
"%v %v %v$ (%v$)",
bold(base),
color.CyanString("HitBTC"),
bold(humanize.Commaf(hitP)),
humanize.Comma(int64(hitV)),
)
l.Info(
"%v %v %v$ (%v$)",
bold(base),
color.GreenString("Bitfinex"),
bold(humanize.Commaf(bitP)),
humanize.Comma(int64(bitV)),
)
l.Info(
"%v %v %v$ (%v$)",
bold(base),
color.HiYellowString("Binance"),
bold(humanize.Commaf(binP)),
humanize.Comma(int64(binV)),
)
l.Notice(
"%v price %v widget!",
color.New(color.Bold).SprintFunc()(base),
l.Snippet("avg", fmt.Sprintf("price:%v", sym), 30),
)
totalV := hitV + bitV + binV
l.Per("volume:hitbtc", hitV, totalV)
l.Per("volume:bitfinex", bitV, totalV)
l.Per("volume:binance", binV, totalV)
totals[sym] = totalV
totals[""] += totalV
time.Sleep(time.Second)
}
for sym, val := range totals {
if sym != "" {
l.Per(fmt.Sprintf("volume:%v", sym), val, totals[""])
}
}
}
}
func request(res interface{}, url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if err = json.Unmarshal(bodyBytes, res); err != nil {
return err
}
return nil
}