Files
btcpayserver/BTCPayServer/Services/Cheater.cs
Andrew Camilleri 75bf8a5086 Use Mempoolspace fees (#5490)
* Use Mempoolspace fees

Since bitcoind's fee estiomates are horrible, I would use an altenrative, but that adds a third party to the mix. We can either:
* Accept the risk (it is only for fee estimation anyway)
* Offer a toggle in the server settings
* Move this code to a plugin

* refactor

* Refactor

---------

Co-authored-by: nicolas.dorier <nicolas.dorier@gmail.com>
2023-11-28 18:26:35 +09:00

73 lines
2.3 KiB
C#

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Services.Invoices;
using Microsoft.Extensions.Hosting;
using NBitcoin;
using NBitcoin.RPC;
namespace BTCPayServer.Services
{
public class Cheater : IHostedService
{
private readonly ExplorerClientProvider _prov;
private readonly InvoiceRepository _invoiceRepository;
public RPCClient CashCow { get; set; }
public Cheater(
ExplorerClientProvider prov,
InvoiceRepository invoiceRepository)
{
CashCow = prov.GetExplorerClient("BTC")?.RPCClient;
_prov = prov;
_invoiceRepository = invoiceRepository;
}
public RPCClient GetCashCow(string cryptoCode)
{
return _prov.GetExplorerClient(cryptoCode)?.RPCClient;
}
public async Task UpdateInvoiceExpiry(string invoiceId, TimeSpan seconds)
{
await _invoiceRepository.UpdateInvoiceExpiry(invoiceId, seconds);
}
async Task IHostedService.StartAsync(CancellationToken cancellationToken)
{
#if ALTCOINS
var liquid = _prov.GetNetwork("LBTC");
if (liquid is not null)
{
var lbtcrpc = GetCashCow(liquid.CryptoCode);
await lbtcrpc.SendCommandAsync("rescanblockchain");
var elements = _prov.NetworkProviders.GetAll().OfType<ElementsBTCPayNetwork>();
foreach (ElementsBTCPayNetwork element in elements)
{
try
{
if (element.AssetId is null)
{
var issueAssetResult = await lbtcrpc.SendCommandAsync("issueasset", 100000, 0);
element.AssetId = uint256.Parse(issueAssetResult.Result["asset"].ToString());
}
}
catch (Exception)
{
}
}
}
#else
if (CashCow is { } c)
await c.ScanRPCCapabilitiesAsync(cancellationToken);
#endif
}
Task IHostedService.StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}