Skip to content

Commit

Permalink
use positons value from summary for wallet balance (#6358)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbbb authored Dec 20, 2024
1 parent b86910e commit 3e7d5b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
24 changes: 5 additions & 19 deletions src/hooks/useWalletBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { useMemo } from 'react';
import { Address } from 'viem';
import useAccountSettings from './useAccountSettings';
import { useAddysSummary } from '@/resources/summary/summary';
import { useQueries } from '@tanstack/react-query';
import { fetchPositions, positionsQueryKey } from '@/resources/defi/PositionsQuery';
import { RainbowPositions } from '@/resources/defi/types';
import { add, convertAmountToNativeDisplay } from '@/helpers/utilities';
import { queryClient } from '@/react-query';

const QUERY_CONFIG = {
staleTime: 60_000, // 1 minute
Expand Down Expand Up @@ -43,25 +39,14 @@ const useWalletBalances = (wallets: AllRainbowWallets): WalletBalanceResult => {
[wallets]
);

const { data: summaryData, isLoading: isSummaryLoading } = useAddysSummary(
const { data: summaryData, isLoading } = useAddysSummary(
{
addresses: allAddresses,
currency: nativeCurrency,
},
QUERY_CONFIG
);

const positionQueries = useQueries({
queries: allAddresses.map(address => ({
queryKey: positionsQueryKey({ address, currency: nativeCurrency }),
queryFn: () => fetchPositions({ address, currency: nativeCurrency }),
enabled: !!address,
...QUERY_CONFIG,
})),
});

const isLoading = isSummaryLoading || positionQueries.some(query => query.isLoading);

const balances = useMemo(() => {
const result: Record<Address, WalletBalance> = {};

Expand All @@ -70,9 +55,10 @@ const useWalletBalances = (wallets: AllRainbowWallets): WalletBalanceResult => {
for (const address of allAddresses) {
const lowerCaseAddress = address.toLowerCase() as Address;
const assetBalance = summaryData?.data?.addresses?.[lowerCaseAddress]?.summary?.asset_value?.toString() || '0';
const positionData = queryClient.getQueryData<RainbowPositions | undefined>(positionsQueryKey({ address, currency: nativeCurrency }));
const positionsBalance = positionData ? positionData.totals.total.amount : '0';
const totalAccountBalance = add(assetBalance, positionsBalance);
const positionsBalance = summaryData?.data?.addresses?.[lowerCaseAddress]?.summary?.positions_value?.toString() || '0';
const claimablesBalance = summaryData?.data?.addresses?.[lowerCaseAddress]?.summary?.claimables_value?.toString() || '0';

const totalAccountBalance = add(assetBalance, add(positionsBalance, claimablesBalance));

result[lowerCaseAddress] = {
assetBalanceAmount: assetBalance,
Expand Down
4 changes: 4 additions & 0 deletions src/resources/summary/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ interface AddysSummary {
num_erc20s: number;
last_activity: number;
asset_value: number | null;
claimables_value: number | null;
positions_value: number | null;
};
};
summary_by_chain: {
Expand All @@ -63,6 +65,8 @@ interface AddysSummary {
num_erc20s: number;
last_activity: number;
asset_value: number | null;
claimables_value: number | null;
positions_value: number | null;
};
};
};
Expand Down

0 comments on commit 3e7d5b1

Please sign in to comment.