Skip to content

Commit

Permalink
fix: impersonated connector and wagmi config chains
Browse files Browse the repository at this point in the history
  • Loading branch information
Argeare5 committed Jan 13, 2024
1 parent 5987562 commit 036b7f0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 39 deletions.
8 changes: 5 additions & 3 deletions src/web3/connectors/ImpersonatedConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export function impersonated(parameters: ImpersonatedParameters) {
type: impersonated.type,
async setup() {
connectedChainId = config.chains[0].id;
accountAddress = [parameters.getAccountAddress() || zeroAddress];
},
async connect({ chainId } = {}) {
if (features.connectError) {
Expand Down Expand Up @@ -117,9 +116,12 @@ export function impersonated(parameters: ImpersonatedParameters) {
async onDisconnect(_error) {
config.emitter.emit('disconnect');
connected = false;
accountAddress = undefined;
},
async getProvider({ chainId }: { chainId?: number } = {}) {
accountAddress = [parameters.getAccountAddress() || zeroAddress];
accountAddress = parameters.getAccountAddress()
? [parameters.getAccountAddress() || zeroAddress]
: undefined;
const chain =
config.chains.find((x) => x.id === chainId) ?? config.chains[0];
const url = chain.rpcUrls.default.http[0]!;
Expand Down Expand Up @@ -173,7 +175,7 @@ export function impersonated(parameters: ImpersonatedParameters) {

return result;
};
return custom({ request })({ retryCount: 0 });
return custom({ request })({ retryCount: 1 });
},
}));
}
34 changes: 0 additions & 34 deletions src/web3/store/walletSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,6 @@ export function createWalletSlice({
isActiveWalletSetting: false,
setActiveWallet: async (wallet) => {
const config = get().wagmiConfig;
if (
config &&
config.chains.some((chain) => chain.id !== wallet.chainId)
) {
config.setState((state) => ({
...state,
chains: state.current
? [VIEM_CHAINS[state.chainId], ...config.chains]
: [],
}));
}

const getPublicClientLocal = (localConf: Config, chain: Chain) => {
let publicClient = undefined;
try {
Expand Down Expand Up @@ -348,28 +336,6 @@ export function createWalletSlice({
isContractAddress: activeWallet.isContractAddress,
});
set({ isActiveWalletAccountChanging: false });
// on initial connection
} else if (
account &&
account.address &&
account.chainId &&
!activeWallet &&
config &&
!get().isActiveWalletAccountChanging
) {
console.log('changed acc on initial', account);

set({ isActiveWalletAccountChanging: true });
await get().setActiveWallet({
walletType: config.state.connections.get(config.state.current || '')
?.connector.type as WalletType,
address: account.address,
chainId: account.chainId,
chain: account.chain || VIEM_CHAINS[account.chainId],
isActive: true,
isContractAddress: false,
});
set({ isActiveWalletAccountChanging: false });
}
},

Expand Down
16 changes: 14 additions & 2 deletions src/web3/utils/createWagmiConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Chain, mainnet } from 'viem/chains';
import { Config, createConfig, http } from 'wagmi';

import { fallBackConfig } from '../../utils/chainInfoHelpers';
import { VIEM_CHAINS } from '../../utils/chains';
import { AllConnectorsInitProps, initAllConnectors } from '../connectors';

interface ICreateWagmiConfig {
Expand Down Expand Up @@ -48,12 +49,23 @@ export function createWagmiConfig({

const chainsArrayUnique = [
...new Map(chainsArray.map((item) => [item['id'], item])).values(),
];
].filter((chain) => chain.id !== chainsArray[0].id);

console.log('chainsArrayUnique', chainsArrayUnique);
console.log(
'chainsArrayTotal',
Object.values(VIEM_CHAINS).filter((chain) =>
chainsArrayUnique.forEach((c) => chain.id !== c.id),
),
);

return createConfig({
chains: [
chainsArray[0],
...chainsArrayUnique.filter((chain) => chain.id !== chainsArray[0].id),
...chainsArrayUnique,
...Object.values(VIEM_CHAINS).filter((chain) =>
chainsArrayUnique.forEach((c) => chain.id !== c.id),
),
],
multiInjectedProviderDiscovery: false,
connectors,
Expand Down

0 comments on commit 036b7f0

Please sign in to comment.