ponder 0.17.12 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/dist/esm/bin/commands/codegen.js +3 -3
- package/dist/esm/bin/commands/codegen.js.map +1 -1
- package/dist/esm/bin/commands/dev.js +3 -3
- package/dist/esm/bin/commands/dev.js.map +1 -1
- package/dist/esm/bin/commands/serve.js +3 -3
- package/dist/esm/bin/commands/serve.js.map +1 -1
- package/dist/esm/bin/commands/start.js +3 -3
- package/dist/esm/bin/commands/start.js.map +1 -1
- package/dist/esm/build/config.js +19 -20
- package/dist/esm/build/config.js.map +1 -1
- package/dist/esm/build/index.js +79 -65
- package/dist/esm/build/index.js.map +1 -1
- package/dist/esm/build/plugin.js +9 -0
- package/dist/esm/build/plugin.js.map +1 -1
- package/dist/esm/build/stacktrace.js +25 -68
- package/dist/esm/build/stacktrace.js.map +1 -1
- package/dist/esm/database/index.js +83 -67
- package/dist/esm/database/index.js.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/indexing/client.js +35 -23
- package/dist/esm/indexing/client.js.map +1 -1
- package/dist/esm/indexing/index.js +3 -1
- package/dist/esm/indexing/index.js.map +1 -1
- package/dist/esm/rpc/actions.js +143 -67
- package/dist/esm/rpc/actions.js.map +1 -1
- package/dist/esm/runtime/events.js +33 -19
- package/dist/esm/runtime/events.js.map +1 -1
- package/dist/esm/runtime/filter.js +10 -9
- package/dist/esm/runtime/filter.js.map +1 -1
- package/dist/esm/runtime/fragments.js +50 -24
- package/dist/esm/runtime/fragments.js.map +1 -1
- package/dist/esm/runtime/historical.js +212 -68
- package/dist/esm/runtime/historical.js.map +1 -1
- package/dist/esm/runtime/index.js +11 -15
- package/dist/esm/runtime/index.js.map +1 -1
- package/dist/esm/runtime/isolated.js +1 -0
- package/dist/esm/runtime/isolated.js.map +1 -1
- package/dist/esm/runtime/multichain.js +1 -0
- package/dist/esm/runtime/multichain.js.map +1 -1
- package/dist/esm/runtime/omnichain.js +1 -0
- package/dist/esm/runtime/omnichain.js.map +1 -1
- package/dist/esm/runtime/realtime.js +17 -16
- package/dist/esm/runtime/realtime.js.map +1 -1
- package/dist/esm/server/index.js +8 -6
- package/dist/esm/server/index.js.map +1 -1
- package/dist/esm/sync-historical/in-memory.js +539 -0
- package/dist/esm/sync-historical/in-memory.js.map +1 -0
- package/dist/esm/sync-historical/index.js +46 -63
- package/dist/esm/sync-historical/index.js.map +1 -1
- package/dist/esm/sync-realtime/index.js +56 -83
- package/dist/esm/sync-realtime/index.js.map +1 -1
- package/dist/esm/sync-store/encode.js +1 -6
- package/dist/esm/sync-store/encode.js.map +1 -1
- package/dist/esm/sync-store/index.js +58 -62
- package/dist/esm/sync-store/index.js.map +1 -1
- package/dist/esm/sync-store/schema.js +23 -15
- package/dist/esm/sync-store/schema.js.map +1 -1
- package/dist/esm/sync-store/sql/ponder_rpc_cache_1/copy.sql +23 -0
- package/dist/esm/sync-store/sql/ponder_rpc_cache_1/create.sql +130 -0
- package/dist/esm/utils/finality.js +41 -32
- package/dist/esm/utils/finality.js.map +1 -1
- package/dist/esm/utils/pglite.js +0 -65
- package/dist/esm/utils/pglite.js.map +1 -1
- package/dist/types/bin/commands/codegen.d.ts.map +1 -1
- package/dist/types/bin/commands/dev.d.ts.map +1 -1
- package/dist/types/bin/commands/serve.d.ts.map +1 -1
- package/dist/types/bin/commands/start.d.ts.map +1 -1
- package/dist/types/build/config.d.ts.map +1 -1
- package/dist/types/build/index.d.ts.map +1 -1
- package/dist/types/build/plugin.d.ts.map +1 -1
- package/dist/types/build/stacktrace.d.ts +1 -12
- package/dist/types/build/stacktrace.d.ts.map +1 -1
- package/dist/types/config/index.d.ts +12 -2
- package/dist/types/config/index.d.ts.map +1 -1
- package/dist/types/database/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/indexing/client.d.ts.map +1 -1
- package/dist/types/internal/types.d.ts +8 -13
- package/dist/types/internal/types.d.ts.map +1 -1
- package/dist/types/rpc/actions.d.ts +30 -0
- package/dist/types/rpc/actions.d.ts.map +1 -1
- package/dist/types/runtime/events.d.ts.map +1 -1
- package/dist/types/runtime/filter.d.ts.map +1 -1
- package/dist/types/runtime/fragments.d.ts +9 -1
- package/dist/types/runtime/fragments.d.ts.map +1 -1
- package/dist/types/runtime/historical.d.ts +18 -1
- package/dist/types/runtime/historical.d.ts.map +1 -1
- package/dist/types/runtime/index.d.ts +1 -0
- package/dist/types/runtime/index.d.ts.map +1 -1
- package/dist/types/runtime/isolated.d.ts.map +1 -1
- package/dist/types/runtime/multichain.d.ts.map +1 -1
- package/dist/types/runtime/omnichain.d.ts.map +1 -1
- package/dist/types/runtime/realtime.d.ts.map +1 -1
- package/dist/types/server/index.d.ts.map +1 -1
- package/dist/types/sync-historical/in-memory.d.ts +24 -0
- package/dist/types/sync-historical/in-memory.d.ts.map +1 -0
- package/dist/types/sync-historical/index.d.ts.map +1 -1
- package/dist/types/sync-realtime/index.d.ts +3 -2
- package/dist/types/sync-realtime/index.d.ts.map +1 -1
- package/dist/types/sync-store/encode.d.ts.map +1 -1
- package/dist/types/sync-store/index.d.ts +3 -3
- package/dist/types/sync-store/index.d.ts.map +1 -1
- package/dist/types/sync-store/schema.d.ts +63 -98
- package/dist/types/sync-store/schema.d.ts.map +1 -1
- package/dist/types/types/eth.d.ts +2 -8
- package/dist/types/types/eth.d.ts.map +1 -1
- package/dist/types/utils/finality.d.ts +12 -9
- package/dist/types/utils/finality.d.ts.map +1 -1
- package/dist/types/utils/pglite.d.ts +0 -18
- package/dist/types/utils/pglite.d.ts.map +1 -1
- package/package.json +5 -11
- package/src/bin/commands/codegen.ts +3 -3
- package/src/bin/commands/dev.ts +3 -3
- package/src/bin/commands/serve.ts +3 -3
- package/src/bin/commands/start.ts +3 -3
- package/src/build/config.ts +29 -28
- package/src/build/index.ts +88 -71
- package/src/build/plugin.ts +6 -0
- package/src/build/stacktrace.ts +38 -65
- package/src/config/index.ts +12 -2
- package/src/database/index.ts +132 -89
- package/src/index.ts +5 -0
- package/src/indexing/client.ts +56 -40
- package/src/indexing/index.ts +4 -1
- package/src/internal/types.ts +10 -13
- package/src/rpc/actions.ts +211 -77
- package/src/runtime/events.ts +40 -19
- package/src/runtime/filter.ts +11 -11
- package/src/runtime/fragments.ts +70 -26
- package/src/runtime/historical.ts +314 -97
- package/src/runtime/index.ts +27 -14
- package/src/runtime/isolated.ts +1 -0
- package/src/runtime/multichain.ts +1 -0
- package/src/runtime/omnichain.ts +1 -0
- package/src/runtime/realtime.ts +24 -25
- package/src/server/index.ts +14 -7
- package/src/sync-historical/in-memory.ts +834 -0
- package/src/sync-historical/index.ts +76 -120
- package/src/sync-realtime/index.ts +73 -133
- package/src/sync-store/encode.ts +1 -6
- package/src/sync-store/index.ts +90 -95
- package/src/sync-store/schema.ts +24 -27
- package/src/types/eth.ts +2 -8
- package/src/utils/finality.ts +67 -34
- package/src/utils/pglite.ts +0 -79
- package/dist/esm/sync-store/migrations.js +0 -1595
- package/dist/esm/sync-store/migrations.js.map +0 -1
- package/dist/types/sync-store/migrations.d.ts +0 -8
- package/dist/types/sync-store/migrations.d.ts.map +0 -1
- package/src/sync-store/migrations.ts +0 -1801
package/src/sync-store/index.ts
CHANGED
|
@@ -6,7 +6,6 @@ import {
|
|
|
6
6
|
eq,
|
|
7
7
|
gte,
|
|
8
8
|
inArray,
|
|
9
|
-
isNull,
|
|
10
9
|
lt,
|
|
11
10
|
lte,
|
|
12
11
|
or,
|
|
@@ -26,6 +25,7 @@ import type { Logger } from "@/internal/logger.js";
|
|
|
26
25
|
import type {
|
|
27
26
|
BlockFilter,
|
|
28
27
|
Factory,
|
|
28
|
+
FactoryFragmentId,
|
|
29
29
|
Filter,
|
|
30
30
|
Fragment,
|
|
31
31
|
FragmentId,
|
|
@@ -61,6 +61,7 @@ import {
|
|
|
61
61
|
} from "@/runtime/filter.js";
|
|
62
62
|
import {
|
|
63
63
|
encodeFragment,
|
|
64
|
+
getFactoryFragmentIds,
|
|
64
65
|
getFactoryFragments,
|
|
65
66
|
getFragments,
|
|
66
67
|
} from "@/runtime/fragments.js";
|
|
@@ -98,8 +99,8 @@ export type SyncStore = {
|
|
|
98
99
|
>;
|
|
99
100
|
insertChildAddresses(
|
|
100
101
|
args: {
|
|
101
|
-
|
|
102
|
-
childAddresses: Map<Address, number
|
|
102
|
+
/** Child addresses and block numbers per `factory_log` fragment ID. */
|
|
103
|
+
childAddresses: Map<FactoryFragmentId, Map<Address, number>>;
|
|
103
104
|
chainId: number;
|
|
104
105
|
},
|
|
105
106
|
context?: { logger?: Logger },
|
|
@@ -431,40 +432,52 @@ export const createSyncStore = ({
|
|
|
431
432
|
|
|
432
433
|
return result;
|
|
433
434
|
},
|
|
434
|
-
insertChildAddresses: async (
|
|
435
|
-
{ factory, childAddresses, chainId },
|
|
436
|
-
context,
|
|
437
|
-
) => {
|
|
435
|
+
insertChildAddresses: async ({ childAddresses, chainId }, context) => {
|
|
438
436
|
if (childAddresses.size === 0) return;
|
|
439
437
|
|
|
440
|
-
const
|
|
438
|
+
const factories = await qb.wrap(
|
|
439
|
+
{ label: "insert_factories" },
|
|
440
|
+
(db) =>
|
|
441
|
+
db
|
|
442
|
+
.insert(PONDER_SYNC.factories)
|
|
443
|
+
.values(
|
|
444
|
+
Array.from(childAddresses.keys()).map((fragmentId) => ({
|
|
445
|
+
fragmentId,
|
|
446
|
+
})),
|
|
447
|
+
)
|
|
448
|
+
// @ts-expect-error bug with drizzle-orm
|
|
449
|
+
.returning({
|
|
450
|
+
id: PONDER_SYNC.factories.id,
|
|
451
|
+
fragmentId: PONDER_SYNC.factories.fragmentId,
|
|
452
|
+
})
|
|
453
|
+
// Note: `DO UPDATE` makes `RETURNING` include rows that already exist.
|
|
454
|
+
.onConflictDoUpdate({
|
|
455
|
+
target: PONDER_SYNC.factories.fragmentId,
|
|
456
|
+
set: { fragmentId: sql`excluded.fragment_id` },
|
|
457
|
+
}),
|
|
458
|
+
context,
|
|
459
|
+
);
|
|
460
|
+
|
|
461
|
+
const factoryIds = new Map<FactoryFragmentId, number>();
|
|
462
|
+
for (const { id, fragmentId } of factories) {
|
|
463
|
+
factoryIds.set(fragmentId, id);
|
|
464
|
+
}
|
|
441
465
|
|
|
442
466
|
const batchSize = Math.floor(
|
|
443
|
-
common.options.databaseMaxQueryParameters /
|
|
467
|
+
common.options.databaseMaxQueryParameters / 4,
|
|
444
468
|
);
|
|
445
469
|
|
|
446
470
|
const values: (typeof PONDER_SYNC.factoryAddresses.$inferInsert)[] = [];
|
|
447
471
|
|
|
448
|
-
const
|
|
449
|
-
|
|
450
|
-
.
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
}),
|
|
458
|
-
);
|
|
459
|
-
|
|
460
|
-
for (const [address, blockNumber] of childAddresses) {
|
|
461
|
-
values.push({
|
|
462
|
-
// @ts-expect-error
|
|
463
|
-
factoryId: sql`(SELECT id FROM factory_insert)`,
|
|
464
|
-
chainId: BigInt(chainId),
|
|
465
|
-
blockNumber: BigInt(blockNumber),
|
|
466
|
-
address,
|
|
467
|
-
});
|
|
472
|
+
for (const [fragmentId, _childAddresses] of childAddresses) {
|
|
473
|
+
for (const [address, blockNumber] of _childAddresses) {
|
|
474
|
+
values.push({
|
|
475
|
+
factoryId: factoryIds.get(fragmentId)!,
|
|
476
|
+
chainId: BigInt(chainId),
|
|
477
|
+
blockNumber: BigInt(blockNumber),
|
|
478
|
+
address,
|
|
479
|
+
});
|
|
480
|
+
}
|
|
468
481
|
}
|
|
469
482
|
|
|
470
483
|
for (let i = 0; i < values.length; i += batchSize) {
|
|
@@ -472,59 +485,58 @@ export const createSyncStore = ({
|
|
|
472
485
|
{ label: "insert_child_addresses" },
|
|
473
486
|
(db) =>
|
|
474
487
|
db
|
|
475
|
-
.with(factoryInsert)
|
|
476
488
|
.insert(PONDER_SYNC.factoryAddresses)
|
|
477
|
-
.values(values.slice(i, i + batchSize))
|
|
489
|
+
.values(values.slice(i, i + batchSize))
|
|
490
|
+
// Note: Live and historical sync can insert a child address that is
|
|
491
|
+
// already stored. Keep one row per parent with the earliest block number.
|
|
492
|
+
.onConflictDoUpdate({
|
|
493
|
+
target: [
|
|
494
|
+
PONDER_SYNC.factoryAddresses.factoryId,
|
|
495
|
+
PONDER_SYNC.factoryAddresses.address,
|
|
496
|
+
],
|
|
497
|
+
set: { blockNumber: sql`excluded.block_number` },
|
|
498
|
+
setWhere: sql`excluded.block_number < ${PONDER_SYNC.factoryAddresses.blockNumber}`,
|
|
499
|
+
}),
|
|
478
500
|
context,
|
|
479
501
|
);
|
|
480
502
|
}
|
|
481
503
|
},
|
|
482
|
-
getChildAddresses: ({ factory }, context) => {
|
|
483
|
-
const
|
|
484
|
-
|
|
485
|
-
const factoryInsert = qb.raw.$with("factory_insert").as(
|
|
486
|
-
qb.raw
|
|
487
|
-
.insert(PONDER_SYNC.factories)
|
|
488
|
-
.values({ factory: _factory })
|
|
489
|
-
// @ts-expect-error bug with drizzle-orm
|
|
490
|
-
.returning({ id: PONDER_SYNC.factories.id })
|
|
491
|
-
.onConflictDoUpdate({
|
|
492
|
-
target: PONDER_SYNC.factories.factory,
|
|
493
|
-
set: { factory: sql`excluded.factory` },
|
|
494
|
-
}),
|
|
495
|
-
);
|
|
504
|
+
getChildAddresses: async ({ factory }, context) => {
|
|
505
|
+
const fragmentIds = getFactoryFragmentIds(factory);
|
|
496
506
|
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
.
|
|
503
|
-
.
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
qb.raw.select({ id: factoryInsert.id }).from(factoryInsert),
|
|
512
|
-
),
|
|
507
|
+
const rows = await qb.wrap(
|
|
508
|
+
{ label: "select_child_addresses" },
|
|
509
|
+
(db) =>
|
|
510
|
+
db
|
|
511
|
+
.select({
|
|
512
|
+
address: PONDER_SYNC.factoryAddresses.address,
|
|
513
|
+
blockNumber: PONDER_SYNC.factoryAddresses.blockNumber,
|
|
514
|
+
})
|
|
515
|
+
.from(PONDER_SYNC.factoryAddresses)
|
|
516
|
+
.innerJoin(
|
|
517
|
+
PONDER_SYNC.factories,
|
|
518
|
+
eq(
|
|
519
|
+
PONDER_SYNC.factoryAddresses.factoryId,
|
|
520
|
+
PONDER_SYNC.factories.id,
|
|
513
521
|
),
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
522
|
+
)
|
|
523
|
+
.where(inArray(PONDER_SYNC.factories.fragmentId, fragmentIds)),
|
|
524
|
+
context,
|
|
525
|
+
);
|
|
526
|
+
|
|
527
|
+
// Note: A child address can have more than one row, because more than one parent
|
|
528
|
+
// can emit it. The earliest block number is used.
|
|
529
|
+
|
|
530
|
+
const result = new Map<Address, number>();
|
|
531
|
+
for (const { address, blockNumber } of rows) {
|
|
532
|
+
if (
|
|
533
|
+
result.has(address) === false ||
|
|
534
|
+
result.get(address)! > Number(blockNumber)
|
|
535
|
+
) {
|
|
536
|
+
result.set(address, Number(blockNumber));
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return result;
|
|
528
540
|
},
|
|
529
541
|
getSafeCrashRecoveryBlock: async ({ chainId, timestamp }, context) => {
|
|
530
542
|
const rows = await qb.wrap(
|
|
@@ -734,7 +746,7 @@ export const createSyncStore = ({
|
|
|
734
746
|
PONDER_SYNC.traces.chainId,
|
|
735
747
|
PONDER_SYNC.traces.blockNumber,
|
|
736
748
|
PONDER_SYNC.traces.transactionIndex,
|
|
737
|
-
PONDER_SYNC.traces.
|
|
749
|
+
PONDER_SYNC.traces.traceAddress,
|
|
738
750
|
],
|
|
739
751
|
}),
|
|
740
752
|
context,
|
|
@@ -916,8 +928,7 @@ export const createSyncStore = ({
|
|
|
916
928
|
output: PONDER_SYNC.traces.output,
|
|
917
929
|
value: PONDER_SYNC.traces.value,
|
|
918
930
|
type: PONDER_SYNC.traces.type,
|
|
919
|
-
|
|
920
|
-
traceIndex: PONDER_SYNC.traces.traceIndex,
|
|
931
|
+
traceAddress: PONDER_SYNC.traces.traceAddress,
|
|
921
932
|
};
|
|
922
933
|
|
|
923
934
|
for (const column of unionFilterIncludeTrace(filters)) {
|
|
@@ -948,7 +959,7 @@ export const createSyncStore = ({
|
|
|
948
959
|
.orderBy(
|
|
949
960
|
asc(PONDER_SYNC.traces.blockNumber),
|
|
950
961
|
asc(PONDER_SYNC.traces.transactionIndex),
|
|
951
|
-
asc(PONDER_SYNC.traces.
|
|
962
|
+
asc(PONDER_SYNC.traces.traceAddress),
|
|
952
963
|
)
|
|
953
964
|
.limit(limit);
|
|
954
965
|
|
|
@@ -1191,14 +1202,6 @@ export const createSyncStore = ({
|
|
|
1191
1202
|
if (trace.output === null) {
|
|
1192
1203
|
internalTrace.output = undefined;
|
|
1193
1204
|
}
|
|
1194
|
-
|
|
1195
|
-
if (trace.error === null) {
|
|
1196
|
-
internalTrace.error = undefined;
|
|
1197
|
-
}
|
|
1198
|
-
|
|
1199
|
-
if (trace.revertReason === null) {
|
|
1200
|
-
internalTrace.revertReason = undefined;
|
|
1201
|
-
}
|
|
1202
1205
|
}
|
|
1203
1206
|
|
|
1204
1207
|
for (let i = 0; i < logsRows.length; i++) {
|
|
@@ -1589,10 +1592,6 @@ export const transferFilter = (filter: TransferFilter): SQL => {
|
|
|
1589
1592
|
conditions.push(addressFilter(filter.fromAddress, PONDER_SYNC.traces.from));
|
|
1590
1593
|
conditions.push(addressFilter(filter.toAddress, PONDER_SYNC.traces.to));
|
|
1591
1594
|
|
|
1592
|
-
if (filter.includeReverted === false) {
|
|
1593
|
-
conditions.push(isNull(PONDER_SYNC.traces.error));
|
|
1594
|
-
}
|
|
1595
|
-
|
|
1596
1595
|
return and(...conditions)!;
|
|
1597
1596
|
};
|
|
1598
1597
|
|
|
@@ -1602,10 +1601,6 @@ export const traceFilter = (filter: TraceFilter): SQL => {
|
|
|
1602
1601
|
conditions.push(addressFilter(filter.fromAddress, PONDER_SYNC.traces.from));
|
|
1603
1602
|
conditions.push(addressFilter(filter.toAddress, PONDER_SYNC.traces.to));
|
|
1604
1603
|
|
|
1605
|
-
if (filter.includeReverted === false) {
|
|
1606
|
-
conditions.push(isNull(PONDER_SYNC.traces.error));
|
|
1607
|
-
}
|
|
1608
|
-
|
|
1609
1604
|
if (filter.callType !== undefined) {
|
|
1610
1605
|
conditions.push(eq(PONDER_SYNC.traces.type, filter.callType));
|
|
1611
1606
|
}
|
package/src/sync-store/schema.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
unique,
|
|
7
7
|
} from "drizzle-orm/pg-core";
|
|
8
8
|
import type { Address, Hash, Hex } from "viem";
|
|
9
|
-
import type {
|
|
9
|
+
import type { FactoryFragmentId, FragmentId } from "@/internal/types.js";
|
|
10
10
|
|
|
11
11
|
const nummultirange = customType<{ data: string }>({
|
|
12
12
|
dataType() {
|
|
@@ -27,9 +27,16 @@ const numeric78 = customType<{ data: bigint; driverData: string }>({
|
|
|
27
27
|
* Database schemas for the sync.
|
|
28
28
|
*
|
|
29
29
|
* @dev The order of the schemas represents the order of the migrations.
|
|
30
|
-
* @dev
|
|
30
|
+
* @dev Each schema after the first must have a directory in "./sql" with
|
|
31
|
+
* "create.sql" and "copy.sql".
|
|
32
|
+
*
|
|
33
|
+
* @dev The first schema is the legacy "ponder_sync" schema. Ponder no longer
|
|
34
|
+
* creates it, and only copies data from it.
|
|
31
35
|
*/
|
|
32
|
-
export const PONDER_SYNC_SCHEMAS = [
|
|
36
|
+
export const PONDER_SYNC_SCHEMAS = [
|
|
37
|
+
"ponder_sync",
|
|
38
|
+
"ponder_rpc_cache_1",
|
|
39
|
+
] as const;
|
|
33
40
|
/**
|
|
34
41
|
* Latest database schema for the sync.
|
|
35
42
|
*/
|
|
@@ -157,7 +164,7 @@ export const traces = PONDER_SYNC.table(
|
|
|
157
164
|
chainId: t.bigint({ mode: "bigint" }).notNull(),
|
|
158
165
|
blockNumber: t.bigint({ mode: "bigint" }).notNull(),
|
|
159
166
|
transactionIndex: t.integer().notNull(),
|
|
160
|
-
|
|
167
|
+
traceAddress: t.integer().array().notNull(),
|
|
161
168
|
from: t.varchar({ length: 42 }).notNull().$type<Address>(),
|
|
162
169
|
to: t.varchar({ length: 42 }).$type<Address>(),
|
|
163
170
|
input: t.text().notNull().$type<Hex>(),
|
|
@@ -166,9 +173,6 @@ export const traces = PONDER_SYNC.table(
|
|
|
166
173
|
type: t.text().notNull(),
|
|
167
174
|
gas: numeric78().notNull(),
|
|
168
175
|
gasUsed: numeric78().notNull(),
|
|
169
|
-
error: t.text(),
|
|
170
|
-
revertReason: t.text(),
|
|
171
|
-
subcalls: t.integer().notNull(),
|
|
172
176
|
}),
|
|
173
177
|
(table) => [
|
|
174
178
|
primaryKey({
|
|
@@ -177,7 +181,7 @@ export const traces = PONDER_SYNC.table(
|
|
|
177
181
|
table.chainId,
|
|
178
182
|
table.blockNumber,
|
|
179
183
|
table.transactionIndex,
|
|
180
|
-
table.
|
|
184
|
+
table.traceAddress,
|
|
181
185
|
],
|
|
182
186
|
}),
|
|
183
187
|
],
|
|
@@ -209,40 +213,33 @@ export const intervals = PONDER_SYNC.table("intervals", (t) => ({
|
|
|
209
213
|
blocks: nummultirange().notNull(),
|
|
210
214
|
}));
|
|
211
215
|
|
|
216
|
+
/**
|
|
217
|
+
* Parents of child addresses.
|
|
218
|
+
*/
|
|
212
219
|
export const factories = PONDER_SYNC.table(
|
|
213
220
|
"factories",
|
|
214
221
|
(t) => ({
|
|
215
222
|
id: t.integer().primaryKey().generatedAlwaysAsIdentity(),
|
|
216
|
-
|
|
217
|
-
.jsonb()
|
|
218
|
-
.$type<
|
|
219
|
-
Pick<
|
|
220
|
-
Factory,
|
|
221
|
-
| "type"
|
|
222
|
-
| "chainId"
|
|
223
|
-
| "address"
|
|
224
|
-
| "eventSelector"
|
|
225
|
-
| "childAddressLocation"
|
|
226
|
-
| "fromBlock"
|
|
227
|
-
| "toBlock"
|
|
228
|
-
>
|
|
229
|
-
>()
|
|
230
|
-
.notNull(),
|
|
223
|
+
fragmentId: t.text().notNull().$type<FactoryFragmentId>(),
|
|
231
224
|
}),
|
|
232
225
|
(table) => [
|
|
233
|
-
index("
|
|
234
|
-
unique("
|
|
226
|
+
index("factories_fragment_id_idx").on(table.fragmentId),
|
|
227
|
+
unique("factories_fragment_id_key").on(table.fragmentId),
|
|
235
228
|
],
|
|
236
229
|
);
|
|
237
230
|
|
|
238
231
|
export const factoryAddresses = PONDER_SYNC.table(
|
|
239
232
|
"factory_addresses",
|
|
240
233
|
(t) => ({
|
|
241
|
-
id: t.integer().primaryKey().generatedAlwaysAsIdentity(),
|
|
242
234
|
factoryId: t.integer().notNull(), // references `factories.id`
|
|
243
235
|
chainId: t.bigint({ mode: "bigint" }).notNull(),
|
|
244
236
|
blockNumber: t.bigint({ mode: "bigint" }).notNull(),
|
|
245
237
|
address: t.text().$type<Address>().notNull(),
|
|
246
238
|
}),
|
|
247
|
-
(table) => [
|
|
239
|
+
(table) => [
|
|
240
|
+
primaryKey({
|
|
241
|
+
name: "factory_addresses_pkey",
|
|
242
|
+
columns: [table.factoryId, table.address],
|
|
243
|
+
}),
|
|
244
|
+
],
|
|
248
245
|
);
|
package/src/types/eth.ts
CHANGED
|
@@ -193,16 +193,10 @@ export type Trace = {
|
|
|
193
193
|
input: Hex;
|
|
194
194
|
/** Output of the call, if any. */
|
|
195
195
|
output?: Hex;
|
|
196
|
-
/** Error message, if any. */
|
|
197
|
-
error?: string;
|
|
198
|
-
/** Why this call reverted, if it reverted. */
|
|
199
|
-
revertReason?: string;
|
|
200
196
|
/** Value transferred. */
|
|
201
197
|
value: bigint | null;
|
|
202
|
-
/**
|
|
203
|
-
|
|
204
|
-
/** Number of subcalls. */
|
|
205
|
-
subcalls: number;
|
|
198
|
+
/** Path through the nested call tree. The top-level call is `[]`. */
|
|
199
|
+
traceAddress: number[];
|
|
206
200
|
};
|
|
207
201
|
|
|
208
202
|
/** A native token transfer. */
|
package/src/utils/finality.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { hexToNumber, numberToHex } from "viem";
|
|
2
|
+
import type { Chain, LightBlock } from "@/internal/types.js";
|
|
3
|
+
import { eth_getBlockByNumber } from "@/rpc/actions.js";
|
|
4
|
+
import type { Rpc } from "@/rpc/index.js";
|
|
5
|
+
|
|
6
|
+
export const DEFAULT_REORG_WINDOW = 180;
|
|
2
7
|
|
|
3
8
|
export const isAsyncExecutionChain = (chainId: number) =>
|
|
4
9
|
chainId === 143 ||
|
|
@@ -7,40 +12,68 @@ export const isAsyncExecutionChain = (chainId: number) =>
|
|
|
7
12
|
chainId === 43113;
|
|
8
13
|
|
|
9
14
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* @param chain The chain to get the finality block count for.
|
|
14
|
-
* @returns The finality block count.
|
|
15
|
+
* Finds a block whose timestamp is outside the reorg window.
|
|
16
|
+
* Block timestamps are monotonic, so a binary search avoids retaining or
|
|
17
|
+
* fetching one block per second on fast chains.
|
|
15
18
|
*/
|
|
16
|
-
export function
|
|
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
|
-
|
|
19
|
+
export async function getFinalizedBlock({
|
|
20
|
+
chain,
|
|
21
|
+
rpc,
|
|
22
|
+
latestBlock,
|
|
23
|
+
lowerBound,
|
|
24
|
+
}: {
|
|
25
|
+
chain: Chain;
|
|
26
|
+
rpc: Rpc;
|
|
27
|
+
latestBlock: LightBlock;
|
|
28
|
+
lowerBound?: LightBlock;
|
|
29
|
+
}): Promise<LightBlock> {
|
|
30
|
+
const { reorgWindow } = chain;
|
|
31
|
+
if (reorgWindow === 0) return latestBlock;
|
|
32
|
+
|
|
33
|
+
const targetTimestamp = hexToNumber(latestBlock.timestamp) - reorgWindow;
|
|
34
|
+
|
|
35
|
+
if (
|
|
36
|
+
lowerBound !== undefined &&
|
|
37
|
+
hexToNumber(lowerBound.timestamp) > targetTimestamp
|
|
38
|
+
) {
|
|
39
|
+
return lowerBound;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let low = lowerBound ? hexToNumber(lowerBound.number) + 1 : 0;
|
|
43
|
+
let high = hexToNumber(latestBlock.number);
|
|
44
|
+
let finalizedBlock = lowerBound;
|
|
45
|
+
|
|
46
|
+
const blockTimeSeconds =
|
|
47
|
+
chain.viemChain?.blockTime === undefined || chain.viemChain.blockTime <= 0
|
|
48
|
+
? 1
|
|
49
|
+
: chain.viemChain.blockTime / 1_000;
|
|
50
|
+
const estimatedBlockCount = Math.ceil(reorgWindow / blockTimeSeconds);
|
|
51
|
+
const estimatedBlockNumber = high - estimatedBlockCount;
|
|
52
|
+
let blockNumber = Math.max(low, Math.min(estimatedBlockNumber, high));
|
|
53
|
+
|
|
54
|
+
while (low <= high) {
|
|
55
|
+
const block = await eth_getBlockByNumber(
|
|
56
|
+
rpc,
|
|
57
|
+
[numberToHex(blockNumber), false],
|
|
58
|
+
{ retryNullBlockRequest: true },
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
if (hexToNumber(block.timestamp) <= targetTimestamp) {
|
|
62
|
+
finalizedBlock = block;
|
|
63
|
+
low = blockNumber + 1;
|
|
64
|
+
} else {
|
|
65
|
+
high = blockNumber - 1;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (low > high) break;
|
|
69
|
+
blockNumber = Math.floor((low + high) / 2);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (finalizedBlock === undefined) {
|
|
73
|
+
return eth_getBlockByNumber(rpc, ["0x0", false], {
|
|
74
|
+
retryNullBlockRequest: true,
|
|
75
|
+
});
|
|
43
76
|
}
|
|
44
77
|
|
|
45
|
-
return
|
|
78
|
+
return finalizedBlock;
|
|
46
79
|
}
|
package/src/utils/pglite.ts
CHANGED
|
@@ -1,16 +1,5 @@
|
|
|
1
1
|
import { mkdirSync } from "node:fs";
|
|
2
2
|
import { type PGliteOptions as Options, PGlite } from "@electric-sql/pglite";
|
|
3
|
-
import {
|
|
4
|
-
CompiledQuery,
|
|
5
|
-
type DatabaseConnection,
|
|
6
|
-
type Dialect,
|
|
7
|
-
type Kysely,
|
|
8
|
-
PostgresAdapter,
|
|
9
|
-
PostgresIntrospector,
|
|
10
|
-
PostgresQueryCompiler,
|
|
11
|
-
type QueryResult,
|
|
12
|
-
type TransactionSettings,
|
|
13
|
-
} from "kysely";
|
|
14
3
|
import type { Prettify } from "@/types/utils.js";
|
|
15
4
|
|
|
16
5
|
export type PGliteOptions = Prettify<Options & { dataDir: string }>;
|
|
@@ -26,71 +15,3 @@ export function createPglite(options: PGliteOptions) {
|
|
|
26
15
|
mkdirSync(options.dataDir, { recursive: true });
|
|
27
16
|
return new PGlite(options);
|
|
28
17
|
}
|
|
29
|
-
|
|
30
|
-
// Adapted from dnlsandiego/kysely-pglite
|
|
31
|
-
// https://github.com/dnlsandiego/kysely-pglite/blob/3891a0c4d9327a21bff26addf371784f0109260b/src/kysely-pglite.ts
|
|
32
|
-
export function createPgliteKyselyDialect(instance: PGlite) {
|
|
33
|
-
return {
|
|
34
|
-
createAdapter: () => new PostgresAdapter(),
|
|
35
|
-
createDriver: () => new PGliteDriver(instance),
|
|
36
|
-
createIntrospector: (db: Kysely<any>) => new PostgresIntrospector(db),
|
|
37
|
-
createQueryCompiler: () => new PostgresQueryCompiler(),
|
|
38
|
-
} satisfies Dialect;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Adapted from dnlsandiego/kysely-pglite
|
|
42
|
-
// https://github.com/dnlsandiego/kysely-pglite/blob/3891a0c4d9327a21bff26addf371784f0109260b/src/pglite-driver.ts
|
|
43
|
-
export class PGliteDriver {
|
|
44
|
-
#client: PGlite;
|
|
45
|
-
|
|
46
|
-
constructor(client: PGlite) {
|
|
47
|
-
this.#client = client;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async acquireConnection(): Promise<DatabaseConnection> {
|
|
51
|
-
return new PGliteConnection(this.#client);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async beginTransaction(
|
|
55
|
-
connection: DatabaseConnection,
|
|
56
|
-
_settings: TransactionSettings,
|
|
57
|
-
): Promise<void> {
|
|
58
|
-
await connection.executeQuery(CompiledQuery.raw("BEGIN"));
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async commitTransaction(connection: DatabaseConnection): Promise<void> {
|
|
62
|
-
await connection.executeQuery(CompiledQuery.raw("COMMIT"));
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
async rollbackTransaction(connection: DatabaseConnection): Promise<void> {
|
|
66
|
-
await connection.executeQuery(CompiledQuery.raw("ROLLBACK"));
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async destroy(): Promise<void> {
|
|
70
|
-
await this.#client.close();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async init(): Promise<void> {}
|
|
74
|
-
async releaseConnection(_connection: DatabaseConnection): Promise<void> {}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
class PGliteConnection implements DatabaseConnection {
|
|
78
|
-
#client: PGlite;
|
|
79
|
-
|
|
80
|
-
constructor(client: PGlite) {
|
|
81
|
-
this.#client = client;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async executeQuery<R>(
|
|
85
|
-
compiledQuery: CompiledQuery<any>,
|
|
86
|
-
): Promise<QueryResult<R>> {
|
|
87
|
-
return await this.#client.query<R>(compiledQuery.sql, [
|
|
88
|
-
...compiledQuery.parameters,
|
|
89
|
-
]);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// biome-ignore lint/correctness/useYield: PGlite never streams, but the interface requires this method.
|
|
93
|
-
async *streamQuery(): AsyncGenerator<never, void, unknown> {
|
|
94
|
-
throw new Error("PGlite does not support streaming.");
|
|
95
|
-
}
|
|
96
|
-
}
|