-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
193 lines (171 loc) · 5.42 KB
/
main.go
File metadata and controls
193 lines (171 loc) · 5.42 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"os/signal"
"sort"
"strings"
"syscall"
"time"
"golang.org/x/text/language"
"golang.org/x/text/message"
"BlockClockETH/blockClock"
coingecko2 "BlockClockETH/coingecko"
)
type TokenPrice struct {
Type string `json:"type"`
Currency string `json:"currency"`
Symbol string `json:"symbol"`
DisplayCurrency string `json:"display_currency"`
ContractAddress string `json:"contract_address"`
Platform string `json:"platform"`
Price float32
MarketCap float32
Volume float32
Change float32
LastUpdate int32
LightsPriceAbove float32 `json:"light_price_above"`
LightsPriceBelow float32 `json:"light_price_below"`
LightsPercent float32 `json:"light_percent"`
ShowDuration time.Duration `json:"show_duration_seconds"`
Notify bool `json:"notify"`
}
type Config struct {
Sort bool `json:"sort_symbols"`
BlockClockAddress string `json:"block_clock_address"`
BlockClockPass string `json:"block_clock_password"`
SMTPServer string `json:"smtp_server"`
SMTPPort string `json:"smtp_port"`
SMTPUser string `json:"smtp_user"`
SMTPPass string `json:"smtp_pass"`
NotifyAddress string `json:"notify_address"`
Tokens []TokenPrice `json:"tokens"`
}
var config Config
var BC *blockClock.Client
func main() {
fileName := "config.json"
args := os.Args[1:]
if len(args) == 1 {
fileName = args[0]
}
jsonFile, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer jsonFile.Close()
if err := json.NewDecoder(jsonFile).Decode(&config); err != nil {
log.Fatal(err)
}
if config.Sort {
sort.SliceStable(config.Tokens, func(i, j int) bool {
return strings.ToLower(config.Tokens[i].Symbol) < strings.ToLower(config.Tokens[j].Symbol)
})
}
var emailClient *blockClock.EmailClient
if config.SMTPServer != "" && config.SMTPPort != "" && config.SMTPUser != "" && config.SMTPPass != "" && config.NotifyAddress != "" {
emailClient, _ = blockClock.NewEmailClient(config.SMTPServer, config.SMTPPort, config.SMTPUser, config.SMTPPass)
emailClient.NotifyAddress = config.NotifyAddress
}
httpClient := &http.Client{
Timeout: time.Second * 10,
}
CG := coingecko2.NewClient(httpClient)
BC = blockClock.NewClient(httpClient, config.BlockClockAddress, config.BlockClockPass)
_ = BC.PauseBuiltInFunctions()
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
_ = BC.ResumeBuiltInFunctions()
os.Exit(1)
}()
for {
for _, token := range config.Tokens {
switch token.Type {
case "coingecko":
log.Println("Getting token price:", token.Symbol)
err := getTokenPrice(CG, &token)
if err != nil {
log.Println(err)
} else {
go updateBlockClock(emailClient, token)
}
time.Sleep(token.ShowDuration * time.Second)
default:
log.Println("Error, not implemented:", token)
}
}
}
}
func getTokenPrice(client *coingecko2.Client, token *TokenPrice) error {
token.ContractAddress = strings.ToLower(token.ContractAddress)
token.Currency = strings.ToLower(token.Currency)
var curPrice map[string]map[string]float32
if token.Symbol == "ETH" {
retVal, err := client.SimplePrice([]string{token.ContractAddress}, []string{token.Currency})
if err != nil {
return err
}
curPrice = *retVal
} else {
retVal, err := client.SimpleTokenPrice(token.Platform, []string{token.ContractAddress}, []string{token.Currency})
if err != nil {
return err
}
curPrice = *retVal
}
token.Price = curPrice[token.ContractAddress][token.Currency]
token.MarketCap = curPrice[token.ContractAddress][token.Currency+"_market_cap"]
token.Volume = curPrice[token.ContractAddress][token.Currency+"_24h_vol"]
token.Change = curPrice[token.ContractAddress][token.Currency+"_24h_change"]
token.LastUpdate = int32(curPrice[token.ContractAddress]["last_updated_at"])
return nil
}
func updateBlockClock(client *blockClock.EmailClient, token TokenPrice) {
p := message.NewPrinter(language.English)
format := "%.6f"
log.Println("Showing token price:", token.Symbol, "-", token.Price)
var err error
if (token.LightsPercent > 0 && token.Change > token.LightsPercent) || (token.LightsPriceAbove > 0 && token.Price >= token.LightsPriceAbove) {
if token.Notify && client != nil {
client.SendEmail(token.Currency+" "+p.Sprintf(format, token.Price), "ALERT - "+token.Symbol, client.NotifyAddress)
}
err = BC.LightsOn("00ff0040") //green
} else if (token.LightsPercent != 0 && token.Change < -token.LightsPercent) || (token.LightsPriceBelow > 0 && token.Price <= token.LightsPriceBelow) {
if token.Notify && client != nil {
client.SendEmail(token.Currency+" "+p.Sprintf(format, token.Price), "ALERT - "+token.Symbol, client.NotifyAddress)
}
err = BC.LightsOn("ff000040") //red
} else {
err = BC.LightsOff()
}
if err != nil {
log.Println(err)
}
price := p.Sprintf(format, token.Price)
price = price + "0"
limit := 0
for x, character := range price {
if character == ',' || character == '.' {
continue
}
limit++
if limit == 7 {
price = price[0:x]
break
}
}
err = BC.DisplayLargeText(price, false)
if err != nil {
log.Println(err)
return
}
time.Sleep(1 * time.Second)
err = BC.DisplayOUText(6, token.Symbol, token.DisplayCurrency)
if err != nil {
log.Println(err)
}
}