network backend: SubscribeTransactions

pull/10/head
Edouard Paris 5 years ago
parent 62406c66c9
commit f5be66f77a

@ -37,4 +37,6 @@ type Backend interface {
SendPayment(context.Context, *models.PayReq) (*models.Payment, error)
GetTransactions(context.Context) ([]*models.Transaction, error)
SubscribeTransactions(context.Context, chan *models.Transaction) error
}

@ -97,6 +97,38 @@ func (l Backend) SubscribeInvoice(ctx context.Context, channelInvoice chan *mode
}
}
func (l Backend) SubscribeTransactions(ctx context.Context, channel chan *models.Transaction) error {
clt, err := l.Client(ctx)
if err != nil {
return err
}
defer clt.Close()
cltTransactions, err := clt.SubscribeTransactions(ctx, &lnrpc.GetTransactionsRequest{})
if err != nil {
return err
}
for {
select {
case <-ctx.Done():
break
default:
transaction, err := cltTransactions.Recv()
if err != nil {
st, ok := status.FromError(err)
if ok && st.Code() == codes.Canceled {
l.logger.Debug("stopping subscribe transactions: context canceled")
return nil
}
return err
}
channel <- protoToTransaction(transaction)
}
}
}
func (l Backend) SubscribeChannels(ctx context.Context, events chan *models.ChannelUpdate) error {
_, err := l.Client(ctx)
if err != nil {

@ -292,16 +292,20 @@ func protoToTransactions(resp *lnrpc.TransactionDetails) []*models.Transaction {
transactions := make([]*models.Transaction, len(resp.Transactions))
for i := range resp.Transactions {
transactions[i] = &models.Transaction{
TxHash: resp.Transactions[i].TxHash,
Amount: resp.Transactions[i].Amount,
NumConfirmations: resp.Transactions[i].NumConfirmations,
BlockHash: resp.Transactions[i].BlockHash,
BlockHeight: resp.Transactions[i].BlockHeight,
Date: time.Unix(int64(resp.Transactions[i].TimeStamp), 0),
TotalFees: resp.Transactions[i].TotalFees,
DestAddresses: resp.Transactions[i].DestAddresses,
}
transactions[i] = protoToTransaction(resp.Transactions[i])
}
return transactions
}
func protoToTransaction(resp *lnrpc.Transaction) *models.Transaction {
return &models.Transaction{
TxHash: resp.TxHash,
Amount: resp.Amount,
NumConfirmations: resp.NumConfirmations,
BlockHash: resp.BlockHash,
BlockHeight: resp.BlockHeight,
Date: time.Unix(int64(resp.TimeStamp), 0),
TotalFees: resp.TotalFees,
DestAddresses: resp.DestAddresses,
}
}

@ -46,7 +46,11 @@ func (b *Backend) SubscribeChannels(context.Context, chan *models.ChannelUpdate)
return nil
}
func (l *Backend) GetNode(ctx context.Context, pubkey string) (*models.Node, error) {
func (b *Backend) SubscribeTransactions(ctx context.Context, channel chan *models.Transaction) error {
return nil
}
func (b *Backend) GetNode(ctx context.Context, pubkey string) (*models.Node, error) {
return &models.Node{}, nil
}

Loading…
Cancel
Save