get onchain txs

This commit is contained in:
Kukks 2024-05-24 10:56:08 +02:00 committed by Dennis Reimann
parent 8c0d1fde45
commit e1b2bc1a05
No known key found for this signature in database
GPG key ID: 5009E1797F03F8D0
2 changed files with 44 additions and 4 deletions

View file

@ -1,7 +1,9 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BTCPayServer.Client.Models;
using BTCPayServer.Lightning;
using NBitcoin;
namespace BTCPayApp.CommonServer;
@ -30,6 +32,15 @@ public interface IBTCPayAppHubClient
Task<List<LightningPayment>> GetLightningPayments(ListPaymentsParams request);
Task<List<LightningPayment>> GetLightningInvoices(ListInvoicesParams request);
}
public record TxResp(long Confirmations, long? Height, decimal BalanceChange, DateTimeOffset Timestamp, string TransactionId)
{
public override string ToString()
{
return $"{{ Confirmations = {Confirmations}, Height = {Height}, BalanceChange = {BalanceChange}, Timestamp = {Timestamp}, TransactionId = {TransactionId} }}";
}
}
//methods available on the hub in the server
public interface IBTCPayAppHubServer
{
@ -47,6 +58,7 @@ public interface IBTCPayAppHubServer
Task TrackScripts(string identifier, string[] scripts);
Task<string> UpdatePsbt(string[] identifiers, string psbt);
Task<CoinResponse[]> GetUTXOs(string[] identifiers);
Task<Dictionary<string, TxResp[]>> GetTransactions(string[] identifiers);
Task SendPaymentUpdate(string identifier, LightningPayment lightningPayment);

View file

@ -244,9 +244,11 @@ var resultPsbt = PSBT.Parse(psbt, explorerClient.Network.NBitcoinNetwork);
foreach (string identifier in identifiers)
{
var ts = TrackedSource.Parse(identifier,explorerClient.Network);
if (ts is not DerivationSchemeTrackedSource derivationSchemeTrackedSource)
if (ts is null)
{
continue;
var utxos = await explorerClient.GetUTXOsAsync(derivationSchemeTrackedSource.DerivationStrategy);
}
var utxos = await explorerClient.GetUTXOsAsync(ts);
result.AddRange(utxos.GetUnspentUTXOs(0).Select(utxo => new CoinResponse()
{
Identifier = identifier,
@ -254,12 +256,36 @@ var resultPsbt = PSBT.Parse(psbt, explorerClient.Network.NBitcoinNetwork);
Script = utxo.ScriptPubKey.ToHex(),
Outpoint = utxo.Outpoint.ToString(),
Value = utxo.Value.GetValue(_btcPayNetworkProvider.BTC),
Path = utxo.KeyPath.ToString()
Path = utxo.KeyPath?.ToString()
}));
}
return result.ToArray();
}
public async Task<Dictionary<string, TxResp[]>> GetTransactions(string[] identifiers)
{
var explorerClient = _explorerClientProvider.GetExplorerClient( _btcPayNetworkProvider.BTC);
var result = new Dictionary<string, TxResp[]>();
foreach (string identifier in identifiers)
{
var ts = TrackedSource.Parse(identifier,explorerClient.Network);
if (ts is null)
{
continue;
}
var txs = await explorerClient.GetTransactionsAsync(ts);
var items = txs.ConfirmedTransactions.Transactions
.Concat(txs.UnconfirmedTransactions.Transactions)
.Concat(txs.ImmatureTransactions.Transactions)
.Concat(txs.ReplacedTransactions.Transactions)
.Select(tx => new TxResp(tx.Confirmations, tx.Height, tx.BalanceChange.GetValue(_btcPayNetworkProvider.BTC), tx.Timestamp, tx.TransactionId.ToString())).OrderByDescending(arg => arg.Timestamp);
result.Add(identifier,items.ToArray());
}
return result;
}
public async Task SendPaymentUpdate(string identifier, LightningPayment lightningPayment)
{
await _appState.PaymentUpdate(identifier, lightningPayment);
@ -286,3 +312,5 @@ var resultPsbt = PSBT.Parse(psbt, explorerClient.Network.NBitcoinNetwork);
return await _appState.Handshake(Context.ConnectionId, request);
}
}