Add currency precision based on network (#1294)

This commit is contained in:
Andrew Camilleri 2020-01-21 14:28:13 +01:00 committed by Nicolas Dorier
parent f324185d82
commit 54c7c0d696
7 changed files with 18 additions and 18 deletions

View file

@ -6,16 +6,16 @@ using NBXplorer.Models;
namespace BTCPayServer namespace BTCPayServer
{ {
public class ElementsBTCPayNetwork:BTCPayNetwork public class ElementsBTCPayNetwork : BTCPayNetwork
{ {
public string NetworkCryptoCode { get; set; } public string NetworkCryptoCode { get; set; }
public uint256 AssetId { get; set; } public uint256 AssetId { get; set; }
public override bool ReadonlyWallet { get; set; } = true; public override bool ReadonlyWallet { get; set; } = true;
public int Divisibility { get; set; } = 8;
public override IEnumerable<(MatchedOutput matchedOutput, OutPoint outPoint)> GetValidOutputs(NewTransactionEvent evtOutputs) public override IEnumerable<(MatchedOutput matchedOutput, OutPoint outPoint)> GetValidOutputs(
NewTransactionEvent evtOutputs)
{ {
return evtOutputs.Outputs.Where(output => return evtOutputs.Outputs.Where(output =>
output.Value is AssetMoney assetMoney && assetMoney.AssetId == AssetId).Select(output => output.Value is AssetMoney assetMoney && assetMoney.AssetId == AssetId).Select(output =>
{ {
var outpoint = new OutPoint(evtOutputs.TransactionData.TransactionHash, output.Index); var outpoint = new OutPoint(evtOutputs.TransactionData.TransactionHash, output.Index);
@ -23,7 +23,7 @@ namespace BTCPayServer
}); });
} }
public override string GenerateBIP21(string cryptoInfoAddress, string cryptoInfoDue) public override string GenerateBIP21(string cryptoInfoAddress, Money cryptoInfoDue)
{ {
return $"{base.GenerateBIP21(cryptoInfoAddress, cryptoInfoDue)}&assetid={AssetId}"; return $"{base.GenerateBIP21(cryptoInfoAddress, cryptoInfoDue)}&assetid={AssetId}";
} }

View file

@ -10,6 +10,7 @@ namespace BTCPayServer
{ {
CryptoCode = "XMR", CryptoCode = "XMR",
DisplayName = "Monero", DisplayName = "Monero",
Divisibility = 12,
BlockExplorerLink = BlockExplorerLink =
NetworkType == NetworkType.Mainnet NetworkType == NetworkType.Mainnet
? "https://www.exploremonero.com/transaction/{0}" ? "https://www.exploremonero.com/transaction/{0}"

View file

@ -113,9 +113,9 @@ namespace BTCPayServer
}); });
} }
public virtual string GenerateBIP21(string cryptoInfoAddress, string cryptoInfoDue) public virtual string GenerateBIP21(string cryptoInfoAddress, Money cryptoInfoDue)
{ {
return $"{UriScheme}:{cryptoInfoAddress}?amount={cryptoInfoDue}"; return $"{UriScheme}:{cryptoInfoAddress}?amount={cryptoInfoDue.ToString(false, true)}";
} }
} }
@ -124,7 +124,7 @@ namespace BTCPayServer
public string CryptoCode { get; internal set; } public string CryptoCode { get; internal set; }
public string BlockExplorerLink { get; internal set; } public string BlockExplorerLink { get; internal set; }
public string DisplayName { get; set; } public string DisplayName { get; set; }
public int Divisibility { get; set; } = 8;
[Obsolete("Should not be needed")] [Obsolete("Should not be needed")]
public bool IsBTC public bool IsBTC
{ {

View file

@ -47,7 +47,7 @@ namespace BTCPayServer.Payments.Lightning
var storeBlob = store.GetStoreBlob(); var storeBlob = store.GetStoreBlob();
var test = GetNodeInfo(paymentMethod.PreferOnion, supportedPaymentMethod, (BTCPayNetwork)network); var test = GetNodeInfo(paymentMethod.PreferOnion, supportedPaymentMethod, (BTCPayNetwork)network);
var invoice = paymentMethod.ParentEntity; var invoice = paymentMethod.ParentEntity;
var due = Extensions.RoundUp(invoice.ProductInformation.Price / paymentMethod.Rate, 8); var due = Extensions.RoundUp(invoice.ProductInformation.Price / paymentMethod.Rate, network.Divisibility);
var client = _lightningClientFactory.Create(supportedPaymentMethod.GetLightningUrl(), (BTCPayNetwork)network); var client = _lightningClientFactory.Create(supportedPaymentMethod.GetLightningUrl(), (BTCPayNetwork)network);
var expiry = invoice.ExpirationTime - DateTimeOffset.UtcNow; var expiry = invoice.ExpirationTime - DateTimeOffset.UtcNow;
if (expiry < TimeSpan.Zero) if (expiry < TimeSpan.Zero)

View file

@ -64,7 +64,6 @@ namespace BTCPayServer.Payments
public abstract string SerializePaymentData(BTCPayNetworkBase network, CryptoPaymentData paymentData); public abstract string SerializePaymentData(BTCPayNetworkBase network, CryptoPaymentData paymentData);
public abstract IPaymentMethodDetails DeserializePaymentMethodDetails(string str); public abstract IPaymentMethodDetails DeserializePaymentMethodDetails(string str);
public abstract ISupportedPaymentMethod DeserializeSupportedPaymentMethod(BTCPayNetworkBase network, JToken value); public abstract ISupportedPaymentMethod DeserializeSupportedPaymentMethod(BTCPayNetworkBase network, JToken value);
public abstract string GetTransactionLink(BTCPayNetworkBase network, string txId); public abstract string GetTransactionLink(BTCPayNetworkBase network, string txId);
public abstract string InvoiceViewPaymentPartialName { get; } public abstract string InvoiceViewPaymentPartialName { get; }
} }

View file

@ -847,7 +847,7 @@ namespace BTCPayServer.Services.Invoices
var paid = 0m; var paid = 0m;
var cryptoPaid = 0.0m; var cryptoPaid = 0.0m;
int precision = 8; int precision = Network?.Divisibility ?? 8;
var totalDueNoNetworkCost = Money.Coins(Extensions.RoundUp(totalDue, precision)); var totalDueNoNetworkCost = Money.Coins(Extensions.RoundUp(totalDue, precision));
bool paidEnough = paid >= Extensions.RoundUp(totalDue, precision); bool paidEnough = paid >= Extensions.RoundUp(totalDue, precision);
int txRequired = 0; int txRequired = 0;
@ -857,8 +857,8 @@ namespace BTCPayServer.Services.Invoices
.OrderBy(p => p.ReceivedTime) .OrderBy(p => p.ReceivedTime)
.Select(_ => .Select(_ =>
{ {
var txFee = _.GetValue(paymentMethods, GetId(), _.NetworkFee); var txFee = _.GetValue(paymentMethods, GetId(), _.NetworkFee, precision);
paid += _.GetValue(paymentMethods, GetId()); paid += _.GetValue(paymentMethods, GetId(), null, precision);
if (!paidEnough) if (!paidEnough)
{ {
totalDue += txFee; totalDue += txFee;
@ -991,18 +991,18 @@ namespace BTCPayServer.Services.Invoices
#pragma warning restore CS0618 #pragma warning restore CS0618
return this; return this;
} }
internal decimal GetValue(PaymentMethodDictionary paymentMethods, PaymentMethodId paymentMethodId, decimal? value = null) internal decimal GetValue(PaymentMethodDictionary paymentMethods, PaymentMethodId paymentMethodId, decimal? value, int precision)
{ {
value = value ?? this.GetCryptoPaymentData().GetValue(); value = value ?? this.GetCryptoPaymentData().GetValue();
var to = paymentMethodId; var to = paymentMethodId;
var from = this.GetPaymentMethodId(); var from = this.GetPaymentMethodId();
if (to == from) if (to == from)
return decimal.Round(value.Value, 8); return decimal.Round(value.Value, precision);
var fromRate = paymentMethods[from].Rate; var fromRate = paymentMethods[from].Rate;
var toRate = paymentMethods[to].Rate; var toRate = paymentMethods[to].Rate;
var fiatValue = fromRate * decimal.Round(value.Value, 8); var fiatValue = fromRate * decimal.Round(value.Value, precision);
var otherCurrencyValue = toRate == 0 ? 0.0m : fiatValue / toRate; var otherCurrencyValue = toRate == 0 ? 0.0m : fiatValue / toRate;
return otherCurrencyValue; return otherCurrencyValue;
} }

View file

@ -96,7 +96,7 @@ namespace BTCPayServer.Services.Rates
foreach (var network in new BTCPayNetworkProvider(NetworkType.Mainnet).GetAll()) foreach (var network in new BTCPayNetworkProvider(NetworkType.Mainnet).GetAll())
{ {
AddCurrency(_CurrencyProviders, network.CryptoCode, 8, network.CryptoCode); AddCurrency(_CurrencyProviders, network.CryptoCode, network.Divisibility, network.CryptoCode);
} }
} }
return _CurrencyProviders.TryGet(currency.ToUpperInvariant()); return _CurrencyProviders.TryGet(currency.ToUpperInvariant());
@ -180,7 +180,7 @@ namespace BTCPayServer.Services.Rates
if (!dico.TryAdd(network.CryptoCode, new CurrencyData() if (!dico.TryAdd(network.CryptoCode, new CurrencyData()
{ {
Code = network.CryptoCode, Code = network.CryptoCode,
Divisibility = 8, Divisibility = network.Divisibility,
Name = network.CryptoCode, Name = network.CryptoCode,
Crypto = true Crypto = true
})) }))