forked from mycroft/eth-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
251 lines (201 loc) · 4.79 KB
/
database.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"fmt"
"log"
"math/big"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
type DB struct {
Interface *sql.DB
}
func DbOpen(config *Config) (*DB, error) {
dsn := fmt.Sprintf("%s:%s@%s(%s)/%s",
config.DBUser,
config.DBPass,
config.DBProtocol,
config.DBHostname,
config.DBName,
)
log.Printf("Connecting to DB using dsn: %s", dsn)
dbInterface, err := sql.Open("mysql", dsn)
if err != nil {
return nil, err
}
dbInterface.SetMaxIdleConns(100)
err = dbInterface.Ping()
if err != nil {
return nil, err
}
log.Println("Connected to DB")
db := new(DB)
db.Interface = dbInterface
return db, nil
}
func (db *DB) Close() {
if db.Interface != nil {
db.Interface.Close()
db.Interface = nil
}
}
func (db *DB) InitTables() error {
queries := []string{`
CREATE TABLE eth_keys(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
address VARCHAR(40) UNIQUE,
private VARCHAR(64)
);`,
`CREATE INDEX eth_keys_address_idx ON eth_keys(address);`,
`CREATE TABLE notifications(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
address_from VARCHAR(40),
address_to VARCHAR(40),
address_contract VARCHAR(40),
amount VARCHAR(32),
is_pending BOOLEAN NOT NULL DEFAULT false,
tx_hash VARCHAR(64),
created_at DATETIME DEFAULT NOW()
);`,
`CREATE TABLE settings(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(32) UNIQUE,
value VARCHAR(64)
);`,
}
for _, query := range queries {
_, err := db.Interface.Query(query)
if err != nil {
return err
}
}
return nil
}
func (db *DB) InsertKey(address, private string) error {
stmt, err := db.Interface.Prepare("INSERT INTO eth_keys(address, private) VALUES(LOWER(?), ?) ON DUPLICATE KEY UPDATE private = ?")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(address, private, private)
if err != nil {
return err
}
return nil
}
func (db *DB) InsertNotification(
address_from, address_to, address_contract, amount string,
is_pending bool, tx_hash string) error {
stmt, err := db.Interface.Prepare(`
INSERT INTO notifications(address_from, address_to, address_contract, amount, is_pending, tx_hash)
VALUES(?, ?, ?, ?, ?, ?)`)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(address_from, address_to, address_contract, amount, is_pending, tx_hash)
if err != nil {
return err
}
return nil
}
func (db *DB) IsAddressKnown(address string) (bool, error) {
stmt, err := db.Interface.Prepare("SELECT id FROM eth_keys WHERE address = LOWER(?)")
if err != nil {
return false, err
}
defer stmt.Close()
rows, err := stmt.Query(address)
if err != nil {
return false, err
}
defer rows.Close()
if false == rows.Next() {
return false, nil
}
return true, nil
}
func (db *DB) GetSetting(name string) (string, error) {
var value string
stmt, err := db.Interface.Prepare("SELECT value FROM settings WHERE name = LOWER(?)")
if err != nil {
return "", err
}
defer stmt.Close()
err = stmt.QueryRow(name).Scan(&value)
if err != nil {
return "", err
}
return value, nil
}
func (db *DB) GetKey(address string) (string, error) {
var value string
stmt, err := db.Interface.Prepare("SELECT private FROM eth_keys WHERE address = LOWER(?)")
if err != nil {
return "", err
}
defer stmt.Close()
err = stmt.QueryRow(address).Scan(&value)
if err != nil {
return "", err
}
return value, nil
}
func (db *DB) SetSetting(name, value string) error {
stmt, err := db.Interface.Prepare(`INSERT INTO settings(name, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value = ?`)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(name, value, value)
if err != nil {
return err
}
return nil
}
func (db *DB) GetNotifications(remove bool) ([]NotifyMessage, error) {
var id uint64
stmt, err := db.Interface.Prepare(
`SELECT id, address_from, address_to, address_contract, amount, is_pending, tx_hash
FROM notifications ORDER BY id ASC LIMIT 100`)
if err != nil {
return []NotifyMessage{}, err
}
defer stmt.Close()
msgs := make([]NotifyMessage, 0)
rows, err := stmt.Query()
for rows.Next() {
var msg NotifyMessage
var amount string
err := rows.Scan(
&id,
&msg.AddressFrom,
&msg.AddressTo,
&msg.ContractAddress,
&amount,
&msg.IsPending,
&msg.TxHash,
)
msg.Amount = new(big.Int)
msg.Amount.SetString(amount, 10)
if err != nil {
return []NotifyMessage{}, err
}
msgs = append(msgs, msg)
}
if err := rows.Err(); err != nil {
return []NotifyMessage{}, err
}
if false == remove {
return msgs, nil
}
// Remove notifications from database
stmt, err = db.Interface.Prepare("DELETE FROM notifications WHERE id <= ?")
if err != nil {
return msgs, err
}
_, err = stmt.Exec(id)
if err != nil {
return msgs, err
}
return msgs, nil
}