-
Notifications
You must be signed in to change notification settings - Fork 3
/
eth-watcher.go
96 lines (78 loc) · 1.93 KB
/
eth-watcher.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
package main
import (
"flag"
"log"
"math/big"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
const (
NOTIFY_TYPE_NONE = iota
NOTIFY_TYPE_TX
NOTIFY_TYPE_ADMIN
)
type NotifyMessage struct {
MessageType int
AddressFrom string
AddressTo string
Amount *big.Int
ContractAddress string
IsPending bool
TxHash string
}
var (
fDebug bool
fInit bool
fConfigFile string
)
func init() {
flag.BoolVar(&fInit, "init", false, "DB Init")
flag.BoolVar(&fDebug, "debug", false, "Debug")
flag.StringVar(&fConfigFile, "config", "config.ini", "Configuration file")
}
func main() {
var last_id uint64
flag.Parse()
config, err := LoadConfiguration(fConfigFile)
if err != nil {
panic(err)
}
db, err := DbOpen(config)
if err != nil {
panic(err)
}
defer db.Close()
if fInit {
err = db.InitTables()
if err != nil {
panic(err)
}
log.Println("Schema created in database.")
return
}
last_id_str, err := db.GetSetting("last_block")
if err != nil {
log.Println("Warning: Could not get last block id parsed from database: No recovery.")
last_id = 0
} else {
last_id, err = strconv.ParseUint(last_id_str, 10, 64)
if err != nil {
log.Printf("Warning: Could not convert %s as integer", last_id_str)
last_id = 0
}
}
r := mux.NewRouter()
r.HandleFunc("/createAddress", CreateAddressHandler(config, db)).Methods("POST")
r.HandleFunc("/registerAddress", RegisterAddressHandler(config, db)).Methods("POST")
r.HandleFunc("/getBalance", GetBalanceHandler(config))
r.HandleFunc("/sendEth", SendEthHandler(config))
r.HandleFunc("/sendErc20", SendERC20Handler(config, db))
r.HandleFunc("/getNotifications", GetNotificationsHandler(config, db))
r.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
ch := make(chan NotifyMessage, 1024)
go Notifier(config, db, ch)
go Subscriber(config, ch, last_id)
log.Println("Starting webserver...")
http.ListenAndServe(":8080", r)
}