diff --git a/BTCPayApp.CommonServer/IBTCPayAppHubClient.cs b/BTCPayApp.CommonServer/IBTCPayAppHubClient.cs index beda30c69..3cec0c4a4 100644 --- a/BTCPayApp.CommonServer/IBTCPayAppHubClient.cs +++ b/BTCPayApp.CommonServer/IBTCPayAppHubClient.cs @@ -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> GetLightningPayments(ListPaymentsParams request); Task> 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 UpdatePsbt(string[] identifiers, string psbt); Task GetUTXOs(string[] identifiers); + Task> GetTransactions(string[] identifiers); Task SendPaymentUpdate(string identifier, LightningPayment lightningPayment); diff --git a/BTCPayServer/App/BTCPayAppHub.cs b/BTCPayServer/App/BTCPayAppHub.cs index 4acde1a11..692679d0c 100644 --- a/BTCPayServer/App/BTCPayAppHub.cs +++ b/BTCPayServer/App/BTCPayAppHub.cs @@ -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> GetTransactions(string[] identifiers) + { + var explorerClient = _explorerClientProvider.GetExplorerClient( _btcPayNetworkProvider.BTC); + var result = new Dictionary(); + 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); } } + +