Skip to content
Permalink
Browse files

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
  • Loading branch information
janoside committed Dec 23, 2019
1 parent d6a9e4c commit a08f4a65486cb361fa2791b20331dd9ab906c592
Showing with 67 additions and 2 deletions.
  1. +2 −0 CHANGELOG.md
  2. +1 −0 app.js
  3. +61 −1 app/utils.js
  4. +3 −1 views/index.pug
@@ -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)) {
@@ -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,
@@ -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") -

0 comments on commit a08f4a6

Please sign in to comment.
You can’t perform that action at this time.