diff --git a/packages/client/src/lib/mud/setupNetwork.ts b/packages/client/src/lib/mud/setupNetwork.ts index 8799623..761709a 100644 --- a/packages/client/src/lib/mud/setupNetwork.ts +++ b/packages/client/src/lib/mud/setupNetwork.ts @@ -3,22 +3,29 @@ * (https://viem.sh/docs/getting-started.html). * This line imports the functions we need from it. */ -import type { ClientConfig, Hex } from "viem" -import type { ContractWrite } from "@latticexyz/common"; - -import { get } from "svelte/store" -import { createPublicClient, fallback, webSocket, http, createWalletClient, parseEther } from "viem"; -import { createFaucetService } from "@latticexyz/services/faucet"; -import { encodeEntity, syncToRecs } from "@latticexyz/store-sync/recs"; - -import { walletState } from "../metamask" - -import { getNetworkConfig } from "./getNetworkConfig"; -import { world } from "./world"; -import IWorldAbi from "contracts/out/IWorld.sol/IWorld.abi.json"; -import { createBurnerAccount, getContract, transportObserver } from "@latticexyz/common"; - -import { share, Subject } from "rxjs"; +import type { ClientConfig, Hex } from 'viem'; +import type { ContractWrite } from '@latticexyz/common'; + +import { get } from 'svelte/store'; +import { + createPublicClient, + fallback, + webSocket, + http, + createWalletClient, + parseEther +} from 'viem'; +import { createFaucetService } from '@latticexyz/services/faucet'; +import { encodeEntity, syncToRecs } from '@latticexyz/store-sync/recs'; + +import { walletState } from '../metamask'; + +import { getNetworkConfig } from './getNetworkConfig'; +import { world } from './world'; +import IWorldAbi from 'contracts/out/IWorld.sol/IWorld.abi.json'; +import { createBurnerAccount, getContract, transportObserver } from '@latticexyz/common'; + +import { share, Subject } from 'rxjs'; /* * Import our MUD config, which includes strong types for @@ -28,7 +35,7 @@ import { share, Subject } from "rxjs"; * See https://mud.dev/templates/typescript/contracts#mudconfigts * for the source of this information. */ -import mudConfig from "contracts/mud.config"; +import mudConfig from 'contracts/mud.config'; export type SetupNetworkResult = Awaited>; @@ -41,100 +48,103 @@ export type SetupNetworkResult = Awaited>; // }); export async function setupNetwork() { - const networkConfig = await getNetworkConfig(); - - /* - * Create a viem public (read only) client - * (https://viem.sh/docs/clients/public.html) - */ - const clientOptions = { - chain: networkConfig.chain, - transport: transportObserver(fallback([webSocket(), http()])), - pollingInterval: 1000, - } as const satisfies ClientConfig; - - const publicClient = createPublicClient(clientOptions); - - /* - * Create a temporary wallet and a viem client for it - * (see https://viem.sh/docs/clients/wallet.html). - */ - // const burnerAccount = createBurnerAccount(networkConfig.privateKey as Hex); - const { account } = get(walletState) - const burnerWalletClient = createWalletClient({ - ...clientOptions, - account: account, - }); - - /* - * Create an observable for contract writes that we can - * pass into MUD dev tools for transaction observability. - */ - const write$ = new Subject(); - - /* - * Create an object for communicating with the deployed World. - */ - const worldContract = getContract({ - address: networkConfig.worldAddress as Hex, - abi: IWorldAbi, - publicClient, - walletClient: burnerWalletClient, - onWrite: (write) => write$.next(write), - }); - - /* - * 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 { components, latestBlock$, storedBlockLogs$, waitForTransaction } = await syncToRecs({ - world, - config: mudConfig, - address: networkConfig.worldAddress as Hex, - publicClient, - startBlock: BigInt(networkConfig.initialBlockNumber), - }); - - /* - * If there is a faucet, request (test) ETH if you have - * less than 1 ETH. Repeat every 20 seconds to ensure you don't - * run out. - */ - if (networkConfig.faucetServiceUrl) { - const address = burnerAccount.address; - console.info("[Dev Faucet]: Player address -> ", address); - - const faucet = createFaucetService(networkConfig.faucetServiceUrl); - - const requestDrip = async () => { - const balance = await publicClient.getBalance({ address }); - console.info(`[Dev Faucet]: Player balance -> ${balance}`); - const lowBalance = balance < parseEther("1"); - if (lowBalance) { - console.info("[Dev Faucet]: Balance is low, dripping funds to player"); - // Double drip - await faucet.dripDev({ address }); - await faucet.dripDev({ address }); - } - }; - - requestDrip(); - // Request a drip every 20 seconds - setInterval(requestDrip, 20000); - } - - return { - world, - components, - playerEntity: encodeEntity({ address: "address" }, { address: burnerWalletClient.account.address }), - publicClient, - walletClient: burnerWalletClient, - latestBlock$, - storedBlockLogs$, - waitForTransaction, - worldContract, - write$: write$.asObservable().pipe(share()), - }; + const networkConfig = await getNetworkConfig(); + + /* + * Create a viem public (read only) client + * (https://viem.sh/docs/clients/public.html) + */ + const clientOptions = { + chain: networkConfig.chain, + transport: transportObserver(fallback([http()])), + pollingInterval: 1000 + } as const satisfies ClientConfig; + + const publicClient = createPublicClient(clientOptions); + + /* + * Create a temporary wallet and a viem client for it + * (see https://viem.sh/docs/clients/wallet.html). + */ + // const burnerAccount = createBurnerAccount(networkConfig.privateKey as Hex); + const { account } = get(walletState); + const burnerWalletClient = createWalletClient({ + ...clientOptions, + account: account + }); + + /* + * Create an observable for contract writes that we can + * pass into MUD dev tools for transaction observability. + */ + const write$ = new Subject(); + + /* + * Create an object for communicating with the deployed World. + */ + const worldContract = getContract({ + address: networkConfig.worldAddress as Hex, + abi: IWorldAbi, + publicClient, + walletClient: burnerWalletClient, + onWrite: (write) => write$.next(write) + }); + + /* + * 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 { components, latestBlock$, storedBlockLogs$, waitForTransaction } = await syncToRecs({ + world, + config: mudConfig, + address: networkConfig.worldAddress as Hex, + publicClient, + startBlock: BigInt(networkConfig.initialBlockNumber) + }); + + /* + * If there is a faucet, request (test) ETH if you have + * less than 1 ETH. Repeat every 20 seconds to ensure you don't + * run out. + */ + if (networkConfig.faucetServiceUrl) { + const address = burnerAccount.address; + console.info('[Dev Faucet]: Player address -> ', address); + + const faucet = createFaucetService(networkConfig.faucetServiceUrl); + + const requestDrip = async () => { + const balance = await publicClient.getBalance({ address }); + console.info(`[Dev Faucet]: Player balance -> ${balance}`); + const lowBalance = balance < parseEther('1'); + if (lowBalance) { + console.info('[Dev Faucet]: Balance is low, dripping funds to player'); + // Double drip + await faucet.dripDev({ address }); + await faucet.dripDev({ address }); + } + }; + + requestDrip(); + // Request a drip every 20 seconds + setInterval(requestDrip, 20000); + } + + return { + world, + components, + playerEntity: encodeEntity( + { address: 'address' }, + { address: burnerWalletClient.account.address } + ), + publicClient, + walletClient: burnerWalletClient, + latestBlock$, + storedBlockLogs$, + waitForTransaction, + worldContract, + write$: write$.asObservable().pipe(share()) + }; } diff --git a/packages/client/src/lib/mud/supportedChains.ts b/packages/client/src/lib/mud/supportedChains.ts index 65cee10..5a39ea1 100644 --- a/packages/client/src/lib/mud/supportedChains.ts +++ b/packages/client/src/lib/mud/supportedChains.ts @@ -13,16 +13,12 @@ import type { MUDChain } from '@latticexyz/common/chains'; import { latticeTestnet, mudFoundry } from '@latticexyz/common/chains'; -export const baseSepolia = /*#__PURE__*/ { +export const baseSepolia = { id: 84532, network: 'base-sepolia', name: 'Base Sepolia', nativeCurrency: { name: 'Sepolia Ether', symbol: 'ETH', decimals: 18 }, rpcUrls: { - alchemy: { - http: ['https://base-sepolia.g.alchemy.com/v2'], - webSocket: ['wss://base-sepolia.g.alchemy.com/v2'] - }, default: { http: ['https://sepolia.base.org'] },