-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use API to fetch domain names
- Loading branch information
1 parent
55c9ee3
commit eb9c8ab
Showing
19 changed files
with
160 additions
and
197 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
64 changes: 42 additions & 22 deletions
64
apps/evm/src/clients/api/queries/getAddressDomainName/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,52 @@ | ||
import { createWeb3Name } from '@web3-name-sdk/core'; | ||
import config from 'config'; | ||
import { ChainId } from 'types'; | ||
import { create, windowScheduler } from '@yornaath/batshit'; | ||
import type { ChainId } from 'types'; | ||
import { restService } from 'utilities'; | ||
import type { Address } from 'viem'; | ||
|
||
export interface GetAddressDomainNameInput { | ||
accountAddress: string; | ||
chainId: ChainId; | ||
} | ||
const getDomainNameFromApi = async ({ | ||
addresses, | ||
chainIds, | ||
}: { addresses: Address[]; chainIds?: ChainId[] }) => { | ||
const response = await restService<GetApiDomainNameResponse>({ | ||
endpoint: '/web3-domain-name', | ||
method: 'GET', | ||
params: { | ||
addresses: JSON.stringify(addresses), | ||
chainIds: JSON.stringify(chainIds), | ||
}, | ||
}); | ||
|
||
if (response.data && 'error' in response.data) { | ||
throw new Error(response.data.error); | ||
} | ||
|
||
export type GetAddressDomainNameOutput = Awaited<ReturnType<typeof web3Name.getDomainName>> | null; | ||
return response.data; | ||
}; | ||
|
||
// the client will query the VerifiedTldHub contract on Ethereum | ||
// to fetch TLD medatada | ||
const web3Name = createWeb3Name({ | ||
rpcUrl: config.rpcUrls[ChainId.ETHEREUM], | ||
const domainNamesBatcher = create({ | ||
fetcher: async (queries: { accountAddress: Address; chainId: ChainId }[]) => | ||
getDomainNameFromApi({ | ||
addresses: queries.map(q => q.accountAddress), | ||
chainIds: queries.map(q => q.chainId), | ||
}), | ||
resolver: (data, query) => data?.[query.accountAddress], | ||
scheduler: windowScheduler(10), | ||
}); | ||
|
||
const getAddressDomainName = async ({ | ||
accountAddress, | ||
chainId, | ||
}: GetAddressDomainNameInput): Promise<GetAddressDomainNameOutput> => { | ||
const rpcUrl = config.rpcUrls[chainId]; | ||
type ChainIdDomainNameMap = { [Key in ChainId]?: string | null }; | ||
type GetApiDomainNameResponse = Record<Address, ChainIdDomainNameMap>; | ||
|
||
const addressDomainName = await web3Name.getDomainName({ | ||
address: accountAddress, | ||
rpcUrl, | ||
}); | ||
export interface GetAddressDomainNameInput { | ||
accountAddress: Address; | ||
chainId: ChainId; | ||
} | ||
|
||
export type GetAddressDomainNameOutput = ChainIdDomainNameMap; | ||
|
||
const getAddressDomainName = async ({ accountAddress, chainId }: GetAddressDomainNameInput) => { | ||
const response = await domainNamesBatcher.fetch({ accountAddress, chainId }); | ||
|
||
return addressDomainName; | ||
return response || {}; | ||
}; | ||
|
||
export default getAddressDomainName; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 0 additions & 74 deletions
74
apps/evm/src/components/DomainNameOrEllipseAddress/index.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { useGetAddressDomainName } from 'clients/api'; | ||
import { Icon, Tooltip } from 'components'; | ||
import EllipseAddress, { type EllipseAddressProps } from 'components/EllipseAddress'; | ||
import { useTranslation } from 'libs/translations'; | ||
import { useChainId } from 'libs/wallet'; | ||
import { ChainId } from 'types'; | ||
import { cn, truncateAddress } from 'utilities'; | ||
import type { Address } from 'viem'; | ||
|
||
type UsernameProps = { | ||
showProvider?: boolean; | ||
showTooltip?: boolean; | ||
shouldEllipseAddress?: boolean; | ||
} & EllipseAddressProps; | ||
|
||
export const Username: React.FC<UsernameProps> = ({ | ||
address, | ||
className, | ||
showProvider = true, | ||
showTooltip = true, | ||
shouldEllipseAddress = true, | ||
...ellipseAddressProps | ||
}) => { | ||
const { t } = useTranslation(); | ||
const { chainId } = useChainId(); | ||
const { data: domainNames, isLoading: isGetAddressDomainNameLoading } = useGetAddressDomainName( | ||
{ | ||
accountAddress: address as Address, | ||
chainId, | ||
}, | ||
{ | ||
enabled: !!address, | ||
}, | ||
); | ||
|
||
const chainDomainName = domainNames?.[chainId]; | ||
// Ethereum and Sepolia use ENS, other chains use SpaceID | ||
const providerIcon = | ||
chainId === ChainId.ETHEREUM || chainId === ChainId.SEPOLIA ? 'ensLogo' : 'spaceIdLogo'; | ||
const providerText = | ||
chainId === ChainId.ETHEREUM || chainId === ChainId.SEPOLIA | ||
? t('web3DomainNames.providers.ens') | ||
: t('web3DomainNames.providers.spaceId'); | ||
|
||
let dom = shouldEllipseAddress ? ( | ||
<EllipseAddress className={className} address={address} {...ellipseAddressProps} /> | ||
) : ( | ||
<span className={className}>{address}</span> | ||
); | ||
|
||
if (!isGetAddressDomainNameLoading && chainDomainName) { | ||
dom = ( | ||
<div className={cn('flex flex-row items-center space-x-1', className)}> | ||
{showProvider && ( | ||
<Tooltip title={providerText}> | ||
<Icon className="cursor-pointer" name={providerIcon} /> | ||
</Tooltip> | ||
)} | ||
<span>{chainDomainName}</span> | ||
{showTooltip && ( | ||
<Tooltip | ||
title={ | ||
<div className="flex flex-col text-center"> | ||
<span className="text-center">{chainDomainName}</span> | ||
<span className="md:hidden"> | ||
{t('web3DomainNames.tooltip.address', { address: truncateAddress(address) })} | ||
</span> | ||
<span className="hidden md:block"> | ||
{t('web3DomainNames.tooltip.address', { address })} | ||
</span> | ||
</div> | ||
} | ||
className="inline-flex" | ||
> | ||
<Icon className="cursor-pointer" name="info" /> | ||
</Tooltip> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
return dom; | ||
}; | ||
|
||
export default Username; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.