Skip to content

Commit

Permalink
use reverse resolution to fetch main ENS name from address
Browse files Browse the repository at this point in the history
  • Loading branch information
santteegt committed Jan 31, 2023
1 parent 21f8df9 commit 322870a
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 17 deletions.
2 changes: 1 addition & 1 deletion apps/admin/src/components/MemberProfileAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const MemberProfileAvatar = ({

const fetchMemberProfile = useCallback(
async (address: string, setter: typeof setMemberProfile) => {
const profile = await fetchProfile(address);
const profile = await fetchProfile(address, daochain);
setter(profile);
},
[]
Expand Down
4 changes: 2 additions & 2 deletions apps/admin/src/components/ProposalCardOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
charLimit,
formatShortDateTimeFromSeconds,
} from '@daohaus/utils';
import { Keychain } from '@daohaus/keychain-utils';
import { Keychain, ValidNetwork } from '@daohaus/keychain-utils';

import { MolochV3Proposal } from '@daohaus/moloch-v3-data';
import {
Expand Down Expand Up @@ -80,7 +80,7 @@ export const ProposalCardOverview = ({

const fetchMemberProfile = useCallback(
async (address: string, setter: typeof setSubmitterProfile) => {
const profile = await fetchProfile(address);
const profile = await fetchProfile(address, daochain as ValidNetwork);
setter(profile);
},
[]
Expand Down
4 changes: 2 additions & 2 deletions apps/admin/src/pages/Member.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
AccountProfile,
} from '@daohaus/utils';

import { Keychain } from '@daohaus/keychain-utils';
import { Keychain, ValidNetwork } from '@daohaus/keychain-utils';

import { ButtonRouterLink } from '../components/ButtonRouterLink';
import { DaoTable } from '../components/DaohausTable';
Expand Down Expand Up @@ -126,7 +126,7 @@ export function Member() {
const fetchMemberProfile = useCallback(
async (address: string, loadState: typeof setCurrentMemberLoading) => {
loadState(true);
const profile = await fetchProfile(address);
const profile = await fetchProfile(address, daochain as ValidNetwork);
setCurrentProfile(profile);
loadState(false);
},
Expand Down
6 changes: 4 additions & 2 deletions apps/admin/src/utils/cacheProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getlocalForage,
} from '@daohaus/utils';
import { getProfileForAddress } from '@daohaus/profile-data';
import { ValidNetwork } from '@daohaus/keychain-utils';

export const getProfileStore = async () =>
(await getlocalForage(CacheStoreName.MEMBERS_PROFILE)) as ArbitraryState;
Expand Down Expand Up @@ -60,11 +61,12 @@ export const cacheProfile = async ({
};

export const fetchProfile = async (
address: string
address: string,
chainId: ValidNetwork
): Promise<AccountProfile> => {
const cachedProfile = await getCachedProfile({ address });
if (cachedProfile) return cachedProfile;
const profile = await getProfileForAddress(address);
const profile = await getProfileForAddress(address, chainId);
cacheProfile({
address,
profile,
Expand Down
5 changes: 3 additions & 2 deletions libs/connect-context/src/ConnectContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ export const ConnectProvider = ({

useEffect(() => {
let shouldUpdate = true;
if (address && isConnected && address !== profile?.address) {
if (address && chainId && isConnected && address !== profile?.address) {
loadProfile({
address,
chainId,
setProfile,
setProfileLoading,
shouldUpdate,
Expand All @@ -118,7 +119,7 @@ export const ConnectProvider = ({
return () => {
shouldUpdate = false;
};
}, [address, isConnected, networks, lifeCycleFns, profile]);
}, [address, chainId, isConnected, networks, lifeCycleFns, profile]);

const connectWallet = useCallback(async () => {
handleConnectWallet({
Expand Down
4 changes: 3 additions & 1 deletion libs/connect-context/src/utils/contextHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,14 @@ export const loadWallet = async ({

export const loadProfile = async ({
address,
chainId,
setProfile,
setProfileLoading,
shouldUpdate,
lifeCycleFns,
}: {
address: string;
chainId: ValidNetwork;
setProfile: Dispatch<SetStateAction<UserProfile>>;
setProfileLoading: Dispatch<SetStateAction<boolean>>;
shouldUpdate: boolean;
Expand All @@ -149,7 +151,7 @@ export const loadProfile = async ({
}) => {
try {
setProfileLoading(true);
const profile = await getProfileForAddress(address);
const profile = await getProfileForAddress(address, chainId);

if (profile && shouldUpdate) {
const displayName =
Expand Down
36 changes: 32 additions & 4 deletions libs/profile-data/src/profile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { providers } from 'ethers';
import { graphFetchList } from '@daohaus/data-fetch-utils';
import { HAUS_RPC, ValidNetwork } from '@daohaus/keychain-utils';
import { AccountProfile } from '@daohaus/utils';
import {
ListActiveDomainsDocument,
Expand All @@ -14,18 +16,44 @@ import { ENSDomain, LensProfile } from './types';
import { transformProfile } from './utils';

export const getProfileForAddress = async (
address: string
address: string,
chainId: ValidNetwork
): Promise<AccountProfile> => {
const ensDomain = await getENSDomain({
address,
});
const reverseEnsRecord = await getENSReverseResolver({ address, chainId });
const ensDomain = reverseEnsRecord
? reverseEnsRecord
: await getENSDomain({
address,
});
const lensProfile = await getLensProfile({
memberAddress: address,
} as ListProfileQueryVariables);

return transformProfile({ address, lensProfile, ensDomain });
};

const getENSReverseResolver = async ({
address,
chainId
}: {
address: string;
chainId: ValidNetwork;
}) => {
try {
const provider = new providers.JsonRpcProvider(HAUS_RPC[chainId]);
const domainName = await provider.lookupAddress(address);
if (domainName) {
return {
domain: {
name: domainName,
},
};
}
} catch (error) {
console.error('Unable to fetch ENS reverse record', error);
}
};

const getENSDomain = async ({
address,
now = (new Date().getTime() / 1000).toFixed(0),
Expand Down
6 changes: 3 additions & 3 deletions libs/profile-data/src/types/ens.types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export type ENSDomain =
| {
id: string;
registrationDate: string;
expiryDate: string;
id?: string;
registrationDate?: string;
expiryDate?: string;
domain?: {
name?: string;
};
Expand Down

0 comments on commit 322870a

Please sign in to comment.