diff --git a/NBXplorer/Backend/Repository.cs b/NBXplorer/Backend/Repository.cs index ea6c5a25c..c958dffc3 100644 --- a/NBXplorer/Backend/Repository.cs +++ b/NBXplorer/Backend/Repository.cs @@ -742,16 +742,33 @@ public async Task GetSavedTransactions(uint256 txid) }}; } - public async Task GetTransactions(TrackedSource trackedSource, uint256 txId = null, bool includeTransactions = true, CancellationToken cancellation = default) + public async Task GetTransactions( + TrackedSource trackedSource, + uint256 txId = null, + bool includeTransactions = true, + ulong? fromSeen = null, + ulong? toSeen = null, + CancellationToken cancellation = default) { await using var connection = await connectionFactory.CreateConnectionHelper(Network); var tip = await connection.GetTip(); var txIdCond = txId is null ? string.Empty : " AND tx_id=@tx_id"; + + var fromSeenCond = fromSeen is null ? string.Empty : " AND seen_at>=@fromSeen"; + var toSeenCond = toSeen is null ? string.Empty : " AND seen_at<=@toSeen"; + var utxos = await connection.Connection.QueryAsync<(string tx_id, long idx, string blk_id, long? blk_height, int? blk_idx, bool is_out, string spent_tx_id, long spent_idx, string script, string addr, long value, string asset_id, bool immature, string keypath, DateTime seen_at)>( "SELECT tx_id, idx, blk_id, blk_height, blk_idx, is_out, spent_tx_id, spent_idx, script, s.addr, value, asset_id, immature, keypath, seen_at " + "FROM nbxv1_tracked_txs LEFT JOIN scripts s USING (code, script) " + - $"WHERE code=@code AND wallet_id=@walletId{txIdCond}", new { code = Network.CryptoCode, walletId = GetWalletKey(trackedSource).wid, tx_id = txId?.ToString() }); + $"WHERE code=@code AND wallet_id=@walletId{txIdCond}{fromSeenCond}{toSeenCond}", new { + code = Network.CryptoCode, + walletId = GetWalletKey(trackedSource).wid, + tx_id = txId?.ToString(), + fromSeen = NBitcoin.Utils.UnixTimeToDateTime(fromSeen ?? 0), + toSeen = NBitcoin.Utils.UnixTimeToDateTime(toSeen ?? 0) + } + ); utxos.TryGetNonEnumeratedCount(out int c); var trackedById = new Dictionary(c); foreach (var utxo in utxos) diff --git a/NBXplorer/Controllers/MainController.cs b/NBXplorer/Controllers/MainController.cs index ef12fa296..1f903bdad 100644 --- a/NBXplorer/Controllers/MainController.cs +++ b/NBXplorer/Controllers/MainController.cs @@ -523,7 +523,10 @@ public async Task GetTransactions( TrackedSourceContext trackedSourceContext, [ModelBinder(BinderType = typeof(UInt256ModelBinding))] uint256 txId = null, - bool includeTransaction = true) + bool includeTransaction = true, + ulong? fromSeen = null, + ulong? toSeen = null + ) { TransactionInformation fetchedTransactionInfo = null; var network = trackedSourceContext.Network; @@ -533,7 +536,7 @@ public async Task GetTransactions( var response = new GetTransactionsResponse(); int currentHeight = (await repo.GetTip()).Height; response.Height = currentHeight; - var txs = await GetAnnotatedTransactions(repo, trackedSource, includeTransaction, txId); + var txs = await GetAnnotatedTransactions(repo, trackedSource, includeTransaction, txId, fromSeen, toSeen); foreach (var item in new[] { new @@ -717,9 +720,16 @@ public async Task Rescan(TrackedSourceContext trackedSourceContex } } - internal async Task GetAnnotatedTransactions(Repository repo, TrackedSource trackedSource, bool includeTransaction, uint256 txId = null) + internal async Task GetAnnotatedTransactions( + Repository repo, + TrackedSource trackedSource, + bool includeTransaction, + uint256 txId = null, + ulong? fromSeen = null, + ulong? toSeen = null + ) { - var transactions = await repo.GetTransactions(trackedSource, txId, includeTransaction, this.HttpContext?.RequestAborted ?? default); + var transactions = await repo.GetTransactions(trackedSource, txId, includeTransaction, fromSeen, toSeen, this.HttpContext?.RequestAborted ?? default); // If the called is interested by only a single txId, we need to fetch the parents as well if (txId != null) diff --git a/docs/API.md b/docs/API.md index 0939943e6..fc747727b 100644 --- a/docs/API.md +++ b/docs/API.md @@ -318,6 +318,10 @@ Optional Parameters: * `includeTransaction` includes the hex of the transaction, not only information (default: true) +* `fromSeen` indicates the earliest date from which transactions should be retrieved, (default: null, unix time) + +* `toSeen` indicates the latest date up to which transactions should be retrieved, (default: null, unix time) + Returns: ```json @@ -376,6 +380,10 @@ Optional Parameters: * `includeTransaction` includes the hex of the transaction, not only information (default: true) +* `fromSeen` indicates the earliest date from which transactions should be retrieved, (default: null, unix time) + +* `toSeen` indicates the latest date up to which transactions should be retrieved, (default: null, unix time) + Error codes: * HTTP 404: Transaction not found