From 5387a6287ea5b8e4aec416af72c325cd29604443 Mon Sep 17 00:00:00 2001 From: Nicolas Dorier Date: Thu, 22 Sep 2022 10:39:48 +0900 Subject: [PATCH] Fix pagination of wallet's transactions (#4150) --- .../Controllers/UIWalletsController.cs | 8 ++++++-- BTCPayServer/Services/Wallets/BTCPayWallet.cs | 19 ++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/BTCPayServer/Controllers/UIWalletsController.cs b/BTCPayServer/Controllers/UIWalletsController.cs index 33324c0be..e4f64b780 100644 --- a/BTCPayServer/Controllers/UIWalletsController.cs +++ b/BTCPayServer/Controllers/UIWalletsController.cs @@ -317,8 +317,12 @@ namespace BTCPayServer.Controllers } model.Total = preFiltering ? null : model.Transactions.Count; - model.Transactions = model.Transactions.Skip(skip).Take(count) - .ToList(); + // if we couldn't filter at the db level, we need to apply skip and count + if (!preFiltering) + { + model.Transactions = model.Transactions.Skip(skip).Take(count) + .ToList(); + } } model.CryptoCode = walletId.CryptoCode; diff --git a/BTCPayServer/Services/Wallets/BTCPayWallet.cs b/BTCPayServer/Services/Wallets/BTCPayWallet.cs index 04922570c..3edd3a44e 100644 --- a/BTCPayServer/Services/Wallets/BTCPayWallet.cs +++ b/BTCPayServer/Services/Wallets/BTCPayWallet.cs @@ -201,7 +201,7 @@ namespace BTCPayServer.Services.Wallets } return await completionSource.Task; } - + bool? get_wallets_recentBugFixed = null; List dummy = new List(); public async Task> FetchTransactionHistory(DerivationStrategyBase derivationStrategyBase, int? skip = null, int? count = null, TimeSpan? interval = null) { @@ -231,6 +231,10 @@ namespace BTCPayServer.Services.Wallets else { await using var ctx = await NbxplorerConnectionFactory.OpenConnection(); + if (get_wallets_recentBugFixed is null) + { + get_wallets_recentBugFixed = await ctx.QuerySingleAsync("SELECT COUNT(*) = 1 FROM nbxv1_migrations WHERE script_name='011.FixGetWalletsRecent';"); + } var rows = await ctx.QueryAsync<(string tx_id, DateTimeOffset seen_at, string blk_id, long? blk_height, long balance_change, string asset_id, long confs)>( "SELECT r.tx_id, r.seen_at, t.blk_id, t.blk_height, r.balance_change, r.asset_id, COALESCE((SELECT height FROM get_tip('BTC')) - t.blk_height + 1, 0) AS confs " + "FROM get_wallets_recent(@wallet_id, @code, @interval, @count, @skip) r " + @@ -239,14 +243,23 @@ namespace BTCPayServer.Services.Wallets { wallet_id = NBXplorer.Client.DBUtils.nbxv1_get_wallet_id(Network.CryptoCode, derivationStrategyBase.ToString()), code = Network.CryptoCode, - count, - skip, + count = get_wallets_recentBugFixed is true ? count : skip + count, + skip = get_wallets_recentBugFixed is true ? skip : 0, interval = interval is TimeSpan t ? t : TimeSpan.FromDays(365 * 1000) }); rows.TryGetNonEnumeratedCount(out int c); var lines = new List(c); foreach (var row in rows) { + if (get_wallets_recentBugFixed is false) + { + if (skip > 0) + { + // We skip row manually so version of nbx before 2.3.34, return the expected... Remove in a year. + skip--; + continue; + } + } lines.Add(new TransactionHistoryLine() { BalanceChange = string.IsNullOrEmpty(row.asset_id) ? Money.Satoshis(row.balance_change) : new AssetMoney(uint256.Parse(row.asset_id), row.balance_change),