-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathregistrationStatus.ts
More file actions
152 lines (139 loc) · 3.96 KB
/
registrationStatus.ts
File metadata and controls
152 lines (139 loc) · 3.96 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {
GetAddressRecordReturnType,
GetExpiryReturnType,
GetOwnerReturnType,
GetPriceReturnType,
GetWrapperDataReturnType,
} from '@ensdomains/ensjs/public'
import { ParsedInputResult } from '@ensdomains/ensjs/utils'
import { getChainsFromUrl } from '@app/constants/chains'
import { emptyAddress } from './constants'
/**
* Checks if a wrapped name is out of sync by comparing expiry timestamps.
* A synced wrapped name has: wrapperExpiry === registrarExpiry + gracePeriod
*/
const isWrappedNameDesynced = (
wrapperData: GetWrapperDataReturnType | undefined,
expiryData: GetExpiryReturnType | undefined,
): boolean => {
if (
!wrapperData?.expiry?.value ||
!expiryData?.expiry?.value ||
expiryData.gracePeriod === undefined
) {
return false
}
const expectedWrapperExpiry = expiryData.expiry.value + BigInt(expiryData.gracePeriod)
return wrapperData.expiry.value !== expectedWrapperExpiry
}
export type RegistrationStatus =
| 'invalid'
| 'registered'
| 'gracePeriod'
| 'premium'
| 'available'
| 'short'
| 'imported'
| 'owned'
| 'notImported'
| 'notOwned'
| 'unsupportedTLD'
| 'offChain'
| 'desynced'
| 'desynced:gracePeriod'
export const getRegistrationStatus = ({
timestamp,
validation: { isETH, is2LD, isShort, type },
ownerData,
wrapperData,
expiryData,
priceData,
addrData,
supportedTLD,
name,
}: {
timestamp: number
validation: Partial<Omit<ParsedInputResult, 'normalised' | 'isValid'>>
ownerData?: GetOwnerReturnType
wrapperData?: GetWrapperDataReturnType
expiryData?: GetExpiryReturnType
priceData?: GetPriceReturnType
addrData?: GetAddressRecordReturnType
supportedTLD?: boolean | null
name?: string
}): RegistrationStatus => {
if (name === '[root]') return 'owned'
if (isETH && is2LD && isShort) {
return 'short'
}
if (!ownerData && ownerData !== null && !wrapperData) return 'invalid'
if (!isETH && !supportedTLD) {
return 'unsupportedTLD'
}
if (isETH && is2LD) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
if (expiryData && expiryData.expiry) {
const { expiry: _expiry, gracePeriod } = expiryData
const expiry = new Date(_expiry.date)
if (expiry.getTime() > timestamp) {
// Proactive desync check: compare expiry timestamps
if (isWrappedNameDesynced(wrapperData, expiryData)) {
return 'desynced'
}
// Fallback: owner-based check (for edge cases where wrapper fully expired)
if (
ownerData &&
ownerData.owner === emptyAddress &&
ownerData.ownershipLevel === 'nameWrapper'
) {
return 'desynced'
}
return 'registered'
}
if (expiry.getTime() + gracePeriod * 1000 > timestamp) {
// Proactive desync check: compare expiry timestamps
if (isWrappedNameDesynced(wrapperData, expiryData)) {
return 'desynced:gracePeriod'
}
// Fallback: owner-based check (for edge cases)
// Will need to rethink this when we add multiple chains to manager app.
const chain = getChainsFromUrl()[0]
if (
chain &&
ownerData &&
ownerData.owner === chain.contracts.ensNameWrapper.address &&
wrapperData === null
) {
return 'desynced:gracePeriod'
}
return 'gracePeriod'
}
const { premium } = priceData || { premium: 0n }
if (premium > 0n) {
return 'premium'
}
}
return 'available'
}
if (ownerData && ownerData.owner !== emptyAddress) {
if (is2LD) {
return 'imported'
}
return 'owned'
}
if (type === 'name' && !is2LD) {
// more than 2 labels
if (addrData?.value && addrData.value !== emptyAddress) {
return 'offChain'
}
return 'notOwned'
}
if (
addrData?.value &&
addrData.value !== '0x0000000000000000000000000000000000000020' &&
addrData.value !== emptyAddress
) {
return 'imported'
}
return 'notImported'
}