From 189b4b165a64bfc01ef1a01a8518dbe01f155338 Mon Sep 17 00:00:00 2001 From: Dima Rifai <dima.rifai@gmail.com> Date: Thu, 23 Jan 2025 21:06:17 +0200 Subject: [PATCH] Issue #417 - Calculate account_value from dollars entry and pass it to formatter --- hooks/common/useConvertedAccountDetails.tsx | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/hooks/common/useConvertedAccountDetails.tsx b/hooks/common/useConvertedAccountDetails.tsx index 5e9667da..49a9fa01 100644 --- a/hooks/common/useConvertedAccountDetails.tsx +++ b/hooks/common/useConvertedAccountDetails.tsx @@ -25,6 +25,7 @@ const useConvertedAccountDetails = (accountName: string, liveDataEnabled: boolea return formattedHP as string; } + const { hiveChain } = useHiveChainContext(); const {accountDetails, notFound}= useAccountDetails(accountName, liveDataEnabled); if (!dynamicGlobalData || !hiveChain || !accountDetails) return {formattedAccountDetails: undefined, notFound: undefined}; @@ -75,6 +76,7 @@ const useConvertedAccountDetails = (accountName: string, liveDataEnabled: boolea created: formatAndDelocalizeTime(accountDetails.created), }; + // Prepare HBD for $ display const dollars = { hbd_balance: accountDetailsForFormat.hbd_balance, @@ -89,8 +91,23 @@ const useConvertedAccountDetails = (accountName: string, liveDataEnabled: boolea vesting_shares: hiveChain.hiveToHbd(vesting_shares, rawFeedPrice, rawQuote), vesting_withdraw_rate: hiveChain.hiveToHbd(vesting_withdraw_rate, rawFeedPrice, rawQuote) } + interface NaiAsset { + amount: string; + nai: string; + precision: number + } + + /* Account Value Calculations */ + const skipCalculation = ["delegated_vesting_shares", "received_vesting_shares"]; + let totalAccountValue = Object.entries(dollars).reduce((sum, [key, value]) => { + if (value && value.hasOwnProperty('amount') && !skipCalculation.includes(key)) { + return sum + Number((value as NaiAsset).amount); + } + return sum; + }, 0); + (dollars as any)["account_value"] = hiveChain.hbd(totalAccountValue); const formattedAccountDetails = {...hiveChain?.formatter.format({ ...accountDetailsForFormat, @@ -98,7 +115,7 @@ const useConvertedAccountDetails = (accountName: string, liveDataEnabled: boolea vests, has_hbd_reward: !!accountDetails.reward_hbd_balance, has_vesting_reward: !!Number(accountDetails.reward_vesting_balance), - has_hive_reward: !!accountDetails.reward_hive_balance + has_hive_reward: !!accountDetails.reward_hive_balance, }), } as Explorer.FormattedAccountDetails; delete formattedAccountDetails.last_post; -- GitLab