From 6c38baa9cc9154cd33edd1645c54e455298f21c6 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 29 Oct 2024 19:33:38 +0400 Subject: [PATCH] feat: add token balance to accounts total --- src/composables/balances.ts | 10 ++++------ src/composables/fungibleTokens.ts | 28 +++++++++++++++++++++++++++- src/popup/pages/Dashboard.vue | 10 ++++++++-- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/composables/balances.ts b/src/composables/balances.ts index 259465b34..0d0b17a4a 100644 --- a/src/composables/balances.ts +++ b/src/composables/balances.ts @@ -42,8 +42,8 @@ export function useBalances() { const balance = computed(() => balances.value[activeAccount.value.address] || new BigNumber(0)); - const accountsTotalBalance = computed(() => { - const result = accounts.value.reduce( + const accountsTotalBalance = computed( + () => accounts.value.reduce( (total, account) => { const accountBalance = balances.value?.[account.address]; if (!accountBalance) { @@ -52,10 +52,8 @@ export function useBalances() { return total + (getCurrentCurrencyRate(account.protocol) * accountBalance.toNumber()); }, 0, - ); - - return result.toFixed(2); - }); + ).toFixed(2), + ); function getAccountBalance(address: string) { return balances.value[address] || new BigNumber(0); diff --git a/src/composables/fungibleTokens.ts b/src/composables/fungibleTokens.ts index 5b8dd5b2f..72cd5fa5b 100644 --- a/src/composables/fungibleTokens.ts +++ b/src/composables/fungibleTokens.ts @@ -1,8 +1,10 @@ /* eslint-disable no-param-reassign */ import { computed, watch } from 'vue'; +import { uniqBy, isEmpty } from 'lodash-es'; import BigNumber from 'bignumber.js'; import { Encoding, Tag } from '@aeternity/aepp-sdk'; + import { isAssetCoin, toShiftedBigNumber } from '@/utils'; import type { AccountAddress, @@ -24,7 +26,7 @@ import FungibleTokenFullInterfaceACI from '@/protocols/aeternity/aci/FungibleTok import { AE_COIN_PRECISION } from '@/protocols/aeternity/config'; import { aettosToAe, categorizeContractCallTxObject } from '@/protocols/aeternity/helpers'; -import { uniqBy } from 'lodash-es'; +import { useCurrencies } from './currencies'; import { useAccounts } from './accounts'; import { useAeSdk } from './aeSdk'; import { useTippingContracts } from './tippingContracts'; @@ -107,11 +109,34 @@ export function useFungibleTokens() { protocolsInUse, getLastActiveProtocolAccount, } = useAccounts(); + const { getCurrentCurrencyRate } = useCurrencies(); function getAccountTokenBalances(address: AccountAddress): ITokenBalance[] { return tokenBalances.value.filter((token) => token.address === address) || []; } + const accountsTotalTokenBalance = computed( + () => accounts.value.reduce( + (total, account) => { + const accountTokenBalance = getAccountTokenBalances(account.address); + if (isEmpty(accountTokenBalance)) { + return total; + } + let accountTotal = 0; + accountTokenBalance + .filter((token) => token.price && token.convertedBalance) + .forEach((token) => { + if (token.convertedBalance && token.price) { + accountTotal += new BigNumber(token.convertedBalance) + .multipliedBy(token.price).toNumber(); + } + }); + return total + (getCurrentCurrencyRate(account.protocol) * accountTotal); + }, + 0, + ).toFixed(2), + ); + function getAccountTokenBalance( address: AccountAddress, contractId: string, @@ -322,6 +347,7 @@ export function useFungibleTokens() { } return { + accountsTotalTokenBalance, tokenBalances, tokensAvailable, createOrChangeAllowance, diff --git a/src/popup/pages/Dashboard.vue b/src/popup/pages/Dashboard.vue index ff97b6d41..8d90a7fbe 100644 --- a/src/popup/pages/Dashboard.vue +++ b/src/popup/pages/Dashboard.vue @@ -7,7 +7,7 @@ :accounts-select-options="accountsSelectOptions" :active-account-address="activeAccount.address" :active-idx="activeAccountGlobalIdx" - :balances-total="accountsTotalBalance" + :balances-total="totalBalance" @select-account="(address) => setActiveAccountByAddress(address)" >