From 4835781d2970203c1f1e7e44b5c24ce9938bd96c Mon Sep 17 00:00:00 2001 From: ZENODE Date: Tue, 20 Feb 2024 20:14:44 +0100 Subject: [PATCH] Reviewed #544, made the logic in useFormatter instead --- src/stores/useDashboard.ts | 16 ++++++++-------- src/stores/useFormatter.ts | 20 ++++++++++++++++---- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/stores/useDashboard.ts b/src/stores/useDashboard.ts index 531b0b4a..8d166cd2 100644 --- a/src/stores/useDashboard.ts +++ b/src/stores/useDashboard.ts @@ -290,15 +290,15 @@ export const useDashboard = defineStore('dashboard', { keys.forEach(k => { if(this.chains[k]) this.chains[k].assets.forEach(a => { if(a.coingecko_id !== undefined && a.coingecko_id.length > 0) { - coinIds.push(a.coingecko_id) + coinIds.push(a.coingecko_id) + a.denom_units.forEach(u => { + this.coingecko[u.denom] = { + coinId: a.coingecko_id || '', + exponent: u.exponent, + symbol: a.symbol + } + }) } - a.denom_units.forEach(u => { - this.coingecko[u.denom] = { - coinId: a.coingecko_id || '', - exponent: u.exponent, - symbol: a.symbol - } - }) }) }) diff --git a/src/stores/useFormatter.ts b/src/stores/useFormatter.ts index 178e9f19..d14ec1d3 100644 --- a/src/stores/useFormatter.ts +++ b/src/stores/useFormatter.ts @@ -121,16 +121,15 @@ export const useFormatter = defineStore('formatter', { case denom.startsWith("a"): return 18 case denom==='inj': return 18 } - return 0 + return this.exponentForDenom(denom) }, tokenAmountNumber(token?: Coin) { if(!token || !token.denom) return 0 // find the symbol const symbol = this.dashboard.coingecko[token.denom]?.symbol || token.denom - // convert denomination to to symbol - const exponent = - this.dashboard.coingecko[symbol?.toLowerCase()]?.exponent || this.specialDenom(token.denom); + // convert denomination to symbol + const exponent = this.dashboard.coingecko[symbol?.toLowerCase()]?.exponent || this.specialDenom(token.denom); // caculate amount of symbol const amount = Number(token.amount) / (10 ** exponent) return amount @@ -160,7 +159,20 @@ export const useFormatter = defineStore('formatter', { } return undefined }, + exponentForDenom(denom: string) { + const asset: Asset | undefined = this.findGlobalAssetConfig(denom) + let exponent = 0; + if (asset) { + // find the max exponent for display + asset.denom_units.forEach((x) => { + if (x.exponent >= exponent) { + exponent = x.exponent; + } + }); + } + return exponent; + }, tokenDisplayDenom(denom?: string) { if (denom) { let asset: Asset | undefined;