Skip to content

Commit

Permalink
feat: improve farcaster address filtering for non-evm addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
martines3000 committed Jun 12, 2024
1 parent ac99b60 commit 712d541
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const getMinimalProfileFromAddress = async (
identity: address,
},
}),
next: { revalidate: 86400 }, // Cache for 1 day
next: { revalidate: 86401 }, // Cache for 1 day
});

// Check if request was successful
Expand Down
39 changes: 23 additions & 16 deletions packages/dapp/src/lib/airstack/getMinimalProfileInfoByPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type GetProfileFromFarcasterQuery,
} from '@/__generated__/airstack/graphql';
import { PlatformType } from '@/utils';
import { isAddress } from 'viem';

type GetMinimalProfileInfoByPlatformResult = {
displayName: string | null;
Expand Down Expand Up @@ -59,7 +60,7 @@ export const getMinimalProfileInfoByPlatform = async (
: identity,
},
}),
next: { revalidate: 86400 }, // Cache for 1 day
next: { revalidate: 86401 }, // Cache for 1 day
});

// Check if request was successful
Expand Down Expand Up @@ -117,12 +118,9 @@ export const getMinimalProfileInfoByPlatform = async (
}
case PlatformType.farcaster: {
const data = jsonResponse.data as GetProfileFromFarcasterQuery;
const farcasterSocials = data.farcasterSocials?.Social;

if (
!data.farcasterSocials ||
!data.farcasterSocials.Social ||
data.farcasterSocials.Social.length === 0
) {
if (!farcasterSocials || farcasterSocials.length === 0) {
return {
displayName: null,
address: null,
Expand All @@ -132,8 +130,7 @@ export const getMinimalProfileInfoByPlatform = async (
};
}

const connectedAddresses =
data.farcasterSocials.Social[0].connectedAddresses;
let connectedAddresses = farcasterSocials[0].connectedAddresses;

if (!connectedAddresses || connectedAddresses.length === 0) {
return {
Expand All @@ -146,10 +143,18 @@ export const getMinimalProfileInfoByPlatform = async (
}

// Double check that connected address is not the same as the farcaster user address
if (
connectedAddresses[0].address ===
data.farcasterSocials.Social[0].userAddress
) {
// Remove the Farcaster user address from the addresses array
connectedAddresses = connectedAddresses.filter(
(connectedAddress) =>
connectedAddress.address !== farcasterSocials[0].userAddress
);

// Also filter out all non-evm addresses
connectedAddresses = connectedAddresses.filter((connectedAddress) =>
isAddress(connectedAddress.address)
);

if (connectedAddresses.length === 0) {
return {
displayName: null,
address: null,
Expand All @@ -160,13 +165,15 @@ export const getMinimalProfileInfoByPlatform = async (
};
}

const mainAddress =
connectedAddresses[connectedAddresses.length - 1].address;

return {
displayName: identity,
address: connectedAddresses[0].address,
description: data.farcasterSocials.Social[0].profileBio ?? null,
address: mainAddress,
description: farcasterSocials[0].profileBio ?? null,
avatar:
data.farcasterSocials.Social[0].profileImageContentValue?.image
?.small ?? null,
farcasterSocials[0].profileImageContentValue?.image?.small ?? null,
error: null,
};
}
Expand Down
10 changes: 8 additions & 2 deletions packages/dapp/src/lib/airstack/getProfileInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '@/__generated__/airstack/graphql';
import { PlatformType } from '@/utils/platform';
import { notFound } from 'next/navigation';
import { isAddress } from 'viem';

export const getProfileInfo = async (
identity: string,
Expand All @@ -25,7 +26,7 @@ export const getProfileInfo = async (
query: GetProfileInfoDocument,
variables,
}),
next: { revalidate: 86400 }, // Cache for 1 day
next: { revalidate: 86401 }, // Cache for 1 day
});

// Check if request was successful
Expand Down Expand Up @@ -70,11 +71,16 @@ export const getProfileInfo = async (
return notFound();
}

// Just in case, we remove the Farcaster address from the addresses array
// Just in case, we remove the Farcaster user address from the addresses array
data.Wallet.addresses = data.Wallet.addresses.filter(
(address) => address !== farcasterSocials[0].userAddress
);

// Also filter out all non-evm addresses
data.Wallet.addresses = data.Wallet.addresses.filter((address) =>
isAddress(address)
);

// Check if still more than address remove all except the last one
if (data.Wallet.addresses.length > 1) {
data.Wallet.addresses = [
Expand Down

0 comments on commit 712d541

Please sign in to comment.