forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet_listener.go
50 lines (46 loc) · 1.23 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
package bitcoin
import (
"encoding/hex"
"github.com/OpenBazaar/openbazaar-go/api/notifications"
"github.com/OpenBazaar/openbazaar-go/repo"
"github.com/OpenBazaar/wallet-interface"
"github.com/btcsuite/btcd/chaincfg/chainhash"
)
type WalletListener struct {
db repo.Datastore
broadcast chan interface{}
}
func NewWalletListener(db repo.Datastore, broadcast chan interface{}) *WalletListener {
l := &WalletListener{db, broadcast}
return l
}
func (l *WalletListener) OnTransactionReceived(cb wallet.TransactionCallback) {
if !cb.WatchOnly {
txid := hex.EncodeToString(cb.Txid)
metadata, _ := l.db.TxMetadata().Get(txid)
status := "UNCONFIRMED"
confirmations := 0
if cb.Height > 0 {
status = "PENDING"
confirmations = 1
}
ch, err := chainhash.NewHash(cb.Txid)
if err != nil {
return
}
n := notifications.IncomingTransaction{
Txid: ch.String(),
Value: cb.Value,
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 > 0,
}
l.broadcast <- n
}
}