From a208831cf9911326720eb69f1975fd0169350c4c Mon Sep 17 00:00:00 2001 From: liangping <18786721@qq.com> Date: Mon, 11 Sep 2023 12:27:17 +0800 Subject: [PATCH] add ibc denom cache --- auto-imports.d.ts | 2 -- src/stores/useFormatter.ts | 32 +++++++++++++++++++++----------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/auto-imports.d.ts b/auto-imports.d.ts index 1150b743..10eab88d 100644 --- a/auto-imports.d.ts +++ b/auto-imports.d.ts @@ -121,7 +121,6 @@ declare global { const useArrayFilter: typeof import('@vueuse/core')['useArrayFilter'] const useArrayFind: typeof import('@vueuse/core')['useArrayFind'] const useArrayFindIndex: typeof import('@vueuse/core')['useArrayFindIndex'] - const useArrayFindLast: typeof import('@vueuse/core')['useArrayFindLast'] const useArrayJoin: typeof import('@vueuse/core')['useArrayJoin'] const useArrayMap: typeof import('@vueuse/core')['useArrayMap'] const useArrayReduce: typeof import('@vueuse/core')['useArrayReduce'] @@ -424,7 +423,6 @@ declare module 'vue' { readonly useArrayFilter: UnwrapRef readonly useArrayFind: UnwrapRef readonly useArrayFindIndex: UnwrapRef - readonly useArrayFindLast: UnwrapRef readonly useArrayJoin: UnwrapRef readonly useArrayMap: UnwrapRef readonly useArrayReduce: UnwrapRef diff --git a/src/stores/useFormatter.ts b/src/stores/useFormatter.ts index edceaf6d..c99863e5 100644 --- a/src/stores/useFormatter.ts +++ b/src/stores/useFormatter.ts @@ -9,10 +9,11 @@ import utc from 'dayjs/plugin/utc'; import localeData from 'dayjs/plugin/localeData'; import { useStakingStore } from './useStakingStore'; import { fromBase64, fromBech32, fromHex, toHex } from '@cosmjs/encoding'; -import { consensusPubkeyToHexAddress } from '@/libs'; +import { consensusPubkeyToHexAddress, get } from '@/libs'; import { useBankStore } from './useBankStore'; import type { Coin, DenomTrace } from '@/types'; import { useDashboard } from './useDashboard'; +import type { Asset } from '@ping-pub/chain-registry-client/dist/types' dayjs.extend(localeData); dayjs.extend(duration); @@ -41,6 +42,7 @@ export const useFormatter = defineStore('formatter', { state: () => { return { ibcDenoms: {} as Record, + ibcMetadata: {} as Record, }; }, getters: { @@ -68,6 +70,10 @@ export const useFormatter = defineStore('formatter', { } return trace; }, + async fetchDenomMetadata(denom: string) { + const asset = await get(`https://metadata.ping.pug/metadata/${denom}`) as Asset + this.ibcMetadata[denom] = asset + }, priceInfo(denom: string) { const id = this.dashboard.coingecko[denom]?.coinId || ""; const prices = this.dashboard.prices[id]; @@ -135,28 +141,32 @@ export const useFormatter = defineStore('formatter', { findGlobalAssetConfig(denom: string) { const chains = Object.values(this.dashboard.chains) for ( let i =0; i < chains.length; i++ ) { - const conf = chains[i].assets.find(a => a.base === denom) + const assets = chains[i].assets + const conf = assets.find(a => a.base === denom) if(conf) { return conf } } - return null + return undefined }, tokenDisplayDenom(denom?: string) { if (denom) { + let asset: Asset | undefined; if (denom && denom.startsWith('ibc/')) { - let ibcDenom = this.ibcDenoms[denom.replace('ibc/', '')]; - if (ibcDenom) { - denom = ibcDenom.base_denom; + const ibcDenom = denom.replace('ibc/', '') + asset = this.ibcMetadata[ibcDenom]; + if(!asset) { + // update ibc metadata if not exits in local cache + this.fetchDenomMetadata(ibcDenom) } + } else { + asset = this.findGlobalAssetConfig(denom) } - const conf = this.findGlobalAssetConfig(denom) - - if (conf) { - let unit = { exponent: 6, denom: '' }; + if (asset) { + let unit = { exponent: 0, denom: '' }; // find the max exponent for display - conf.denom_units.forEach((x) => { + asset.denom_units.forEach((x) => { if (x.exponent >= unit.exponent) { unit = x; }