Browse Source

improvements to currency value formatting

fix-133-memory-crash
Dan Janosik 7 years ago
parent
commit
f6bbe518d1
  1. 15
      app/coins/btc.js
  2. 6
      app/coins/ltc.js
  3. 26
      app/utils.js
  4. 10
      views/includes/block-content.pug
  5. 4
      views/mempool-summary.pug
  6. 16
      views/transaction.pug

15
app/coins/btc.js

@ -11,17 +11,26 @@ module.exports = {
name:"BTC",
multiplier:1,
default:true,
values:["", "btc", "BTC"]
values:["", "btc", "BTC"],
decimalPlaces:8
},
{
name:"mBTC",
multiplier:1000,
values:["mbtc"]
values:["mbtc"],
decimalPlaces:5
},
{
name:"bits",
multiplier:1000000,
values:["bits"]
values:["bits"],
decimalPlaces:2
},
{
name:"sat",
multiplier:100000000,
values:["sat"],
decimalPlaces:0
}
],
genesisBlockHash: "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",

6
app/coins/ltc.js

@ -9,12 +9,14 @@ module.exports = {
name:"LTC",
multiplier:1,
default:true,
values:["", "btc", "BTC"]
values:["", "btc", "BTC"],
decimalPlaces:8
},
{
name:"mLTC",
multiplier:1000,
values:["mltc"]
values:["mltc"],
decimalPlaces:5
}
],
genesisBlockHash: "12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2",

26
app/utils.js

@ -102,14 +102,16 @@ function formatBytes(bytesInt) {
return bytesInt + " B";
}
var formatBtcMap = {};
var formatCurrencyCache = {};
function formatBtcAmount(amountBtc, formatType) {
if (formatBtcMap[formatType]) {
var dec = new Decimal(amountBtc);
dec = dec.times(formatBtcMap[formatType].multiplier);
function formatCurrencyAmount(amount, formatType) {
if (formatCurrencyCache[formatType]) {
var dec = new Decimal(amount);
dec = dec.times(formatCurrencyCache[formatType].multiplier);
return addThousandsSeparators(dec.toDecimalPlaces(8)) + " " + formatBtcMap[formatType].name;
var decimalPlaces = formatCurrencyCache[formatType].decimalPlaces;
return addThousandsSeparators(dec.toDecimalPlaces(decimalPlaces)) + " " + formatCurrencyCache[formatType].name;
}
for (var x = 0; x < coins[env.coin].currencyUnits.length; x++) {
@ -119,17 +121,19 @@ function formatBtcAmount(amountBtc, formatType) {
var currencyUnitValue = currencyUnit.values[y];
if (currencyUnitValue == formatType) {
formatBtcMap[formatType] = currencyUnit;
formatCurrencyCache[formatType] = currencyUnit;
var dec = new Decimal(amountBtc);
var dec = new Decimal(amount);
dec = dec.times(currencyUnit.multiplier);
return addThousandsSeparators(dec.toDecimalPlaces(8)) + " " + currencyUnit.name;
var decimalPlaces = currencyUnit.decimalPlaces;
return addThousandsSeparators(dec.toDecimalPlaces(decimalPlaces)) + " " + currencyUnit.name;
}
}
}
return amountBtc;
return amount;
}
// ref: https://stackoverflow.com/a/2901298/673828
@ -149,5 +153,5 @@ module.exports = {
splitArrayIntoChunks: splitArrayIntoChunks,
getRandomString: getRandomString,
formatBytes: formatBytes,
formatBtcAmount: formatBtcAmount
formatCurrencyAmount: formatCurrencyAmount,
};

10
views/includes/block-content.pug

@ -141,7 +141,7 @@ div(class="tab-content")
td
span(class="tag monospace") coinbase
span(class="monospace") Newly minted coins
td(class="monospace") #{utils.formatBtcAmount(utils.getBlockReward(result.getblock.height), currencyFormatType)}
td(class="monospace") #{utils.formatCurrencyAmount(utils.getBlockReward(result.getblock.height), currencyFormatType)}
each txInput, txInputIndex in result.txInputsByTransaction[tx.txid]
if (txInput)
@ -160,7 +160,7 @@ div(class="tab-content")
td
if (vout.value)
- totalInputValue = totalInputValue.plus(new Decimal(vout.value));
span(class="monospace") #{utils.formatBtcAmount(vout.value, currencyFormatType)}
span(class="monospace") #{utils.formatCurrencyAmount(vout.value, currencyFormatType)}
- var coinbaseCount = tx.vin[0].coinbase ? 1 : 0;
if ((tx.vin.length - coinbaseCount) > result.txInputsByTransaction[tx.txid].length)
@ -174,7 +174,7 @@ div(class="tab-content")
td
td
td
strong(class="monospace") #{utils.formatBtcAmount(totalInputValue, currencyFormatType)}
strong(class="monospace") #{utils.formatCurrencyAmount(totalInputValue, currencyFormatType)}
div(class="col-md-6")
@ -205,14 +205,14 @@ div(class="tab-content")
span(class="monospace") OP_RETURN:
span(class="monospace text-muted") #{utils.hex2ascii(vout.scriptPubKey.asm.substring("OP_RETURN ".length))}
td
span(class="monospace") #{utils.formatBtcAmount(vout.value, currencyFormatType)}
span(class="monospace") #{utils.formatCurrencyAmount(vout.value, currencyFormatType)}
- totalOutputValue = totalOutputValue.plus(vout.value);
tr
td
td
td
strong(class="monospace") #{utils.formatBtcAmount(totalOutputValue, currencyFormatType)}
strong(class="monospace") #{utils.formatCurrencyAmount(totalOutputValue, currencyFormatType)}
//pre
// code #{JSON.stringify(tx, null, 4)}

4
views/mempool-summary.pug

@ -28,7 +28,7 @@ block content
td(class="monospace") #{getmempoolinfo.mempoolminfee.toLocaleString()}
tr
th(class="table-active properties-header") Total Fees
td(class="monospace") #{utils.formatBtcAmount(mempoolstats.totalFee, currencyFormatType)}
td(class="monospace") #{utils.formatCurrencyAmount(mempoolstats.totalFee, currencyFormatType)}
h4 Transaction count by fee level
@ -49,7 +49,7 @@ block content
script var feeBucketLabels = [];
each feeBucketLabel, index in feeBucketLabels
- var percentTx = Math.round(100 * feeBucketTxCounts[index] / getmempoolinfo.size).toLocaleString();
script feeBucketLabels.push(["#{feeBucketLabel}","#{feeBucketTxCounts[index]} tx (#{percentTx}%)","#{utils.formatBtcAmount(totalfeeBuckets[index], currencyFormatType)}"]);
script feeBucketLabels.push(["#{feeBucketLabel}","#{feeBucketTxCounts[index]} tx (#{percentTx}%)","#{utils.formatCurrencyAmount(totalfeeBuckets[index], currencyFormatType)}"]);
script var feeBucketTxCounts = [#{feeBucketTxCounts}];

16
views/transaction.pug

@ -116,13 +116,13 @@ block content
if (result.getrawtransaction.vin[0].coinbase)
tr
th(class="table-active properties-header") Total Network Fees
td(class="monospace") #{utils.formatBtcAmount(new Decimal(totalOutputValue).minus(totalInputValue), currencyFormatType)}
td(class="monospace") #{utils.formatCurrencyAmount(new Decimal(totalOutputValue).minus(totalInputValue), currencyFormatType)} (#{global.exchangeRate})
else
tr
th(class="table-active properties-header") Network Fee Paid
td(class="monospace")
strong #{utils.formatBtcAmount(new Decimal(totalInputValue).minus(totalOutputValue), currencyFormatType)}
span(class="text-muted") (#{utils.formatBtcAmount(totalInputValue, currencyFormatType)} - #{utils.formatBtcAmount(totalOutputValue, currencyFormatType)})
strong #{utils.formatCurrencyAmount(new Decimal(totalInputValue).minus(totalOutputValue), currencyFormatType)}
span(class="text-muted") (#{utils.formatCurrencyAmount(totalInputValue, currencyFormatType)} - #{utils.formatCurrencyAmount(totalOutputValue, currencyFormatType)})
br
span ~#{new DecimalRounded(totalInputValue).minus(totalOutputValue).dividedBy(result.getrawtransaction.size).times(100000000)} sat/B
@ -164,7 +164,7 @@ block content
td
span(class="tag monospace") coinbase
span(class="monospace") Newly minted coins
td(class="monospace") #{utils.formatBtcAmount(utils.getBlockReward(result.getblock.height), currencyFormatType)}
td(class="monospace") #{utils.formatCurrencyAmount(utils.getBlockReward(result.getblock.height), currencyFormatType)}
each txInput, txInputIndex in result.txInputs
if (txInput)
@ -182,13 +182,13 @@ block content
a(href=("/tx/" + txInput.txid + "#output-" + result.getrawtransaction.vin[txInputIndex].vout), class="monospace") #{txInput.txid.substring(0, 14)}..., Output ##{result.getrawtransaction.vin[txInputIndex].vout + 1}
td
if (vout.value)
span(class="monospace") #{utils.formatBtcAmount(vout.value, currencyFormatType)}
span(class="monospace") #{utils.formatCurrencyAmount(vout.value, currencyFormatType)}
tr
td
td
td
strong(class="monospace") #{utils.formatBtcAmount(totalInputValue, currencyFormatType)}
strong(class="monospace") #{utils.formatCurrencyAmount(totalInputValue, currencyFormatType)}
div(class="col-md-6")
@ -217,13 +217,13 @@ block content
span(class="monospace") OP_RETURN:
span(class="monospace text-muted") #{utils.hex2ascii(vout.scriptPubKey.asm.substring("OP_RETURN ".length))}
td
span(class="monospace") #{utils.formatBtcAmount(vout.value, currencyFormatType)}
span(class="monospace") #{utils.formatCurrencyAmount(vout.value, currencyFormatType)}
tr
td
td
td
strong(class="monospace") #{utils.formatBtcAmount(totalOutputValue, currencyFormatType)}
strong(class="monospace") #{utils.formatCurrencyAmount(totalOutputValue, currencyFormatType)}
div(id="tab-scripts", class="tab-pane", role="tabpanel")
h3 Input Scripts

Loading…
Cancel
Save