@latticexyz/store-indexer 2.0.0-next.1 → 2.0.0-next.2
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/package.json +8 -7
- package/src/sqlite/createIndexer.ts +0 -92
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@latticexyz/store-indexer",
|
3
|
-
"version": "2.0.0-next.
|
3
|
+
"version": "2.0.0-next.2",
|
4
4
|
"description": "Minimal Typescript indexer for Store",
|
5
5
|
"repository": {
|
6
6
|
"type": "git",
|
@@ -14,21 +14,22 @@
|
|
14
14
|
},
|
15
15
|
"types": "src/index.ts",
|
16
16
|
"dependencies": {
|
17
|
+
"@fastify/cors": "^8.3.0",
|
17
18
|
"@trpc/client": "10.34.0",
|
18
19
|
"@trpc/server": "10.34.0",
|
19
20
|
"@wagmi/chains": "^0.2.22",
|
20
21
|
"better-sqlite3": "^8.4.0",
|
21
|
-
"cors": "^2.8.5",
|
22
22
|
"debug": "^4.3.4",
|
23
23
|
"drizzle-orm": "^0.27.0",
|
24
|
+
"fastify": "^4.21.0",
|
24
25
|
"rxjs": "7.5.5",
|
25
26
|
"superjson": "^1.12.4",
|
26
|
-
"viem": "1.
|
27
|
+
"viem": "1.6.0",
|
27
28
|
"zod": "^3.21.4",
|
28
|
-
"@latticexyz/block-logs-stream": "2.0.0-next.
|
29
|
-
"@latticexyz/common": "2.0.0-next.
|
30
|
-
"@latticexyz/store": "2.0.0-next.
|
31
|
-
"@latticexyz/store-sync": "2.0.0-next.
|
29
|
+
"@latticexyz/block-logs-stream": "2.0.0-next.2",
|
30
|
+
"@latticexyz/common": "2.0.0-next.2",
|
31
|
+
"@latticexyz/store": "2.0.0-next.2",
|
32
|
+
"@latticexyz/store-sync": "2.0.0-next.2"
|
32
33
|
},
|
33
34
|
"devDependencies": {
|
34
35
|
"@types/better-sqlite3": "^7.6.4",
|
@@ -1,92 +0,0 @@
|
|
1
|
-
import { PublicClient } from "viem";
|
2
|
-
import {
|
3
|
-
createBlockStream,
|
4
|
-
isNonPendingBlock,
|
5
|
-
blockRangeToLogs,
|
6
|
-
groupLogsByBlockNumber,
|
7
|
-
} from "@latticexyz/block-logs-stream";
|
8
|
-
import { concatMap, filter, from, map, mergeMap, tap } from "rxjs";
|
9
|
-
import { storeEventsAbi } from "@latticexyz/store";
|
10
|
-
import { blockLogsToStorage } from "@latticexyz/store-sync";
|
11
|
-
import { sqliteStorage } from "@latticexyz/store-sync/sqlite";
|
12
|
-
import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core";
|
13
|
-
import { debug } from "../debug";
|
14
|
-
|
15
|
-
type CreateIndexerOptions = {
|
16
|
-
/**
|
17
|
-
* [SQLite database object from Drizzle][0].
|
18
|
-
*
|
19
|
-
* [0]: https://orm.drizzle.team/docs/installation-and-db-connection/sqlite/better-sqlite3
|
20
|
-
*/
|
21
|
-
database: BaseSQLiteDatabase<"sync", any>;
|
22
|
-
/**
|
23
|
-
* [viem `PublicClient`][0] used for fetching logs from the RPC.
|
24
|
-
*
|
25
|
-
* [0]: https://viem.sh/docs/clients/public.html
|
26
|
-
*/
|
27
|
-
publicClient: PublicClient;
|
28
|
-
/**
|
29
|
-
* Optional block number to start indexing from. Useful for resuming the indexer from a particular point in time or starting after a particular contract deployment.
|
30
|
-
*/
|
31
|
-
startBlock?: bigint;
|
32
|
-
/**
|
33
|
-
* Optional maximum block range, if your RPC limits the amount of blocks fetched at a time.
|
34
|
-
*/
|
35
|
-
maxBlockRange?: bigint;
|
36
|
-
};
|
37
|
-
|
38
|
-
/**
|
39
|
-
* Creates an indexer to process and store blockchain events.
|
40
|
-
*
|
41
|
-
* @param {CreateIndexerOptions} options See `CreateIndexerOptions`.
|
42
|
-
* @returns A function to unsubscribe from the block stream, effectively stopping the indexer.
|
43
|
-
*/
|
44
|
-
export async function createIndexer({
|
45
|
-
database,
|
46
|
-
publicClient,
|
47
|
-
startBlock = 0n,
|
48
|
-
maxBlockRange,
|
49
|
-
}: CreateIndexerOptions): Promise<() => void> {
|
50
|
-
const latestBlock$ = createBlockStream({ publicClient, blockTag: "latest" });
|
51
|
-
|
52
|
-
const latestBlockNumber$ = latestBlock$.pipe(
|
53
|
-
filter(isNonPendingBlock),
|
54
|
-
map((block) => block.number)
|
55
|
-
);
|
56
|
-
|
57
|
-
let latestBlockNumber: bigint | null = null;
|
58
|
-
const blockLogs$ = latestBlockNumber$.pipe(
|
59
|
-
tap((blockNumber) => {
|
60
|
-
latestBlockNumber = blockNumber;
|
61
|
-
debug("latest block number", blockNumber);
|
62
|
-
}),
|
63
|
-
map((blockNumber) => ({ startBlock, endBlock: blockNumber })),
|
64
|
-
blockRangeToLogs({
|
65
|
-
publicClient,
|
66
|
-
events: storeEventsAbi,
|
67
|
-
maxBlockRange,
|
68
|
-
}),
|
69
|
-
tap(({ fromBlock, toBlock, logs }) => {
|
70
|
-
debug("found", logs.length, "logs for block", fromBlock, "-", toBlock);
|
71
|
-
}),
|
72
|
-
mergeMap(({ toBlock, logs }) => from(groupLogsByBlockNumber(logs, toBlock)))
|
73
|
-
);
|
74
|
-
|
75
|
-
let lastBlockNumberProcessed: bigint | null = null;
|
76
|
-
const sub = blockLogs$
|
77
|
-
.pipe(
|
78
|
-
concatMap(blockLogsToStorage(await sqliteStorage({ database, publicClient }))),
|
79
|
-
tap(({ blockNumber, operations }) => {
|
80
|
-
lastBlockNumberProcessed = blockNumber;
|
81
|
-
debug("stored", operations.length, "operations for block", blockNumber);
|
82
|
-
if (latestBlockNumber === lastBlockNumberProcessed) {
|
83
|
-
debug("all caught up");
|
84
|
-
}
|
85
|
-
})
|
86
|
-
)
|
87
|
-
.subscribe();
|
88
|
-
|
89
|
-
return () => {
|
90
|
-
sub.unsubscribe();
|
91
|
-
};
|
92
|
-
}
|