From a2b4cf5c59a1430a3e6e5e17142abef8949980eb Mon Sep 17 00:00:00 2001 From: Pham Tu Date: Wed, 17 Jan 2024 18:29:46 +0700 Subject: [PATCH] fix proto --- src/components/CardParameter.vue | 14 +- src/components/dynamic/DynamicComponent.vue | 2 +- src/libs/utils.ts | 40 +- src/modules/[chain]/block/index.vue | 7 +- .../ibc/connection/[connection_id].vue | 12 +- src/modules/[chain]/staking/index.vue | 806 ++++++++++-------- src/stores/useFormatter.ts | 4 +- 7 files changed, 488 insertions(+), 397 deletions(-) diff --git a/src/components/CardParameter.vue b/src/components/CardParameter.vue index b8635c76..2e4312ef 100644 --- a/src/components/CardParameter.vue +++ b/src/components/CardParameter.vue @@ -10,17 +10,17 @@ const props = defineProps({ }); const formatter = useFormatter(); -function calculateValue(value: any, item: any) { +function calculateValue(value: any) { + if (!value) return; if (value instanceof Uint8Array) return fromAscii(value); if (Array.isArray(value)) { return (value[0] && value[0].amount) || '-'; } - if (typeof value === 'object' && 'seconds' in value) { - return value.seconds.toString() + ' seconds'; - } - - if (String(value).search(/^\d+s$/g) > -1) { + if ( + (typeof value === 'object' && 'seconds' in value) || + String(value).search(/^\d+s$/g) > -1 + ) { return formatSeconds(value); } const newValue = Number(value); @@ -57,7 +57,7 @@ function formatTitle(v: string) { {{ formatTitle(item?.subtitle) }}
- {{ calculateValue(item?.value, item) }} + {{ calculateValue(item?.value) }}
diff --git a/src/components/dynamic/DynamicComponent.vue b/src/components/dynamic/DynamicComponent.vue index 60a9875b..a25fa392 100644 --- a/src/components/dynamic/DynamicComponent.vue +++ b/src/components/dynamic/DynamicComponent.vue @@ -2,7 +2,7 @@ import { select, decodeProto } from './index'; const props = defineProps(['value', 'direct']); -if (props.value.typeUrl) { +if (props.value?.typeUrl) { props.value.value = decodeProto(props.value); } diff --git a/src/libs/utils.ts b/src/libs/utils.ts index d7548afa..7463b768 100644 --- a/src/libs/utils.ts +++ b/src/libs/utils.ts @@ -1,3 +1,5 @@ +import type { Timestamp } from 'cosmjs-types/google/protobuf/timestamp'; + export function getLocalObject(name: string) { const text = localStorage.getItem(name); if (text) { @@ -67,10 +69,11 @@ export function formatTokenAmount( tokenDenom = 'uatom', format = true ) { - const denom = typeof tokenDenom === 'string' - ? tokenDenom - // @ts-ignore - : tokenDenom?.denom_trace?.base_denom; + const denom = + typeof tokenDenom === 'string' + ? tokenDenom + : // @ts-ignore + tokenDenom?.denom_trace?.base_denom; let amount = 0; const asset = assets.find((a: any) => a.base === denom); let exp = asset @@ -120,24 +123,25 @@ export function isHexAddress(v: any) { } export function isBech32Address(v?: string) { - if(!v) return "" - const pattern = /^[a-z\d]+1[a-z\d]{38}$/g - return String(v).search(pattern) > -1 + if (!v) return ''; + const pattern = /^[a-z\d]+1[a-z\d]{38}$/g; + return String(v).search(pattern) > -1; } -export function formatSeconds(value?: string) { - if(!value) return '' - const duration = Number(value.replace(/s/, '')) - if(duration > 24*60*60) { - return `${(duration / ( 24 * 60 * 60)).toFixed()} days` +export function formatSeconds(value?: string | Timestamp) { + if (!value) return ''; + const seconds = typeof value === 'string' ? value : value.seconds.toString(); + const duration = Number(seconds.replace(/s/, '')); + if (duration > 24 * 60 * 60) { + return `${(duration / (24 * 60 * 60)).toFixed()} days`; } - if(duration > 60*60) { - return `${(duration / (60 * 60)).toFixed()} hours` - } - if(duration > 60) { - return `${duration / 60} mins` + if (duration > 60 * 60) { + return `${(duration / (60 * 60)).toFixed()} hours`; } - return value + if (duration > 60) { + return `${duration / 60} mins`; + } + return seconds; } export function hexToRgb(hex: string) { diff --git a/src/modules/[chain]/block/index.vue b/src/modules/[chain]/block/index.vue index f55f67fd..3b66ae7a 100644 --- a/src/modules/[chain]/block/index.vue +++ b/src/modules/[chain]/block/index.vue @@ -1,7 +1,7 @@ @@ -481,7 +569,7 @@ loadAvatars(); diff --git a/src/stores/useFormatter.ts b/src/stores/useFormatter.ts index 00986d23..f985fd5a 100644 --- a/src/stores/useFormatter.ts +++ b/src/stores/useFormatter.ts @@ -332,7 +332,9 @@ export const useFormatter = defineStore('formatter', { return this.percent(rate); }, percent(decimal?: string | number) { - return decimal ? numeral(decimal).format('0.[00]%') : '-'; + return decimal + ? numeral(decimal).divide('1000000000000000000').format('0.[00]%') + : '-'; }, formatNumber(input?: number, fmt = '0.[00]') { if (!input) return '';