Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(store-sync): use client instead of decorated public client #3441

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-experts-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/store-sync": patch
---

All sync methods using a `publicClient` argument now accept a plain Viem `Client` rather than a decorated `PublicClient`, allowing for more flexibility and better tree-shaking for lighter bundles.
8 changes: 4 additions & 4 deletions packages/store-sync/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, Block, Hex, Log, PublicClient, TransactionReceipt } from "viem";
import { Address, Block, Client, Hex, Log, TransactionReceipt } from "viem";
import { StoreEventsAbiItem, StoreEventsAbi } from "@latticexyz/store";
import { Observable } from "rxjs";
import { UnionPick } from "@latticexyz/common/type-utils";
Expand Down Expand Up @@ -67,11 +67,11 @@ export type SyncFilter = {

export type SyncOptions = {
/**
* [viem `PublicClient`][0] used for fetching logs from the RPC.
* [viem `Client`][0] used for fetching logs from the RPC.
*
* [0]: https://viem.sh/docs/clients/public.html
* [0]: https://viem.sh/docs/clients/custom
*/
publicClient: PublicClient;
publicClient: Client;
/**
* MUD Store/World contract address
*/
Expand Down
10 changes: 8 additions & 2 deletions packages/store-sync/src/createStoreSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import { fetchAndStoreLogs } from "./fetchAndStoreLogs";
import { isLogsApiResponse } from "./indexer-client/isLogsApiResponse";
import { toStorageAdatperBlock } from "./indexer-client/toStorageAdapterBlock";
import { watchLogs } from "./watchLogs";
import { getAction } from "viem/utils";
import { getChainId, getTransactionReceipt } from "viem/actions";

const debug = parentDebug.extend("createStoreSync");

Expand Down Expand Up @@ -93,7 +95,7 @@ export async function createStoreSync({
: undefined)
: undefined;

const chainId = publicClient.chain?.id ?? (await publicClient.getChainId());
const chainId = publicClient.chain?.id ?? (await getAction(publicClient, getChainId, "getChainId")({}));

const initialBlockLogs$ = defer(async (): Promise<StorageAdapterBlock | undefined> => {
onProgress?.({
Expand Down Expand Up @@ -352,7 +354,11 @@ export async function createStoreSync({
try {
const lastBlock = blocks[0];
debug("fetching tx receipt for block", lastBlock.blockNumber);
const { status, blockNumber, transactionHash } = await publicClient.getTransactionReceipt({ hash: tx });
const { status, blockNumber, transactionHash } = await getAction(
publicClient,
getTransactionReceipt,
"getTransactionReceipt",
)({ hash: tx });
if (lastBlock.blockNumber >= blockNumber) {
return { status, blockNumber, transactionHash };
}
Expand Down
4 changes: 2 additions & 2 deletions packages/store-sync/src/getRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fetchBlockLogs } from "@latticexyz/block-logs-stream";
import { Table } from "@latticexyz/config";
import { getSchemaPrimitives } from "@latticexyz/protocol-parser/internal";
import { LogToRecordArgs, flattenStoreLogs, getStoreLogs, logToRecord } from "@latticexyz/store/internal";
import { Address, Client, createPublicClient, http } from "viem";
import { Address, Client, createClient, http } from "viem";
import { getAction } from "viem/utils";
import { getBlockNumber } from "viem/actions";
import { debug } from "./debug";
Expand Down Expand Up @@ -59,7 +59,7 @@ export async function getRecords<table extends Table>(
} else {
const client =
options.client ??
createPublicClient({
createClient({
transport: http(options.rpcUrl),
});
debug("fetching records for", options.table.label, "via RPC from", client.transport.url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it } from "vitest";
import { 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 { Hex, RpcLog, createClient, decodeEventLog, formatLog, http } from "viem";
import { foundry } from "viem/chains";
import { getTables } from "./getTables";
import { PostgresStorageAdapter, createStorageAdapter } from "./createStorageAdapter";
Expand Down Expand Up @@ -31,7 +31,7 @@ describe("createStorageAdapter", async () => {
// logger: new DefaultLogger(),
});

const publicClient = createPublicClient({
const publicClient = createClient({
chain: foundry,
transport: http(),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hex, PublicClient, concatHex, getAddress } from "viem";
import { Client, Hex, concatHex, getAddress } from "viem";
import { PgDatabase, QueryResultHKT } from "drizzle-orm/pg-core";
import { and, eq } from "drizzle-orm";
import { buildTable } from "./buildTable";
Expand Down Expand Up @@ -26,7 +26,7 @@ export async function createStorageAdapter({
publicClient,
}: {
database: PgDatabase<QueryResultHKT>;
publicClient: PublicClient;
publicClient: Client;
}): Promise<PostgresStorageAdapter> {
const bytesStorageAdapter = await createBytesStorageAdapter({ database, publicClient });
const cleanUp: (() => Promise<void>)[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it } from "vitest";
import { 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 { Hex, RpcLog, createClient, decodeEventLog, formatLog, http } from "viem";
import { foundry } from "viem/chains";
import { PostgresStorageAdapter, createStorageAdapter } from "./createStorageAdapter";
import { groupLogsByBlockNumber } from "@latticexyz/block-logs-stream";
Expand Down Expand Up @@ -30,7 +30,7 @@ describe("createStorageAdapter", async () => {
// logger: new DefaultLogger(),
});

const publicClient = createPublicClient({
const publicClient = createClient({
chain: foundry,
transport: http(),
});
Expand Down
8 changes: 5 additions & 3 deletions packages/store-sync/src/postgres/createStorageAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PublicClient, encodePacked, size } from "viem";
import { Client, encodePacked, size } from "viem";
import { PgDatabase, QueryResultHKT } from "drizzle-orm/pg-core";
import { and, eq } from "drizzle-orm";
import { debug } from "./debug";
Expand All @@ -7,6 +7,8 @@ import { spliceHex } from "@latticexyz/common";
import { setupTables } from "./setupTables";
import { StorageAdapter, StorageAdapterBlock } from "../common";
import { version } from "./version";
import { getAction } from "viem/utils";
import { getChainId } from "viem/actions";

// Currently assumes one DB per chain ID

Expand All @@ -21,11 +23,11 @@ export async function createStorageAdapter({
publicClient,
}: {
database: PgDatabase<QueryResultHKT>;
publicClient: PublicClient;
publicClient: Client;
}): Promise<PostgresStorageAdapter> {
const cleanUp: (() => Promise<void>)[] = [];

const chainId = publicClient.chain?.id ?? (await publicClient.getChainId());
const chainId = publicClient.chain?.id ?? (await getAction(publicClient, getChainId, "getChainId")({}));

cleanUp.push(await setupTables(database, Object.values(tables)));

Expand Down
4 changes: 2 additions & 2 deletions packages/store-sync/src/sqlite/sqliteStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core";
import { buildTable } from "./buildTable";
import initSqlJs from "sql.js";
import { drizzle } from "drizzle-orm/sql-js";
import { Hex, RpcLog, createPublicClient, decodeEventLog, formatLog, http } from "viem";
import { Hex, RpcLog, createClient, decodeEventLog, formatLog, http } from "viem";
import { foundry } from "viem/chains";
import worldRpcLogs from "../../../../test-data/world-logs.json";
import { storeEventsAbi } from "@latticexyz/store";
Expand All @@ -32,7 +32,7 @@ describe("sqliteStorage", async () => {
const SqlJs = await initSqlJs();
let db: BaseSQLiteDatabase<"sync", void>;

const publicClient = createPublicClient({
const publicClient = createClient({
chain: foundry,
transport: http(),
});
Expand Down
8 changes: 5 additions & 3 deletions packages/store-sync/src/sqlite/sqliteStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hex, PublicClient, concatHex, getAddress, size } from "viem";
import { Client, Hex, concatHex, getAddress, size } from "viem";
import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core";
import { and, eq, sql } from "drizzle-orm";
import { sqliteTableToSql } from "./sqliteTableToSql";
Expand All @@ -13,6 +13,8 @@ import { isTableRegistrationLog } from "../isTableRegistrationLog";
import { logToTable } from "../logToTable";
import { hexToResource, resourceToLabel, spliceHex } from "@latticexyz/common";
import { KeySchema, decodeKey, decodeValueArgs } from "@latticexyz/protocol-parser/internal";
import { getChainId } from "viem/actions";
import { getAction } from "viem/utils";

// TODO: upgrade drizzle and use async sqlite interface for consistency

Expand All @@ -21,9 +23,9 @@ export async function sqliteStorage({
publicClient,
}: {
database: BaseSQLiteDatabase<"sync", void>;
publicClient: PublicClient;
publicClient: Client;
}): Promise<StorageAdapter> {
const chainId = publicClient.chain?.id ?? (await publicClient.getChainId());
const chainId = publicClient.chain?.id ?? (await getAction(publicClient, getChainId, "getChainId")({}));

// TODO: should these run lazily before first `registerTables`?
database.run(sql.raw(sqliteTableToSql(chainState)));
Expand Down
Loading