diff --git a/packages/messenger-widget/src/components/ConfigureProfile/chain/ens/ConfigureEnsProfile.tsx b/packages/messenger-widget/src/components/ConfigureProfile/chain/ens/ConfigureEnsProfile.tsx index d883974c2..b000cc449 100644 --- a/packages/messenger-widget/src/components/ConfigureProfile/chain/ens/ConfigureEnsProfile.tsx +++ b/packages/messenger-widget/src/components/ConfigureProfile/chain/ens/ConfigureEnsProfile.tsx @@ -6,7 +6,7 @@ import { AuthContext } from '../../../../context/AuthContext'; import { ConfigureProfileContext } from '../../context/ConfigureProfileContext'; import { SubmitOnChainProfile } from '../SubmitOnChainProfile'; import { IChain, NAME_TYPE, validateEnsName } from '../common'; -import { submitEnsNameTransaction } from './bl'; +import { fetchExistingEnsName, submitEnsNameTransaction } from './bl'; import { ModalContext } from '../../../../context/ModalContext'; import { ConfigureDM3NameContext } from '../../context/ConfigureDM3NameContext'; import { fetchChainIdFromServiceName } from '../../bl'; @@ -101,6 +101,18 @@ export const ConfigureEnsProfile = (props: IChain) => { } }, [connectedChainId]); + // Fetches existing ENS name if exists + useEffect(() => { + const fetchEnsName = async () => { + if (ethAddress) { + setExistingEnsName( + await fetchExistingEnsName(ethAddress, provider), + ); + } + }; + fetchEnsName(); + }, []); + // on change of dropdown selected, error vanishes useEffect(() => { onShowError(undefined, ''); diff --git a/packages/messenger-widget/src/components/ConfigureProfile/chain/ens/bl.ts b/packages/messenger-widget/src/components/ConfigureProfile/chain/ens/bl.ts index 82c8df80f..8089ed35b 100644 --- a/packages/messenger-widget/src/components/ConfigureProfile/chain/ens/bl.ts +++ b/packages/messenger-widget/src/components/ConfigureProfile/chain/ens/bl.ts @@ -150,3 +150,15 @@ export async function getPublishProfileOnchainTransaction( args: [node, key, value], }; } + +export const fetchExistingEnsName = async ( + address: string, + provider: ethers.providers.StaticJsonRpcProvider, +): Promise => { + try { + return await provider.lookupAddress(address); + } catch (error) { + console.log('Failed to query ENS name : ', error); + return null; + } +}; diff --git a/packages/messenger-widget/src/components/ConfigureProfile/chain/genome/ConfigureGenomeProfile.tsx b/packages/messenger-widget/src/components/ConfigureProfile/chain/genome/ConfigureGenomeProfile.tsx index 5db247a28..db7775802 100644 --- a/packages/messenger-widget/src/components/ConfigureProfile/chain/genome/ConfigureGenomeProfile.tsx +++ b/packages/messenger-widget/src/components/ConfigureProfile/chain/genome/ConfigureGenomeProfile.tsx @@ -1,6 +1,10 @@ import { useContext, useEffect, useState } from 'react'; import { SubmitOnChainProfile } from '../SubmitOnChainProfile'; -import { submitGenomeNameTransaction, validateGenomeName } from './bl'; +import { + fetchExistingGnoName, + submitGenomeNameTransaction, + validateGenomeName, +} from './bl'; import { ConfigureProfileContext } from '../../context/ConfigureProfileContext'; import { useChainId, useSwitchNetwork } from 'wagmi'; import { AuthContext } from '../../../../context/AuthContext'; @@ -10,6 +14,7 @@ import { ModalContext } from '../../../../context/ModalContext'; import { DM3ConfigurationContext } from '../../../../context/DM3ConfigurationContext'; import { ConfigureDM3NameContext } from '../../context/ConfigureDM3NameContext'; import { fetchChainIdFromServiceName } from '../../bl'; +import { supportedChains } from '../../../../utils/common-utils'; export const ConfigureGenomeProfile = (props: IChain) => { const connectedChainId = useChainId(); @@ -105,6 +110,21 @@ export const ConfigureGenomeProfile = (props: IChain) => { setEnsName(''); }, [namingServiceSelected]); + // Fetches existing GNO name if exists + useEffect(() => { + const fetchGnoName = async () => { + if (ethAddress) { + setExistingEnsName( + await fetchExistingGnoName( + ethAddress, + supportedChains.gnosisMainnet, + ), + ); + } + }; + fetchGnoName(); + }, []); + const propertyName = 'GNO Name'; const label = diff --git a/packages/messenger-widget/src/components/ConfigureProfile/chain/genome/bl.ts b/packages/messenger-widget/src/components/ConfigureProfile/chain/genome/bl.ts index f1d365ebf..9a36ef3bb 100644 --- a/packages/messenger-widget/src/components/ConfigureProfile/chain/genome/bl.ts +++ b/packages/messenger-widget/src/components/ConfigureProfile/chain/genome/bl.ts @@ -1,7 +1,6 @@ import { createAlias } from '@dm3-org/dm3-lib-delivery-api'; import { Account, SignedUserProfile } from '@dm3-org/dm3-lib-profile'; -import { ethersHelper, stringify } from '@dm3-org/dm3-lib-shared'; -import { getConractInstance } from '@dm3-org/dm3-lib-shared/dist/ethersHelper'; +import { ethersHelper, log, stringify } from '@dm3-org/dm3-lib-shared'; import { ethers } from 'ethers'; import { closeLoader, startLoader } from '../../../Loader/Loader'; import { Address, namehash, toHex } from 'viem'; @@ -161,3 +160,19 @@ export const submitGenomeNameTransaction = async ( export const validateGenomeName = (ensName: string) => { return ensName.endsWith('.gno'); }; + +export const fetchExistingGnoName = async ( + address: string, + chainId: number, +): Promise => { + try { + const web3Name = createWeb3Name(); + return await web3Name.getDomainName({ + address, + queryChainIdList: [chainId], + }); + } catch (error) { + console.log('Failed to query GNO name : ', error); + return null; + } +};