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

feat: add ENS resolution and update dependencies #1738

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,6 @@
"cross-spawn@<7.0.5": ">=7.0.5",
"nanoid@<3.3.8": "3.3.8"
}
}
},
"packageManager": "[email protected]+sha512.1acb565e6193efbebda772702950469150cf12bcc764262e7587e71d19dc98a423dff9536e57ea44c49bdf790ff694e83c27be5faa23d67e0c033b583be4bfcf"
}
2 changes: 2 additions & 0 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"dayjs": "1.11.10",
"dexie": "4.0.9",
"dexie-observable": "4.0.1-beta.13",
"ethers": "5.7.2",
"events": "3.3.0",
"fake-indexeddb": "4.0.2",
"framer-motion": "10.16.4",
Expand All @@ -57,6 +58,7 @@
"react-qr-code": "2.0.12",
"react-router-dom": "6.26.2",
"tai64": "1.0.0",
"uuid": "^9.0.1",
"vite-plugin-markdown": "2.2.0",
"xstate": "4.38.2",
"yup": "1.4.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { ThemeUtilsCSS } from '@fuel-ui/css';
import { cssObj } from '@fuel-ui/css';
import { Box, Copyable, Text, Tooltip } from '@fuel-ui/react';
import { Avatar, Box, Copyable, Text, Tooltip } from '@fuel-ui/react';
import { bn } from 'fuels';
import { isValidEthAddress, shortAddress } from '~/systems/Core';
import { useEns } from '../../hooks/useEns';

export type EthAddressProps = {
address: string;
Expand All @@ -12,26 +13,50 @@ export type EthAddressProps = {
export const EthAddress = ({ address, css }: EthAddressProps) => {
const isValidAddress = isValidEthAddress(address);
const ethAddress = isValidAddress ? bn(address).toHex(20) : '';
const { name: ensName, avatar: ensAvatar, loading } = useEns(ethAddress);

return (
<Box.Flex css={styles.root}>
<Box.Flex css={{ ...styles.root, ...css }} gap="$2" align="center">
{ensAvatar ? (
<Box css={styles.avatar}>
<img
src={ensAvatar}
alt={ensName || address}
style={{ width: '100%', height: '100%', borderRadius: '50%' }}
/>
</Box>
) : (
<Avatar.Generated
size="sm"
hash={ethAddress}
aria-label={ensName || address}
/>
)}
<Copyable value={address} aria-label={address}>
<Tooltip content={address} className="address_tooltip" side="top">
<Text css={css}>{shortAddress(ethAddress)}</Text>
<Text>
{loading ? 'Loading...' : ensName || shortAddress(ethAddress)}
</Text>
</Tooltip>
</Copyable>
</Box.Flex>
);
};

const styles = {
root: {
'.address_tooltip': cssObj({
root: cssObj({
'.address_tooltip': {
fontSize: '$xs',
lineHeight: '$4',
maxWidth: 125,
textAlign: 'center',
wordWrap: 'break-word',
}),
},
},
}),
avatar: cssObj({
width: '24px',
height: '24px',
borderRadius: '$full',
overflow: 'hidden',
}),
};
4 changes: 4 additions & 0 deletions packages/app/src/systems/Account/hooks/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export * from './useAccounts';
export * from './useAccountForm';
export * from './useAccountFormName';
export * from './useAddAccount';
export * from './useEditAccount';
export * from './useExplorerLink';
export * from './useExportAccount';
export * from './useImportAccount';
export * from './useImportAccountForm';
export * from './useEns';
72 changes: 72 additions & 0 deletions packages/app/src/systems/Account/hooks/useEns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { ethers } from 'ethers';
import { useEffect, useState } from 'react';
import { isValidEthAddress } from '~/systems/Core';

export type EnsData = {
name: string | null;
avatar: string | null;
loading: boolean;
error: Error | null;
};

export function useEns(address?: string) {
const [ensData, setEnsData] = useState<EnsData>({
name: null,
avatar: null,
loading: false,
error: null,
});

useEffect(() => {
async function resolveEns() {
if (!address || !isValidEthAddress(address)) {
setEnsData({
name: null,
avatar: null,
loading: false,
error: null,
});
return;
}

setEnsData((prev) => ({ ...prev, loading: true, error: null }));

try {
// Using Ethereum mainnet for ENS resolution
const provider = new ethers.providers.JsonRpcProvider(
'https://mainnet.infura.io/v3/84842078b09946638c03157f83405213'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

);

// Get ENS name for the address (reverse lookup)
const name = await provider.lookupAddress(address);

// Get avatar if ENS name exists
let avatar = null;
if (name) {
const resolver = await provider.getResolver(name);
if (resolver) {
avatar = await resolver.getText('avatar');
}
}

setEnsData({
name,
avatar,
loading: false,
error: null,
});
} catch (error) {
setEnsData({
name: null,
avatar: null,
loading: false,
error: error as Error,
});
}
}

resolveEns();
}, [address]);

return ensData;
}
Loading