mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-17 05:54:26 +01:00
The Big Cleanup: Refactor BTCPay internals (#5809)
This commit is contained in:
@@ -19,8 +19,10 @@ using BTCPayServer.ModelBinders;
|
||||
using BTCPayServer.Models;
|
||||
using BTCPayServer.Models.WalletViewModels;
|
||||
using BTCPayServer.Payments;
|
||||
using BTCPayServer.Payments.Bitcoin;
|
||||
using BTCPayServer.Payments.PayJoin;
|
||||
using BTCPayServer.Services;
|
||||
using BTCPayServer.Services.Invoices;
|
||||
using BTCPayServer.Services.Labels;
|
||||
using BTCPayServer.Services.Rates;
|
||||
using BTCPayServer.Services.Stores;
|
||||
@@ -69,6 +71,8 @@ namespace BTCPayServer.Controllers
|
||||
private readonly DelayedTransactionBroadcaster _broadcaster;
|
||||
private readonly PayjoinClient _payjoinClient;
|
||||
private readonly LabelService _labelService;
|
||||
private readonly PaymentMethodHandlerDictionary _handlers;
|
||||
private readonly Dictionary<PaymentMethodId, IPaymentModelExtension> _paymentModelExtensions;
|
||||
private readonly TransactionLinkProviders _transactionLinkProviders;
|
||||
private readonly PullPaymentHostedService _pullPaymentHostedService;
|
||||
private readonly WalletHistogramService _walletHistogramService;
|
||||
@@ -94,10 +98,14 @@ namespace BTCPayServer.Controllers
|
||||
IServiceProvider serviceProvider,
|
||||
PullPaymentHostedService pullPaymentHostedService,
|
||||
LabelService labelService,
|
||||
PaymentMethodHandlerDictionary handlers,
|
||||
Dictionary<PaymentMethodId, IPaymentModelExtension> paymentModelExtensions,
|
||||
TransactionLinkProviders transactionLinkProviders)
|
||||
{
|
||||
_currencyTable = currencyTable;
|
||||
_labelService = labelService;
|
||||
_handlers = handlers;
|
||||
_paymentModelExtensions = paymentModelExtensions;
|
||||
_transactionLinkProviders = transactionLinkProviders;
|
||||
Repository = repo;
|
||||
WalletRepository = walletRepository;
|
||||
@@ -176,11 +184,11 @@ namespace BTCPayServer.Controllers
|
||||
var stores = await Repository.GetStoresByUserId(GetUserId());
|
||||
|
||||
var onChainWallets = stores
|
||||
.SelectMany(s => s.GetSupportedPaymentMethods(NetworkProvider)
|
||||
.OfType<DerivationSchemeSettings>()
|
||||
.Select(d => ((Wallet: _walletProvider.GetWallet(d.Network),
|
||||
DerivationStrategy: d.AccountDerivation,
|
||||
Network: d.Network)))
|
||||
.SelectMany(s => s.GetPaymentMethodConfigs<DerivationSchemeSettings>(_handlers)
|
||||
.Select(d => (
|
||||
Wallet: _walletProvider.GetWallet(((IHasNetwork)_handlers[d.Key]).Network),
|
||||
DerivationStrategy: d.Value.AccountDerivation,
|
||||
Network: ((IHasNetwork)_handlers[d.Key]).Network))
|
||||
.Where(_ => _.Wallet != null && _.Network.WalletSupported)
|
||||
.Select(_ => (Wallet: _.Wallet,
|
||||
Store: s,
|
||||
@@ -225,8 +233,8 @@ namespace BTCPayServer.Controllers
|
||||
var paymentMethod = GetDerivationSchemeSettings(walletId);
|
||||
if (paymentMethod == null)
|
||||
return NotFound();
|
||||
|
||||
var wallet = _walletProvider.GetWallet(paymentMethod.Network);
|
||||
var network = _handlers.GetBitcoinHandler(walletId.CryptoCode).Network;
|
||||
var wallet = _walletProvider.GetWallet(network);
|
||||
|
||||
// We can't filter at the database level if we need to apply label filter
|
||||
var preFiltering = string.IsNullOrEmpty(labelFilter);
|
||||
@@ -253,12 +261,12 @@ namespace BTCPayServer.Controllers
|
||||
}
|
||||
else
|
||||
{
|
||||
var pmi = new PaymentMethodId(walletId.CryptoCode, PaymentTypes.BTCLike);
|
||||
var pmi = PaymentTypes.CHAIN.GetPaymentMethodId(walletId.CryptoCode);
|
||||
foreach (var tx in transactions)
|
||||
{
|
||||
var vm = new ListTransactionsViewModel.TransactionViewModel();
|
||||
vm.Id = tx.TransactionId.ToString();
|
||||
vm.Link = _transactionLinkProviders.GetTransactionLink(pmi, vm.Id);
|
||||
vm.Link = _transactionLinkProviders.GetTransactionLink(network.CryptoCode, vm.Id);
|
||||
vm.Timestamp = tx.SeenAt;
|
||||
vm.Positive = tx.BalanceChange.GetValue(wallet.Network) >= 0;
|
||||
vm.Balance = tx.BalanceChange.ShowMoney(wallet.Network);
|
||||
@@ -325,7 +333,7 @@ namespace BTCPayServer.Controllers
|
||||
{
|
||||
bip21.QueryParams.Add(PayjoinClient.BIP21EndpointKey,
|
||||
Request.GetAbsoluteUri(Url.Action(nameof(PayJoinEndpointController.Submit), "PayJoinEndpoint",
|
||||
new { walletId.CryptoCode })));
|
||||
new { cryptoCode = walletId.CryptoCode })));
|
||||
}
|
||||
|
||||
string[]? labels = null;
|
||||
@@ -340,7 +348,7 @@ namespace BTCPayServer.Controllers
|
||||
{
|
||||
CryptoCode = walletId.CryptoCode,
|
||||
Address = address?.ToString(),
|
||||
CryptoImage = GetImage(paymentMethod.PaymentId, network),
|
||||
CryptoImage = GetImage(network),
|
||||
PaymentLink = bip21.ToString(),
|
||||
ReturnUrl = returnUrl ?? HttpContext.Request.GetTypedHeaders().Referer?.AbsolutePath,
|
||||
SelectedLabels = labels ?? Array.Empty<string>()
|
||||
@@ -449,7 +457,7 @@ namespace BTCPayServer.Controllers
|
||||
var storeData = store.GetStoreBlob();
|
||||
var rateRules = store.GetStoreBlob().GetRateRules(NetworkProvider);
|
||||
rateRules.Spread = 0.0m;
|
||||
var currencyPair = new Rating.CurrencyPair(paymentMethod.PaymentId.CryptoCode, storeData.DefaultCurrency);
|
||||
var currencyPair = new Rating.CurrencyPair(walletId.CryptoCode, storeData.DefaultCurrency);
|
||||
double.TryParse(defaultAmount, out var amount);
|
||||
|
||||
var model = new WalletSendModel
|
||||
@@ -591,7 +599,7 @@ namespace BTCPayServer.Controllers
|
||||
|
||||
var utxos = await _walletProvider.GetWallet(network)
|
||||
.GetUnspentCoins(schemeSettings.AccountDerivation, false, cancellation);
|
||||
var pmi = new PaymentMethodId(vm.CryptoCode, PaymentTypes.BTCLike);
|
||||
var pmi = PaymentTypes.CHAIN.GetPaymentMethodId(vm.CryptoCode);
|
||||
var walletTransactionsInfoAsync = await this.WalletRepository.GetWalletTransactionsInfo(walletId,
|
||||
utxos.SelectMany(GetWalletObjectsQuery.Get).Distinct().ToArray());
|
||||
vm.InputsAvailable = utxos.Select(coin =>
|
||||
@@ -606,7 +614,7 @@ namespace BTCPayServer.Controllers
|
||||
Amount = coin.Value.GetValue(network),
|
||||
Comment = info?.Comment,
|
||||
Labels = _labelService.CreateTransactionTagModels(info, Request),
|
||||
Link = _transactionLinkProviders.GetTransactionLink(pmi, coin.OutPoint.ToString()),
|
||||
Link = _transactionLinkProviders.GetTransactionLink(network.CryptoCode, coin.OutPoint.ToString()),
|
||||
Confirmations = coin.Confirmations
|
||||
};
|
||||
}).ToArray();
|
||||
@@ -753,7 +761,7 @@ namespace BTCPayServer.Controllers
|
||||
CreatePSBTResponse psbtResponse;
|
||||
if (command == "schedule")
|
||||
{
|
||||
var pmi = new PaymentMethodId(walletId.CryptoCode, BitcoinPaymentType.Instance);
|
||||
var pmi = PaymentTypes.CHAIN.GetPaymentMethodId(walletId.CryptoCode);
|
||||
var claims =
|
||||
vm.Outputs.Where(output => string.IsNullOrEmpty(output.PayoutId)).Select(output => new ClaimRequest()
|
||||
{
|
||||
@@ -1211,7 +1219,7 @@ namespace BTCPayServer.Controllers
|
||||
|
||||
internal DerivationSchemeSettings? GetDerivationSchemeSettings(WalletId walletId)
|
||||
{
|
||||
return GetCurrentStore().GetDerivationSchemeSettings(NetworkProvider, walletId.CryptoCode);
|
||||
return GetCurrentStore().GetDerivationSchemeSettings(_handlers, walletId.CryptoCode);
|
||||
}
|
||||
|
||||
private static async Task<IMoney> GetBalanceAsMoney(BTCPayWallet wallet,
|
||||
@@ -1251,7 +1259,8 @@ namespace BTCPayServer.Controllers
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var derivationScheme = GetDerivationSchemeSettings(walletId);
|
||||
if (derivationScheme == null || derivationScheme.Network.ReadonlyWallet)
|
||||
var network = _handlers.GetBitcoinHandler(walletId.CryptoCode).Network;
|
||||
if (derivationScheme == null || network.ReadonlyWallet)
|
||||
return NotFound();
|
||||
|
||||
switch (command)
|
||||
@@ -1335,7 +1344,8 @@ namespace BTCPayServer.Controllers
|
||||
if (paymentMethod == null)
|
||||
return NotFound();
|
||||
|
||||
var wallet = _walletProvider.GetWallet(paymentMethod.Network);
|
||||
var network = _handlers.GetBitcoinHandler(walletId.CryptoCode).Network;
|
||||
var wallet = _walletProvider.GetWallet(network);
|
||||
var walletTransactionsInfoAsync = WalletRepository.GetWalletTransactionsInfo(walletId, (string[]?)null);
|
||||
var input = await wallet.FetchTransactionHistory(paymentMethod.AccountDerivation, cancellationToken: cancellationToken);
|
||||
var walletTransactionsInfo = await walletTransactionsInfoAsync;
|
||||
@@ -1469,12 +1479,14 @@ namespace BTCPayServer.Controllers
|
||||
return RedirectToAction(nameof(WalletLabels), new { walletId });
|
||||
}
|
||||
|
||||
private string GetImage(PaymentMethodId paymentMethodId, BTCPayNetwork network)
|
||||
private string? GetImage(BTCPayNetwork network)
|
||||
{
|
||||
var res = paymentMethodId.PaymentType == PaymentTypes.BTCLike
|
||||
? Url.Content(network.CryptoImagePath)
|
||||
: Url.Content(network.LightningImagePath);
|
||||
return Request.GetRelativePathOrAbsolute(res);
|
||||
var pmi = PaymentTypes.CHAIN.GetPaymentMethodId(network.CryptoCode);
|
||||
if (_paymentModelExtensions.TryGetValue(pmi, out var extension))
|
||||
{
|
||||
return Request.GetRelativePathOrAbsolute(Url.Content(extension.Image));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private string GetUserId() => _userManager.GetUserId(User)!;
|
||||
|
||||
Reference in New Issue
Block a user