-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathregistration.ts
More file actions
316 lines (292 loc) · 10.5 KB
/
registration.ts
File metadata and controls
316 lines (292 loc) · 10.5 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import { type ResolveCursorConnectionArgs, resolveCursorConnection } from "@pothos/plugin-relay";
import { and, eq } from "drizzle-orm";
import { hexToBigInt } from "viem";
import * as schema from "@ensnode/ensdb-sdk";
import {
type ENSv1DomainId,
isRegistrationFullyExpired,
isRegistrationInGracePeriod,
type RegistrationId,
type RequiredAndNotNull,
} from "@ensnode/ensnode-sdk";
import { builder } from "@/graphql-api/builder";
import { orderPaginationBy, paginateByInt } from "@/graphql-api/lib/connection-helpers";
import { getModelId } from "@/graphql-api/lib/get-model-id";
import { lazyConnection } from "@/graphql-api/lib/lazy-connection";
import { AccountIdRef } from "@/graphql-api/schema/account-id";
import { INDEX_PAGINATED_CONNECTION_ARGS } from "@/graphql-api/schema/constants";
import { DomainInterfaceRef } from "@/graphql-api/schema/domain";
import { EventRef } from "@/graphql-api/schema/event";
import { RenewalRef } from "@/graphql-api/schema/renewal";
import { db } from "@/lib/db";
export const RegistrationInterfaceRef = builder.loadableInterfaceRef("Registration", {
load: (ids: RegistrationId[]) =>
db.query.registration.findMany({
where: (t, { inArray }) => inArray(t.id, ids),
}),
toKey: getModelId,
cacheResolved: true,
sort: true,
});
export type Registration = Exclude<typeof RegistrationInterfaceRef.$inferType, RegistrationId>;
export type RegistrationInterface = Pick<
Registration,
| "id"
| "type"
| "index"
| "domainId"
| "expiry"
| "registrarChainId"
| "registrarAddress"
| "registrantId"
| "referrer"
>;
export type NameWrapperRegistration = RequiredAndNotNull<Registration, "fuses">;
export type BaseRegistrarRegistration = RequiredAndNotNull<
Registration,
"gracePeriod" | "wrapped" | "fuses"
> & {
baseCost: bigint | null;
premium: bigint | null;
};
export type ThreeDNSRegistration = Registration;
export type ENSv2RegistryRegistration = Registration;
RegistrationInterfaceRef.implement({
description:
"A Registration represents a Domain's registration status within the various registries.",
fields: (t) => ({
//////////////////////
// Registration.id
//////////////////////
id: t.field({
description: "A unique reference to this Registration.",
type: "RegistrationId",
nullable: false,
resolve: (parent) => parent.id,
}),
///////////////////////
// Registration.domain
///////////////////////
domain: t.field({
description: "The Domain for which this Registration exists.",
type: DomainInterfaceRef,
nullable: false,
resolve: (parent) => parent.domainId,
}),
//////////////////////////
// Registration.registrar
//////////////////////////
registrar: t.field({
description: "The Registrar contract under which this Registration is managed.",
type: AccountIdRef,
nullable: false,
resolve: (parent) => ({ chainId: parent.registrarChainId, address: parent.registrarAddress }),
}),
//////////////////////
// Registration.start
//////////////////////
start: t.field({
description: "A UnixTimestamp indicating when this Registration was created.",
type: "BigInt",
nullable: false,
resolve: (parent) => parent.start,
}),
///////////////////////////
// Registration.expiry
///////////////////////////
expiry: t.field({
description: "A UnixTimestamp indicating the Registration's expiry, if exists.",
type: "BigInt",
nullable: true,
resolve: (parent) => parent.expiry,
}),
////////////////////////
// Registration.expired
////////////////////////
expired: t.field({
description:
"Indicates whether this Registration is expired. If the Registration is for an ENSv1Domain, a Registration is only considered `expired` after the Grace Period has elapsed.",
type: "Boolean",
nullable: false,
resolve: (parent, args, context) => isRegistrationFullyExpired(parent, context.now),
}),
/////////////////////////
// Registration.referrer
/////////////////////////
referrer: t.field({
description: "The extra `referrer` data provided with a Registration, if exists.",
type: "Hex",
nullable: true,
resolve: (parent) => parent.referrer,
}),
/////////////////////////
// Registration.renewals
/////////////////////////
renewals: t.connection({
description:
"Renewals that have occurred within this Registration's lifespan to extend its expiration.",
type: RenewalRef,
resolve: (parent, args) => {
const scope = and(
eq(schema.renewal.domainId, parent.domainId),
eq(schema.renewal.registrationIndex, parent.index),
);
return lazyConnection({
totalCount: () => db.$count(schema.renewal, scope),
connection: () =>
resolveCursorConnection(
{ ...INDEX_PAGINATED_CONNECTION_ARGS, args },
({ before, after, limit, inverted }: ResolveCursorConnectionArgs) =>
db
.select()
.from(schema.renewal)
.where(and(scope, paginateByInt(schema.renewal.index, before, after)))
.orderBy(orderPaginationBy(schema.renewal.index, inverted))
.limit(limit),
),
});
},
}),
//////////////////////
// Registration.event
//////////////////////
event: t.field({
description: "The Event for which this Registration was created.",
type: EventRef,
nullable: false,
resolve: (parent) => parent.eventId,
}),
}),
});
///////////////////////////
// NameWrapperRegistration
///////////////////////////
export const NameWrapperRegistrationRef =
builder.objectRef<NameWrapperRegistration>("NameWrapperRegistration");
NameWrapperRegistrationRef.implement({
description:
"A NameWrapperRegistration represents a Registration initiated by the ENSv1 NameWrapper.",
interfaces: [RegistrationInterfaceRef],
isTypeOf: (value) => (value as RegistrationInterface).type === "NameWrapper",
fields: (t) => ({
/////////////////////////////////
// NameWrapperRegistration.fuses
/////////////////////////////////
fuses: t.field({
description: "The Fuses for this Registration's Domain in the NameWrapper.",
type: "Int",
nullable: false,
// TODO: decode/render Fuses enum
resolve: (parent) => parent.fuses,
}),
}),
});
/////////////////////////////
// BaseRegistrarRegistration
/////////////////////////////
export const BaseRegistrarRegistrationRef = builder.objectRef<BaseRegistrarRegistration>(
"BaseRegistrarRegistration",
);
BaseRegistrarRegistrationRef.implement({
description:
"A BaseRegistrarRegistration represents a Registration within an ENSv1 BaseRegistrar contract, including those deployed by Basenames and Lineanames.",
interfaces: [RegistrationInterfaceRef],
isTypeOf: (value) => (value as RegistrationInterface).type === "BaseRegistrar",
fields: (t) => ({
//////////////////////////////////////
// BaseRegistrarRegistration.baseCost
//////////////////////////////////////
baseCost: t.field({
description: "The `baseCost` for registering this Domain, in wei.",
type: "BigInt",
nullable: true,
resolve: (parent) => parent.baseCost,
}),
/////////////////////////////////////
// BaseRegistrarRegistration.premium
/////////////////////////////////////
premium: t.field({
description: "The `premium` for registering this Domain, in wei.",
type: "BigInt",
nullable: true,
resolve: (parent) => parent.premium,
}),
/////////////////////////////////////
// BaseRegistrarRegistration.wrapped
/////////////////////////////////////
wrapped: t.field({
description:
"Additional metadata if this BaseRegistrarRegistration is wrapped by the NameWrapper (i.e. in the case of wrapped .eth names).",
type: WrappedBaseRegistrarRegistrationRef,
nullable: true,
resolve: (parent) => (parent.wrapped ? parent : null),
}),
////////////////////////////////
// Registration.isInGracePeriod
////////////////////////////////
isInGracePeriod: t.field({
description:
"Whether this Registration is in the Grace Period (90 days) and can be renewed by the current owner.",
type: "Boolean",
nullable: false,
resolve: (parent, args, context) => isRegistrationInGracePeriod(parent, context.now),
}),
}),
});
////////////////////////
// ThreeDNSRegistration
////////////////////////
export const ThreeDNSRegistrationRef =
builder.objectRef<ThreeDNSRegistration>("ThreeDNSRegistration");
ThreeDNSRegistrationRef.implement({
description: "ThreeDNSRegistration represents a Registration within ThreeDNSToken.",
interfaces: [RegistrationInterfaceRef],
isTypeOf: (value) => (value as RegistrationInterface).type === "ThreeDNS",
fields: (t) => ({
//
}),
});
/////////////////////////////
// ENSv2RegistryRegistration
/////////////////////////////
export const ENSv2RegistryRegistrationRef = builder.objectRef<ENSv2RegistryRegistration>(
"ENSv2RegistryRegistration",
);
ENSv2RegistryRegistrationRef.implement({
description: "ENSv2RegistryRegistration represents a Registration within an ENSv2 Registry.",
interfaces: [RegistrationInterfaceRef],
isTypeOf: (value) => (value as RegistrationInterface).type === "ENSv2Registry",
fields: (t) => ({}),
});
////////////////////////////////////
// WrappedBaseRegistrarRegistration
////////////////////////////////////
export const WrappedBaseRegistrarRegistrationRef = builder.objectRef<BaseRegistrarRegistration>(
"WrappedBaseRegistrarRegistration",
);
WrappedBaseRegistrarRegistrationRef.implement({
description:
"Additional metadata for BaseRegistrar Registrations wrapped by the NameWrapper (i.e. in the case of a wrapped .eth name)",
fields: (t) => ({
///////////////////
// Wrapped.tokenId
///////////////////
tokenId: t.field({
description: "The TokenID for this Domain in the NameWrapper.",
type: "BigInt",
nullable: false,
// NOTE: only ENSv1 Domains can be wrapped, id is guaranteed to be ENSv1DomainId === Node
resolve: (parent) => hexToBigInt(parent.domainId as ENSv1DomainId),
}),
/////////////////
// Wrapped.fuses
/////////////////
fuses: t.field({
description: "The Fuses for this Registration's Domain in the NameWrapper.",
type: "Int",
nullable: false,
// TODO: decode/render Fuses enum
resolve: (parent) => parent.fuses,
}),
}),
});