Further isolate bitcoin related stuff inside BitcoinLikePaymentData

This commit is contained in:
nicolas.dorier
2018-02-18 02:35:02 +09:00
parent a1ee09cd85
commit 3bc232e1da
3 changed files with 31 additions and 26 deletions

View File

@@ -80,14 +80,14 @@ namespace BTCPayServer.Controllers
var payments = invoice var payments = invoice
.GetPayments() .GetPayments()
.Where(p => p.GetCryptoPaymentData() is BitcoinLikePaymentData) .Where(p => p.GetCryptoPaymentDataType() == BitcoinLikePaymentData.OnchainBitcoinType)
.Select(async payment => .Select(async payment =>
{ {
var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData(); var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData();
var m = new InvoiceDetailsModel.Payment(); var m = new InvoiceDetailsModel.Payment();
var paymentNetwork = _NetworkProvider.GetNetwork(payment.GetCryptoCode()); var paymentNetwork = _NetworkProvider.GetNetwork(payment.GetCryptoCode());
m.CryptoCode = payment.GetCryptoCode(); m.CryptoCode = payment.GetCryptoCode();
m.DepositAddress = payment.GetScriptPubKey().GetDestinationAddress(paymentNetwork.NBitcoinNetwork); m.DepositAddress = paymentData.Output.ScriptPubKey.GetDestinationAddress(paymentNetwork.NBitcoinNetwork);
int confirmationCount = 0; int confirmationCount = 0;
if(paymentData.Legacy) // The confirmation count in the paymentData is not up to date if(paymentData.Legacy) // The confirmation count in the paymentData is not up to date

View File

@@ -137,7 +137,7 @@ namespace BTCPayServer.HostedServices
await session.ListenNewBlockAsync(_Cts.Token).ConfigureAwait(false); await session.ListenNewBlockAsync(_Cts.Token).ConfigureAwait(false);
await session.ListenDerivationSchemesAsync((await GetStrategies(network)).ToArray(), _Cts.Token).ConfigureAwait(false); await session.ListenDerivationSchemesAsync((await GetStrategies(network)).ToArray(), _Cts.Token).ConfigureAwait(false);
Logs.PayServer.LogInformation($"{network.CryptoCode}: if any pending invoice got paid while offline..."); Logs.PayServer.LogInformation($"{network.CryptoCode}: checking if any pending invoice got paid while offline...");
int paymentCount = await FindPaymentViaPolling(wallet, network); int paymentCount = await FindPaymentViaPolling(wallet, network);
Logs.PayServer.LogInformation($"{network.CryptoCode}: {paymentCount} payments happened while offline"); Logs.PayServer.LogInformation($"{network.CryptoCode}: {paymentCount} payments happened while offline");
@@ -209,8 +209,8 @@ namespace BTCPayServer.HostedServices
IEnumerable<BitcoinLikePaymentData> GetAllBitcoinPaymentData(InvoiceEntity invoice) IEnumerable<BitcoinLikePaymentData> GetAllBitcoinPaymentData(InvoiceEntity invoice)
{ {
return invoice.GetPayments() return invoice.GetPayments()
.Select(p => p.GetCryptoPaymentData() as BitcoinLikePaymentData) .Where(p => p.GetCryptoPaymentDataType() == BitcoinLikePaymentData.OnchainBitcoinType)
.Where(p => p != null); .Select(p => (BitcoinLikePaymentData)p.GetCryptoPaymentData());
} }
async Task<InvoiceEntity> UpdatePaymentStates(BTCPayWallet wallet, string invoiceId) async Task<InvoiceEntity> UpdatePaymentStates(BTCPayWallet wallet, string invoiceId)
@@ -223,9 +223,9 @@ namespace BTCPayServer.HostedServices
var conflicts = GetConflicts(transactions.Select(t => t.Value)); var conflicts = GetConflicts(transactions.Select(t => t.Value));
foreach (var payment in invoice.GetPayments(wallet.Network)) foreach (var payment in invoice.GetPayments(wallet.Network))
{ {
var paymentData = payment.GetCryptoPaymentData() as BitcoinLikePaymentData; if (payment.GetCryptoPaymentDataType() != BitcoinLikePaymentData.OnchainBitcoinType)
if (paymentData == null)
continue; continue;
var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData();
if (!transactions.TryGetValue(paymentData.Outpoint.Hash, out TransactionResult tx)) if (!transactions.TryGetValue(paymentData.Outpoint.Hash, out TransactionResult tx))
continue; continue;
var txId = tx.Transaction.GetHash(); var txId = tx.Transaction.GetHash();
@@ -346,9 +346,10 @@ namespace BTCPayServer.HostedServices
private async Task<InvoiceEntity> ReceivedPayment(BTCPayWallet wallet, string invoiceId, PaymentEntity payment, DerivationStrategyBase strategy) private async Task<InvoiceEntity> ReceivedPayment(BTCPayWallet wallet, string invoiceId, PaymentEntity payment, DerivationStrategyBase strategy)
{ {
var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData();
var invoice = (await UpdatePaymentStates(wallet, invoiceId)); var invoice = (await UpdatePaymentStates(wallet, invoiceId));
var cryptoData = invoice.GetCryptoData(wallet.Network, _ExplorerClients.NetworkProviders); var cryptoData = invoice.GetCryptoData(wallet.Network, _ExplorerClients.NetworkProviders);
if (cryptoData.GetDepositAddress().ScriptPubKey == payment.GetScriptPubKey() && cryptoData.Calculate().Due > Money.Zero) if (cryptoData.GetDepositAddress().ScriptPubKey == paymentData.Output.ScriptPubKey && cryptoData.Calculate().Due > Money.Zero)
{ {
var address = await wallet.ReserveAddressAsync(strategy); var address = await wallet.ReserveAddressAsync(strategy);
await _InvoiceRepository.NewAddress(invoiceId, address, wallet.Network); await _InvoiceRepository.NewAddress(invoiceId, address, wallet.Network);

View File

@@ -573,7 +573,7 @@ namespace BTCPayServer.Services.Invoices
paidEnough |= totalDue <= paid; paidEnough |= totalDue <= paid;
if (CryptoCode == _.GetCryptoCode()) if (CryptoCode == _.GetCryptoCode())
{ {
cryptoPaid += _.GetValue(); cryptoPaid += _.GetCryptoPaymentData().GetValue();
txCount++; txCount++;
} }
return _; return _;
@@ -617,13 +617,6 @@ namespace BTCPayServer.Services.Invoices
get; set; get; set;
} }
public Script GetScriptPubKey()
{
#pragma warning disable CS0618
return Output.ScriptPubKey;
#pragma warning restore CS0618
}
public bool Accounted public bool Accounted
{ {
get; set; get; set;
@@ -639,9 +632,16 @@ namespace BTCPayServer.Services.Invoices
[Obsolete("Use GetCryptoPaymentData() instead")] [Obsolete("Use GetCryptoPaymentData() instead")]
public string CryptoPaymentData { get; set; } public string CryptoPaymentData { get; set; }
[Obsolete("Use GetCryptoPaymentData() instead")] [Obsolete("Use GetCryptoPaymentDataType() instead")]
public string CryptoPaymentDataType { get; set; } public string CryptoPaymentDataType { get; set; }
public string GetCryptoPaymentDataType()
{
#pragma warning disable CS0618 // Type or member is obsolete
return String.IsNullOrEmpty(CryptoPaymentDataType) ? BitcoinLikePaymentData.OnchainBitcoinType : CryptoPaymentDataType;
#pragma warning restore CS0618 // Type or member is obsolete
}
public CryptoPaymentData GetCryptoPaymentData() public CryptoPaymentData GetCryptoPaymentData()
{ {
#pragma warning disable CS0618 #pragma warning disable CS0618
@@ -656,7 +656,7 @@ namespace BTCPayServer.Services.Invoices
paymentData.Legacy = true; paymentData.Legacy = true;
return paymentData; return paymentData;
} }
if (CryptoPaymentDataType == "BTCLike") if (CryptoPaymentDataType == BitcoinLikePaymentData.OnchainBitcoinType)
{ {
var paymentData = JsonConvert.DeserializeObject<BitcoinLikePaymentData>(CryptoPaymentData); var paymentData = JsonConvert.DeserializeObject<BitcoinLikePaymentData>(CryptoPaymentData);
// legacy // legacy
@@ -674,7 +674,7 @@ namespace BTCPayServer.Services.Invoices
#pragma warning disable CS0618 #pragma warning disable CS0618
if (cryptoPaymentData is BitcoinLikePaymentData paymentData) if (cryptoPaymentData is BitcoinLikePaymentData paymentData)
{ {
CryptoPaymentDataType = "BTCLike"; CryptoPaymentDataType = BitcoinLikePaymentData.OnchainBitcoinType;
// Legacy // Legacy
Outpoint = paymentData.Outpoint; Outpoint = paymentData.Outpoint;
Output = paymentData.Output; Output = paymentData.Output;
@@ -685,13 +685,6 @@ namespace BTCPayServer.Services.Invoices
CryptoPaymentData = JsonConvert.SerializeObject(cryptoPaymentData); CryptoPaymentData = JsonConvert.SerializeObject(cryptoPaymentData);
#pragma warning restore CS0618 #pragma warning restore CS0618
} }
public Money GetValue()
{
#pragma warning disable CS0618
return Output.Value;
#pragma warning restore CS0618
}
public Money GetValue(Dictionary<string, CryptoData> cryptoData, string cryptoCode, Money value = null) public Money GetValue(Dictionary<string, CryptoData> cryptoData, string cryptoCode, Money value = null)
{ {
#pragma warning disable CS0618 #pragma warning disable CS0618
@@ -730,6 +723,11 @@ namespace BTCPayServer.Services.Invoices
/// </summary> /// </summary>
/// <returns>The search terms</returns> /// <returns>The search terms</returns>
string[] GetSearchTerms(); string[] GetSearchTerms();
/// <summary>
/// Get value of what as been paid
/// </summary>
/// <returns>The amount paid</returns>
Money GetValue();
bool PaymentCompleted(PaymentEntity entity, BTCPayNetwork network); bool PaymentCompleted(PaymentEntity entity, BTCPayNetwork network);
bool PaymentConfirmed(PaymentEntity entity, SpeedPolicy speedPolicy, BTCPayNetwork network); bool PaymentConfirmed(PaymentEntity entity, SpeedPolicy speedPolicy, BTCPayNetwork network);
@@ -737,6 +735,7 @@ namespace BTCPayServer.Services.Invoices
public class BitcoinLikePaymentData : CryptoPaymentData public class BitcoinLikePaymentData : CryptoPaymentData
{ {
public readonly static string OnchainBitcoinType = "BTCLike";
public BitcoinLikePaymentData() public BitcoinLikePaymentData()
{ {
@@ -770,6 +769,11 @@ namespace BTCPayServer.Services.Invoices
return new[] { Outpoint.Hash.ToString() }; return new[] { Outpoint.Hash.ToString() };
} }
public Money GetValue()
{
return Output.Value;
}
public bool PaymentCompleted(PaymentEntity entity, BTCPayNetwork network) public bool PaymentCompleted(PaymentEntity entity, BTCPayNetwork network)
{ {
return ConfirmationCount >= network.MaxTrackedConfirmation; return ConfirmationCount >= network.MaxTrackedConfirmation;