Skip to content

Commit

Permalink
fix: not supported chain
Browse files Browse the repository at this point in the history
  • Loading branch information
Argeare5 committed Jan 12, 2024
1 parent cac73ea commit de5f52f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 65 deletions.
63 changes: 21 additions & 42 deletions src/web3/connectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,13 @@ export enum WalletType {
}

export const initAllConnectors = (props: AllConnectorsInitProps) => {
const injectedConnector = {
connector: injected(),
type: WalletType.Injected,
};
const coinbaseConnector = {
connector: coinbaseWallet({
appName: props.appName,
}),
type: WalletType.Coinbase,
};
const gnosisSafeConnector = {
connector: safe({
...safeSdkOptions,
}),
type: WalletType.Safe,
};
const injectedConnector = injected();
const coinbaseConnector = coinbaseWallet({
appName: props.appName,
});
const gnosisSafeConnector = safe({
...safeSdkOptions,
});

const connectors = [
injectedConnector,
Expand All @@ -59,36 +50,24 @@ export const initAllConnectors = (props: AllConnectorsInitProps) => {

const wcParams = props.wcParams;
if (wcParams && !props.getImpersonatedAccount) {
const walletConnectConnector = {
connector: walletConnect({
projectId: wcParams.projectId,
metadata: wcParams.metadata,
}),
type: WalletType.WalletConnect,
};
const walletConnectConnector = walletConnect({
projectId: wcParams.projectId,
metadata: wcParams.metadata,
});
return [walletConnectConnector, ...connectors];
} else if (!wcParams && !!props.getImpersonatedAccount) {
const impersonatedConnector = {
connector: impersonated({
getAccountAddress: props.getImpersonatedAccount,
}),
type: WalletType.Impersonated,
};
const impersonatedConnector = impersonated({
getAccountAddress: props.getImpersonatedAccount,
});
return [impersonatedConnector, ...connectors];
} else if (wcParams && !!props.getImpersonatedAccount) {
const walletConnectConnector = {
connector: walletConnect({
projectId: wcParams.projectId,
metadata: wcParams.metadata,
}),
type: WalletType.WalletConnect,
};
const impersonatedConnector = {
connector: impersonated({
getAccountAddress: props.getImpersonatedAccount,
}),
type: WalletType.Impersonated,
};
const walletConnectConnector = walletConnect({
projectId: wcParams.projectId,
metadata: wcParams.metadata,
});
const impersonatedConnector = impersonated({
getAccountAddress: props.getImpersonatedAccount,
});
return [walletConnectConnector, impersonatedConnector, ...connectors];
} else {
return connectors;
Expand Down
4 changes: 1 addition & 3 deletions src/web3/providers/WagmiProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,13 @@ export function WagmiProvider({
);
});

console.log('chains in config', chains);

return {
wagmiConfig: createConfig({
chains: [
chains[formattedProps.defaultChainId || 0] || mainnet,
...chains,
],
connectors: connectors.map((connector) => connector.connector),
connectors,
transports,
}),
queryClient: new QueryClient(),
Expand Down
87 changes: 67 additions & 20 deletions src/web3/store/walletSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { TransactionsSliceBaseType } from './transactionsSlice';
export interface Wallet {
walletType: WalletType;
address: Hex;
chainId: number;
chain?: Chain;
publicClient?: PublicClient;
walletClient?: WalletClient;
Expand Down Expand Up @@ -115,37 +116,78 @@ export function createWalletSlice({
setActiveWallet: async (wallet) => {
const config = get().wagmiConfig;

const setWallet = async (
walletData: Omit<Wallet, 'publicClient' | 'walletClient'>,
publicClient: PublicClient,
walletClient: WalletClient,
) => {
const walletWithClients = {
...walletData,
publicClient,
walletClient,
};

const isContractAddress =
await get().checkIsContractWallet(walletWithClients);
const activeWallet = { ...walletWithClients, isContractAddress };

set({ activeWallet });

if (walletData.chain?.id) {
get().setClient(walletData.chain.id, publicClient);
}

walletConnected(activeWallet);
set({ isActiveWalletSetting: false });
};

if (wallet.isActive && config) {
if (
wallet.chain &&
config.chains.some((chain) => chain.id === wallet.chain?.id)
config.chains.some((chain) => chain.id === wallet.chainId)
) {
set({ isActiveWalletSetting: true });
const publicClient = getPublicClient(config);
const walletClient = await getWalletClient(config);

if (publicClient && walletClient) {
const walletWithClients = {
...wallet,
publicClient,
walletClient,
await setWallet(wallet, publicClient, walletClient);
}
} else {
set({ isActiveWalletSetting: true });
let walletData = wallet;
let newConfig = config;

if (!wallet.chain) {
walletData = {
...walletData,
chain: getChainByChainId(walletData.chainId),
};
} else if (
config.chains.every((chain) => chain.id !== wallet.chainId)
) {
newConfig = {
...config,
chains: [
getChainByChainId(wallet.chainId) || mainnet,
...config.chains,
],
};

set({ wagmiConfig: newConfig });
}

const isContractAddress =
await get().checkIsContractWallet(walletWithClients);
const activeWallet = { ...walletWithClients, isContractAddress };
if (
walletData.chain &&
newConfig.chains.some((chain) => chain.id === wallet.chainId)
) {
const publicClient = getPublicClient(newConfig);
const walletClient = await getWalletClient(newConfig);

set({ activeWallet });
get().setClient(wallet.chain.id, publicClient);
walletConnected(activeWallet);
set({ isActiveWalletSetting: false });
if (publicClient && walletClient) {
await setWallet(walletData, publicClient, walletClient);
}
}
} else {
set({ isActiveWalletSetting: true });
const activeWallet = { ...wallet, isContractAddress: false };
set({ activeWallet });
walletConnected(activeWallet);
set({ isActiveWalletSetting: false });
}
}
},
Expand Down Expand Up @@ -197,6 +239,7 @@ export function createWalletSlice({
await get().setActiveWallet({
walletType,
address: account.address,
chainId: chainId || 1,
chain: account.chain || getChainByChainId(chainId || 1),
isActive: account.isConnected,
isContractAddress: false,
Expand Down Expand Up @@ -287,14 +330,18 @@ export function createWalletSlice({
account?.address &&
activeWallet &&
(activeWallet.address !== account.address ||
activeWallet.chain?.id !== account.chain?.id) &&
activeWallet.chainId !== account.chainId) &&
!get().isActiveWalletAccountChanging
) {
set({ isActiveWalletAccountChanging: true });
await get().setActiveWallet({
walletType: activeWallet.walletType,
address: account.address,
chain: account.chain,
chainId: account.chainId || 1,
chain:
account.chain || activeWallet.chain === account.chainId
? activeWallet.chain
: getChainByChainId(account.chainId || 1),
isActive: activeWallet.isActive,
isContractAddress: activeWallet.isContractAddress,
});
Expand Down

0 comments on commit de5f52f

Please sign in to comment.