Skip to content

Commit

Permalink
useApprovedNamesForMigration
Browse files Browse the repository at this point in the history
  • Loading branch information
talentlessguy committed Oct 24, 2024
1 parent f0677dd commit 30410fa
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 9 deletions.
28 changes: 28 additions & 0 deletions src/hooks/migration/useApprovedNamesForMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { erc721Abi } from 'viem'
import { usePublicClient, useReadContracts } from 'wagmi'

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

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

const { data } = useReadContracts({
contracts: names.map((name) => ({
address: getChainContractAddress({
client,
contract: name.wrappedOwner ? 'ensNameWrapper' : 'ensBaseRegistrarImplementation',
}),
abi: erc721Abi,
functionName: 'isApprovedForAll',
args: ['migrator-contract-go-here', true],
})),
multicallAddress: getChainContractAddress({ client, contract: 'multicall3' }),
})

return names.filter((d, i) => Boolean(data?.[i].result))
}
17 changes: 11 additions & 6 deletions src/pages/migrate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import { Carousel } from '@app/components/pages/migrate/Carousel'
import { MigrationNamesList, NameListTab } from '@app/components/pages/migrate/MigrationNamesList'
import { useNamesForAddress } from '@app/hooks/ensjs/subgraph/useNamesForAddress'
import { useApprovedNamesForMigration } from '@app/hooks/migration/useApprovedNamesForMigration'
import { useQueryParameterState } from '@app/hooks/useQueryParameterState'
import { makeIntroItem } from '@app/transaction-flow/intro'
import { createTransactionItem, TransactionData } from '@app/transaction-flow/transaction'
Expand Down Expand Up @@ -242,7 +243,7 @@ const tabs = ['ensv2', 'migrations', 'extension'] as const
const filter: Record<NameListTab, GetNamesForAddressParameters['filter']> = {
eligible: { owner: false, wrappedOwner: true, registrant: true, resolvedAddress: false },
ineligible: { owner: true, wrappedOwner: false, registrant: false, resolvedAddress: false },
approved: {},
approved: { owner: false, wrappedOwner: true, registrant: true, resolvedAddress: false },
}

type Tab = (typeof tabs)[number]
Expand Down Expand Up @@ -272,6 +273,8 @@ export default function Page() {
(activeNameListTab === 'ineligible' ? name.registrant !== name.owner : true),
)

const approvedNames = useApprovedNamesForMigration({ names })

return (
<>
<Main>
Expand Down Expand Up @@ -342,20 +345,22 @@ export default function Page() {
onClick={() => {
if (isConnected) {
const transactions: {
name: 'approveNameWrapper' | 'approveEthRegistrar'
data: TransactionData<'approveNameWrapper' | 'approveEthRegistrar'>
name: 'approveNameWrapperForMigration' | 'approveRegistrarForMigration'
data: TransactionData<
'approveNameWrapperForMigration' | 'approveRegistrarForMigration'
>
}[] = []

if (names.find((name) => name.wrappedOwner)) {
transactions.push(
createTransactionItem('approveNameWrapper', {
createTransactionItem('approveNameWrapperForMigration', {
address: address!,
}),
)
}
if (names.find((name) => name.relation.registrant)) {
transactions.push(
createTransactionItem('approveEthRegistrar', {
createTransactionItem('approveRegistrarForMigration', {
address: address!,
}),
)
Expand Down Expand Up @@ -467,7 +472,7 @@ export default function Page() {
<>
{isConnected ? (
<MigrationNamesList
{...{ names }}
names={activeNameListTab === 'approved' ? approvedNames : names}
activeTab={activeNameListTab}
setTab={setNameListTab}
/>
Expand Down
52 changes: 52 additions & 0 deletions src/transaction-flow/transaction/approveNameWrapperForMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { TFunction } from 'react-i18next'
import { Address, encodeFunctionData } from 'viem'

import {
getChainContractAddress,
registrySetApprovalForAllSnippet,
} from '@ensdomains/ensjs/contracts'

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

type Data = { address: Address }

const displayItems = (
{ address }: Data,
t: TFunction<'translation', undefined>,
): TransactionDisplayItem[] => [
{
label: 'address',
value: address,
type: 'address',
},
{
label: 'action',
value: t('transaction.description.approveNameWrapper'),
},
{
label: 'info',
value: t('transaction.info.approveNameWrapper'),
},
]

const transaction = async ({ client }: TransactionFunctionParameters<Data>) => {
return {
to: getChainContractAddress({
client,
contract: 'ensNameWrapper',
}),
data: encodeFunctionData({
abi: registrySetApprovalForAllSnippet,
functionName: 'setApprovalForAll',
args: [
getChainContractAddress({
client,
contract: 'migrator-contract-address-will-go-here',
}),
true,
],
}),
}
}

export default { displayItems, transaction } satisfies Transaction<Data>
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const transaction = async ({ client }: TransactionFunctionParameters<Data>) => {
args: [
getChainContractAddress({
client,
contract: 'ensBaseRegistrarImplementation',
contract: 'migrator-contract-address-will-go-here',
}),
true,
],
Expand Down
6 changes: 4 additions & 2 deletions src/transaction-flow/transaction/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import approveDnsRegistrar from './approveDnsRegistrar'
import approveEthRegistrar from './approveEthRegistrar'
import approveNameWrapper from './approveNameWrapper'
import approveNameWrapperForMigration from './approveNameWrapperForMigration'
import approveRegistrarForMigration from './approveRegistrarForMigration'
import burnFuses from './burnFuses'
import changePermissions from './changePermissions'
import claimDnsName from './claimDnsName'
Expand Down Expand Up @@ -32,7 +33,8 @@ import wrapName from './wrapName'

export const transactions = {
approveDnsRegistrar,
approveEthRegistrar,
approveNameWrapperForMigration,
approveRegistrarForMigration,
approveNameWrapper,
burnFuses,
changePermissions,
Expand Down

0 comments on commit 30410fa

Please sign in to comment.