From e38001e5fefbbb2d1b9268a6f7687ca2392640ee Mon Sep 17 00:00:00 2001 From: liangping <18786721@qq.com> Date: Mon, 15 May 2023 14:06:46 +0800 Subject: [PATCH] add basic portfolio --- src/components/charts/DonutChart.vue | 6 +++ src/modules/wallet/portfolio.vue | 77 ++++++++++++++++++++++++---- src/stores/useFormatter.ts | 3 ++ 3 files changed, 76 insertions(+), 10 deletions(-) diff --git a/src/components/charts/DonutChart.vue b/src/components/charts/DonutChart.vue index 8a71d5ae..4479b5fd 100644 --- a/src/components/charts/DonutChart.vue +++ b/src/components/charts/DonutChart.vue @@ -20,3 +20,9 @@ const expenseRationChartConfig = computed(() => :series="series" /> + + diff --git a/src/modules/wallet/portfolio.vue b/src/modules/wallet/portfolio.vue index cb7a60f4..6b4f1b97 100644 --- a/src/modules/wallet/portfolio.vue +++ b/src/modules/wallet/portfolio.vue @@ -2,15 +2,17 @@ import { CosmosRestClient } from '@/libs/client'; import type { Coin, Delegation } from '@/types'; import { ref } from 'vue'; -import { scanLocalKeys } from './utils'; +import { scanLocalKeys, type AccountEntry } from './utils'; import { fromBech32, toBase64 } from '@cosmjs/encoding'; import { computed } from 'vue'; import { useFormatter } from '@/stores'; +import DonutChart from '@/components/charts/DonutChart.vue'; const format = useFormatter() const conf = ref(JSON.parse(localStorage.getItem("imported-addresses") || "{}") as Record) const balances = ref({} as Record) const delegations = ref({} as Record) +const tokenMeta = ref({} as Record) scanLocalKeys().forEach(wallet => { const { data } = fromBech32(wallet.cosmosAddress) @@ -22,10 +24,17 @@ scanLocalKeys().forEach(wallet => { if (x.endpoint) { const client = CosmosRestClient.newDefault(x.endpoint) client.getBankBalances(x.address).then(res => { - balances.value[x.address] = res.balances.filter(x => x.denom.length < 10) + const bal = res.balances.filter(x => x.denom.length < 10) + balances.value[x.address] = bal + bal.forEach(b => { + tokenMeta.value[b.denom] = x + }) }) client.getStakingDelegations(x.address).then(res => { delegations.value[x.address] = res.delegation_responses + res.delegation_responses.forEach(del => { + tokenMeta.value[del.balance.denom] = x + }) }) } }) @@ -35,19 +44,25 @@ const tokenValues = computed(() => { const values = {} as Record Object.values(balances.value).forEach(b => { b.forEach(coin => { - if(values[coin.denom]) { - values[coin.denom] += format.tokenValueNumber(coin) - } else { - values[coin.denom] = format.tokenValueNumber(coin) + const v = format.tokenValueNumber(coin) + if(v) { + if (values[coin.denom]) { + values[coin.denom] += v + } else { + values[coin.denom] = v + } } }) }) Object.values(delegations.value).forEach(b => { b.forEach(d => { - if(values[d.balance.denom]) { - values[d.balance.denom] += format.tokenValueNumber(d.balance) - } else { - values[d.balance.denom] = format.tokenValueNumber(d.balance) + const v = format.tokenValueNumber(d.balance) + if(v) { + if (values[d.balance.denom]) { + values[d.balance.denom] += v + } else { + values[d.balance.denom] = v + } } }) }) @@ -58,6 +73,20 @@ const totalValue = computed(() => { return Object.values(tokenValues.value).reduce((a, s) => (a + s), 0) }) +const tokenList = computed(() => { + const list = [] as { denom: string, value: number, logo: string, chainName: string, percentage: number }[] + Object.keys(tokenValues.value).map(x => { + list.push({ + denom: x, + value: tokenValues.value[x], + chainName: tokenMeta.value[x]?.chainName, + logo: tokenMeta.value[x]?.logo, + percentage: tokenValues.value[x] / totalValue.value + }) + }) + return list.filter(x => x.value > 0).sort((a, b) => b.value - a.value) +}) + diff --git a/src/stores/useFormatter.ts b/src/stores/useFormatter.ts index af6d5c80..47d87eb5 100644 --- a/src/stores/useFormatter.ts +++ b/src/stores/useFormatter.ts @@ -216,6 +216,9 @@ export const useFormatter = defineStore('formatter', { percent(decimal?: string | number) { return decimal ? numeral(decimal).format('0.[00]%') : '-'; }, + formatNumber(input: number, fmt = '0.[00]') { + return numeral(input).format(fmt) + }, numberAndSign(input: number, fmt = '+0,0') { return numeral(input).format(fmt); },