/
createStorageAdapter.test.ts
89 lines (80 loc) · 2.8 KB
/
createStorageAdapter.test.ts
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
import { beforeEach, describe, expect, it } from "vitest";
import { DefaultLogger, eq } from "drizzle-orm";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { Hex, RpcLog, createPublicClient, decodeEventLog, formatLog, http } from "viem";
import { foundry } from "viem/chains";
import { PostgresStorageAdapter, createStorageAdapter } from "./createStorageAdapter";
import { groupLogsByBlockNumber } from "@latticexyz/block-logs-stream";
import { storeEventsAbi } from "@latticexyz/store";
import { StoreEventsLog } from "../common";
import worldRpcLogs from "../../../../test-data/world-logs.json";
import { resourceToHex } from "@latticexyz/common";
const blocks = groupLogsByBlockNumber(
worldRpcLogs.map((log) => {
const { eventName, args } = decodeEventLog({
abi: storeEventsAbi,
data: log.data as Hex,
topics: log.topics as [Hex, ...Hex[]],
strict: true,
});
return formatLog(log as any as RpcLog, { args, eventName: eventName as string }) as StoreEventsLog;
}),
);
describe("createStorageAdapter", async () => {
const db = drizzle(postgres(process.env.DATABASE_URL!), {
// TODO: make a debug-based logger so this can be toggled by env var
// logger: new DefaultLogger(),
});
const publicClient = createPublicClient({
chain: foundry,
transport: http(),
});
let storageAdapter: PostgresStorageAdapter;
beforeEach(async () => {
storageAdapter = await createStorageAdapter({ database: db, publicClient });
return storageAdapter.cleanUp;
});
it("should create tables and data from block log", async () => {
for (const block of blocks) {
await storageAdapter.storageAdapter(block);
}
expect(await db.select().from(storageAdapter.tables.configTable)).toMatchInlineSnapshot(`
[
{
"blockNumber": 21n,
"chainId": 31337,
"version": "0.0.7",
},
]
`);
expect(
await db
.select()
.from(storageAdapter.tables.recordsTable)
.where(
eq(
storageAdapter.tables.recordsTable.tableId,
resourceToHex({ type: "table", namespace: "", name: "NumberList" }),
),
),
).toMatchInlineSnapshot(`
[
{
"address": "0x7C78d585F136d7247f9deA68f60CE8A2D3F311E2",
"blockNumber": 21n,
"dynamicData": "0x000001a400000045",
"encodedLengths": "0x0000000000000000000000000000000000000000000000000800000000000008",
"isDeleted": false,
"key0": null,
"key1": null,
"keyBytes": "0x",
"logIndex": 1,
"staticData": null,
"tableId": "0x746200000000000000000000000000004e756d6265724c697374000000000000",
},
]
`);
await storageAdapter.cleanUp();
}, 15_000);
});