-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet_listener.go
55 lines (47 loc) · 1.43 KB
/
wallet_listener.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
package bitcoin
import (
"math/big"
"github.com/developertask/openbazaar-go/repo"
"github.com/developertask/wallet-interface"
)
type WalletListener struct {
db repo.Datastore
broadcast chan repo.Notifier
coinType wallet.CoinType
}
func NewWalletListener(db repo.Datastore, broadcast chan repo.Notifier, coinType wallet.CoinType) *WalletListener {
l := &WalletListener{db, broadcast, coinType}
return l
}
func (l *WalletListener) OnTransactionReceived(cb wallet.TransactionCallback) {
if !cb.WatchOnly {
metadata, err := l.db.TxMetadata().Get(cb.Txid)
if err != nil {
log.Debugf("tx metadata not found for id (%s): %s", cb.Txid, err.Error())
}
status := "UNCONFIRMED"
confirmations := 0
if cb.Height > 0 {
status = "PENDING"
confirmations = 1
}
txValue, err := repo.NewCurrencyValueWithLookup(cb.Value.String(), l.coinType.CurrencyCode())
if err != nil {
log.Errorf("failed parsing currency value (%s %s): %s", cb.Value.String(), l.coinType.CurrencyCode(), err.Error())
return
}
l.broadcast <- repo.IncomingTransaction{
Txid: cb.Txid,
Value: txValue,
Address: metadata.Address,
Status: status,
Memo: metadata.Memo,
Timestamp: cb.Timestamp,
Confirmations: int32(confirmations),
OrderId: metadata.OrderId,
Thumbnail: metadata.Thumbnail,
Height: cb.Height,
CanBumpFee: cb.Value.Cmp(big.NewInt(0)) > 0,
}
}
}