From dfaa73803edc04642d973c0f70e0edeb965fb6ee Mon Sep 17 00:00:00 2001 From: softsimon Date: Tue, 29 Sep 2020 03:54:56 +0700 Subject: [PATCH] Bugfix: Liquid asset issuance / circulating / burned amounts are missing decimal point fixes #112 --- frontend/src/app/bitcoin.utils.ts | 62 +++++++++++++++++++ .../app/components/asset/asset.component.html | 12 ++-- .../app/components/asset/asset.component.ts | 5 ++ 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/frontend/src/app/bitcoin.utils.ts b/frontend/src/app/bitcoin.utils.ts index b023ac2b1..b3c76dc31 100644 --- a/frontend/src/app/bitcoin.utils.ts +++ b/frontend/src/app/bitcoin.utils.ts @@ -63,6 +63,68 @@ export function calcSegwitFeeGains(tx: Transaction) { }; } +// https://github.com/shesek/move-decimal-point +export function moveDec(num: number, n: number) { + let frac, int, neg, ref; + if (n === 0) { + return num; + } + ref = ('' + num).split('.'), int = ref[0], frac = ref[1]; + int || (int = '0'); + frac || (frac = '0'); + neg = (int[0] === '-' ? '-' : ''); + if (neg) { + int = int.slice(1); + } + if (n > 0) { + if (n > frac.length) { + frac += zeros(n - frac.length); + } + int += frac.slice(0, n); + frac = frac.slice(n); + } else { + n = n * -1; + if (n > int.length) { + int = (zeros(n - int.length)) + int; + } + frac = int.slice(n * -1) + frac; + int = int.slice(0, n * -1); + } + while (int[0] === '0') { + int = int.slice(1); + } + while (frac[frac.length - 1] === '0') { + frac = frac.slice(0, -1); + } + return neg + (int || '0') + (frac.length ? '.' + frac : ''); +} + +function zeros(n) { + return new Array(n + 1).join('0'); +} + +// Formats a number for display. Treats the number as a string to avoid rounding errors. +export const formatNumber = (s, precision = null) => { + let [ whole, dec ] = s.toString().split('.'); + + // divide numbers into groups of three separated with a thin space (U+202F, "NARROW NO-BREAK SPACE"), + // but only when there are more than a total of 5 non-decimal digits. + if (whole.length >= 5) { + whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, '\u202F'); + } + + if (precision != null && precision > 0) { + if (dec == null) { + dec = '0'.repeat(precision); + } + else if (dec.length < precision) { + dec += '0'.repeat(precision - dec.length); + } + } + + return whole + (dec != null ? '.' + dec : ''); +}; + // Utilities for segwitFeeGains const witnessSize = (vin: Vin) => vin.witness.reduce((S, w) => S + (w.length / 2), 0); const scriptSigSize = (vin: Vin) => vin.scriptsig ? vin.scriptsig.length / 2 : 0; diff --git a/frontend/src/app/components/asset/asset.component.html b/frontend/src/app/components/asset/asset.component.html index 9a3af1ea3..b72c1c094 100644 --- a/frontend/src/app/components/asset/asset.component.html +++ b/frontend/src/app/components/asset/asset.component.html @@ -41,27 +41,27 @@ Pegged in - {{ asset.chain_stats.peg_in_amount / 100000000 | number: '1.0-' + assetContract[3] }} {{ assetContract[1] }} + {{ formatAmount(asset.chain_stats.peg_in_amount, assetContract[3]) | number: '1.0-' + assetContract[3] }} {{ assetContract[1] }} Pegged out - {{ asset.chain_stats.peg_out_amount / 100000000 | number: '1.0-' + assetContract[3] }} {{ assetContract[1] }} + {{ formatAmount(asset.chain_stats.peg_out_amount, assetContract[3]) | number: '1.0-' + assetContract[3] }} {{ assetContract[1] }} Issued amount - {{ asset.chain_stats.issued_amount / 100000000| number: '1.0-' + assetContract[3] }} {{ assetContract[1] }} + {{ formatAmount(asset.chain_stats.issued_amount, assetContract[3]) | number: '1.0-' + assetContract[3] }} {{ assetContract[1] }} Burned amount - {{ asset.chain_stats.burned_amount / 100000000 | number: '1.0-' + assetContract[3] }} {{ assetContract[1] }} + {{ formatAmount(asset.chain_stats.burned_amount, assetContract[3]) | number: '1.0-' + assetContract[3] }} {{ assetContract[1] }} Circulating amount - {{ (asset.chain_stats.issued_amount - asset.chain_stats.burned_amount) / 100000000 | number: '1.0-' + assetContract[3] }} {{ assetContract[1] }} + {{ formatAmount(asset.chain_stats.issued_amount - asset.chain_stats.burned_amount, assetContract[3]) | number: '1.0-' + assetContract[3] }} {{ assetContract[1] }} Circulating amount - {{ (asset.chain_stats.peg_in_amount - asset.chain_stats.burned_amount - asset.chain_stats.peg_out_amount) / 100000000 | number: '1.0-' + assetContract[3] }} {{ assetContract[1] }} + {{ formatAmount(asset.chain_stats.peg_in_amount - asset.chain_stats.burned_amount - asset.chain_stats.peg_out_amount, assetContract[3]) | number: '1.0-' + assetContract[3] }} {{ assetContract[1] }} diff --git a/frontend/src/app/components/asset/asset.component.ts b/frontend/src/app/components/asset/asset.component.ts index f13d72b18..13775ba7c 100644 --- a/frontend/src/app/components/asset/asset.component.ts +++ b/frontend/src/app/components/asset/asset.component.ts @@ -11,6 +11,7 @@ import { of, merge, Subscription, combineLatest } from 'rxjs'; import { SeoService } from 'src/app/services/seo.service'; import { environment } from 'src/environments/environment'; import { AssetsService } from 'src/app/services/assets.service'; +import { formatNumber, moveDec } from 'src/app/bitcoin.utils'; @Component({ selector: 'app-asset', @@ -189,6 +190,10 @@ export class AssetComponent implements OnInit, OnDestroy { this.totalConfirmedTxCount = this.asset.chain_stats.tx_count; } + formatAmount(value: number, precision = 0): number | string { + return moveDec(value, -precision); + } + ngOnDestroy() { this.mainSubscription.unsubscribe(); this.websocketService.stopTrackingAsset();