From 2ad4393c7a06a9377658f02abdba0e0a57d9e585 Mon Sep 17 00:00:00 2001 From: Matthew Wall Date: Mon, 16 Dec 2024 11:44:32 -0500 Subject: [PATCH] Retrieve FID for wallet addresses (#6330) --- src/hooks/useFarcasterAccountForWallets.ts | 64 ++++++++++++++++++++++ src/hooks/useWalletBalances.ts | 2 +- src/hooks/useWalletsHiddenBalances.ts | 2 +- src/resources/summary/summary.ts | 24 ++++++++ 4 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 src/hooks/useFarcasterAccountForWallets.ts diff --git a/src/hooks/useFarcasterAccountForWallets.ts b/src/hooks/useFarcasterAccountForWallets.ts new file mode 100644 index 00000000000..4c5702051b7 --- /dev/null +++ b/src/hooks/useFarcasterAccountForWallets.ts @@ -0,0 +1,64 @@ +import { queryClient } from '@/react-query'; +import useWallets from './useWallets'; +import { useEffect, useMemo, useState } from 'react'; +import { addysSummaryQueryKey, useAddysSummary } from '@/resources/summary/summary'; +import { Address } from 'viem'; +import useAccountSettings from './useAccountSettings'; +import store from '@/redux/store'; +import { isEmpty } from 'lodash'; +import walletTypes from '@/helpers/walletTypes'; +import { isLowerCaseMatch } from '@/utils'; +import { AllRainbowWallets } from '@/model/wallet'; + +type SummaryData = ReturnType['data']; + +const getWalletForAddress = (wallets: AllRainbowWallets, address: string) => { + return Object.values(wallets || {}).find(wallet => wallet.addresses.some(addr => isLowerCaseMatch(addr.address, address))); +}; + +export const useFarcasterWalletAddress = () => { + const [farcasterWalletAddress, setFarcasterWalletAddress] = useState(null); + const { accountAddress } = useAccountSettings(); + const { wallets } = useWallets(); + + const allAddresses = useMemo( + () => Object.values(wallets || {}).flatMap(wallet => (wallet.addresses || []).map(account => account.address as Address)), + [wallets] + ); + + useEffect(() => { + const summaryData = queryClient.getQueryData( + addysSummaryQueryKey({ + addresses: allAddresses, + currency: store.getState().settings.nativeCurrency, + }) + ); + if (isEmpty(summaryData?.data.addresses) || isEmpty(wallets)) { + setFarcasterWalletAddress(null); + return; + } + + const selectedAddressFid = summaryData?.data.addresses[accountAddress as Address]?.meta?.farcaster?.fid; + + if (selectedAddressFid && getWalletForAddress(wallets || {}, accountAddress)?.type !== walletTypes.readOnly) { + setFarcasterWalletAddress(accountAddress); + return; + } + + const farcasterWalletAddress = Object.keys(summaryData?.data.addresses || {}).find(addr => { + const address = addr as Address; + const faracsterId = summaryData?.data.addresses[address]?.meta?.farcaster?.fid; + if (faracsterId && getWalletForAddress(wallets || {}, address)?.type !== walletTypes.readOnly) { + return faracsterId; + } + }); + + if (farcasterWalletAddress) { + setFarcasterWalletAddress(farcasterWalletAddress); + return; + } + setFarcasterWalletAddress(null); + }, [wallets, allAddresses, accountAddress]); + + return farcasterWalletAddress; +}; diff --git a/src/hooks/useWalletBalances.ts b/src/hooks/useWalletBalances.ts index ac16985a623..fbafc1c909e 100644 --- a/src/hooks/useWalletBalances.ts +++ b/src/hooks/useWalletBalances.ts @@ -6,7 +6,7 @@ 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, subtract } from '@/helpers/utilities'; +import { add, convertAmountToNativeDisplay } from '@/helpers/utilities'; import { queryClient } from '@/react-query'; const QUERY_CONFIG = { diff --git a/src/hooks/useWalletsHiddenBalances.ts b/src/hooks/useWalletsHiddenBalances.ts index 724ff266ab5..d12b3292f22 100644 --- a/src/hooks/useWalletsHiddenBalances.ts +++ b/src/hooks/useWalletsHiddenBalances.ts @@ -7,7 +7,7 @@ import { NativeCurrencyKey } from '@/entities/nativeCurrencyTypes'; import { userAssetsStore } from '@/state/assets/userAssets'; import { queryClient } from '@/react-query'; import { userAssetsQueryKey, UserAssetsResult } from '@/resources/assets/UserAssetsQuery'; -import { convertAmountAndPriceToNativeDisplay, add, isEqual, multiply } from '@/helpers/utilities'; +import { add, isEqual, multiply } from '@/helpers/utilities'; import { isEqual as _isEqual } from 'lodash'; export type WalletBalanceResult = { diff --git a/src/resources/summary/summary.ts b/src/resources/summary/summary.ts index 6f902a391f0..cd0ef1d8542 100644 --- a/src/resources/summary/summary.ts +++ b/src/resources/summary/summary.ts @@ -16,6 +16,30 @@ interface AddysSummary { data: { addresses: { [key: Address]: { + meta: { + farcaster?: { + object: string; + fid: number; + username: string; + display_name: string; + pfp_url: string; + custody_address: string; + profile: { + Bio: { + text: string; + }; + }; + follower_count: number; + following_count: number; + verifications: string[]; + verified_addresses: { + eth_addresses: string[]; + sol_addresses: string[]; + }; + verified_accounts: string[]; + power_badge: boolean; + }; + }; summary: { native_balance_by_symbol: { [key in 'ETH' | 'MATIC' | 'BNB' | 'AVAX']: {