Skip to content

Commit

Permalink
wip: upgrading wagmi
Browse files Browse the repository at this point in the history
  • Loading branch information
tash-2s committed Feb 23, 2024
1 parent f2ddcc7 commit 59960ad
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
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

0 comments on commit 59960ad

Please sign in to comment.