Browse Source

homepage header tweaks to exchange rates:

- show exchange rate for selected currency (if non-USD "exchanged" currency)
- show sat/$ or sat/eur value in addition to btc/usd value
master
Dan Janosik 5 years ago
parent
commit
a08f4a6548
No known key found for this signature in database GPG Key ID: C6F8CE9FFDB2CED2
  1. 2
      CHANGELOG.md
  2. 1
      app.js
  3. 62
      app/utils.js
  4. 4
      views/index.pug

2
CHANGELOG.md

@ -2,6 +2,8 @@
##### 2019-12-22
* Fix startup issues when connecting to a node that's not ready to serve data (e.g. verifying blocks)
* Homepage header: show exchange rate in selected currency (rather than hardcoded USD)
* Homepage header: show sat/USD or sat/EUR
#### v1.1.4
###### 2019-12-04

1
app.js

@ -404,6 +404,7 @@ app.use(function(req, res, next) {
}
res.locals.currencyFormatType = req.session.currencyFormatType;
global.currencyFormatType = req.session.currencyFormatType;
if (!["/", "/connect"].includes(req.originalUrl)) {

62
app/utils.js

@ -198,13 +198,71 @@ function addThousandsSeparators(x) {
return parts.join(".");
}
function formatValueInActiveCurrency(amount) {
if (global.currencyFormatType && global.exchangeRates[global.currencyFormatType.toLowerCase()]) {
return formatExchangedCurrency(amount, global.currencyFormatType);
} else {
return formatExchangedCurrency(amount, "usd");
}
}
function satoshisPerUnitOfActiveCurrency() {
if (global.currencyFormatType != null && global.exchangeRates != null) {
var exchangeType = global.currencyFormatType.toLowerCase();
if (!global.exchangeRates[global.currencyFormatType.toLowerCase()]) {
// if current display currency is a native unit, default to USD for exchange values
exchangeType = "usd";
}
var dec = new Decimal(1);
var one = new Decimal(1);
dec = dec.times(global.exchangeRates[exchangeType]);
// USD/BTC -> BTC/USD
dec = one.dividedBy(dec);
var unitName = coins[config.coin].baseCurrencyUnit.name;
var formatInfo = getCurrencyFormatInfo(unitName);
// BTC/USD -> sat/USD
dec = dec.times(formatInfo.multiplier);
var exchangedAmt = parseInt(dec);
if (exchangeType == "eur") {
return addThousandsSeparators(exchangedAmt) + ` ${unitName}/€`;
} else {
return addThousandsSeparators(exchangedAmt) + ` ${unitName}/$`;
}
}
return null;
if (global.currencyFormatType) {
return formatExchangedCurrency(amount, global.currencyFormatType);
} else {
return formatExchangedCurrency(amount, "usd");
}
}
function formatExchangedCurrency(amount, exchangeType) {
if (global.exchangeRates != null && global.exchangeRates[exchangeType.toLowerCase()] != null) {
var dec = new Decimal(amount);
dec = dec.times(global.exchangeRates[exchangeType.toLowerCase()]);
var exchangedAmt = parseFloat(Math.round(dec * 100) / 100).toFixed(2);
return "$" + addThousandsSeparators(exchangedAmt);
if (exchangeType == "eur") {
return "€" + addThousandsSeparators(exchangedAmt);
} else {
return "$" + addThousandsSeparators(exchangedAmt);
}
}
return "";
@ -566,6 +624,8 @@ module.exports = {
formatCurrencyAmount: formatCurrencyAmount,
formatCurrencyAmountWithForcedDecimalPlaces: formatCurrencyAmountWithForcedDecimalPlaces,
formatExchangedCurrency: formatExchangedCurrency,
formatValueInActiveCurrency: formatValueInActiveCurrency,
satoshisPerUnitOfActiveCurrency: satoshisPerUnitOfActiveCurrency,
addThousandsSeparators: addThousandsSeparators,
formatCurrencyAmountInSmallestUnits: formatCurrencyAmountInSmallestUnits,
seededRandom: seededRandom,

4
views/index.pug

@ -159,7 +159,9 @@ block content
span.font-weight-bold.border-dotted(data-toggle="tooltip", title=("Exchange-rate data from: " + coinConfig.exchangeRateData.jsonUrl)) Exchange Rate
if (global.exchangeRates)
p(class="lead") #{utils.formatExchangedCurrency(1.0, "usd")}
p(class="lead") #{utils.formatValueInActiveCurrency(1.0)}
small.text-muted.font-weight-light (#{utils.satoshisPerUnitOfActiveCurrency()})
else
p(class="lead") -

Loading…
Cancel
Save