Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use reverse resolution to fetch main ENS name from address #190

Merged
merged 3 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 9 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 { HAUS_NETWORK_DATA, ValidNetwork } from '@daohaus/keychain-utils';

export const getProfileStore = async () =>
(await getlocalForage(CacheStoreName.MEMBERS_PROFILE)) as ArbitraryState;
Expand Down Expand Up @@ -60,11 +61,17 @@ 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);
// Workaround when poiting to a network where ENS is not deployed
const daochain = !['0x1', '0x5'].includes(chainId) ? '0x1' : chainId;
const profile = await getProfileForAddress(
address,
HAUS_NETWORK_DATA[daochain]?.rpc
);
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
10 changes: 9 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,15 @@ export const loadWallet = async ({

export const loadProfile = async ({
address,
chainId,
setProfile,
setProfileLoading,
shouldUpdate,
lifeCycleFns,
networks,
}: {
address: string;
chainId: ValidNetwork;
setProfile: Dispatch<SetStateAction<UserProfile>>;
setProfileLoading: Dispatch<SetStateAction<boolean>>;
shouldUpdate: boolean;
Expand All @@ -149,7 +152,12 @@ export const loadProfile = async ({
}) => {
try {
setProfileLoading(true);
const profile = await getProfileForAddress(address);
// Workaround when poiting to a network where ENS is not deployed
const daochain = !['0x1', '0x5'].includes(chainId) ? '0x1' : chainId;
const profile = await getProfileForAddress(
address,
networks[daochain]?.rpc
);

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,3 +1,4 @@
import { providers } from 'ethers';
import { graphFetchList } from '@daohaus/data-fetch-utils';
import { AccountProfile } from '@daohaus/utils';
import {
Expand All @@ -14,18 +15,45 @@ import { ENSDomain, LensProfile } from './types';
import { transformProfile } from './utils';

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

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

const getENSReverseResolver = async ({
address,
rpcUri,
}: {
address: string;
rpcUri: string;
}) => {
try {
const provider = new providers.JsonRpcProvider(rpcUri);
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