-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
79 lines (71 loc) · 1.88 KB
/
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
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
package wallet
import (
"context"
"encoding/json"
"time"
"github.com/developertask/multiwallet"
"github.com/developertask/openbazaar-go/repo"
"github.com/op/go-logging"
)
type StatusUpdater struct {
mw multiwallet.MultiWallet
c chan repo.Notifier
ctx context.Context
log *logging.Logger
}
type walletUpdateWrapper struct {
WalletUpdate map[string]walletUpdate `json:"walletUpdate"`
}
type walletUpdate struct {
Height uint32 `json:"height"`
Unconfirmed string `json:"unconfirmed"`
Confirmed string `json:"confirmed"`
Currency *repo.CurrencyDefinition `json:"currency"`
}
func NewStatusUpdater(mw multiwallet.MultiWallet, c chan repo.Notifier, ctx context.Context) *StatusUpdater {
var log = logging.MustGetLogger("walletStatus")
return &StatusUpdater{
mw: mw,
c: c,
ctx: ctx,
log: log,
}
}
func (s *StatusUpdater) Start() {
var (
t = time.NewTicker(time.Second * 15)
)
for {
select {
case <-t.C:
ret := make(map[string]walletUpdate)
for ct, wal := range s.mw {
confirmed, unconfirmed := wal.Balance()
height, _ := wal.ChainTip()
def, err := repo.MainnetCurrencies().Lookup(ct.CurrencyCode())
if err != nil {
def, err = repo.TestnetCurrencies().Lookup(ct.CurrencyCode())
if err != nil {
s.log.Errorf("unable to find definition (%s): %s", ct.CurrencyCode(), err.Error())
continue
}
}
u := walletUpdate{
Height: height,
Unconfirmed: unconfirmed.Value.String(),
Confirmed: confirmed.Value.String(),
Currency: &def,
}
ret[def.CurrencyCode().String()] = u
}
ser, err := json.MarshalIndent(walletUpdateWrapper{ret}, "", " ")
if err != nil {
s.log.Errorf("unable to marhsal wallet update: %s", err.Error())
continue
}
s.c <- repo.PremarshalledNotifier{ser}
case <-s.ctx.Done():
break
}
}
}