/
createStorageAdapter.test.ts
120 lines (109 loc) · 3.58 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
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
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 { getTables } from "./getTables";
import { PostgresStorageAdapter, createStorageAdapter } from "./createStorageAdapter";
import { buildTable } from "./buildTable";
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!), {
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": 20n,
"chainId": 31337,
"version": "0.0.6",
},
]
`);
expect(
await db
.select()
.from(storageAdapter.tables.recordsTable)
.where(
eq(
storageAdapter.tables.recordsTable.tableId,
resourceToHex({ type: "table", namespace: "", name: "NumberList" })
)
)
).toMatchInlineSnapshot(`
[
{
"address": "0x2964aF56c8aACdE425978a28b018956D21cF50f0",
"blockNumber": 20n,
"dynamicData": "0x000001a400000045",
"encodedLengths": "0x0000000000000000000000000000000000000000000000000800000000000008",
"isDeleted": false,
"key0": null,
"key1": null,
"keyBytes": "0x",
"logIndex": 1,
"staticData": null,
"tableId": "0x746200000000000000000000000000004e756d6265724c697374000000000000",
},
]
`);
const tables = (await getTables(db)).filter((table) => table.name === "NumberList");
expect(tables).toMatchInlineSnapshot(`
[
{
"address": "0x2964aF56c8aACdE425978a28b018956D21cF50f0",
"keySchema": {},
"name": "NumberList",
"namespace": "",
"tableId": "0x746200000000000000000000000000004e756d6265724c697374000000000000",
"valueSchema": {
"value": "uint32[]",
},
},
]
`);
const sqlTable = buildTable(tables[0]);
expect(await db.select().from(sqlTable)).toMatchInlineSnapshot(`
[
{
"__keyBytes": "0x",
"__lastUpdatedBlockNumber": 20n,
"value": [
420,
69,
],
},
]
`);
await storageAdapter.cleanUp();
}, 15_000);
});