/
subscriber.go
61 lines (56 loc) 路 1.2 KB
/
subscriber.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
package main
import (
"time"
"github.com/accept-nano/accept-nano/nano"
"github.com/cenkalti/log"
)
func runSubscriber() {
for {
err := subscribe()
if err != nil {
log.Errorf("websocket error: %s", err.Error())
time.Sleep(time.Second)
}
}
}
func subscribe() (err error) {
ws := nano.NewWebsocket(config.NodeWebsocketURL)
err = ws.Connect()
if err != nil {
return err
}
defer ws.Close()
err = ws.Send("subscribe", "confirmation", true, map[string]interface{}{"include_election_info": "false", "include_block": "true"})
if err != nil {
return err
}
var msg struct {
Message struct {
FromAccount string `json:"account"`
Block struct {
ToAccount string `json:"link_as_account"`
} `json:"block"`
} `json:"message"`
}
for {
err := ws.Recv(&msg)
if err != nil {
return err
}
confirmations <- msg.Message.Block.ToAccount
}
}
func runChecker() {
for account := range confirmations {
p, err := LoadPayment([]byte(account))
if err == errPaymentNotFound {
continue
}
if err != nil {
log.Errorf("cannot load payment: %s", err.Error())
continue
}
log.Debugf("received confirmation from websocket, checking account: %s", account)
go p.checkOnce()
}
}