Skip to content

Commit

Permalink
fix: wallet connect connection chain id
Browse files Browse the repository at this point in the history
  • Loading branch information
Argeare5 committed Nov 3, 2023
1 parent 0d60762 commit eb596a6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@bgd-labs/frontend-web3-utils",
"description": "Frontend utilities common to multiple Web3 projects",
"version": "0.4.25",
"version": "0.4.26",
"author": "BGD labs",
"license": "MIT",
"private": false,
Expand Down
6 changes: 5 additions & 1 deletion src/web3/connectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ export const initAllConnectors = (props: AllConnectorsInitProps) => {
const gnosisSafe = new SafeConnector({
chains,
options: {
allowedDomains: [/gnosis-safe.io$/, /app.safe.global$/],
allowedDomains: [
/gnosis-safe.io$/,
/app.safe.global$/,
/metissafe.tech$/,
],
debug: false,
},
});
Expand Down
26 changes: 22 additions & 4 deletions src/web3/providers/WagmiProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface WagmiProviderProps {
changeActiveWalletAccount: (account?: GetAccountResult) => Promise<void>;
changeActiveWalletChain: (chain?: Chain) => Promise<void>;
setConnectors: (connectors: ConnectorType[]) => void;
setDefaultChainId: (chainId: number) => void;
}>
>;
connectorsInitProps: AllConnectorsInitProps;
Expand All @@ -30,11 +31,16 @@ interface WagmiProviderProps {
function Child({
useStore,
connectors,
}: Omit<WagmiProviderProps, 'connectorsInitProps'> & {
connectorsInitProps,
}: WagmiProviderProps & {
connectors: ConnectorType[];
}) {
const { setConnectors, changeActiveWalletAccount, changeActiveWalletChain } =
useStore();
const {
setConnectors,
changeActiveWalletAccount,
changeActiveWalletChain,
setDefaultChainId,
} = useStore();

watchAccount(async (data) => {
if (data.address) {
Expand All @@ -53,6 +59,12 @@ function Child({
}
}, [connectors]);

useEffect(() => {
if (connectorsInitProps.defaultChainId) {
setDefaultChainId(connectorsInitProps.defaultChainId);
}
}, [connectorsInitProps.defaultChainId]);

return null;
}

Expand Down Expand Up @@ -82,5 +94,11 @@ export function WagmiProvider({
connectors,
});

return <Child useStore={useStore} connectors={mappedConnectors} />;
return (
<Child
useStore={useStore}
connectors={mappedConnectors}
connectorsInitProps={connectorsInitProps}
/>
);
}
16 changes: 15 additions & 1 deletion src/web3/store/walletSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
import { produce } from 'immer';
import { Account, Chain, Hex, isAddress } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { mainnet } from 'viem/chains';
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect';

import { StoreSlice } from '../../types/store';
import {
Expand Down Expand Up @@ -66,6 +68,9 @@ export type IWalletSlice = {
checkIsContractWallet: (
wallet: Omit<Wallet, 'walletClient'>,
) => Promise<boolean>;

defaultChainId: number;
setDefaultChainId: (chainId: number) => void;
};

export function createWalletSlice({
Expand Down Expand Up @@ -151,7 +156,11 @@ export function createWalletSlice({
}
await connect({ connector, chainId });
} else {
await connect({ connector });
if (connector instanceof WalletConnectConnector) {
await connect({ connector, chainId: get().defaultChainId });
} else {
await connect({ connector });
}

setLocalStorageWallet(walletType);
get().updateEthAdapter(walletType === 'GnosisSafe');
Expand Down Expand Up @@ -301,5 +310,10 @@ export function createWalletSlice({
resetWalletConnectionError: () => {
set({ walletConnectionError: '' });
},

defaultChainId: mainnet.id,
setDefaultChainId: (chainId) => {
set({ defaultChainId: chainId });
},
});
}

0 comments on commit eb596a6

Please sign in to comment.