-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathgenerateLegacySubname.ts
More file actions
122 lines (114 loc) · 3.6 KB
/
generateLegacySubname.ts
File metadata and controls
122 lines (114 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-await-in-loop */
import {
getChainContractAddress,
registrySetApprovalForAllSnippet,
} from '@ensdomains/ensjs/contracts'
import { RecordOptions } from '@ensdomains/ensjs/utils'
import { createSubname, wrapName } from '@ensdomains/ensjs/wallet'
import { Accounts, User } from '../../accounts'
import {
testClient,
waitForTransaction,
walletClient,
} from '../../contracts/utils/addTestContracts'
import { generateRecords } from './generateRecords'
export type LegacySubname = {
name: string
nameOwner: User
label: string
owner?: User
resolver?: `0x${string}`
records?: RecordOptions
duration?: number
type?: 'wrapped' | 'legacy'
subnames?: Omit<LegacySubname, 'name' | 'nameOwner'>[]
}
type Dependencies = {
accounts: Accounts
}
// const DEFAULT_RESOLVER = RESOLVER_ADDRESSES['1337'][2] as `0x${string}`
const DEFAULT_RESOLVER = testClient.chain.contracts.legacyPublicResolver.address
export const generateLegacySubname =
({ accounts }: Dependencies) =>
async ({
name,
nameOwner,
label,
owner = nameOwner,
resolver,
records,
type,
subnames,
}: LegacySubname) => {
const subname = `${label}.${name}`
console.log('generating legacy subname:', subname)
const nameOwnerAccount = accounts.getAccountForUser(nameOwner)
const tx = await createSubname(walletClient, {
name: `${label}.${name}`,
contract: 'registry',
owner: accounts.getAddress(owner) as `0x${string}`,
resolverAddress: resolver ?? DEFAULT_RESOLVER,
account: nameOwnerAccount,
// @ts-expect-error
nonceManager: nameOwnerAccount.nonceManager,
})
await waitForTransaction(tx)
// Make records
if (records && resolver) {
await generateRecords({ accounts })({
name: subname,
owner,
resolver,
records,
})
}
if (type === 'wrapped') {
const ownerAccount = accounts.getAccountForUser(owner)
const approveTx = await walletClient.writeContract({
abi: registrySetApprovalForAllSnippet,
address: getChainContractAddress({
client: walletClient,
contract: 'ensRegistry',
}),
functionName: 'setApprovalForAll',
args: [
getChainContractAddress({
client: walletClient,
contract: 'ensNameWrapper',
}),
true,
],
account: ownerAccount,
// @ts-expect-error
nonceManager: ownerAccount.nonceManager,
})
const approve = await waitForTransaction(approveTx)
if (approve.status === 'success') console.log('approved name wrapper')
else throw new Error(`failed to approve name wrapper`)
const wrapTx = await wrapName(walletClient, {
name: subname,
newOwnerAddress: ownerAccount.address,
resolverAddress: getChainContractAddress({
client: walletClient,
contract: 'ensPublicResolver',
}),
account: ownerAccount,
// @ts-expect-error
nonceManager: ownerAccount.nonceManager,
})
const wrap = await waitForTransaction(wrapTx)
if (wrap.status === 'success') console.log('wrapped subname:', subname)
else throw new Error(`failed to wrap subname: ${subname}`)
}
// Create subnames
const _subnames = (subnames || []).map((_subname) => ({
..._subname,
name: `${label}.${name}`,
nameOwner: owner,
resolver: _subname.resolver ?? DEFAULT_RESOLVER,
}))
for (const eachSubname of _subnames) {
await generateLegacySubname({ accounts })(eachSubname)
}
}