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

foundation for MUD workshop #1

Closed
wants to merge 9 commits into from
Closed
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
8 changes: 4 additions & 4 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ services:
container_name: foundry
image: ghcr.io/foundry-rs/foundry:nightly-515a4cc8aba1627a717a1057ff4f09c8cd3bf51f
platform: linux/amd64 # Specify the platform
entrypoint: anvil --block-time 1 --block-base-fee-per-gas 0 --gas-limit 3000000000 --hardfork cancun --host 0.0.0.0
entrypoint: anvil --block-base-fee-per-gas 0 --gas-limit 3000000000 --hardfork cancun --host 0.0.0.0 --port 8546
ports:
- "8545:8545"
- "8546:8546"
healthcheck:
test: [ "CMD-SHELL", "anvil --help || exit 1" ]
test: ["CMD-SHELL", "anvil --help || exit 1"]
interval: 10s
timeout: 5s
retries: 5
Expand All @@ -22,5 +22,5 @@ services:
depends_on:
foundry:
condition: service_healthy
command: "--rpc-url http://foundry:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
command: "--rpc-url http://foundry:8546 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
tty: true
12 changes: 2 additions & 10 deletions smart-turret/mprocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@ procs:
shell: pnpm run dev
contracts:
cwd: packages/contracts
shell: pnpm mud dev-contracts --rpc http://127.0.0.1:8545
shell: pnpm mud dev-contracts --rpc http://127.0.0.1:8545 --worldAddress 0x8a791620dd6260079bf849dc5567adc3f2fdc318
anvil:
cwd: packages/contracts
shell: anvil --base-fee 0 --block-time 2
indexer:
cwd: packages/contracts
shell: shx rm -rf $SQLITE_FILENAME && pnpm sqlite-indexer
env:
DEBUG: mud:*
RPC_HTTP_URL: "http://127.0.0.1:8545"
FOLLOW_BLOCK_TAG: "latest"
SQLITE_FILENAME: "indexer.db"
shell: anvil --base-fee 0 --block-time 2 --fork-url http://127.0.0.1:8546
explorer:
cwd: packages/contracts
shell: pnpm explorer
8 changes: 4 additions & 4 deletions smart-turret/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"test": "pnpm recursive run test"
},
"devDependencies": {
"@latticexyz/cli": "2.2.8",
"@latticexyz/common": "2.2.8",
"@latticexyz/explorer": "2.2.8",
"@latticexyz/store-indexer": "2.2.8",
"@latticexyz/cli": "2.2.10",
"@latticexyz/common": "2.2.10",
"@latticexyz/explorer": "2.2.11-main-85bbeb8be12597f28cd1506dae0d44b34c1427e4",
"@latticexyz/store-indexer": "2.2.10",
"@types/debug": "4.1.7",
"@typescript-eslint/eslint-plugin": "7.1.1",
"@typescript-eslint/parser": "7.1.1",
Expand Down
16 changes: 9 additions & 7 deletions smart-turret/packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
"test": "tsc --noEmit"
},
"dependencies": {
"@latticexyz/common": "2.2.8",
"@latticexyz/dev-tools": "2.2.8",
"@latticexyz/react": "2.2.8",
"@latticexyz/schema-type": "2.2.8",
"@latticexyz/store-sync": "2.2.8",
"@latticexyz/utils": "2.2.8",
"@latticexyz/world": "2.2.8",
"@latticexyz/common": "2.2.10",
"@latticexyz/dev-tools": "2.2.10",
"@latticexyz/explorer": "^2.2.10",
"@latticexyz/react": "2.2.10",
"@latticexyz/schema-type": "2.2.10",
"@latticexyz/stash": "^2.2.10",
"@latticexyz/store-sync": "2.2.10",
"@latticexyz/utils": "2.2.10",
"@latticexyz/world": "2.2.10",
"contracts": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
20 changes: 1 addition & 19 deletions smart-turret/packages/client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
import { useMUD } from "./MUDContext";

const styleUnset = { all: "unset" } as const;

export const App = () => {
const {
network: { tables, useStore }
} = useMUD();

const tasks = useStore((state) => {
const records = Object.values(state.getRecords(tables.Tasks));
records.sort((a, b) => Number(a.value.createdAt - b.value.createdAt));
return records;
});

return (
<>

</>
);
return <>Hello, World</>;
};
21 changes: 0 additions & 21 deletions smart-turret/packages/client/src/MUDContext.tsx

This file was deleted.

31 changes: 4 additions & 27 deletions smart-turret/packages/client/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
import ReactDOM from "react-dom/client";
import { App } from "./App";
import { setup } from "./mud/setup";
import { MUDProvider } from "./MUDContext";
import mudConfig from "contracts/mud.config";
import { sync } from "./mud/sync";

sync();

const rootElement = document.getElementById("react-root");
if (!rootElement) throw new Error("React root not found");
const root = ReactDOM.createRoot(rootElement);

// TODO: figure out if we actually want this to be async or if we should render something else in the meantime
setup().then(async (result) => {
root.render(
<MUDProvider value={result}>
<App />
</MUDProvider>,
);

// https://vitejs.dev/guide/env-and-mode.html
if (import.meta.env.DEV) {
const { mount: mountDevTools } = await import("@latticexyz/dev-tools");
mountDevTools({
config: mudConfig,
publicClient: result.network.publicClient,
walletClient: result.network.walletClient,
latestBlock$: result.network.latestBlock$,
storedBlockLogs$: result.network.storedBlockLogs$,
worldAddress: result.network.worldContract.address,
worldAbi: result.network.worldContract.abi,
write$: result.network.write$,
useStore: result.network.useStore,
});
}
});
root.render(<App />);
34 changes: 34 additions & 0 deletions smart-turret/packages/client/src/mud/clients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
ClientConfig,
createPublicClient,
createWalletClient,
http,
PublicClient,
Transport,
WalletClient,
} from "viem";
import { networkConfig } from "./networkConfig";
import { createBurnerAccount } from "@latticexyz/common";
import { transactionQueue } from "@latticexyz/common/actions";
import { observer } from "@latticexyz/explorer/observer";

const clientOptions = {
chain: networkConfig.chain,
transport: http(),
pollingInterval: 1000,
} as const satisfies ClientConfig;

export const publicClient: PublicClient<Transport, typeof networkConfig.chain> =
createPublicClient(clientOptions);

const burnerAccount = createBurnerAccount(networkConfig.privateKey);
export const burnerWalletClient: WalletClient<
Transport,
typeof networkConfig.chain,
typeof burnerAccount
> = createWalletClient({
...clientOptions,
account: burnerAccount,
})
.extend(transactionQueue())
.extend(observer());
34 changes: 0 additions & 34 deletions smart-turret/packages/client/src/mud/createSystemCalls.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ import worlds from "contracts/worlds.json";
* The supported chains.
*/
import { supportedChains } from "./supportedChains";
import { Hex } from "viem";
import { MUDChain } from "@latticexyz/common/chains";

export async function getNetworkConfig() {
type NetworkConfig = {
privateKey: Hex;
chainId: number;
chain: MUDChain;
worldAddress: Hex;
initialBlockNumber: bigint;
};

function getNetworkConfig(): NetworkConfig {
const params = new URLSearchParams(window.location.search);

/*
Expand All @@ -33,7 +43,12 @@ export async function getNetworkConfig() {
* vite dev server was started or client was built
* 4. The default, 31337 (anvil)
*/
const chainId = Number(params.get("chainId") || params.get("chainid") || import.meta.env.VITE_CHAIN_ID || 31337);
const chainId = Number(
params.get("chainId") ||
params.get("chainid") ||
import.meta.env.VITE_CHAIN_ID ||
31337
);

/*
* Find the chain (unless it isn't in the list of supported chains).
Expand All @@ -50,9 +65,11 @@ export async function getNetworkConfig() {
* provide it as worldAddress in the query string.
*/
const world = worlds[chain.id.toString()];
const worldAddress = params.get("worldAddress") || world?.address;
const worldAddress = (params.get("worldAddress") as Hex) || world?.address;
if (!worldAddress) {
throw new Error(`No world address found for chain ${chainId}. Did you run \`mud deploy\`?`);
throw new Error(
`No world address found for chain ${chainId}. Did you run \`mud deploy\`?`
);
}

/*
Expand All @@ -62,15 +79,22 @@ export async function getNetworkConfig() {
* on the URL (as initialBlockNumber) or in the worlds.json
* file. If neither has it, it starts at the first block, zero.
*/
const initialBlockNumber = params.has("initialBlockNumber")
? Number(params.get("initialBlockNumber"))
: world?.blockNumber ?? 0n;
const initialBlockNumber = BigInt(
params.has("initialBlockNumber")
? params.get("initialBlockNumber")!
: world?.blockNumber ?? 0
);

return {
privateKey: getBurnerPrivateKey(),
// For testing purposes private key funded on local chain.
// Replace with `getBurnerPrivateKey()` or external wallet connection.
privateKey:
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
chainId,
chain,
worldAddress,
initialBlockNumber,
};
}

export const networkConfig: NetworkConfig = getNetworkConfig();
18 changes: 0 additions & 18 deletions smart-turret/packages/client/src/mud/setup.ts

This file was deleted.

Loading