Remove additional cryptoCode from events (#6277)

This commit is contained in:
Nicolas Dorier 2024-10-07 09:37:56 +09:00 committed by GitHub
parent 471bf57835
commit 01e12329e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 35 additions and 38 deletions

View File

@ -3776,7 +3776,7 @@ namespace BTCPayServer.Tests
{
await tester.ExplorerNode.GenerateAsync(1);
}, bevent => bevent.CryptoCode.Equals("BTC", StringComparison.Ordinal));
}, bevent => bevent.PaymentMethodId == PaymentTypes.CHAIN.GetPaymentMethodId("BTC"));
Assert.Contains(
await client.ShowOnChainWalletTransactions(walletId.StoreId, walletId.CryptoCode,

View File

@ -35,7 +35,6 @@ using StoreData = BTCPayServer.Data.StoreData;
public class BitcoinLikePayoutHandler : IPayoutHandler, IHasNetwork
{
public string Currency { get; }
private readonly BTCPayNetworkProvider _btcPayNetworkProvider;
private readonly PaymentMethodHandlerDictionary _paymentHandlers;
private readonly ExplorerClientProvider _explorerClientProvider;
private readonly BTCPayNetworkJsonSerializerSettings _jsonSerializerSettings;
@ -65,7 +64,6 @@ public class BitcoinLikePayoutHandler : IPayoutHandler, IHasNetwork
EventAggregator eventAggregator,
TransactionLinkProviders transactionLinkProviders)
{
_btcPayNetworkProvider = btcPayNetworkProvider;
PayoutMethodId = payoutMethodId;
PaymentMethodId = PaymentTypes.CHAIN.GetPaymentMethodId(network.CryptoCode);
Network = network;
@ -174,12 +172,14 @@ public class BitcoinLikePayoutHandler : IPayoutHandler, IHasNetwork
public async Task BackgroundCheck(object o)
{
if (o is NewOnChainTransactionEvent newTransaction && newTransaction.NewTransactionEvent.TrackedSource is AddressTrackedSource addressTrackedSource)
if (o is NewOnChainTransactionEvent newTransaction && newTransaction.NewTransactionEvent.TrackedSource is AddressTrackedSource addressTrackedSource
&& newTransaction.PaymentMethodId == PaymentMethodId)
{
await UpdatePayoutsAwaitingForPayment(newTransaction, addressTrackedSource);
}
if (o is NewBlockEvent || o is NewOnChainTransactionEvent)
if ((o is NewBlockEvent nbe && nbe.PaymentMethodId == PaymentMethodId) ||
(o is NewOnChainTransactionEvent nct && nct.PaymentMethodId == PaymentMethodId))
{
await UpdatePayoutsInProgress();
}
@ -416,18 +416,17 @@ public class BitcoinLikePayoutHandler : IPayoutHandler, IHasNetwork
{
try
{
var network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(newTransaction.CryptoCode);
var destinationSum =
newTransaction.NewTransactionEvent.Outputs.Sum(output => output.Value.GetValue(network));
newTransaction.NewTransactionEvent.Outputs.Sum(output => output.Value.GetValue(Network));
var destination = addressTrackedSource.Address.ToString();
var paymentMethodId = PaymentTypes.CHAIN.GetPaymentMethodId(newTransaction.CryptoCode);
await using var ctx = _dbContextFactory.CreateContext();
var payout = await ctx.Payouts
.Include(o => o.StoreData)
.Include(o => o.PullPaymentData)
.Where(p => p.State == PayoutState.AwaitingPayment)
.Where(p => p.PayoutMethodId == paymentMethodId.ToString())
.Where(p => p.PayoutMethodId == PaymentMethodId.ToString())
#pragma warning disable CA1307 // Specify StringComparison
.Where(p => destination.Equals(p.DedupId))
#pragma warning restore CA1307 // Specify StringComparison
@ -440,15 +439,15 @@ public class BitcoinLikePayoutHandler : IPayoutHandler, IHasNetwork
// The round up here is not strictly necessary, this is temporary to fix existing payout before we
// were properly roundup the crypto amount
destinationSum !=
BTCPayServer.Extensions.RoundUp(payout.Amount.Value, network.Divisibility))
BTCPayServer.Extensions.RoundUp(payout.Amount.Value, Network.Divisibility))
return;
var derivationSchemeSettings = payout.StoreData
.GetDerivationSchemeSettings(_paymentHandlers, newTransaction.CryptoCode)?.AccountDerivation;
.GetDerivationSchemeSettings(_paymentHandlers, Network.CryptoCode)?.AccountDerivation;
if (derivationSchemeSettings is null)
return;
var storeWalletMatched = (await _explorerClientProvider.GetExplorerClient(newTransaction.CryptoCode)
var storeWalletMatched = (await _explorerClientProvider.GetExplorerClient(Network.CryptoCode)
.GetTransactionAsync(derivationSchemeSettings,
newTransaction.NewTransactionEvent.TransactionData.TransactionHash));
//if the wallet related to the store of the payout does not have the tx: it has been paid externally
@ -463,7 +462,7 @@ public class BitcoinLikePayoutHandler : IPayoutHandler, IHasNetwork
{
payout.State = PayoutState.InProgress;
await WalletRepository.AddWalletTransactionAttachment(
new WalletId(payout.StoreDataId, newTransaction.CryptoCode),
new WalletId(payout.StoreDataId, Network.CryptoCode),
newTransaction.NewTransactionEvent.TransactionData.TransactionHash,
Attachment.Payout(payout.PullPaymentDataId, payout.Id));
}

View File

@ -1,11 +1,13 @@
using BTCPayServer.Payments;
namespace BTCPayServer.Events
{
public class NewBlockEvent
{
public string CryptoCode { get; set; }
public PaymentMethodId PaymentMethodId { get; set; }
public override string ToString()
{
return $"{CryptoCode}: New block";
return $"{PaymentMethodId}: New block";
}
}
}

View File

@ -1,3 +1,4 @@
using BTCPayServer.Payments;
using NBXplorer.Models;
namespace BTCPayServer.Events
@ -5,12 +6,12 @@ namespace BTCPayServer.Events
public class NewOnChainTransactionEvent
{
public NewTransactionEvent NewTransactionEvent { get; set; }
public string CryptoCode { get; set; }
public PaymentMethodId PaymentMethodId { get; set; }
public override string ToString()
{
var state = NewTransactionEvent.BlockId == null ? "Unconfirmed" : "Confirmed";
return $"{CryptoCode}: New transaction {NewTransactionEvent.TransactionData.TransactionHash} ({state})";
return $"{PaymentMethodId}: New transaction {NewTransactionEvent.TransactionData.TransactionHash} ({state})";
}
}
}

View File

@ -410,20 +410,17 @@ namespace BTCPayServer
}
public static BitcoinLikePaymentHandler? TryGetBitcoinHandler(this PaymentMethodHandlerDictionary handlers, BTCPayNetwork network)
{
return handlers.TryGetBitcoinHandler(network.CryptoCode);
}
=> handlers.TryGetBitcoinHandler(network.CryptoCode);
public static BitcoinLikePaymentHandler? TryGetBitcoinHandler(this PaymentMethodHandlerDictionary handlers, string cryptoCode)
=> handlers.TryGetBitcoinHandler(PaymentTypes.CHAIN.GetPaymentMethodId(cryptoCode));
public static BitcoinLikePaymentHandler? TryGetBitcoinHandler(this PaymentMethodHandlerDictionary handlers, PaymentMethodId paymentMethodId)
{
var pmi = PaymentTypes.CHAIN.GetPaymentMethodId(cryptoCode);
if (handlers.TryGetValue(pmi, out var h) && h is BitcoinLikePaymentHandler b)
if (handlers.TryGetValue(paymentMethodId, out var h) && h is BitcoinLikePaymentHandler b)
return b;
return null;
}
public static BitcoinLikePaymentHandler GetBitcoinHandler(this PaymentMethodHandlerDictionary handlers, BTCPayNetwork network)
{
return handlers.GetBitcoinHandler(network.CryptoCode);
}
=> handlers.GetBitcoinHandler(network.CryptoCode);
public static BitcoinLikePaymentHandler GetBitcoinHandler(this PaymentMethodHandlerDictionary handlers, string cryptoCode)
{
var pmi = PaymentTypes.CHAIN.GetPaymentMethodId(cryptoCode);

View File

@ -43,7 +43,7 @@ namespace BTCPayServer.HostedServices
// If we find, then we create a link between them and the tx object.
case NewOnChainTransactionEvent transactionEvent:
{
var handler = _handlers.TryGetBitcoinHandler(transactionEvent.CryptoCode);
var handler = _handlers.TryGetBitcoinHandler(transactionEvent.PaymentMethodId);
var derivation = transactionEvent.NewTransactionEvent.DerivationStrategy;
if (handler is null || derivation is null)
break;

View File

@ -152,7 +152,7 @@ namespace BTCPayServer.Payments.Bitcoin
{
case NBXplorer.Models.NewBlockEvent evt:
await UpdatePaymentStates(wallet);
_Aggregator.Publish(new Events.NewBlockEvent() { CryptoCode = evt.CryptoCode });
_Aggregator.Publish(new Events.NewBlockEvent() { PaymentMethodId = pmi });
break;
case NBXplorer.Models.NewTransactionEvent evt:
if (evt.DerivationStrategy != null)
@ -202,7 +202,7 @@ namespace BTCPayServer.Payments.Bitcoin
_Aggregator.Publish(new NewOnChainTransactionEvent()
{
CryptoCode = wallet.Network.CryptoCode,
PaymentMethodId = pmi,
NewTransactionEvent = evt
});

View File

@ -267,7 +267,7 @@ namespace BTCPayServer.Services.Altcoins.Monero.Services
private async Task OnNewBlock(string cryptoCode)
{
await UpdateAnyPendingMoneroLikePayment(cryptoCode);
_eventAggregator.Publish(new NewBlockEvent() { CryptoCode = cryptoCode });
_eventAggregator.Publish(new NewBlockEvent() { PaymentMethodId = PaymentTypes.CHAIN.GetPaymentMethodId(cryptoCode) });
}
private async Task OnTransactionUpdated(string cryptoCode, string transactionHash)

View File

@ -265,7 +265,7 @@ namespace BTCPayServer.Services.Altcoins.Zcash.Services
private async Task OnNewBlock(string cryptoCode)
{
await UpdateAnyPendingZcashLikePayment(cryptoCode);
_eventAggregator.Publish(new NewBlockEvent() { CryptoCode = cryptoCode });
_eventAggregator.Publish(new NewBlockEvent() { PaymentMethodId = PaymentTypes.CHAIN.GetPaymentMethodId(cryptoCode) });
}
private async Task OnTransactionUpdated(string cryptoCode, string transactionHash)

View File

@ -6,6 +6,7 @@ using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Data;
using BTCPayServer.Events;
using BTCPayServer.Payments;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Stores;
using Microsoft.Extensions.Hosting;
@ -102,11 +103,11 @@ namespace BTCPayServer.Services.Wallets
_walletReceiveState.AddOrReplace(walletId, information);
}
public IEnumerable<KeyValuePair<WalletId, KeyPathInformation>> GetByDerivation(string cryptoCode,
public IEnumerable<KeyValuePair<WalletId, KeyPathInformation>> GetByDerivation(PaymentMethodId paymentMethodId,
DerivationStrategyBase derivationStrategyBase)
{
return _walletReceiveState.Where(pair =>
pair.Key.CryptoCode.Equals(cryptoCode, StringComparison.InvariantCulture) &&
pair.Key.PaymentMethodId == paymentMethodId &&
pair.Value.DerivationStrategy == derivationStrategyBase);
}
@ -117,7 +118,7 @@ namespace BTCPayServer.Services.Wallets
_leases.Add(_eventAggregator.Subscribe<NewOnChainTransactionEvent>(evt =>
{
var matching = GetByDerivation(evt.CryptoCode, evt.NewTransactionEvent.DerivationStrategy).Where(pair =>
var matching = GetByDerivation(evt.PaymentMethodId, evt.NewTransactionEvent.DerivationStrategy).Where(pair =>
evt.NewTransactionEvent.Outputs.Any(output => output.ScriptPubKey == pair.Value.ScriptPubKey));
foreach (var keyValuePair in matching)

View File

@ -27,14 +27,11 @@ namespace BTCPayServer
ArgumentNullException.ThrowIfNull(cryptoCode);
StoreId = storeId;
CryptoCode = cryptoCode;
PaymentMethodId = PaymentTypes.CHAIN.GetPaymentMethodId(CryptoCode);
}
public string StoreId { get; }
public string CryptoCode { get; }
public PaymentMethodId GetPaymentMethodId()
{
return PaymentTypes.CHAIN.GetPaymentMethodId(CryptoCode);
}
public PaymentMethodId PaymentMethodId { get; }
public static WalletId Parse(string id)
{