Skip to content

Commit

Permalink
Retrieve FID for wallet addresses (#6330)
Browse files Browse the repository at this point in the history
  • Loading branch information
walmat authored Dec 16, 2024
1 parent 583ef49 commit 2ad4393
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
64 changes: 64 additions & 0 deletions src/hooks/useFarcasterAccountForWallets.ts
Original file line number Diff line number Diff line change
@@ -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<typeof useAddysSummary>['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<string | null>(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<SummaryData>(
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;
};
2 changes: 1 addition & 1 deletion src/hooks/useWalletBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useWalletsHiddenBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
24 changes: 24 additions & 0 deletions src/resources/summary/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']: {
Expand Down

0 comments on commit 2ad4393

Please sign in to comment.