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

WIP: viem custom client within external wallet example #2294

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
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
"@latticexyz/common": "link:../../../../packages/common",
"@latticexyz/dev-tools": "link:../../../../packages/dev-tools",
"@latticexyz/store-sync": "link:../../../../packages/store-sync",
"@tanstack/react-query": "5.22.2",
"contracts": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rxjs": "7.5.5",
"viem": "1.14.0",
"wagmi": "1.4.13"
"viem": "2.7.12",
"wagmi": "2.5.7"
},
"devDependencies": {
"@types/react": "18.2.22",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseError } from "viem";
import { useAccount, useConnect, useDisconnect, useNetwork, useSwitchNetwork } from "wagmi";
import { type BaseError } from "viem";
import { useAccount, useConnect, useDisconnect, useNetwork, useSwitchChain, useSwitchNetwork } from "wagmi";

export const ExternalWallet = () => {
const { isConnected } = useAccount();
Expand All @@ -14,34 +14,32 @@ export const ExternalWallet = () => {

// Based on https://github.com/wevm/create-wagmi/blob/create-wagmi%401.0.5/templates/vite-react/default/src/components/Connect.tsx
function Connect() {
const { connector, isConnected } = useAccount();
const { connect, connectors, error, isLoading, pendingConnector } = useConnect();
const account = useAccount();
const { connect, connectors, status, error } = useConnect();
const { disconnect } = useDisconnect();

return (
<div>
<div>
{isConnected && <button onClick={() => disconnect()}>Disconnect from {connector?.name}</button>}
{account.status === "connected" && <button onClick={() => disconnect()}>Disconnect</button>}

{connectors
.filter((x) => x.ready && x.id !== connector?.id)
.map((x) => (
<button key={x.id} onClick={() => connect({ connector: x })}>
Connect {x.name}
{isLoading && x.id === pendingConnector?.id && " (connecting)"}
</button>
))}
{connectors.map((x) => (
<button key={x.uid} onClick={() => connect({ connector: x })}>
Connect {x.name}
</button>
))}
</div>

{error && <div>{(error as BaseError).shortMessage}</div>}
<div>{status}</div>
{error && <div>{error.message}</div>}
</div>
);
}

// Based on https://github.com/wevm/create-wagmi/blob/create-wagmi%401.0.5/templates/vite-react/default/src/components/NetworkSwitcher.tsx
function Network() {
const { chain } = useNetwork();
const { chains, error, isLoading, pendingChainId, switchNetwork } = useSwitchNetwork();
const { chains, error, status, switchChain } = useSwitchChain();
const { address } = useAccount();

const otherChains = chains.filter((x) => x.id !== chain?.id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ReactDOM from "react-dom/client";
import { WagmiConfig } from "wagmi";
import { WagmiProvider } from "wagmi";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ExternalWallet } from "./ExternalWallet";
import { MUDReadProvider } from "./mud/read";
import { MUDWriteProvider } from "./mud/write";
Expand All @@ -11,17 +12,21 @@ const rootElement = document.getElementById("react-root");
if (!rootElement) throw new Error("React root not found");
const root = ReactDOM.createRoot(rootElement);

const queryClient = new QueryClient();

// TODO: figure out if we actually want this to be async or if we should render something else in the meantime
setup().then(({ mud, wagmiConfig }) => {
root.render(
<WagmiConfig config={wagmiConfig}>
<ExternalWallet />
<MUDReadProvider value={mud}>
<MUDWriteProvider>
<App />
{import.meta.env.DEV && <DevTools />}
</MUDWriteProvider>
</MUDReadProvider>
</WagmiConfig>
<WagmiProvider config={wagmiConfig}>
<QueryClientProvider client={queryClient}>
<ExternalWallet />
<MUDReadProvider value={mud}>
<MUDWriteProvider>
<App />
{import.meta.env.DEV && <DevTools />}
</MUDWriteProvider>
</MUDReadProvider>
</QueryClientProvider>
</WagmiProvider>
);
});
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { createConfig } from "wagmi";
import { InjectedConnector } from "wagmi/connectors/injected";
import { injected } from "wagmi/connectors";
import { setupNetwork } from "./setupNetwork";

export async function setup() {
const mud = await setupNetwork();

const wagmiConfig = createConfig({
autoConnect: true,
connectors: [
new InjectedConnector({
chains: [mud.publicClient.chain],
}),
],
publicClient: mud.publicClient,
chains: [mud.publicClient.chain],
connectors: [injected()],
client: () => mud.publicClient,
});

return {
Expand Down
Loading
Loading