Skip to content

Commit

Permalink
fix: wallet connect
Browse files Browse the repository at this point in the history
  • Loading branch information
Argeare5 committed Jan 12, 2024
1 parent de5f52f commit 39ac46b
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions src/web3/store/walletSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,21 @@ export function createWalletSlice({

isActiveWalletSetting: false,
setActiveWallet: async (wallet) => {
const config = get().wagmiConfig;
let config = get().wagmiConfig;
if (
config &&
config.chains.every((chain) => chain.id !== wallet.chainId)
) {
config = {
...config,
chains: [
getChainByChainId(wallet.chainId) || mainnet,
...config.chains,
],
};

set({ wagmiConfig: config });
}

const setWallet = async (
walletData: Omit<Wallet, 'publicClient' | 'walletClient'>,
Expand Down Expand Up @@ -156,33 +170,20 @@ export function createWalletSlice({
} 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 });
}

if (
walletData.chain &&
newConfig.chains.some((chain) => chain.id === wallet.chainId)
config.chains.some((chain) => chain.id === wallet.chainId)
) {
const publicClient = getPublicClient(newConfig);
const walletClient = await getWalletClient(newConfig);
const publicClient = getPublicClient(config);
const walletClient = await getWalletClient(config);

if (publicClient && walletClient) {
await setWallet(walletData, publicClient, walletClient);
Expand Down Expand Up @@ -326,13 +327,17 @@ export function createWalletSlice({
isActiveWalletAccountChanging: false,
changeActiveWalletAccount: async (account) => {
const activeWallet = get().activeWallet;
const config = get().wagmiConfig;

if (
account?.address &&
activeWallet &&
(activeWallet.address !== account.address ||
activeWallet.chainId !== account.chainId) &&
!get().isActiveWalletAccountChanging
) {
console.log('changed acc when active wallet', account);

set({ isActiveWalletAccountChanging: true });
await get().setActiveWallet({
walletType: activeWallet.walletType,
Expand All @@ -346,6 +351,25 @@ export function createWalletSlice({
isContractAddress: activeWallet.isContractAddress,
});
set({ isActiveWalletAccountChanging: false });
} else if (
account &&
account.address &&
!activeWallet &&
config &&
!get().isActiveWalletAccountChanging
) {
console.log('changed acc when non active wallet', account);

set({ isActiveWalletAccountChanging: true });
await get().setActiveWallet({
walletType: config.state.current as WalletType,
address: account.address,
chainId: account.chainId || 1,
chain: account.chain || getChainByChainId(account.chainId || 1),
isActive: true,
isContractAddress: false,
});
set({ isActiveWalletAccountChanging: false });
}
},

Expand Down

0 comments on commit 39ac46b

Please sign in to comment.