Skip to content

Commit c70dc9b

Browse files
committed
fixes wagmi rehydrate
1 parent 3e5d3dd commit c70dc9b

File tree

17 files changed

+215
-136
lines changed

17 files changed

+215
-136
lines changed

demo/vue-app-new/package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/vue-app-new/src/components/AppDashboard.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,16 @@ const canSwitchChainNamespace = computed(() => {
316316
});
317317
318318
const onSwitchChain = async () => {
319-
log.info("switching chain");
319+
log.info("switching chain", currentChainId.value);
320320
try {
321321
const { chainId } = provider.value as IProvider;
322322
if (chainId !== currentChainId.value) throw new Error("chainId does not match current chainId");
323323
324324
const currentNamespace = currentChainNamespace.value;
325325
const newChain = props.chains.find((x) => x.chainNamespace === currentNamespace && x.chainId !== chainId);
326326
if (!newChain) throw new Error(`Please configure at least 2 chains for ${currentNamespace} in the config`);
327-
const data =await switchChainAsync({ chainId: Number(newChain.chainId) });
327+
console.log("newChain", newChain.chainId);
328+
const data = await switchChainAsync({ chainId: Number(newChain.chainId) });
328329
printToConsole("switchedChain", { chainId: data.id });
329330
} catch (error) {
330331
printToConsole("switchedChain error", error);

demo/wagmi-react-app/package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import toruslabsTypescript from "@toruslabs/eslint-config-typescript";
33
export default [
44
...toruslabsTypescript,
55
{
6-
files: ["packages/no-modal/src/react/wagmi/provider.ts", "packages/no-modal/src/vue/wagmi/provider.ts"],
6+
files: ["packages/no-modal/src/react/wagmi/**.ts", "packages/no-modal/src/vue/wagmi/**.ts"],
77
rules: {
88
"import/no-extraneous-dependencies": "off",
99
},

packages/modal/eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default [
66
ignores: ["./rollup.config.mjs"],
77
},
88
{
9-
files: ["./src/react/wagmi/provider.ts", "./src/vue/wagmi/provider.ts"],
9+
files: ["./src/react/wagmi/**.ts", "./src/vue/wagmi/**.ts"],
1010
rules: {
1111
"import/no-extraneous-dependencies": "off",
1212
},

packages/modal/src/modalManager.ts

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -62,62 +62,57 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
6262
}
6363

6464
public async initModal(options?: { signal?: AbortSignal }): Promise<void> {
65-
try {
66-
const { signal } = options || {};
67-
68-
super.checkInitRequirements();
69-
// get project config and wallet registry
70-
const { projectConfig, walletRegistry } = await this.getProjectAndWalletConfig();
71-
72-
// init config
73-
this.initUIConfig(projectConfig);
74-
super.initAccountAbstractionConfig(projectConfig);
75-
super.initChainsConfig(projectConfig);
76-
super.initCachedConnectorAndChainId();
77-
78-
// init login modal
79-
const { filteredWalletRegistry, disabledExternalWallets } = this.filterWalletRegistry(walletRegistry, projectConfig);
80-
this.loginModal = new LoginModal(
81-
{
82-
...this.options.uiConfig,
83-
connectorListener: this,
84-
web3authClientId: this.options.clientId,
85-
web3authNetwork: this.options.web3AuthNetwork,
86-
authBuildEnv: this.options.authBuildEnv,
87-
chainNamespaces: this.getChainNamespaces(),
88-
walletRegistry: filteredWalletRegistry,
89-
},
90-
{
91-
onInitExternalWallets: this.onInitExternalWallets,
92-
onSocialLogin: this.onSocialLogin,
93-
onExternalWalletLogin: this.onExternalWalletLogin,
94-
onModalVisibility: this.onModalVisibility,
95-
}
96-
);
97-
await withAbort(() => this.loginModal.initModal(), signal);
65+
const { signal } = options || {};
66+
67+
super.checkInitRequirements();
68+
// get project config and wallet registry
69+
const { projectConfig, walletRegistry } = await this.getProjectAndWalletConfig();
70+
71+
// init config
72+
this.initUIConfig(projectConfig);
73+
super.initAccountAbstractionConfig(projectConfig);
74+
super.initChainsConfig(projectConfig);
75+
super.initCachedConnectorAndChainId();
76+
77+
// init login modal
78+
const { filteredWalletRegistry, disabledExternalWallets } = this.filterWalletRegistry(walletRegistry, projectConfig);
79+
this.loginModal = new LoginModal(
80+
{
81+
...this.options.uiConfig,
82+
connectorListener: this,
83+
web3authClientId: this.options.clientId,
84+
web3authNetwork: this.options.web3AuthNetwork,
85+
authBuildEnv: this.options.authBuildEnv,
86+
chainNamespaces: this.getChainNamespaces(),
87+
walletRegistry: filteredWalletRegistry,
88+
},
89+
{
90+
onInitExternalWallets: this.onInitExternalWallets,
91+
onSocialLogin: this.onSocialLogin,
92+
onExternalWalletLogin: this.onExternalWalletLogin,
93+
onModalVisibility: this.onModalVisibility,
94+
}
95+
);
96+
await withAbort(() => this.loginModal.initModal(), signal);
9897

99-
// setup common JRPC provider
100-
await withAbort(() => this.setupCommonJRPCProvider(), signal);
98+
// setup common JRPC provider
99+
await withAbort(() => this.setupCommonJRPCProvider(), signal);
101100

102-
// initialize connectors
103-
this.on(CONNECTOR_EVENTS.CONNECTORS_UPDATED, ({ connectors: newConnectors }) => {
104-
const onAbortHandler = () => {
105-
log.debug("init aborted");
106-
if (this.connectors?.length > 0) {
107-
super.cleanup();
108-
}
109-
};
110-
withAbort(() => this.initConnectors({ connectors: newConnectors, projectConfig, disabledExternalWallets }), signal, onAbortHandler);
111-
});
101+
// initialize connectors
102+
this.on(CONNECTOR_EVENTS.CONNECTORS_UPDATED, ({ connectors: newConnectors }) => {
103+
const onAbortHandler = () => {
104+
log.debug("init aborted");
105+
if (this.connectors?.length > 0) {
106+
super.cleanup();
107+
}
108+
};
109+
withAbort(() => this.initConnectors({ connectors: newConnectors, projectConfig, disabledExternalWallets }), signal, onAbortHandler);
110+
});
112111

113-
await withAbort(() => super.loadConnectors({ projectConfig, modalMode: true }), signal);
112+
await withAbort(() => super.loadConnectors({ projectConfig, modalMode: true }), signal);
114113

115-
// initialize plugins
116-
await withAbort(() => super.initPlugins(), signal);
117-
} catch (error) {
118-
log.error("Failed to initialize modal", error);
119-
throw error;
120-
}
114+
// initialize plugins
115+
await withAbort(() => super.initPlugins(), signal);
121116
}
122117

123118
public async connect(): Promise<IProvider | null> {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createConfig, http } from "wagmi";
2+
import { mainnet } from "wagmi/chains";
3+
4+
export const defaultWagmiConfig = createConfig({
5+
chains: [mainnet],
6+
connectors: [], // or your basic wallets
7+
ssr: true,
8+
transports: {
9+
[mainnet.id]: http(mainnet.rpcUrls.default.http[0]),
10+
},
11+
});

packages/modal/src/react/wagmi/provider.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { log } from "@web3auth/no-modal";
1+
import { CHAIN_NAMESPACES, log, WalletInitializationError } from "@web3auth/no-modal";
22
import { createElement, Fragment, PropsWithChildren, useEffect, useMemo } from "react";
33
import { type Chain, defineChain, http, webSocket } from "viem";
44
import {
@@ -16,6 +16,7 @@ import {
1616
import { injected } from "wagmi/connectors";
1717

1818
import { useWeb3Auth, useWeb3AuthDisconnect } from "../hooks";
19+
import { defaultWagmiConfig } from "./constants";
1920
import { WagmiProviderProps } from "./interface";
2021

2122
const WEB3AUTH_CONNECTOR_ID = "web3auth";
@@ -123,6 +124,8 @@ export function WagmiProvider({ children, ...props }: PropsWithChildren<WagmiPro
123124
const { web3Auth, isInitialized } = useWeb3Auth();
124125

125126
const finalConfig = useMemo(() => {
127+
if (!isInitialized) return defaultWagmiConfig;
128+
126129
const finalConfig: CreateConfigParameters = {
127130
ssr: true,
128131
...config,
@@ -136,7 +139,9 @@ export function WagmiProvider({ children, ...props }: PropsWithChildren<WagmiPro
136139
const wagmiChains: Chain[] = [];
137140
if (isInitialized && web3Auth?.coreOptions?.chains) {
138141
const defaultChainId = web3Auth.currentChain?.chainId;
139-
const chains = web3Auth.coreOptions.chains;
142+
const chains = web3Auth.coreOptions.chains.filter((chain) => chain.chainNamespace === CHAIN_NAMESPACES.EIP155);
143+
if (chains.length === 0) throw WalletInitializationError.invalidParams("No valid chains found in web3auth config for wagmi.");
144+
140145
chains.forEach((chain) => {
141146
const wagmiChain = defineChain({
142147
id: Number.parseInt(chain.chainId, 16), // id in number form
@@ -173,14 +178,9 @@ export function WagmiProvider({ children, ...props }: PropsWithChildren<WagmiPro
173178
finalConfig.chains = [wagmiChains[0], ...wagmiChains.slice(1)];
174179
}
175180

176-
if (!finalConfig.chains) return;
177181
return createWagmiConfig(finalConfig);
178182
}, [config, web3Auth, isInitialized]);
179183

180-
// WagmiProviderBase requires a config to initialize
181-
// If no config is provided, it will throw an error.
182-
if (!finalConfig) return null;
183-
184184
return createElement(
185185
WagmiProviderBase,
186186
// typecast to WagmiProviderPropsBase to avoid type error

packages/modal/src/vue/solana/composables/useSolanaWallet.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,35 @@ export const useSolanaWallet = (): IUseSolanaWallet => {
1616
const solanaWallet = shallowRef<SolanaWallet | null>(null);
1717
const connection = shallowRef<Connection | null>(null);
1818

19-
watch(provider, async (newVal) => {
20-
if (!web3Auth.value?.currentChain?.chainNamespace || web3Auth.value.currentChain.chainNamespace !== CHAIN_NAMESPACES.SOLANA) {
21-
return;
22-
}
23-
if (!newVal && solanaWallet.value) {
24-
solanaWallet.value = null;
25-
accounts.value = null;
26-
connection.value = null;
27-
return;
28-
}
29-
if (!solanaWallet.value) {
30-
solanaWallet.value = new SolanaWallet(newVal);
31-
}
19+
watch(
20+
provider,
21+
async (newVal) => {
22+
if (!web3Auth.value?.currentChain?.chainNamespace || web3Auth.value.currentChain.chainNamespace !== CHAIN_NAMESPACES.SOLANA) {
23+
return;
24+
}
25+
if (!newVal && solanaWallet.value) {
26+
solanaWallet.value = null;
27+
accounts.value = null;
28+
connection.value = null;
29+
return;
30+
}
31+
if (!solanaWallet.value) {
32+
solanaWallet.value = new SolanaWallet(newVal);
33+
}
3234

33-
if (solanaWallet.value && accounts.value?.length === 0) {
34-
const result = await solanaWallet.value.requestAccounts();
35-
if (result?.length > 0) {
36-
accounts.value = result;
35+
if (solanaWallet.value && accounts.value?.length === 0) {
36+
const result = await solanaWallet.value.requestAccounts();
37+
if (result?.length > 0) {
38+
accounts.value = result;
39+
}
3740
}
38-
}
3941

40-
if (solanaWallet.value && !connection.value) {
41-
connection.value = new Connection(web3Auth.value?.currentChain?.rpcTarget);
42-
}
43-
});
42+
if (solanaWallet.value && !connection.value) {
43+
connection.value = new Connection(web3Auth.value?.currentChain?.rpcTarget);
44+
}
45+
},
46+
{ immediate: true }
47+
);
4448

4549
return { solanaWallet, accounts, connection };
4650
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createConfig, http } from "@wagmi/vue";
2+
import { mainnet } from "@wagmi/vue/chains";
3+
4+
export const defaultWagmiConfig = createConfig({
5+
chains: [mainnet],
6+
connectors: [], // or your basic wallets
7+
ssr: true,
8+
transports: {
9+
[mainnet.id]: http(mainnet.rpcUrls.default.http[0]),
10+
},
11+
});

0 commit comments

Comments
 (0)