From 7d8278804e0bf27b5a713b9612ce0395cc2cd9ec Mon Sep 17 00:00:00 2001 From: Matthew Wall Date: Thu, 12 Dec 2024 15:22:15 -0500 Subject: [PATCH 1/4] add retrieval of farcaster address for wallets --- src/hooks/useFarcasterAccountForWallets.ts | 54 ++++++++++++++++++++++ src/hooks/useWalletBalances.ts | 2 +- src/hooks/useWalletsHiddenBalances.ts | 2 +- src/resources/summary/summary.ts | 24 ++++++++++ 4 files changed, 80 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..c7cdd4b50d5 --- /dev/null +++ b/src/hooks/useFarcasterAccountForWallets.ts @@ -0,0 +1,54 @@ +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'; + +type SummaryData = ReturnType['data']; + +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 && (wallets || {})[accountAddress]?.type !== walletTypes.readOnly) { + setFarcasterWalletAddress(accountAddress); + } + + 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 && (wallets || {})[address]?.type !== walletTypes.readOnly) { + return faracsterId; + } + }); + + if (farcasterWalletAddress) { + setFarcasterWalletAddress(farcasterWalletAddress); + } + }, [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']: { From c2f7e77c60d6fed25df9297f08b901d08cc7701d Mon Sep 17 00:00:00 2001 From: Matthew Wall Date: Thu, 12 Dec 2024 15:36:14 -0500 Subject: [PATCH 2/4] fix wallet finding --- src/hooks/useFarcasterAccountForWallets.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/hooks/useFarcasterAccountForWallets.ts b/src/hooks/useFarcasterAccountForWallets.ts index c7cdd4b50d5..00c31275437 100644 --- a/src/hooks/useFarcasterAccountForWallets.ts +++ b/src/hooks/useFarcasterAccountForWallets.ts @@ -7,9 +7,15 @@ 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(); @@ -33,22 +39,28 @@ export const useFarcasterWalletAddress = () => { } const selectedAddressFid = summaryData?.data.addresses[accountAddress as Address]?.meta?.farcaster?.fid; - if (selectedAddressFid && (wallets || {})[accountAddress]?.type !== walletTypes.readOnly) { + + 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 && (wallets || {})[address]?.type !== walletTypes.readOnly) { + if (faracsterId && getWalletForAddress(wallets || {}, address)?.type !== walletTypes.readOnly) { return faracsterId; } }); if (farcasterWalletAddress) { setFarcasterWalletAddress(farcasterWalletAddress); + return; } + setFarcasterWalletAddress(null); }, [wallets, allAddresses, accountAddress]); + console.log('farcasterWalletAddress', farcasterWalletAddress); + return farcasterWalletAddress; }; From f5af3be61f65d172712f97ca62d62ec90681c090 Mon Sep 17 00:00:00 2001 From: Matthew Wall Date: Mon, 16 Dec 2024 10:17:13 -0500 Subject: [PATCH 3/4] Update src/hooks/useFarcasterAccountForWallets.ts --- src/hooks/useFarcasterAccountForWallets.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/hooks/useFarcasterAccountForWallets.ts b/src/hooks/useFarcasterAccountForWallets.ts index 00c31275437..a789383f2c7 100644 --- a/src/hooks/useFarcasterAccountForWallets.ts +++ b/src/hooks/useFarcasterAccountForWallets.ts @@ -60,7 +60,6 @@ export const useFarcasterWalletAddress = () => { setFarcasterWalletAddress(null); }, [wallets, allAddresses, accountAddress]); - console.log('farcasterWalletAddress', farcasterWalletAddress); return farcasterWalletAddress; }; From 09d31fa79b561ca0c269af34e237239f773a8e77 Mon Sep 17 00:00:00 2001 From: Matthew Wall Date: Mon, 16 Dec 2024 10:17:26 -0500 Subject: [PATCH 4/4] Update src/hooks/useFarcasterAccountForWallets.ts --- src/hooks/useFarcasterAccountForWallets.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/hooks/useFarcasterAccountForWallets.ts b/src/hooks/useFarcasterAccountForWallets.ts index a789383f2c7..4c5702051b7 100644 --- a/src/hooks/useFarcasterAccountForWallets.ts +++ b/src/hooks/useFarcasterAccountForWallets.ts @@ -60,6 +60,5 @@ export const useFarcasterWalletAddress = () => { setFarcasterWalletAddress(null); }, [wallets, allAddresses, accountAddress]); - return farcasterWalletAddress; };