Skip to content

Commit

Permalink
Merge branch 'main' into 240919-query-types
Browse files Browse the repository at this point in the history
  • Loading branch information
qbzzt committed Sep 20, 2024
2 parents 8a25f0a + a08ba5e commit 6798bb8
Show file tree
Hide file tree
Showing 12 changed files with 641 additions and 510 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-gifts-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/store": patch
---

Improved config output type of `enumValues`.
11 changes: 11 additions & 0 deletions .changeset/thick-countries-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@latticexyz/explorer": patch
---

Renamed optional `waitForStateChange` param in `observer()` decorator to `waitForTransaction` to better align with `@latticexyz/store-sync` packages.

```diff
const { waitForTransaction } = syncToZustand(...);
-observer({ waitForStateChange: waitForTransaction });
+observer({ waitForTransaction });
```
36 changes: 15 additions & 21 deletions examples/local-explorer/packages/client/src/mud/setupNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { getNetworkConfig } from "./getNetworkConfig";
import IWorldAbi from "contracts/out/IWorld.sol/IWorld.abi.json";
import { createBurnerAccount, transportObserver } from "@latticexyz/common";
import { transactionQueue } from "@latticexyz/common/actions";
import { observer, type WaitForStateChange } from "@latticexyz/explorer/observer";
import { observer } from "@latticexyz/explorer/observer";

/*
* Import our MUD config, which includes strong types for
Expand All @@ -34,7 +34,6 @@ export type SetupNetworkResult = Awaited<ReturnType<typeof setupNetwork>>;

export async function setupNetwork() {
const networkConfig = await getNetworkConfig();
const waitForStateChange = Promise.withResolvers<WaitForStateChange>();

/*
* Create a viem public (read only) client
Expand All @@ -48,6 +47,19 @@ export async function setupNetwork() {

const publicClient = createPublicClient(clientOptions);

/*
* Sync on-chain state into RECS and keeps our client in sync.
* Uses the MUD indexer if available, otherwise falls back
* to the viem publicClient to make RPC calls to fetch MUD
* events from the chain.
*/
const { tables, useStore, latestBlock$, storedBlockLogs$, waitForTransaction } = await syncToZustand({
config: mudConfig,
address: networkConfig.worldAddress as Hex,
publicClient,
startBlock: BigInt(networkConfig.initialBlockNumber),
});

/*
* Create a temporary wallet and a viem client for it
* (see https://viem.sh/docs/clients/wallet.html).
Expand All @@ -58,11 +70,7 @@ export async function setupNetwork() {
account: burnerAccount,
})
.extend(transactionQueue())
.extend(
observer({
waitForStateChange: (hash) => waitForStateChange.promise.then((fn) => fn(hash)),
}),
);
.extend(observer({ waitForTransaction }));

/*
* Create an object for communicating with the deployed World.
Expand All @@ -73,20 +81,6 @@ export async function setupNetwork() {
client: { public: publicClient, wallet: burnerWalletClient },
});

/*
* Sync on-chain state into RECS and keeps our client in sync.
* Uses the MUD indexer if available, otherwise falls back
* to the viem publicClient to make RPC calls to fetch MUD
* events from the chain.
*/
const { tables, useStore, latestBlock$, storedBlockLogs$, waitForTransaction } = await syncToZustand({
config: mudConfig,
address: networkConfig.worldAddress as Hex,
publicClient,
startBlock: BigInt(networkConfig.initialBlockNumber),
});
waitForStateChange.resolve(waitForTransaction);

return {
tables,
useStore,
Expand Down
5 changes: 2 additions & 3 deletions packages/explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dev": "tsup --watch",
"explorer:dev": "next dev --port 13690",
"explorer:start": "node .next/standalone/packages/explorer/server.js",
"knip": "knip",
"lint": "next lint"
},
"dependencies": {
Expand All @@ -43,13 +44,11 @@
"@latticexyz/world": "workspace:*",
"@radix-ui/react-checkbox": "^1.1.1",
"@radix-ui/react-dialog": "^1.1.1",
"@radix-ui/react-dropdown-menu": "^2.1.1",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-popover": "^1.1.1",
"@radix-ui/react-select": "^2.1.1",
"@radix-ui/react-separator": "^1.1.0",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-toast": "^1.2.1",
"@radix-ui/themes": "^3.0.5",
"@rainbow-me/rainbowkit": "^2.1.5",
"@tanstack/react-query": "^5.51.3",
Expand Down Expand Up @@ -79,12 +78,12 @@
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/better-sqlite3": "^7.6.4",
"@types/debug": "^4.1.7",
"@types/minimist": "^1.2.5",
"@types/node": "^18.15.11",
"@types/react": "18.2.22",
"@types/react-dom": "18.2.7",
"@types/yargs": "^17.0.10",
"eslint-config-next": "14.2.3",
"knip": "^5.30.2",
"postcss": "^8",
"prettier": "3.2.5",
"prettier-plugin-tailwindcss": "^0.6.5",
Expand Down
188 changes: 0 additions & 188 deletions packages/explorer/src/components/ui/DropdownMenu.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion packages/explorer/src/exports/observer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { createBridge, type CreateBridgeOpts } from "../observer/bridge";
export type { Messages, MessageType, EmitMessage } from "../observer/messages";
export { observer, type ObserverOptions, type WaitForStateChange } from "../observer/decorator";
export { observer, type ObserverOptions, type WaitForTransaction } from "../observer/decorator";
14 changes: 7 additions & 7 deletions packages/explorer/src/observer/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { formatAbiItem, getAction } from "viem/utils";
import { createBridge } from "./bridge";
import { ReceiptSummary } from "./common";

export type WaitForStateChange = (hash: Hex) => Promise<ReceiptSummary>;
export type WaitForTransaction = (hash: Hex) => Promise<ReceiptSummary>;

export type ObserverOptions = {
explorerUrl?: string;
waitForStateChange?: WaitForStateChange;
waitForTransaction?: WaitForTransaction;
};

let writeCounter = 0;

export function observer({ explorerUrl = "http://localhost:13690", waitForStateChange }: ObserverOptions = {}): <
export function observer({ explorerUrl = "http://localhost:13690", waitForTransaction }: ObserverOptions = {}): <
transport extends Transport,
chain extends Chain | undefined = Chain | undefined,
account extends Account | undefined = Account | undefined,
Expand Down Expand Up @@ -52,12 +52,12 @@ export function observer({ explorerUrl = "http://localhost:13690", waitForStateC
});
});

if (waitForStateChange) {
if (waitForTransaction) {
write.then((hash) => {
const receipt = waitForStateChange(hash);
emit("waitForStateChange", { writeId });
const receipt = waitForTransaction(hash);
emit("waitForTransaction", { writeId });
Promise.allSettled([receipt]).then(([result]) => {
emit("waitForStateChange:result", { ...result, writeId });
emit("waitForTransaction:result", { ...result, writeId });
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/explorer/src/observer/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export type Messages = {
"waitForTransactionReceipt:result": PromiseSettledResult<ReceiptSummary> & {
writeId: string;
};
waitForStateChange: {
waitForTransaction: {
writeId: string;
};
"waitForStateChange:result": PromiseSettledResult<ReceiptSummary> & {
"waitForTransaction:result": PromiseSettledResult<ReceiptSummary> & {
writeId: string;
};
};
Expand Down
6 changes: 2 additions & 4 deletions packages/store-sync/src/stash/syncToStash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export type SyncToStashOptions = {
startSync?: boolean;
};

export type SyncToStashResult = Omit<SyncResult, "waitForTransaction"> & {
waitForStateChange: SyncResult["waitForTransaction"];
export type SyncToStashResult = SyncResult & {
stopSync: () => void;
};

Expand All @@ -50,7 +49,7 @@ export async function syncToStash({

const storageAdapter = createStorageAdapter({ stash });

const { waitForTransaction: waitForStateChange, ...sync } = await createStoreSync({
const sync = await createStoreSync({
storageAdapter,
publicClient: client.extend(publicActions) as never,
address,
Expand All @@ -70,7 +69,6 @@ export async function syncToStash({

return {
...sync,
waitForStateChange,
stopSync,
};
}
Loading

0 comments on commit 6798bb8

Please sign in to comment.