btcpayserver/BTCPayServer/HostedServices/WalletReceiveCacheUpdater.cs
Andrew Camilleri 025da0261d new feature: Wallet Receive Page (#1065)
* new feature: Wallet Receive Page

closes #965

* Conserve addresses by waiting till address is spent before generating each run

* fix tests

* Filter by cryptocode before matching outpoint

* fix build

* fix edge case issue

* use address in keypathinfo directly

* rebase fixes

* rebase fixes

* remove duplicate code

* fix messy condition

* fixes

* fix e2e

* fix
2020-01-18 14:12:27 +09:00

52 lines
1.8 KiB
C#

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Events;
using BTCPayServer.Services.Wallets;
using Microsoft.Extensions.Hosting;
using NBXplorer;
namespace BTCPayServer.HostedServices
{
public class WalletReceiveCacheUpdater : IHostedService
{
private readonly EventAggregator _EventAggregator;
private readonly WalletReceiveStateService _WalletReceiveStateService;
private readonly CompositeDisposable _Leases = new CompositeDisposable();
public WalletReceiveCacheUpdater(EventAggregator eventAggregator,
WalletReceiveStateService walletReceiveStateService)
{
_EventAggregator = eventAggregator;
_WalletReceiveStateService = walletReceiveStateService;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_Leases.Add(_EventAggregator.Subscribe<WalletChangedEvent>(evt =>
_WalletReceiveStateService.Remove(evt.WalletId)));
_Leases.Add(_EventAggregator.Subscribe<NewOnChainTransactionEvent>(evt =>
{
var matching = _WalletReceiveStateService
.GetByDerivation(evt.CryptoCode, evt.NewTransactionEvent.DerivationStrategy).Where(pair =>
evt.NewTransactionEvent.Outputs.Any(output => output.ScriptPubKey == pair.Value.ScriptPubKey));
foreach (var keyValuePair in matching)
{
_WalletReceiveStateService.Remove(keyValuePair.Key);
}
}));
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_Leases.Dispose();
return Task.CompletedTask;
}
}
}