btc-pay-checker/internal/services/btc/notification.go

80 lines
1.6 KiB
Go

package btc
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
"gitea.urkob.com/urko/btc-pay-checker/internal/domain"
)
func (b *BitcoinService) Notify(ctx context.Context, notifChan chan<- domain.Notification) {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
q := &Zmq{}
if err := q.Connect(b.zmqAddress); err != nil {
log.Fatal("Failed to connect to ZeroMQ socket:", err)
}
blockChan := make(chan string)
go q.Listen(blockChan)
go func() {
for blockHash := range blockChan {
block, err := b.getBlock(blockHash)
if err != nil {
log.Println("Error b.GetBlock:", err)
continue
}
log.Println("block readed", block.Hash)
for _, txid := range block.Tx {
tx, err := b.getRawTransaction(txid, block.Hash)
if err != nil {
log.Println("Error b.getRawTransaction:", err)
continue
}
if !tx.InActiveChain {
log.Printf("tx is not active on chain | block %s | tx %s \n", blockHash, txid)
continue
}
receiverAddress := false
amount := 0.0
for _, output := range tx.Vout {
if output.ScriptPubKey.Address == b.walletAddress {
receiverAddress = true
amount = output.Value
break
}
}
if !receiverAddress {
continue
}
if receiverAddress {
log.Println("Transaction has been completed")
notifChan <- domain.Notification{
BlockHash: blockHash,
Tx: txid,
Amount: amount,
DoneAt: time.Now(),
}
break
}
}
}
}()
<-interrupt
if err := q.Close(); err != nil {
log.Println("Error closing ZeroMQ socket:", err)
}
}