Skip to content

Commit

Permalink
wire up MigrationHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
talentlessguy committed Oct 28, 2024
1 parent adb9375 commit 1f2bbbe
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/components/pages/migrate/MigrationNamesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,16 @@ const MigrationName = ({ name, t }: { name: NameWithRelation; t: TFunction }) =>
)
}

export const MigrationNamesList = ({
export const MigrationNamesList = <T extends NameListTab>({
activeTab,
setTab,
names,
tabs,
}: {
activeTab: NameListTab
activeTab: T
names: NameWithRelation[]
setTab: (tab: NameListTab) => void
tabs: NameListTab[]
setTab: (tab: T) => void
tabs: T[]
}) => {
const { t } = useTranslation('migrate')

Expand Down
35 changes: 25 additions & 10 deletions src/components/pages/migrate/MigrationTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,15 @@ const LandingTab = ({
)
}

type MigrationHelperTab = Exclude<NameListTab, 'claimed'>

const filterNames = (names: NameWithRelation[]) => {
const eligibleNames: NameWithRelation[] = []
const inelegibleNames: NameWithRelation[] = []

for (const name of names) {
for (const name of names.filter(
({ expiryDate }) => expiryDate?.date && expiryDate?.date > new Date(),
)) {
if (name.parentName === 'eth' && (name.relation.wrappedOwner || name.relation.registrant)) {
eligibleNames.push(name)
} else {
Expand All @@ -344,7 +348,7 @@ const filterTabs = ({
eligibleNames: NameWithRelation[]
inelegibleNames: NameWithRelation[]
}) => {
const tabs: ('eligible' | 'ineligible' | 'approved')[] = []
const tabs: MigrationHelperTab[] = []

if (eligibleNames.length) {
tabs.push('eligible')
Expand Down Expand Up @@ -380,10 +384,16 @@ const MigrationsTab = ({
inelegibleNames: NameWithRelation[]
approvedNames: NameWithRelation[]
}) => {
const tabs = filterTabs({ approvedNames, eligibleNames, inelegibleNames })

const { createTransactionFlow } = useTransactionFlow()
const [activeNameListTab, setNameListTab] = useState<NameListTab>('eligible')
const [activeNameListTab, setNameListTab] = useState<MigrationHelperTab>(tabs[0])

const tabs = filterTabs({ approvedNames, eligibleNames, inelegibleNames })
const names: Record<MigrationHelperTab, NameWithRelation[]> = {
eligible: eligibleNames,
ineligible: inelegibleNames,
approved: approvedNames,
}

return (
<>
Expand Down Expand Up @@ -441,12 +451,12 @@ const MigrationsTab = ({
<Button colorStyle="greenSecondary">{t('cta.learn-more')}</Button>
</ButtonContainer>
</Header>
{approvedNames.length === eligibleNames.length ? (
{approvedNames.length !== 0 && approvedNames.length === eligibleNames.length ? (
<AllNamesAreApprovedBanner>{t('banner.all-approved')}</AllNamesAreApprovedBanner>
) : null}
{isConnected ? (
<MigrationNamesList
names={activeNameListTab === 'eligible' ? eligibleNames : inelegibleNames}
names={names[activeNameListTab]}
activeTab={activeNameListTab}
setTab={setNameListTab}
tabs={tabs}
Expand Down Expand Up @@ -510,9 +520,9 @@ const ExtensionTab = ({
filter: { owner: false, wrappedOwner: true, registrant: true, resolvedAddress: false },
})

const names = infiniteData.filter((name) => name.parentName === 'eth')
const names = infiniteData.filter((name) => name.parentName === 'eth' && name.expiryDate)

const approvedNames = useApprovedNamesForMigration({ names })
const approvedNames = useApprovedNamesForMigration({ names, owner: address })

const allNamesAreApproved = approvedNames.length !== 0 && approvedNames.length === names.length

Expand Down Expand Up @@ -574,9 +584,14 @@ export const MigrationTab = ({
filter: { registrant: true, owner: true, resolvedAddress: true, wrappedOwner: true },
})

const { eligibleNames, inelegibleNames } = filterNames(names)
const { eligibleNames: initialEligibleNames, inelegibleNames } = filterNames(names)

const approvedNames = useApprovedNamesForMigration({
names: initialEligibleNames,
owner: address,
})

const approvedNames = useApprovedNamesForMigration({ names: eligibleNames })
const eligibleNames = initialEligibleNames.filter((name) => !approvedNames.includes(name))

return match(tab)
.with('ensv2', () => (
Expand Down
8 changes: 6 additions & 2 deletions src/hooks/migration/useApprovedNamesForMigration.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { erc721Abi } from 'viem'
import { Address, erc721Abi } from 'viem'
import { usePublicClient, useReadContracts } from 'wagmi'

import { getChainContractAddress } from '@ensdomains/ensjs/contracts'
import { NameWithRelation } from '@ensdomains/ensjs/subgraph'

import { migrationHelperContract } from '@app/migrationHelper/migrationHelper'

export const useApprovedNamesForMigration = ({
names,
owner,
}: {
names: NameWithRelation[]
owner?: Address
}): NameWithRelation[] => {
const client = usePublicClient()

Expand All @@ -19,7 +23,7 @@ export const useApprovedNamesForMigration = ({
}),
abi: erc721Abi,
functionName: 'isApprovedForAll',
args: ['contract-go-here', true],
args: [owner, migrationHelperContract[client.chain.id]],
})),
multicallAddress: getChainContractAddress({ client, contract: 'multicall3' }),
})
Expand Down
9 changes: 9 additions & 0 deletions src/migrationHelper/migrationHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { goerli, holesky, localhost, mainnet, sepolia } from 'viem/chains'

export const migrationHelperContract = {
[mainnet.id]: '0xnot-deployed-yet',
[localhost.id]: '0xnot-deployed-yet',
[goerli.id]: '0xwillnotdeploy',
[holesky.id]: '0x76aafA281Ed5155f83926a12ACB92e237e322A8C',
[sepolia.id]: '0xf9c8c83adda8d52d9284cdbef23da10b5f9869bf',
} as const
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
registrySetApprovalForAllSnippet,
} from '@ensdomains/ensjs/contracts'

import { migrationHelperContract } from '@app/migrationHelper/migrationHelper'
import { Transaction, TransactionDisplayItem, TransactionFunctionParameters } from '@app/types'

type Data = { address: Address }
Expand Down Expand Up @@ -38,13 +39,7 @@ const transaction = async ({ client }: TransactionFunctionParameters<Data>) => {
data: encodeFunctionData({
abi: registrySetApprovalForAllSnippet,
functionName: 'setApprovalForAll',
args: [
getChainContractAddress({
client,
contract: 'contract-address-will-go-here',
}),
true,
],
args: [migrationHelperContract[client.chain.id], true],
}),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
registrySetApprovalForAllSnippet,
} from '@ensdomains/ensjs/contracts'

import { migrationHelperContract } from '@app/migrationHelper/migrationHelper'
import type { Transaction, TransactionDisplayItem, TransactionFunctionParameters } from '@app/types'

type Data = { address: Address }
Expand Down Expand Up @@ -39,13 +40,7 @@ const transaction = async ({ client }: TransactionFunctionParameters<Data>) => {
data: encodeFunctionData({
abi: registrySetApprovalForAllSnippet,
functionName: 'setApprovalForAll',
args: [
getChainContractAddress({
client,
contract: 'address-will-go-here',
}),
true,
],
args: [migrationHelperContract[client.chain.id], true],
}),
}
}
Expand Down

0 comments on commit 1f2bbbe

Please sign in to comment.