Skip to content

Commit

Permalink
fix: connectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Argeare5 committed Jan 11, 2024
1 parent d02e711 commit e5c4f43
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 40 deletions.
21 changes: 2 additions & 19 deletions src/web3/providers/WagmiProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ interface WagmiProviderProps {
changeActiveWalletAccount: (
account?: GetAccountReturnType,
) => Promise<void>;
setConnectors: (connectors: Connectors) => void;
setDefaultChainId: (chainId: number) => void;
getImpersonatedAddress?: () => Hex | undefined;
}>
Expand All @@ -37,18 +36,12 @@ interface WagmiProviderProps {
function Child({
wagmiConfig,
useStore,
connectors,
connectorsInitProps,
}: WagmiProviderProps & {
wagmiConfig: Config;
connectors: Connectors;
}) {
const {
setConnectors,
setDefaultChainId,
setWagmiConfig,
changeActiveWalletAccount,
} = useStore();
const { setDefaultChainId, setWagmiConfig, changeActiveWalletAccount } =
useStore();

watchAccount(wagmiConfig, {
onChange: async (account) => {
Expand All @@ -61,12 +54,6 @@ function Child({
setWagmiConfig(wagmiConfig);
}, [wagmiConfig]);

useEffect(() => {
if (connectors) {
setConnectors(connectors);
}
}, [connectors.length]);

useEffect(() => {
if (connectorsInitProps.defaultChainId) {
setDefaultChainId(connectorsInitProps.defaultChainId);
Expand All @@ -90,9 +77,6 @@ export function WagmiProvider({
};

const [connectors] = useState(initAllConnectors(formattedProps));
const [mappedConnectors] = useState<Connectors>(
connectors.map((connector) => connector),
);

const config = useMemo(() => {
const chains = Object.values(formattedProps.chains);
Expand Down Expand Up @@ -124,7 +108,6 @@ export function WagmiProvider({
<Child
wagmiConfig={config.wagmiConfig}
useStore={useStore}
connectors={mappedConnectors}
connectorsInitProps={formattedProps}
/>
</QueryClientProvider>
Expand Down
29 changes: 8 additions & 21 deletions src/web3/store/walletSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
setLocalStorageWallet,
} from '../../utils/localStorage';
import { WalletType } from '../connectors';
import { Connectors } from '../providers/WagmiProvider';
import { TransactionsSliceBaseType } from './transactionsSlice';

export interface Wallet {
Expand All @@ -46,10 +45,7 @@ export interface Wallet {

export type IWalletSlice = {
wagmiConfig?: Config;
setWagmiConfig: (config: Config) => void;

connectors: Connectors;
setConnectors: (connectors: Connectors) => void;
setWagmiConfig: (config: Config) => Promise<void>;

defaultChainId: number;
setDefaultChainId: (chainId: number) => void;
Expand Down Expand Up @@ -92,14 +88,9 @@ export function createWalletSlice({
walletConnected: (wallet: Wallet) => void;
}): StoreSlice<IWalletSlice, TransactionsSliceBaseType> {
return (set, get) => ({
setWagmiConfig: (config) => {
setWagmiConfig: async (config) => {
set({ wagmiConfig: config });
},

connectors: [],
setConnectors: async (connectors) => {
if (get().connectors.length !== connectors.length) {
set(() => ({ connectors }));
if (get().wagmiConfig?.connectors.length !== config.connectors.length) {
await get().initDefaultWallet();
get().initTxPool();
}
Expand Down Expand Up @@ -168,28 +159,26 @@ export function createWalletSlice({
set({ walletActivating: true });
set({ walletConnectionError: '' });

console.log('connectors', get().connectors);
const connector = get().connectors.find(
const connector = get().wagmiConfig?.connectors.find(
(connector) => connector.type === walletType,
);
console.log('connector', connector);

if (config) {
try {
if (connector) {
if (connector.type === WalletType.Impersonated) {
await connect(config, {
connector: connector.connector,
connector,
chainId,
});
} else {
if (connector.type === WalletType.WalletConnect) {
await connect(config, {
connector: connector.connector,
connector,
chainId: get().defaultChainId,
});
} else {
await connect(config, { connector: connector.connector });
await connect(config, { connector });
}
setLocalStorageWallet(walletType);
}
Expand Down Expand Up @@ -294,7 +283,7 @@ export function createWalletSlice({
await get().setActiveWallet({
walletType: activeWallet.walletType,
address: account.address,
chain: account.chain || activeWallet.chain,
chain: account.chain,
isActive: activeWallet.isActive,
isContractAddress: activeWallet.isContractAddress,
});
Expand Down Expand Up @@ -322,8 +311,6 @@ export function createWalletSlice({
getImpersonatedAddress: () => {
const impersonated = get().impersonated;

console.log('getImpersonatedAddress', impersonated);

if (impersonated) {
if (impersonated.isViewOnly) {
return impersonated.address;
Expand Down

0 comments on commit e5c4f43

Please sign in to comment.