add ibc denom cache

This commit is contained in:
liangping 2023-09-11 12:27:17 +08:00
parent e1ab30afab
commit a208831cf9
2 changed files with 21 additions and 13 deletions

2
auto-imports.d.ts vendored
View File

@ -121,7 +121,6 @@ declare global {
const useArrayFilter: typeof import('@vueuse/core')['useArrayFilter'] const useArrayFilter: typeof import('@vueuse/core')['useArrayFilter']
const useArrayFind: typeof import('@vueuse/core')['useArrayFind'] const useArrayFind: typeof import('@vueuse/core')['useArrayFind']
const useArrayFindIndex: typeof import('@vueuse/core')['useArrayFindIndex'] const useArrayFindIndex: typeof import('@vueuse/core')['useArrayFindIndex']
const useArrayFindLast: typeof import('@vueuse/core')['useArrayFindLast']
const useArrayJoin: typeof import('@vueuse/core')['useArrayJoin'] const useArrayJoin: typeof import('@vueuse/core')['useArrayJoin']
const useArrayMap: typeof import('@vueuse/core')['useArrayMap'] const useArrayMap: typeof import('@vueuse/core')['useArrayMap']
const useArrayReduce: typeof import('@vueuse/core')['useArrayReduce'] const useArrayReduce: typeof import('@vueuse/core')['useArrayReduce']
@ -424,7 +423,6 @@ declare module 'vue' {
readonly useArrayFilter: UnwrapRef<typeof import('@vueuse/core')['useArrayFilter']> readonly useArrayFilter: UnwrapRef<typeof import('@vueuse/core')['useArrayFilter']>
readonly useArrayFind: UnwrapRef<typeof import('@vueuse/core')['useArrayFind']> readonly useArrayFind: UnwrapRef<typeof import('@vueuse/core')['useArrayFind']>
readonly useArrayFindIndex: UnwrapRef<typeof import('@vueuse/core')['useArrayFindIndex']> readonly useArrayFindIndex: UnwrapRef<typeof import('@vueuse/core')['useArrayFindIndex']>
readonly useArrayFindLast: UnwrapRef<typeof import('@vueuse/core')['useArrayFindLast']>
readonly useArrayJoin: UnwrapRef<typeof import('@vueuse/core')['useArrayJoin']> readonly useArrayJoin: UnwrapRef<typeof import('@vueuse/core')['useArrayJoin']>
readonly useArrayMap: UnwrapRef<typeof import('@vueuse/core')['useArrayMap']> readonly useArrayMap: UnwrapRef<typeof import('@vueuse/core')['useArrayMap']>
readonly useArrayReduce: UnwrapRef<typeof import('@vueuse/core')['useArrayReduce']> readonly useArrayReduce: UnwrapRef<typeof import('@vueuse/core')['useArrayReduce']>

View File

@ -9,10 +9,11 @@ import utc from 'dayjs/plugin/utc';
import localeData from 'dayjs/plugin/localeData'; import localeData from 'dayjs/plugin/localeData';
import { useStakingStore } from './useStakingStore'; import { useStakingStore } from './useStakingStore';
import { fromBase64, fromBech32, fromHex, toHex } from '@cosmjs/encoding'; import { fromBase64, fromBech32, fromHex, toHex } from '@cosmjs/encoding';
import { consensusPubkeyToHexAddress } from '@/libs'; import { consensusPubkeyToHexAddress, get } from '@/libs';
import { useBankStore } from './useBankStore'; import { useBankStore } from './useBankStore';
import type { Coin, DenomTrace } from '@/types'; import type { Coin, DenomTrace } from '@/types';
import { useDashboard } from './useDashboard'; import { useDashboard } from './useDashboard';
import type { Asset } from '@ping-pub/chain-registry-client/dist/types'
dayjs.extend(localeData); dayjs.extend(localeData);
dayjs.extend(duration); dayjs.extend(duration);
@ -41,6 +42,7 @@ export const useFormatter = defineStore('formatter', {
state: () => { state: () => {
return { return {
ibcDenoms: {} as Record<string, DenomTrace>, ibcDenoms: {} as Record<string, DenomTrace>,
ibcMetadata: {} as Record<string, Asset>,
}; };
}, },
getters: { getters: {
@ -68,6 +70,10 @@ export const useFormatter = defineStore('formatter', {
} }
return trace; 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) { priceInfo(denom: string) {
const id = this.dashboard.coingecko[denom]?.coinId || ""; const id = this.dashboard.coingecko[denom]?.coinId || "";
const prices = this.dashboard.prices[id]; const prices = this.dashboard.prices[id];
@ -135,28 +141,32 @@ export const useFormatter = defineStore('formatter', {
findGlobalAssetConfig(denom: string) { findGlobalAssetConfig(denom: string) {
const chains = Object.values(this.dashboard.chains) const chains = Object.values(this.dashboard.chains)
for ( let i =0; i < chains.length; i++ ) { 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) { if(conf) {
return conf return conf
} }
} }
return null return undefined
}, },
tokenDisplayDenom(denom?: string) { tokenDisplayDenom(denom?: string) {
if (denom) { if (denom) {
let asset: Asset | undefined;
if (denom && denom.startsWith('ibc/')) { if (denom && denom.startsWith('ibc/')) {
let ibcDenom = this.ibcDenoms[denom.replace('ibc/', '')]; const ibcDenom = denom.replace('ibc/', '')
if (ibcDenom) { asset = this.ibcMetadata[ibcDenom];
denom = ibcDenom.base_denom; 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 (asset) {
let unit = { exponent: 0, denom: '' };
if (conf) {
let unit = { exponent: 6, denom: '' };
// find the max exponent for display // find the max exponent for display
conf.denom_units.forEach((x) => { asset.denom_units.forEach((x) => {
if (x.exponent >= unit.exponent) { if (x.exponent >= unit.exponent) {
unit = x; unit = x;
} }