Skip to content

Commit

Permalink
feat: add token balance to accounts total
Browse files Browse the repository at this point in the history
  • Loading branch information
CedrikNikita committed Oct 31, 2024
1 parent c7f2a19 commit cb3ba27
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
10 changes: 4 additions & 6 deletions src/composables/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
28 changes: 27 additions & 1 deletion src/composables/fungibleTokens.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -322,6 +347,7 @@ export function useFungibleTokens() {
}

return {
accountsTotalTokenBalance,
tokenBalances,
tokensAvailable,
createOrChangeAllowance,
Expand Down
10 changes: 8 additions & 2 deletions src/popup/pages/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
>
<template #swiper>
Expand Down Expand Up @@ -107,6 +107,7 @@ import {
useAeSdk,
useBalances,
useDeepLinkApi,
useFungibleTokens,
} from '@/composables';
import { buildAeFaucetUrl, buildSimplexLink } from '@/protocols/aeternity/helpers';
Expand Down Expand Up @@ -157,12 +158,17 @@ export default defineComponent({
} = useAccounts();
const { accountsTotalBalance } = useBalances();
const { accountsTotalTokenBalance } = useFungibleTokens();
const { checkIfOpenTransferSendModal } = useDeepLinkApi();
const { isNodeMainnet, isNodeTestnet } = useAeSdk();
const activeAccountFaucetUrl = computed(() => buildAeFaucetUrl(activeAccount.value.address));
const activeAccountSimplexLink = computed(() => buildSimplexLink(activeAccount.value.address));
const totalBalance = computed(() => (
(+accountsTotalBalance.value + +accountsTotalTokenBalance.value).toString()
));
watch(
() => route.query,
() => checkIfOpenTransferSendModal(route),
Expand Down Expand Up @@ -198,7 +204,6 @@ export default defineComponent({
accounts,
accountsAddressList,
accountsSelectOptions,
accountsTotalBalance,
activeAccount,
activeAccountSimplexLink,
activeAccountFaucetUrl,
Expand All @@ -209,6 +214,7 @@ export default defineComponent({
isNodeMainnet,
isNodeTestnet,
pageIsActive,
totalBalance,
setActiveAccountByGlobalIdx,
setActiveAccountByAddress,
};
Expand Down

0 comments on commit cb3ba27

Please sign in to comment.