Skip to content

Commit dc7e07f

Browse files
fix(ensindexer): update registrars plugin indexing logic and ENSDb schema (#1527)
Co-authored-by: lightwalker.eth <126201998+lightwalker-eth@users.noreply.github.com>
1 parent 12f16c2 commit dc7e07f

11 files changed

Lines changed: 80 additions & 56 deletions

File tree

.changeset/busy-poems-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ensnode/ensnode-schema": minor
3+
---
4+
5+
Update `registrars` schema to enable storing at most one metadata record.

.changeset/social-colts-smell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ensindexer": patch
3+
---
4+
5+
Update `registrars` plugin indexing logic to store at most one metadata record in ENSDb for current "logical registrar action".

apps/ensindexer/src/lib/managed-names.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ const CONTRACTS_BY_MANAGED_NAME: Record<Name, AccountId[]> = {
7070
DatasourceNames.ENSRoot,
7171
"UnwrappedEthRegistrarController",
7272
),
73+
getDatasourceContract(
74+
config.namespace,
75+
DatasourceNames.ENSRoot,
76+
"UniversalRegistrarRenewalWithReferrer",
77+
),
7378
ethnamesNameWrapper,
7479
],
7580
"base.eth": [

apps/ensindexer/src/plugins/registrars/basenames/handlers/Basenames_RegistrarController.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ export default function () {
6363

6464
await handleRegistrarControllerEvent(context, {
6565
id,
66-
subregistryId,
6766
node,
6867
pricing,
6968
referral,
@@ -94,7 +93,6 @@ export default function () {
9493

9594
await handleRegistrarControllerEvent(context, {
9695
id,
97-
subregistryId,
9896
node,
9997
pricing,
10098
referral,
@@ -121,7 +119,6 @@ export default function () {
121119

122120
await handleRegistrarControllerEvent(context, {
123121
id,
124-
subregistryId,
125122
node,
126123
pricing,
127124
referral,
@@ -152,7 +149,6 @@ export default function () {
152149

153150
await handleRegistrarControllerEvent(context, {
154151
id,
155-
subregistryId,
156152
node,
157153
pricing,
158154
referral,
@@ -179,7 +175,6 @@ export default function () {
179175

180176
await handleRegistrarControllerEvent(context, {
181177
id,
182-
subregistryId,
183178
node,
184179
pricing,
185180
referral,

apps/ensindexer/src/plugins/registrars/ethnames/handlers/Ethnames_RegistrarController.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ export default function () {
6767

6868
await handleRegistrarControllerEvent(context, {
6969
id,
70-
subregistryId,
7170
node,
7271
pricing,
7372
referral,
@@ -118,7 +117,6 @@ export default function () {
118117

119118
await handleRegistrarControllerEvent(context, {
120119
id,
121-
subregistryId,
122120
node,
123121
pricing,
124122
referral,
@@ -170,7 +168,6 @@ export default function () {
170168

171169
await handleRegistrarControllerEvent(context, {
172170
id,
173-
subregistryId,
174171
node,
175172
pricing,
176173
referral,
@@ -220,7 +217,6 @@ export default function () {
220217

221218
await handleRegistrarControllerEvent(context, {
222219
id,
223-
subregistryId,
224220
node,
225221
pricing,
226222
referral,
@@ -275,7 +271,6 @@ export default function () {
275271

276272
await handleRegistrarControllerEvent(context, {
277273
id,
278-
subregistryId,
279274
node,
280275
pricing,
281276
referral,
@@ -328,7 +323,6 @@ export default function () {
328323

329324
await handleRegistrarControllerEvent(context, {
330325
id,
331-
subregistryId,
332326
node,
333327
pricing,
334328
referral,

apps/ensindexer/src/plugins/registrars/ethnames/handlers/Ethnames_UniversalRegistrarRenewalWithReferrer.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export default function () {
4545

4646
await handleUniversalRegistrarRenewalEvent(context, {
4747
id,
48-
subregistryId,
4948
node,
5049
referral,
5150
transactionHash,

apps/ensindexer/src/plugins/registrars/lineanames/handlers/Lineanames_RegistrarController.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ export default function () {
6363

6464
await handleRegistrarControllerEvent(context, {
6565
id,
66-
subregistryId,
6766
node,
6867
pricing,
6968
referral,
@@ -101,7 +100,6 @@ export default function () {
101100

102101
await handleRegistrarControllerEvent(context, {
103102
id,
104-
subregistryId,
105103
node,
106104
pricing,
107105
referral,
@@ -137,7 +135,6 @@ export default function () {
137135

138136
await handleRegistrarControllerEvent(context, {
139137
id,
140-
subregistryId,
141138
node,
142139
pricing,
143140
referral,
@@ -173,7 +170,6 @@ export default function () {
173170

174171
await handleRegistrarControllerEvent(context, {
175172
id,
176-
subregistryId,
177173
node,
178174
pricing,
179175
referral,

apps/ensindexer/src/plugins/registrars/shared/lib/registrar-action.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import type { Context } from "ponder:registry";
22
import schema from "ponder:schema";
33
import type { Hash } from "viem";
44

5-
import {
6-
type AccountId,
7-
formatAccountId,
8-
type Node,
9-
type RegistrarAction,
10-
} from "@ensnode/ensnode-sdk";
5+
import { formatAccountId, type Node, type RegistrarAction } from "@ensnode/ensnode-sdk";
116

127
/**
138
* Logical Event Key
@@ -24,15 +19,13 @@ export type LogicalEventKey = string;
2419
* Make a logical event key for a "logical registrar action".
2520
*/
2621
export function makeLogicalEventKey({
27-
subregistryId,
2822
node,
2923
transactionHash,
3024
}: {
31-
subregistryId: AccountId;
3225
node: Node;
3326
transactionHash: Hash;
3427
}): LogicalEventKey {
35-
return [formatAccountId(subregistryId), node, transactionHash].join(":").toLowerCase();
28+
return [node, transactionHash].join(":").toLowerCase();
3629
}
3730

3831
/**
@@ -57,15 +50,25 @@ export async function insertRegistrarAction(
5750
// 1. Create logical event key
5851
const logicalEventKey = makeLogicalEventKey({
5952
node,
60-
subregistryId,
6153
transactionHash,
6254
});
6355

6456
// 2. Store mapping between logical event key and logical event id
65-
await context.db.insert(schema.internal_registrarActionMetadata).values({
66-
logicalEventKey,
67-
logicalEventId: id,
68-
});
57+
// Note: If the metadata record already exists,
58+
// we update it to point to the current logical event key and id.
59+
// This ensures that there is always at most one record in ENSDb
60+
// for the current "logical registrar action".
61+
await context.db
62+
.insert(schema.internal_registrarActionMetadata)
63+
.values({
64+
metadataType: "CURRENT_LOGICAL_REGISTRAR_ACTION",
65+
logicalEventKey,
66+
logicalEventId: id,
67+
})
68+
.onConflictDoUpdate(() => ({
69+
logicalEventKey,
70+
logicalEventId: id,
71+
}));
6972

7073
// 3. Store initial record for the "logical registrar action"
7174
await context.db.insert(schema.registrarActions).values({

apps/ensindexer/src/plugins/registrars/shared/lib/registrar-controller-events.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import schema from "ponder:schema";
33
import type { Address, Hash } from "viem";
44

55
import {
6-
type AccountId,
76
type EncodedReferrer,
87
isRegistrarActionPricingAvailable,
98
isRegistrarActionReferralAvailable,
@@ -24,14 +23,12 @@ export async function handleRegistrarControllerEvent(
2423
context: Context,
2524
{
2625
id,
27-
subregistryId,
2826
node,
2927
pricing,
3028
referral,
3129
transactionHash,
3230
}: {
3331
id: Event["id"];
34-
subregistryId: AccountId;
3532
node: Node;
3633
pricing: RegistrarActionPricing;
3734
referral: RegistrarActionReferral;
@@ -40,37 +37,41 @@ export async function handleRegistrarControllerEvent(
4037
): Promise<void> {
4138
// 1. Make Logical Event Key
4239
const logicalEventKey = makeLogicalEventKey({
43-
subregistryId,
4440
node,
4541
transactionHash,
4642
});
4743

4844
// 2. Use the Logical Event Key to get the "logical registrar action" record
4945
// which needs to be updated.
5046

51-
// 2. a) Find subregistryActionMetadata record by logical event key.
52-
const subregistryActionMetadata = await context.db.find(schema.internal_registrarActionMetadata, {
53-
logicalEventKey,
47+
// 2. a) Find registrarActionMetadata record for the current "logical registrar action".
48+
const registrarActionMetadata = await context.db.find(schema.internal_registrarActionMetadata, {
49+
metadataType: "CURRENT_LOGICAL_REGISTRAR_ACTION",
5450
});
5551

56-
// Invariant: the subregistryActionMetadata record must be available for `logicalEventKey`
57-
if (!subregistryActionMetadata) {
52+
// Invariant: the registrarActionMetadata record must exist
53+
if (!registrarActionMetadata) {
5854
throw new Error(
5955
`The required "logical registrar action" ID could not be found for the following logical event key: '${logicalEventKey}'.`,
6056
);
6157
}
6258

63-
const { logicalEventId } = subregistryActionMetadata;
59+
// Invariant: the stored logical event key must match the current logical event key
60+
if (registrarActionMetadata.logicalEventKey !== logicalEventKey) {
61+
throw new Error(
62+
`The logical event key ('${registrarActionMetadata.logicalEventKey}') for the "logical registrar action" record must be same as the current logical event key ('${logicalEventKey}').`,
63+
);
64+
}
6465

6566
// 2. b) Find "logical registrar action" record by `logicalEventId`.
6667
const logicalRegistrarAction = await context.db.find(schema.registrarActions, {
67-
id: logicalEventId,
68+
id: registrarActionMetadata.logicalEventId,
6869
});
6970

7071
// Invariant: the "logical registrar action" record must be available for `logicalEventId`
7172
if (!logicalRegistrarAction) {
7273
throw new Error(
73-
`The "logical registrar action" record, which could not be found for the following logical event ID: '${logicalEventId}'.`,
74+
`The "logical registrar action" record, which could not be found for the following logical event ID: '${registrarActionMetadata.logicalEventId}'.`,
7475
);
7576
}
7677

apps/ensindexer/src/plugins/registrars/shared/lib/universal-registrar-renewal-with-referrer-events.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import schema from "ponder:schema";
33
import type { Address, Hash } from "viem";
44

55
import {
6-
type AccountId,
76
type EncodedReferrer,
87
isRegistrarActionReferralAvailable,
98
type Node,
@@ -21,51 +20,53 @@ export async function handleUniversalRegistrarRenewalEvent(
2120
context: Context,
2221
{
2322
id,
24-
subregistryId,
2523
node,
2624
referral,
2725
transactionHash,
2826
}: {
2927
id: Event["id"];
30-
subregistryId: AccountId;
3128
node: Node;
3229
referral: RegistrarActionReferral;
3330
transactionHash: Hash;
3431
},
3532
): Promise<void> {
3633
// 1. Make Logical Event Key
3734
const logicalEventKey = makeLogicalEventKey({
38-
subregistryId,
3935
node,
4036
transactionHash,
4137
});
4238

4339
// 2. Use the Logical Event Key to get the "logical registrar action" record
4440
// which needs to be updated.
4541

46-
// 2. a) Find subregistryActionMetadata record by logical event key.
47-
const subregistryActionMetadata = await context.db.find(schema.internal_registrarActionMetadata, {
48-
logicalEventKey,
42+
// 2. a) Find registrarActionMetadata record for the current "logical registrar action".
43+
const registrarActionMetadata = await context.db.find(schema.internal_registrarActionMetadata, {
44+
metadataType: "CURRENT_LOGICAL_REGISTRAR_ACTION",
4945
});
5046

51-
// Invariant: the subregistryActionMetadata record must be available for `logicalEventKey`
52-
if (!subregistryActionMetadata) {
47+
// Invariant: the registrarActionMetadata record must exist
48+
if (!registrarActionMetadata) {
5349
throw new Error(
5450
`The required "logical registrar action" ID could not be found for the following logical event key: '${logicalEventKey}'.`,
5551
);
5652
}
5753

58-
const { logicalEventId } = subregistryActionMetadata;
54+
// Invariant: the stored logical event key must match the current logical event key
55+
if (registrarActionMetadata.logicalEventKey !== logicalEventKey) {
56+
throw new Error(
57+
`The logical event key ('${registrarActionMetadata.logicalEventKey}') for the "logical registrar action" record must be same as the current logical event key ('${logicalEventKey}').`,
58+
);
59+
}
5960

6061
// 2. b) Find "logical registrar action" record by `logicalEventId`.
6162
const logicalRegistrarAction = await context.db.find(schema.registrarActions, {
62-
id: logicalEventId,
63+
id: registrarActionMetadata.logicalEventId,
6364
});
6465

6566
// Invariant: the "logical registrar action" record must be available for `logicalEventId`
6667
if (!logicalRegistrarAction) {
6768
throw new Error(
68-
`The "logical registrar action" record, which could not be found for the following logical event ID: '${logicalEventId}'.`,
69+
`The "logical registrar action" record, which could not be found for the following logical event ID: '${registrarActionMetadata.logicalEventId}'.`,
6970
);
7071
}
7172

0 commit comments

Comments
 (0)