Skip to content

Commit

Permalink
fix: count both defi & asset balances (#8545)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xApotheosis authored Jan 13, 2025
1 parent c1bde24 commit 8a3bbbb
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 33 deletions.
6 changes: 2 additions & 4 deletions src/components/ChainDropdown/ChainDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { GridIcon } from 'components/Icons/GridIcon'
import { bnOrZero } from 'lib/bignumber/bignumber'
import {
selectPortfolioTotalBalanceByChainIdIncludeStaking,
selectPortfolioTotalUserCurrencyBalanceExcludeEarnDupes,
selectPortfolioTotalUserCurrencyBalance,
} from 'state/slices/selectors'
import { useAppSelector } from 'state/store'

Expand Down Expand Up @@ -46,9 +46,7 @@ export const ChainDropdown: React.FC<ChainDropdownProps> = ({
buttonProps,
...menuProps
}) => {
const totalPortfolioUserCurrencyBalance = useAppSelector(
selectPortfolioTotalUserCurrencyBalanceExcludeEarnDupes,
)
const totalPortfolioUserCurrencyBalance = useAppSelector(selectPortfolioTotalUserCurrencyBalance)
const fiatBalanceByChainId = useAppSelector(selectPortfolioTotalBalanceByChainIdIncludeStaking)

const translate = useTranslate()
Expand Down
6 changes: 2 additions & 4 deletions src/pages/Dashboard/components/DashboardChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
selectChartTimeframe,
selectIsPortfolioLoading,
selectPortfolioAssetIds,
selectPortfolioTotalUserCurrencyBalanceExcludeEarnDupes,
selectPortfolioTotalUserCurrencyBalance,
} from 'state/slices/selectors'
import { useAppDispatch, useAppSelector } from 'state/store'

Expand Down Expand Up @@ -63,9 +63,7 @@ export const DashboardChart = () => {

const assetIds = useAppSelector(selectPortfolioAssetIds)

const portfolioTotalUserCurrencyBalance = useAppSelector(
selectPortfolioTotalUserCurrencyBalanceExcludeEarnDupes,
)
const portfolioTotalUserCurrencyBalance = useAppSelector(selectPortfolioTotalUserCurrencyBalance)
const loading = useAppSelector(selectIsPortfolioLoading)
const isLoaded = !loading

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
selectClaimableRewards,
selectEarnBalancesUserCurrencyAmountFull,
selectIsPortfolioLoading,
selectPortfolioTotalUserCurrencyBalanceExcludeEarnDupes,
selectPortfolioTotalUserCurrencyBalance,
} from 'state/slices/selectors'
import { useAppSelector } from 'state/store'

Expand Down Expand Up @@ -41,7 +41,7 @@ export const WalletBalance: React.FC<WalletBalanceProps> = memo(
selectEarnBalancesUserCurrencyAmountFull,
).toFixed()
const portfolioTotalUserCurrencyBalance = useAppSelector(
selectPortfolioTotalUserCurrencyBalanceExcludeEarnDupes,
selectPortfolioTotalUserCurrencyBalance,
)
const netWorth = useMemo(
() =>
Expand Down
24 changes: 1 addition & 23 deletions src/state/slices/portfolioSlice/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSelector } from '@reduxjs/toolkit'
import type { AccountId, AssetId, ChainId } from '@shapeshiftoss/caip'
import { FEE_ASSET_IDS, foxyAssetId, fromAccountId, fromAssetId } from '@shapeshiftoss/caip'
import { FEE_ASSET_IDS, fromAccountId, fromAssetId } from '@shapeshiftoss/caip'
import type {
AccountMetadata,
AccountMetadataById,
Expand Down Expand Up @@ -207,28 +207,6 @@ export const selectPortfolioTotalUserCurrencyBalance = createSelector(
.toFixed(2),
)

export const selectPortfolioTotalUserCurrencyBalanceExcludeEarnDupes = createSelector(
selectPortfolioUserCurrencyBalances,
selectGetReadOnlyOpportunities,
(portfolioUserCurrencyBalances, readOnlyOpportunities): string => {
const readOnlyOpportunitiesDuplicates = Object.values(
readOnlyOpportunities.data?.opportunities ?? {},
).map(opportunity => opportunity.assetId)
// ETH/FOX LP token, FOXy, and other held tokens can be both portfolio assets, but also part of DeFi opportunities
// With the current architecture (having them both as portfolio assets and earn opportunities), we have to remove these two some place or another
// This obviously won't scale as we support more LP tokens, but for now, this at least gives this deduction a sane home we can grep with `dupes` or `duplicates`
const portfolioEarnAssetIdsDuplicates = [foxEthLpAssetId, foxyAssetId].concat(
readOnlyOpportunitiesDuplicates,
)
return Object.entries(portfolioUserCurrencyBalances)
.reduce<BN>((acc, [assetId, assetUserCurrencyBalance]) => {
if (portfolioEarnAssetIdsDuplicates.includes(assetId)) return acc
return acc.plus(bnOrZero(assetUserCurrencyBalance))
}, bn(0))
.toFixed(2)
},
)

export const selectPortfolioUserCurrencyBalanceByAssetId = createCachedSelector(
selectPortfolioUserCurrencyBalances,
selectAssetIdParamFromFilter,
Expand Down

0 comments on commit 8a3bbbb

Please sign in to comment.