Isolating code of on chain specific payment in its own folder

This commit is contained in:
nicolas.dorier 2018-02-19 11:06:08 +09:00
parent 4795bd8108
commit 35f669aa15
9 changed files with 77 additions and 57 deletions

View File

@ -74,7 +74,7 @@ namespace BTCPayServer.Controllers
cryptoPayment.Due = accounting.Due.ToString() + $" {paymentNetwork.CryptoCode}";
cryptoPayment.Paid = accounting.CryptoPaid.ToString() + $" {paymentNetwork.CryptoCode}";
var onchainMethod = data.GetPaymentMethod() as BitcoinLikeOnChainPaymentMethod;
var onchainMethod = data.GetPaymentMethod() as Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod;
if(onchainMethod != null)
{
cryptoPayment.Address = onchainMethod.DepositAddress.ToString();
@ -89,7 +89,7 @@ namespace BTCPayServer.Controllers
.Where(p => p.GetCryptoDataId().PaymentType == PaymentTypes.BTCLike)
.Select(async payment =>
{
var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData();
var paymentData = (Payments.Bitcoin.BitcoinLikePaymentData)payment.GetCryptoPaymentData();
var m = new InvoiceDetailsModel.Payment();
var paymentNetwork = _NetworkProvider.GetNetwork(payment.GetCryptoCode());
m.CryptoCode = payment.GetCryptoCode();

View File

@ -142,7 +142,7 @@ namespace BTCPayServer.Controllers
{
CryptoData cryptoData = new CryptoData();
cryptoData.SetId(new CryptoDataId(q.network.CryptoCode, PaymentTypes.BTCLike));
BitcoinLikeOnChainPaymentMethod onchainMethod = new BitcoinLikeOnChainPaymentMethod();
Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod onchainMethod = new Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod();
onchainMethod.FeeRate = (await q.getFeeRate);
onchainMethod.TxFee = GetTxFee(storeBlob, onchainMethod.FeeRate); // assume price for 100 bytes
cryptoData.Rate = await q.getRate;

View File

@ -143,7 +143,7 @@ namespace BTCPayServer.Hosting
});
services.AddSingleton<IHostedService, NBXplorerWaiters>();
services.AddSingleton<IHostedService, NBXplorerListener>();
services.AddSingleton<IHostedService, Payments.Bitcoin.NBXplorerListener>();
services.AddSingleton<IHostedService, InvoiceNotificationManager>();
services.AddSingleton<IHostedService, InvoiceWatcher>();

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Services.Invoices;
using NBitcoin;
using Newtonsoft.Json;
namespace BTCPayServer.Payments.Bitcoin
{
public class BitcoinLikeOnChainPaymentMethod : IPaymentMethod
{
public PaymentTypes GetPaymentType()
{
return PaymentTypes.BTCLike;
}
public string GetPaymentDestination()
{
return DepositAddress?.ToString();
}
public Money GetTxFee()
{
return TxFee;
}
public void SetPaymentDestination(string newPaymentDestination)
{
if (newPaymentDestination == null)
DepositAddress = null;
else
DepositAddress = BitcoinAddress.Create(newPaymentDestination, DepositAddress.Network);
}
[JsonIgnore]
public FeeRate FeeRate { get; set; }
[JsonIgnore]
public Money TxFee { get; set; }
[JsonIgnore]
public BitcoinAddress DepositAddress { get; set; }
}
}

View File

@ -6,40 +6,8 @@ using BTCPayServer.Services.Invoices;
using NBitcoin;
using Newtonsoft.Json;
namespace BTCPayServer.Payments
namespace BTCPayServer.Payments.Bitcoin
{
public class BitcoinLikeOnChainPaymentMethod : IPaymentMethod
{
public PaymentTypes GetPaymentType()
{
return PaymentTypes.BTCLike;
}
public string GetPaymentDestination()
{
return DepositAddress?.ToString();
}
public Money GetTxFee()
{
return TxFee;
}
public void SetPaymentDestination(string newPaymentDestination)
{
if (newPaymentDestination == null)
DepositAddress = null;
else
DepositAddress = BitcoinAddress.Create(newPaymentDestination, DepositAddress.Network);
}
[JsonIgnore]
public FeeRate FeeRate { get; set; }
[JsonIgnore]
public Money TxFee { get; set; }
[JsonIgnore]
public BitcoinAddress DepositAddress { get; set; }
}
public class BitcoinLikePaymentData : CryptoPaymentData
{

View File

@ -17,8 +17,9 @@ using BTCPayServer.Services.Wallets;
using NBitcoin;
using NBXplorer.Models;
using BTCPayServer.Payments;
using BTCPayServer.HostedServices;
namespace BTCPayServer.HostedServices
namespace BTCPayServer.Payments.Bitcoin
{
public class NBXplorerListener : IHostedService
{
@ -353,7 +354,7 @@ namespace BTCPayServer.HostedServices
var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData();
var invoice = (await UpdatePaymentStates(wallet, invoiceId));
var cryptoData = invoice.GetCryptoData(wallet.Network, PaymentTypes.BTCLike, _ExplorerClients.NetworkProviders);
var method = cryptoData.GetPaymentMethod() as Payments.BitcoinLikeOnChainPaymentMethod;
var method = cryptoData.GetPaymentMethod() as BitcoinLikeOnChainPaymentMethod;
if (method.DepositAddress.ScriptPubKey == paymentData.Output.ScriptPubKey && cryptoData.Calculate().Due > Money.Zero)
{
var address = await wallet.ReserveAddressAsync(strategy);

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NBitcoin;
namespace BTCPayServer.Payments
{
public interface IPaymentMethod
{
string GetPaymentDestination();
PaymentTypes GetPaymentType();
Money GetTxFee();
void SetPaymentDestination(string newPaymentDestination);
}
}

View File

@ -366,8 +366,7 @@ namespace BTCPayServer.Services.Invoices
cryptoInfo.TxCount = accounting.TxCount;
cryptoInfo.CryptoPaid = accounting.CryptoPaid.ToString();
if (info.GetPaymentMethod() is BitcoinLikeOnChainPaymentMethod onchainMethod)
cryptoInfo.Address = onchainMethod.DepositAddress?.ToString();
cryptoInfo.Address = info.GetPaymentMethod()?.GetPaymentDestination();
cryptoInfo.ExRates = new Dictionary<string, double>
{
{ ProductInformation.Currency, (double)cryptoInfo.Rate }
@ -524,14 +523,6 @@ namespace BTCPayServer.Services.Invoices
public Money NetworkFee { get; set; }
}
public interface IPaymentMethod
{
string GetPaymentDestination();
PaymentTypes GetPaymentType();
Money GetTxFee();
void SetPaymentDestination(string newPaymentDestination);
}
public class CryptoDataId
{
public CryptoDataId(string cryptoCode, PaymentTypes paymentType)
@ -627,7 +618,7 @@ namespace BTCPayServer.Services.Invoices
// Legacy, old code does not have PaymentMethods
if (string.IsNullOrEmpty(PaymentType) || PaymentMethod == null)
{
return new BitcoinLikeOnChainPaymentMethod()
return new Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod()
{
FeeRate = FeeRate,
DepositAddress = string.IsNullOrEmpty(DepositAddress) ? null : BitcoinAddress.Create(DepositAddress, Network?.NBitcoinNetwork),
@ -639,7 +630,7 @@ namespace BTCPayServer.Services.Invoices
if (GetId().PaymentType == PaymentTypes.BTCLike)
{
var method = DeserializePaymentMethod<BitcoinLikeOnChainPaymentMethod>(PaymentMethod);
var method = DeserializePaymentMethod<Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod>(PaymentMethod);
method.TxFee = TxFee;
method.DepositAddress = BitcoinAddress.Create(DepositAddress, Network?.NBitcoinNetwork);
method.FeeRate = FeeRate;
@ -665,7 +656,7 @@ namespace BTCPayServer.Services.Invoices
else if (PaymentType != paymentMethod.GetPaymentType().ToString())
throw new InvalidOperationException("Invalid payment method affected");
if (paymentMethod is BitcoinLikeOnChainPaymentMethod bitcoinPaymentMethod)
if (paymentMethod is Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod bitcoinPaymentMethod)
{
TxFee = bitcoinPaymentMethod.TxFee;
FeeRate = bitcoinPaymentMethod.FeeRate;
@ -792,7 +783,7 @@ namespace BTCPayServer.Services.Invoices
if (string.IsNullOrEmpty(CryptoPaymentDataType))
{
// In case this is a payment done before this update, consider it unconfirmed with RBF for safety
var paymentData = new BitcoinLikePaymentData();
var paymentData = new Payments.Bitcoin.BitcoinLikePaymentData();
paymentData.Outpoint = Outpoint;
paymentData.Output = Output;
paymentData.RBF = true;
@ -802,7 +793,7 @@ namespace BTCPayServer.Services.Invoices
}
if (GetCryptoDataId().PaymentType == PaymentTypes.BTCLike)
{
var paymentData = JsonConvert.DeserializeObject<BitcoinLikePaymentData>(CryptoPaymentData);
var paymentData = JsonConvert.DeserializeObject<Payments.Bitcoin.BitcoinLikePaymentData>(CryptoPaymentData);
// legacy
paymentData.Output = Output;
paymentData.Outpoint = Outpoint;
@ -816,7 +807,7 @@ namespace BTCPayServer.Services.Invoices
public void SetCryptoPaymentData(CryptoPaymentData cryptoPaymentData)
{
#pragma warning disable CS0618
if (cryptoPaymentData is BitcoinLikePaymentData paymentData)
if (cryptoPaymentData is Payments.Bitcoin.BitcoinLikePaymentData paymentData)
{
// Legacy
Outpoint = paymentData.Outpoint;

View File

@ -18,6 +18,7 @@ using BTCPayServer.Data;
using System.Globalization;
using BTCPayServer.Models.InvoicingModels;
using BTCPayServer.Logging;
using BTCPayServer.Payments;
namespace BTCPayServer.Services.Invoices
{
@ -166,7 +167,7 @@ namespace BTCPayServer.Services.Invoices
ScriptId hash = null;
if (cryptoData.GetId().PaymentType == Payments.PaymentTypes.BTCLike)
{
hash = ((Payments.BitcoinLikeOnChainPaymentMethod)cryptoData.GetPaymentMethod()).DepositAddress.ScriptPubKey.Hash;
hash = ((Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod)cryptoData.GetPaymentMethod()).DepositAddress.ScriptPubKey.Hash;
}
return hash;
}