forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
49 lines (43 loc) · 952 Bytes
/
status.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
package bitcoin
import (
"context"
"encoding/json"
"time"
)
type StatusUpdater struct {
w BitcoinWallet
c chan interface{}
ctx context.Context
}
type walletUpdateWrapper struct {
WalletUpdate walletUpdate `json:"walletUpdate"`
}
type walletUpdate struct {
Height uint32 `json:"height"`
Unconfirmed int64 `json:"unconfirmed"`
Confirmed int64 `json:"confirmed"`
}
func NewStatusUpdater(w BitcoinWallet, c chan interface{}, ctx context.Context) *StatusUpdater {
return &StatusUpdater{w, c, ctx}
}
func (s *StatusUpdater) Start() {
t := time.NewTicker(time.Second * 15)
for {
select {
case <-t.C:
confirmed, unconfirmed := s.w.Balance()
u := walletUpdate{
Height: s.w.ChainTip(),
Unconfirmed: unconfirmed,
Confirmed: confirmed,
}
ser, err := json.MarshalIndent(walletUpdateWrapper{u}, "", " ")
if err != nil {
continue
}
s.c <- ser
case <-s.ctx.Done():
break
}
}
}