Rename TxFee to NetworkFee and save the Network Fee of each payment under PaymentEntity

This commit is contained in:
nicolas.dorier
2019-01-05 13:31:05 +09:00
parent 3c122bcf53
commit 0f175174f6
13 changed files with 65 additions and 51 deletions

View File

@@ -98,7 +98,7 @@ namespace BTCPayServer.Tests
Rate = 10513.44m, Rate = 10513.44m,
}.SetPaymentMethodDetails(new BTCPayServer.Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod() }.SetPaymentMethodDetails(new BTCPayServer.Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod()
{ {
TxFee = Money.Coins(0.00000100m), NetworkFee = Money.Coins(0.00000100m),
DepositAddress = dummy DepositAddress = dummy
})); }));
paymentMethods.Add(new PaymentMethod() paymentMethods.Add(new PaymentMethod()
@@ -107,7 +107,7 @@ namespace BTCPayServer.Tests
Rate = 216.79m Rate = 216.79m
}.SetPaymentMethodDetails(new BTCPayServer.Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod() }.SetPaymentMethodDetails(new BTCPayServer.Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod()
{ {
TxFee = Money.Coins(0.00010000m), NetworkFee = Money.Coins(0.00010000m),
DepositAddress = dummy DepositAddress = dummy
})); }));
invoiceEntity.SetPaymentMethods(paymentMethods); invoiceEntity.SetPaymentMethods(paymentMethods);
@@ -115,12 +115,12 @@ namespace BTCPayServer.Tests
var btc = invoiceEntity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null); var btc = invoiceEntity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null);
var accounting = btc.Calculate(); var accounting = btc.Calculate();
invoiceEntity.Payments.Add(new PaymentEntity() { Accounted = true, CryptoCode = "BTC" }.SetCryptoPaymentData(new BitcoinLikePaymentData() invoiceEntity.Payments.Add(new PaymentEntity() { Accounted = true, CryptoCode = "BTC", NetworkFee = 0.00000100m }.SetCryptoPaymentData(new BitcoinLikePaymentData()
{ {
Output = new TxOut() { Value = Money.Coins(0.00151263m) } Output = new TxOut() { Value = Money.Coins(0.00151263m) }
})); }));
accounting = btc.Calculate(); accounting = btc.Calculate();
invoiceEntity.Payments.Add(new PaymentEntity() { Accounted = true, CryptoCode = "BTC" }.SetCryptoPaymentData(new BitcoinLikePaymentData() invoiceEntity.Payments.Add(new PaymentEntity() { Accounted = true, CryptoCode = "BTC", NetworkFee = 0.00000100m }.SetCryptoPaymentData(new BitcoinLikePaymentData()
{ {
Output = new TxOut() { Value = accounting.Due } Output = new TxOut() { Value = accounting.Due }
})); }));
@@ -147,7 +147,7 @@ namespace BTCPayServer.Tests
var entity = new InvoiceEntity(); var entity = new InvoiceEntity();
#pragma warning disable CS0618 #pragma warning disable CS0618
entity.Payments = new System.Collections.Generic.List<PaymentEntity>(); entity.Payments = new System.Collections.Generic.List<PaymentEntity>();
entity.SetPaymentMethod(new PaymentMethod() { CryptoCode = "BTC", Rate = 5000, TxFee = Money.Coins(0.1m) }); entity.SetPaymentMethod(new PaymentMethod() { CryptoCode = "BTC", Rate = 5000, NetworkFee = Money.Coins(0.1m) });
entity.ProductInformation = new ProductInformation() { Price = 5000 }; entity.ProductInformation = new ProductInformation() { Price = 5000 };
var paymentMethod = entity.GetPaymentMethods(null).TryGet("BTC", PaymentTypes.BTCLike); var paymentMethod = entity.GetPaymentMethods(null).TryGet("BTC", PaymentTypes.BTCLike);
@@ -155,20 +155,20 @@ namespace BTCPayServer.Tests
Assert.Equal(Money.Coins(1.1m), accounting.Due); Assert.Equal(Money.Coins(1.1m), accounting.Due);
Assert.Equal(Money.Coins(1.1m), accounting.TotalDue); Assert.Equal(Money.Coins(1.1m), accounting.TotalDue);
entity.Payments.Add(new PaymentEntity() { Output = new TxOut(Money.Coins(0.5m), new Key()), Accounted = true }); entity.Payments.Add(new PaymentEntity() { Output = new TxOut(Money.Coins(0.5m), new Key()), Accounted = true, NetworkFee = 0.1m });
accounting = paymentMethod.Calculate(); accounting = paymentMethod.Calculate();
//Since we need to spend one more txout, it should be 1.1 - 0,5 + 0.1 //Since we need to spend one more txout, it should be 1.1 - 0,5 + 0.1
Assert.Equal(Money.Coins(0.7m), accounting.Due); Assert.Equal(Money.Coins(0.7m), accounting.Due);
Assert.Equal(Money.Coins(1.2m), accounting.TotalDue); Assert.Equal(Money.Coins(1.2m), accounting.TotalDue);
entity.Payments.Add(new PaymentEntity() { Output = new TxOut(Money.Coins(0.2m), new Key()), Accounted = true }); entity.Payments.Add(new PaymentEntity() { Output = new TxOut(Money.Coins(0.2m), new Key()), Accounted = true, NetworkFee = 0.1m });
accounting = paymentMethod.Calculate(); accounting = paymentMethod.Calculate();
Assert.Equal(Money.Coins(0.6m), accounting.Due); Assert.Equal(Money.Coins(0.6m), accounting.Due);
Assert.Equal(Money.Coins(1.3m), accounting.TotalDue); Assert.Equal(Money.Coins(1.3m), accounting.TotalDue);
entity.Payments.Add(new PaymentEntity() { Output = new TxOut(Money.Coins(0.6m), new Key()), Accounted = true }); entity.Payments.Add(new PaymentEntity() { Output = new TxOut(Money.Coins(0.6m), new Key()), Accounted = true, NetworkFee = 0.1m });
accounting = paymentMethod.Calculate(); accounting = paymentMethod.Calculate();
Assert.Equal(Money.Zero, accounting.Due); Assert.Equal(Money.Zero, accounting.Due);
@@ -187,13 +187,13 @@ namespace BTCPayServer.Tests
{ {
CryptoCode = "BTC", CryptoCode = "BTC",
Rate = 1000, Rate = 1000,
TxFee = Money.Coins(0.1m) NetworkFee = Money.Coins(0.1m)
}); });
paymentMethods.Add(new PaymentMethod() paymentMethods.Add(new PaymentMethod()
{ {
CryptoCode = "LTC", CryptoCode = "LTC",
Rate = 500, Rate = 500,
TxFee = Money.Coins(0.01m) NetworkFee = Money.Coins(0.01m)
}); });
entity.SetPaymentMethods(paymentMethods); entity.SetPaymentMethods(paymentMethods);
entity.Payments = new List<PaymentEntity>(); entity.Payments = new List<PaymentEntity>();
@@ -205,7 +205,7 @@ namespace BTCPayServer.Tests
accounting = paymentMethod.Calculate(); accounting = paymentMethod.Calculate();
Assert.Equal(Money.Coins(10.01m), accounting.TotalDue); Assert.Equal(Money.Coins(10.01m), accounting.TotalDue);
entity.Payments.Add(new PaymentEntity() { CryptoCode = "BTC", Output = new TxOut(Money.Coins(1.0m), new Key()), Accounted = true }); entity.Payments.Add(new PaymentEntity() { CryptoCode = "BTC", Output = new TxOut(Money.Coins(1.0m), new Key()), Accounted = true, NetworkFee = 0.1m });
paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null); paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null);
accounting = paymentMethod.Calculate(); accounting = paymentMethod.Calculate();
@@ -222,7 +222,7 @@ namespace BTCPayServer.Tests
Assert.Equal(Money.Coins(2.0m), accounting.Paid); Assert.Equal(Money.Coins(2.0m), accounting.Paid);
Assert.Equal(Money.Coins(10.01m + 0.1m * 2), accounting.TotalDue); Assert.Equal(Money.Coins(10.01m + 0.1m * 2), accounting.TotalDue);
entity.Payments.Add(new PaymentEntity() { CryptoCode = "LTC", Output = new TxOut(Money.Coins(1.0m), new Key()), Accounted = true }); entity.Payments.Add(new PaymentEntity() { CryptoCode = "LTC", Output = new TxOut(Money.Coins(1.0m), new Key()), Accounted = true, NetworkFee = 0.01m });
paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null); paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null);
@@ -242,7 +242,7 @@ namespace BTCPayServer.Tests
Assert.Equal(2, accounting.TxRequired); Assert.Equal(2, accounting.TxRequired);
var remaining = Money.Coins(4.2m - 0.5m + 0.01m / 2); var remaining = Money.Coins(4.2m - 0.5m + 0.01m / 2);
entity.Payments.Add(new PaymentEntity() { CryptoCode = "BTC", Output = new TxOut(remaining, new Key()), Accounted = true }); entity.Payments.Add(new PaymentEntity() { CryptoCode = "BTC", Output = new TxOut(remaining, new Key()), Accounted = true, NetworkFee = 0.1m });
paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null); paymentMethod = entity.GetPaymentMethod(new PaymentMethodId("BTC", PaymentTypes.BTCLike), null);
accounting = paymentMethod.Calculate(); accounting = paymentMethod.Calculate();
@@ -272,7 +272,7 @@ namespace BTCPayServer.Tests
var entity = new InvoiceEntity(); var entity = new InvoiceEntity();
#pragma warning disable CS0618 #pragma warning disable CS0618
entity.Payments = new List<PaymentEntity>(); entity.Payments = new List<PaymentEntity>();
entity.SetPaymentMethod(new PaymentMethod() { CryptoCode = "BTC", Rate = 5000, TxFee = Money.Coins(0.1m) }); entity.SetPaymentMethod(new PaymentMethod() { CryptoCode = "BTC", Rate = 5000, NetworkFee = Money.Coins(0.1m) });
entity.ProductInformation = new ProductInformation() { Price = 5000 }; entity.ProductInformation = new ProductInformation() { Price = 5000 };
entity.PaymentTolerance = 0; entity.PaymentTolerance = 0;

View File

@@ -304,7 +304,7 @@ namespace BTCPayServer.Controllers
#pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete
Status = invoice.StatusString, Status = invoice.StatusString,
#pragma warning restore CS0618 // Type or member is obsolete #pragma warning restore CS0618 // Type or member is obsolete
NetworkFee = paymentMethodDetails.GetTxFee(), NetworkFee = paymentMethodDetails.GetNetworkFee(),
IsMultiCurrency = invoice.GetPayments().Select(p => p.GetPaymentMethodId()).Concat(new[] { paymentMethod.GetId() }).Distinct().Count() > 1, IsMultiCurrency = invoice.GetPayments().Select(p => p.GetPaymentMethodId()).Concat(new[] { paymentMethod.GetId() }).Distinct().Count() > 1,
ChangellyEnabled = changelly != null, ChangellyEnabled = changelly != null,
ChangellyMerchantId = changelly?.ChangellyMerchantId, ChangellyMerchantId = changelly?.ChangellyMerchantId,

View File

@@ -201,7 +201,7 @@ namespace BTCPayServer.Controllers
paymentMethod.Rate = rate.BidAsk.Bid; paymentMethod.Rate = rate.BidAsk.Bid;
var paymentDetails = await handler.CreatePaymentMethodDetails(supportedPaymentMethod, paymentMethod, store, network, preparePayment); var paymentDetails = await handler.CreatePaymentMethodDetails(supportedPaymentMethod, paymentMethod, store, network, preparePayment);
if (storeBlob.NetworkFeeDisabled) if (storeBlob.NetworkFeeDisabled)
paymentDetails.SetNoTxFee(); paymentDetails.SetNoNetworkFee();
paymentMethod.SetPaymentMethodDetails(paymentDetails); paymentMethod.SetPaymentMethodDetails(paymentDetails);
Func<Money, Money, bool> compare = null; Func<Money, Money, bool> compare = null;
@@ -241,7 +241,7 @@ namespace BTCPayServer.Controllers
#pragma warning disable CS0618 #pragma warning disable CS0618
if (paymentMethod.GetId().IsBTCOnChain) if (paymentMethod.GetId().IsBTCOnChain)
{ {
entity.TxFee = paymentMethod.TxFee; entity.TxFee = paymentMethod.NetworkFee;
entity.Rate = paymentMethod.Rate; entity.Rate = paymentMethod.Rate;
entity.DepositAddress = paymentMethod.DepositAddress; entity.DepositAddress = paymentMethod.DepositAddress;
} }

View File

@@ -20,14 +20,14 @@ namespace BTCPayServer.Payments.Bitcoin
return DepositAddress; return DepositAddress;
} }
public decimal GetTxFee() public decimal GetNetworkFee()
{ {
return TxFee.ToDecimal(MoneyUnit.BTC); return NetworkFee.ToDecimal(MoneyUnit.BTC);
} }
public void SetNoTxFee() public void SetNoNetworkFee()
{ {
TxFee = Money.Zero; NetworkFee = Money.Zero;
} }
@@ -40,7 +40,7 @@ namespace BTCPayServer.Payments.Bitcoin
[JsonIgnore] [JsonIgnore]
public FeeRate FeeRate { get; set; } public FeeRate FeeRate { get; set; }
[JsonIgnore] [JsonIgnore]
public Money TxFee { get; set; } public Money NetworkFee { get; set; }
[JsonIgnore] [JsonIgnore]
public String DepositAddress { get; set; } public String DepositAddress { get; set; }
public BitcoinAddress GetDepositAddress(Network network) public BitcoinAddress GetDepositAddress(Network network)

View File

@@ -32,6 +32,7 @@ namespace BTCPayServer.Payments.Bitcoin
public TxOut Output { get; set; } public TxOut Output { get; set; }
public int ConfirmationCount { get; set; } public int ConfirmationCount { get; set; }
public bool RBF { get; set; } public bool RBF { get; set; }
public decimal NetworkFee { get; set; }
/// <summary> /// <summary>
/// This is set to true if the payment was created before CryptoPaymentData existed in BTCPayServer /// This is set to true if the payment was created before CryptoPaymentData existed in BTCPayServer

View File

@@ -49,7 +49,7 @@ namespace BTCPayServer.Payments.Bitcoin
var prepare = (Prepare)preparePaymentObject; var prepare = (Prepare)preparePaymentObject;
Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod onchainMethod = new Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod(); Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod onchainMethod = new Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod();
onchainMethod.FeeRate = await prepare.GetFeeRate; onchainMethod.FeeRate = await prepare.GetFeeRate;
onchainMethod.TxFee = onchainMethod.FeeRate.GetFee(100); // assume price for 100 bytes onchainMethod.NetworkFee = onchainMethod.FeeRate.GetFee(100); // assume price for 100 bytes
onchainMethod.DepositAddress = (await prepare.ReserveAddress).ToString(); onchainMethod.DepositAddress = (await prepare.ReserveAddress).ToString();
return onchainMethod; return onchainMethod;
} }

View File

@@ -21,8 +21,8 @@ namespace BTCPayServer.Payments
/// Returns what a merchant would need to pay to cashout this payment /// Returns what a merchant would need to pay to cashout this payment
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
decimal GetTxFee(); decimal GetNetworkFee();
void SetNoTxFee(); void SetNoNetworkFee();
/// <summary> /// <summary>
/// Change the payment destination (internal plumbing) /// Change the payment destination (internal plumbing)
/// </summary> /// </summary>

View File

@@ -22,6 +22,8 @@ namespace BTCPayServer.Payments.Lightning
return GetPaymentId(); return GetPaymentId();
} }
public decimal NetworkFee { get; set; }
public string GetPaymentId() public string GetPaymentId()
{ {
return BOLT11; return BOLT11;

View File

@@ -21,12 +21,12 @@ namespace BTCPayServer.Payments.Lightning
return PaymentTypes.LightningLike; return PaymentTypes.LightningLike;
} }
public decimal GetTxFee() public decimal GetNetworkFee()
{ {
return 0.0m; return 0.0m;
} }
public void SetNoTxFee() public void SetNoNetworkFee()
{ {
} }

View File

@@ -65,8 +65,7 @@ namespace BTCPayServer.Services.Invoices.Export
var pdata = payment.GetCryptoPaymentData(); var pdata = payment.GetCryptoPaymentData();
var pmethod = invoice.GetPaymentMethod(payment.GetPaymentMethodId(), Networks); var pmethod = invoice.GetPaymentMethod(payment.GetPaymentMethodId(), Networks);
var paymentFee = pmethod.GetPaymentMethodDetails().GetTxFee(); var paidAfterNetworkFees = pdata.GetValue() - payment.NetworkFee;
var paidAfterNetworkFees = pdata.GetValue() - paymentFee;
invoiceDue -= paidAfterNetworkFees * pmethod.Rate; invoiceDue -= paidAfterNetworkFees * pmethod.Rate;
var target = new ExportInvoiceHolder var target = new ExportInvoiceHolder
@@ -83,7 +82,7 @@ namespace BTCPayServer.Services.Invoices.Export
// so if fee is 10000 satoshis, customer can essentially send infinite number of tx // so if fee is 10000 satoshis, customer can essentially send infinite number of tx
// and merchant effectivelly would receive 0 BTC, invoice won't be paid // and merchant effectivelly would receive 0 BTC, invoice won't be paid
// while looking just at export you could sum Paid and assume merchant "received payments" // while looking just at export you could sum Paid and assume merchant "received payments"
NetworkFee = paymentFee.ToString(CultureInfo.InvariantCulture), NetworkFee = payment.NetworkFee.ToString(CultureInfo.InvariantCulture),
InvoiceDue = invoiceDue, InvoiceDue = invoiceDue,
OrderId = invoice.OrderId, OrderId = invoice.OrderId,
StoreId = invoice.StoreId, StoreId = invoice.StoreId,

View File

@@ -485,7 +485,7 @@ namespace BTCPayServer.Services.Invoices
public PaymentMethodDictionary GetPaymentMethods(BTCPayNetworkProvider networkProvider) public PaymentMethodDictionary GetPaymentMethods(BTCPayNetworkProvider networkProvider)
{ {
PaymentMethodDictionary rates = new PaymentMethodDictionary(networkProvider); PaymentMethodDictionary paymentMethods = new PaymentMethodDictionary();
var serializer = new Serializer(Dummy); var serializer = new Serializer(Dummy);
#pragma warning disable CS0618 #pragma warning disable CS0618
if (PaymentMethod != null) if (PaymentMethod != null)
@@ -499,11 +499,11 @@ namespace BTCPayServer.Services.Invoices
r.ParentEntity = this; r.ParentEntity = this;
r.Network = networkProvider?.GetNetwork(r.CryptoCode); r.Network = networkProvider?.GetNetwork(r.CryptoCode);
if (r.Network != null || networkProvider == null) if (r.Network != null || networkProvider == null)
rates.Add(r); paymentMethods.Add(r);
} }
} }
#pragma warning restore CS0618 #pragma warning restore CS0618
return rates; return paymentMethods;
} }
Network Dummy = Network.Main; Network Dummy = Network.Main;
@@ -517,8 +517,6 @@ namespace BTCPayServer.Services.Invoices
public void SetPaymentMethods(PaymentMethodDictionary paymentMethods) public void SetPaymentMethods(PaymentMethodDictionary paymentMethods)
{ {
if (paymentMethods.NetworkProvider != null)
throw new InvalidOperationException($"{nameof(paymentMethods)} should have NetworkProvider to null");
var obj = new JObject(); var obj = new JObject();
var serializer = new Serializer(Dummy); var serializer = new Serializer(Dummy);
#pragma warning disable CS0618 #pragma warning disable CS0618
@@ -731,7 +729,7 @@ namespace BTCPayServer.Services.Invoices
{ {
FeeRate = FeeRate, FeeRate = FeeRate,
DepositAddress = string.IsNullOrEmpty(DepositAddress) ? null : DepositAddress, DepositAddress = string.IsNullOrEmpty(DepositAddress) ? null : DepositAddress,
TxFee = TxFee NetworkFee = NetworkFee
}; };
} }
else else
@@ -739,7 +737,7 @@ namespace BTCPayServer.Services.Invoices
var details = PaymentMethodExtensions.DeserializePaymentMethodDetails(GetId(), PaymentMethodDetails); var details = PaymentMethodExtensions.DeserializePaymentMethodDetails(GetId(), PaymentMethodDetails);
if (details is Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod btcLike) if (details is Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod btcLike)
{ {
btcLike.TxFee = TxFee; btcLike.NetworkFee = NetworkFee;
btcLike.DepositAddress = string.IsNullOrEmpty(DepositAddress) ? null : DepositAddress; btcLike.DepositAddress = string.IsNullOrEmpty(DepositAddress) ? null : DepositAddress;
btcLike.FeeRate = FeeRate; btcLike.FeeRate = FeeRate;
} }
@@ -761,7 +759,7 @@ namespace BTCPayServer.Services.Invoices
if (paymentMethod is Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod bitcoinPaymentMethod) if (paymentMethod is Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod bitcoinPaymentMethod)
{ {
TxFee = bitcoinPaymentMethod.TxFee; NetworkFee = bitcoinPaymentMethod.NetworkFee;
FeeRate = bitcoinPaymentMethod.FeeRate; FeeRate = bitcoinPaymentMethod.FeeRate;
DepositAddress = bitcoinPaymentMethod.DepositAddress; DepositAddress = bitcoinPaymentMethod.DepositAddress;
} }
@@ -777,7 +775,7 @@ namespace BTCPayServer.Services.Invoices
public FeeRate FeeRate { get; set; } public FeeRate FeeRate { get; set; }
[JsonProperty(PropertyName = "txFee")] [JsonProperty(PropertyName = "txFee")]
[Obsolete("Use ((BitcoinLikeOnChainPaymentMethod)GetPaymentMethod()).TxFee")] [Obsolete("Use ((BitcoinLikeOnChainPaymentMethod)GetPaymentMethod()).TxFee")]
public Money TxFee { get; set; } public Money NetworkFee { get; set; }
[JsonProperty(PropertyName = "depositAddress")] [JsonProperty(PropertyName = "depositAddress")]
[Obsolete("Use ((BitcoinLikeOnChainPaymentMethod)GetPaymentMethod()).DepositAddress")] [Obsolete("Use ((BitcoinLikeOnChainPaymentMethod)GetPaymentMethod()).DepositAddress")]
public string DepositAddress { get; set; } public string DepositAddress { get; set; }
@@ -801,7 +799,7 @@ namespace BTCPayServer.Services.Invoices
.OrderBy(p => p.ReceivedTime) .OrderBy(p => p.ReceivedTime)
.Select(_ => .Select(_ =>
{ {
var txFee = _.GetValue(paymentMethods, GetId(), paymentMethods[_.GetPaymentMethodId()].GetTxFee()); var txFee = _.GetValue(paymentMethods, GetId(), _.NetworkFee);
paid += _.GetValue(paymentMethods, GetId()); paid += _.GetValue(paymentMethods, GetId());
if (!paidEnough) if (!paidEnough)
{ {
@@ -842,17 +840,18 @@ namespace BTCPayServer.Services.Invoices
var method = GetPaymentMethodDetails(); var method = GetPaymentMethodDetails();
if (method == null) if (method == null)
return 0.0m; return 0.0m;
return method.GetTxFee(); return method.GetNetworkFee();
} }
} }
public class PaymentEntity public class PaymentEntity
{ {
public int Version { get; set; }
public DateTimeOffset ReceivedTime public DateTimeOffset ReceivedTime
{ {
get; set; get; set;
} }
public decimal NetworkFee { get; set; }
[Obsolete("Use ((BitcoinLikePaymentData)GetCryptoPaymentData()).Outpoint")] [Obsolete("Use ((BitcoinLikePaymentData)GetCryptoPaymentData()).Outpoint")]
public OutPoint Outpoint public OutPoint Outpoint
{ {
@@ -889,7 +888,7 @@ namespace BTCPayServer.Services.Invoices
#pragma warning disable CS0618 #pragma warning disable CS0618
if (string.IsNullOrEmpty(CryptoPaymentDataType)) if (string.IsNullOrEmpty(CryptoPaymentDataType))
{ {
// In case this is a payment done before this update, consider it unconfirmed with RBF for safety // For invoices created when CryptoPaymentDataType was not existing, we just consider that it is a RBFed payment for safety
var paymentData = new Payments.Bitcoin.BitcoinLikePaymentData(); var paymentData = new Payments.Bitcoin.BitcoinLikePaymentData();
paymentData.Outpoint = Outpoint; paymentData.Outpoint = Outpoint;
paymentData.Output = Output; paymentData.Output = Output;

View File

@@ -379,11 +379,24 @@ retry:
private InvoiceEntity ToEntity(Data.InvoiceData invoice) private InvoiceEntity ToEntity(Data.InvoiceData invoice)
{ {
var entity = ToObject<InvoiceEntity>(invoice.Blob, null); var entity = ToObject<InvoiceEntity>(invoice.Blob, null);
PaymentMethodDictionary paymentMethods = null;
#pragma warning disable CS0618 #pragma warning disable CS0618
entity.Payments = invoice.Payments.Select(p => entity.Payments = invoice.Payments.Select(p =>
{ {
var paymentEntity = ToObject<PaymentEntity>(p.Blob, null); var paymentEntity = ToObject<PaymentEntity>(p.Blob, null);
paymentEntity.Accounted = p.Accounted; paymentEntity.Accounted = p.Accounted;
// PaymentEntity on version 0 does not have their own fee, because it was assumed that the payment method have fixed fee.
// We want to hide this legacy detail in InvoiceRepository, so we fetch the fee from the PaymentMethod and assign it to the PaymentEntity.
if (paymentEntity.Version == 0)
{
if (paymentMethods == null)
paymentMethods = entity.GetPaymentMethods(null);
var paymentMethodDetails = paymentMethods.TryGet(paymentEntity.GetPaymentMethodId())?.GetPaymentMethodDetails();
if (paymentMethodDetails != null) // == null should never happen, but we never know.
paymentEntity.NetworkFee = paymentMethodDetails.GetNetworkFee();
}
return paymentEntity; return paymentEntity;
}) })
.OrderBy(a => a.ReceivedTime).ToList(); .OrderBy(a => a.ReceivedTime).ToList();
@@ -551,13 +564,20 @@ retry:
{ {
using (var context = _ContextFactory.CreateContext()) using (var context = _ContextFactory.CreateContext())
{ {
var invoice = context.Invoices.Find(invoiceId);
if (invoice == null)
return null;
PaymentEntity entity = new PaymentEntity PaymentEntity entity = new PaymentEntity
{ {
Version = 1,
#pragma warning disable CS0618 #pragma warning disable CS0618
CryptoCode = cryptoCode, CryptoCode = cryptoCode,
#pragma warning restore CS0618 #pragma warning restore CS0618
ReceivedTime = date.UtcDateTime, ReceivedTime = date.UtcDateTime,
Accounted = accounted Accounted = accounted,
NetworkFee = ToObject<InvoiceEntity>(invoice.Blob, null)
.GetPaymentMethod(new PaymentMethodId(cryptoCode, paymentData.GetPaymentType()), null)
.GetPaymentMethodDetails().GetNetworkFee()
}; };
entity.SetCryptoPaymentData(paymentData); entity.SetCryptoPaymentData(paymentData);

View File

@@ -15,13 +15,6 @@ namespace BTCPayServer.Services.Invoices
} }
public PaymentMethodDictionary(BTCPayNetworkProvider networkProvider)
{
NetworkProvider = networkProvider;
}
public BTCPayNetworkProvider NetworkProvider { get; set; }
public PaymentMethod this[PaymentMethodId index] public PaymentMethod this[PaymentMethodId index]
{ {
get get