diff --git a/app/coins/btc.js b/app/coins/btc.js index 7221eee..2ad4e0a 100644 --- a/app/coins/btc.js +++ b/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", diff --git a/app/coins/ltc.js b/app/coins/ltc.js index 93dd2f2..bd4cf32 100644 --- a/app/coins/ltc.js +++ b/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", diff --git a/app/utils.js b/app/utils.js index 7eddfb8..3cfd9f6 100644 --- a/app/utils.js +++ b/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, }; diff --git a/views/includes/block-content.pug b/views/includes/block-content.pug index ef2a6b2..5cbdf8b 100644 --- a/views/includes/block-content.pug +++ b/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)} diff --git a/views/mempool-summary.pug b/views/mempool-summary.pug index 0987b7b..ac35091 100644 --- a/views/mempool-summary.pug +++ b/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}]; diff --git a/views/transaction.pug b/views/transaction.pug index 44b7406..d5c1a21 100644 --- a/views/transaction.pug +++ b/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