Skip to content

Commit

Permalink
fix(client): base sepolia network config
Browse files Browse the repository at this point in the history
  • Loading branch information
noyyyy committed Dec 4, 2023
1 parent 94e418b commit 4825e45
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 118 deletions.
236 changes: 123 additions & 113 deletions packages/client/src/lib/mud/setupNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<ReturnType<typeof setupNetwork>>;

Expand All @@ -41,100 +48,103 @@ export type SetupNetworkResult = Awaited<ReturnType<typeof setupNetwork>>;
// });

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<ContractWrite>();

/*
* 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<ContractWrite>();

/*
* 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())
};
}
6 changes: 1 addition & 5 deletions packages/client/src/lib/mud/supportedChains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
},
Expand Down

0 comments on commit 4825e45

Please # to comment.