-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathgenerateWrappedSubname.ts
More file actions
102 lines (88 loc) · 2.61 KB
/
generateWrappedSubname.ts
File metadata and controls
102 lines (88 loc) · 2.61 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
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-await-in-loop */
import { EncodeFusesInputObject, RecordOptions } from '@ensdomains/ensjs/utils'
import { createSubname, unwrapName } from '@ensdomains/ensjs/wallet'
import { Accounts, User } from '../../accounts'
import {
publicClient,
testClient,
waitForTransaction,
walletClient,
} from '../../contracts/utils/addTestContracts'
import { generateRecords } from './generateRecords'
// type Fuse = ParentFuses['fuse'] | ChildFuses['fuse']
export type WrappedSubname = {
name: string
nameOwner: User
label: string
owner: User
resolver?: `0x${string}`
records?: RecordOptions
fuses?: EncodeFusesInputObject
duration?: number
type?: 'wrapped' | 'legacy'
subnames?: Omit<WrappedSubname, 'name' | 'nameOwner '>[]
}
type Dependencies = {
accounts: Accounts
}
const DEFAULT_RESOLVER = testClient.chain.contracts.ensPublicResolver.address
export const generateWrappedSubname =
({ accounts }: Dependencies) =>
async ({
name,
nameOwner,
label,
owner,
resolver = DEFAULT_RESOLVER,
records,
fuses,
duration = 31536000,
subnames,
type,
}: WrappedSubname) => {
const subname = `${label}.${name}`
console.log('generating wrapped subname:', subname)
const blockTimestamp = Number((await publicClient.getBlock()).timestamp)
const expiry = duration + blockTimestamp
// Make subname with resolver
const account = accounts.getAccountForUser(nameOwner)
const tx = await createSubname(walletClient, {
name: `${label}.${name}`,
contract: 'nameWrapper',
fuses,
owner: accounts.getAddress(owner) as `0x${string}`,
resolverAddress: resolver,
expiry,
account,
// @ts-expect-error
nonceManager: account.nonceManager,
})
await waitForTransaction(tx)
// Make records
if (records) {
await generateRecords({ accounts })({
name: `${label}.${name}`,
owner,
resolver,
records,
})
}
if (type === 'legacy') {
const wrapTx = await unwrapName(walletClient, {
name: `${label}.${name}`,
newOwnerAddress: accounts.getAddress(owner) as `0x${string}`,
account: accounts.getAccountForUser(owner),
})
await waitForTransaction(wrapTx)
}
const _subNames = (subnames || []).map((subName) => ({
...subName,
name: `${label}.${name}`,
nameOwner: owner,
resolver: subName.resolver ?? DEFAULT_RESOLVER,
}))
for (const eachSubname of _subNames) {
await generateWrappedSubname({ accounts })({ ...eachSubname })
}
}