diff --git a/modules/delegates/hooks/useHasV2VoteDelegate.ts b/modules/delegates/hooks/useHasV2VoteDelegate.ts new file mode 100644 index 000000000..5a154c055 --- /dev/null +++ b/modules/delegates/hooks/useHasV2VoteDelegate.ts @@ -0,0 +1,35 @@ +/* + +SPDX-FileCopyrightText: © 2023 Dai Foundation + +SPDX-License-Identifier: AGPL-3.0-or-later + +*/ + +import useSWR from 'swr'; +import { useContracts } from 'modules/web3/hooks/useContracts'; +import { ZERO_ADDRESS } from 'modules/web3/constants/addresses'; + +type HasV1VoteDelegateResponse = { + data?: boolean | undefined; + loading: boolean; + error: Error; + mutate: () => void; +}; + +// Returns whether the address has a v1 vote delegate contract +export const useHasV1VoteDelegate = (account?: string): HasV1VoteDelegateResponse => { + const { voteDelegateFactoryOld } = useContracts(); + const { data, error, mutate } = useSWR(account ? `${account}/has-v1-vote-delegate-address` : null, async () => { + if (!account) return false; + const newVdAddress = await voteDelegateFactoryOld.delegates(account); + + return newVdAddress !== ZERO_ADDRESS; + }); + return { + data, + loading: !error && !data, + error, + mutate + }; +}; diff --git a/pages/migration/delegate.tsx b/pages/migration/delegate.tsx index 6e023568b..66d2ff62c 100644 --- a/pages/migration/delegate.tsx +++ b/pages/migration/delegate.tsx @@ -22,6 +22,7 @@ import { ConnectWallet } from 'modules/migration/components/ConnectWallet'; import { NewDelegateContract } from 'modules/migration/components/NewDelegateContract'; import { sign } from 'modules/web3/helpers/sign'; import { useLinkedDelegateInfo } from 'modules/migration/hooks/useLinkedDelegateInfo'; +import { useHasV1VoteDelegate } from 'modules/delegates/hooks/useHasV2VoteDelegate'; export default function DelegateMigrationPage(): React.ReactElement { const { account, provider } = useWeb3(); @@ -38,7 +39,9 @@ export default function DelegateMigrationPage(): React.ReactElement { latestOwnerHasDelegateContract } = useLinkedDelegateInfo(); - const connectedAddressFound = !!originalOwnerAddress || !!latestOwnerAddress; + //if latest is a v1 delegate contract, then they still haven't started the v2 linking process + const { data: latestIsV1Delegate } = useHasV1VoteDelegate(latestOwnerAddress); + const connectedAddressFound = (!!originalOwnerAddress || !!latestOwnerAddress) && !latestIsV1Delegate; // the user should be shown the steps to take action if: // a - the connected account needs to migrate to v2 @@ -53,7 +56,7 @@ export default function DelegateMigrationPage(): React.ReactElement { const getCurrentStep = useMemo((): string => { // delegate contract is v1 and we don't have - // a request to migrate the address yet, show migration info + // a request to migrate the address to v2 yet, show migration info if ( (isDelegateV1Contract) && !connectedAddressFound && @@ -72,7 +75,7 @@ export default function DelegateMigrationPage(): React.ReactElement { return STEPS.NEW_ADDRESS; } - // delegate contract is either expired or expiring or needs to migrate to v2 + // delegate contract needs to migrate to v2 // and we have processed the request to migrate // but user is connected with old address if (