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
.GetPayments()
.Where(p => p.GetCryptoPaymentData() is BitcoinLikePaymentData)
.Where(p => p.GetCryptoPaymentDataType() == BitcoinLikePaymentData.OnchainBitcoinType)
.Select(async payment =>
{
var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData();
var m = new InvoiceDetailsModel.Payment();
var paymentNetwork = _NetworkProvider.GetNetwork(payment.GetCryptoCode());
m.CryptoCode = payment.GetCryptoCode();
m.DepositAddress = payment.GetScriptPubKey().GetDestinationAddress(paymentNetwork.NBitcoinNetwork);
m.DepositAddress = paymentData.Output.ScriptPubKey.GetDestinationAddress(paymentNetwork.NBitcoinNetwork);
int confirmationCount = 0;
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.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);
Logs.PayServer.LogInformation($"{network.CryptoCode}: {paymentCount} payments happened while offline");
@ -209,8 +209,8 @@ namespace BTCPayServer.HostedServices
IEnumerable<BitcoinLikePaymentData> GetAllBitcoinPaymentData(InvoiceEntity invoice)
{
return invoice.GetPayments()
.Select(p => p.GetCryptoPaymentData() as BitcoinLikePaymentData)
.Where(p => p != null);
.Where(p => p.GetCryptoPaymentDataType() == BitcoinLikePaymentData.OnchainBitcoinType)
.Select(p => (BitcoinLikePaymentData)p.GetCryptoPaymentData());
}
async Task<InvoiceEntity> UpdatePaymentStates(BTCPayWallet wallet, string invoiceId)
@ -223,9 +223,9 @@ namespace BTCPayServer.HostedServices
var conflicts = GetConflicts(transactions.Select(t => t.Value));
foreach (var payment in invoice.GetPayments(wallet.Network))
{
var paymentData = payment.GetCryptoPaymentData() as BitcoinLikePaymentData;
if (paymentData == null)
if (payment.GetCryptoPaymentDataType() != BitcoinLikePaymentData.OnchainBitcoinType)
continue;
var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData();
if (!transactions.TryGetValue(paymentData.Outpoint.Hash, out TransactionResult tx))
continue;
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)
{
var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData();
var invoice = (await UpdatePaymentStates(wallet, invoiceId));
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);
await _InvoiceRepository.NewAddress(invoiceId, address, wallet.Network);

View file

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