Skip to content

Commit

Permalink
fix: eagerConnect (#36)
Browse files Browse the repository at this point in the history
* fix: eagerConnect

* fix: signer.provider calc
  • Loading branch information
Addminus authored Nov 16, 2022
1 parent cb2b691 commit b2b7dde
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 22 deletions.
31 changes: 20 additions & 11 deletions packages/wallet-management/src/LiFiWalletManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ethers } from 'ethers';
import type { Connector } from '@web3-react/types';
import type { Signer } from 'ethers';
import { useCallback, useEffect, useState } from 'react';
import { getSelectedConnector } from '@web3-react/core';
import { usePriorityConnector, usePriorityProvider } from './priorityConnector';
// import { usePriorityConnector, usePriorityProvider } from './connectorHooks';
import {
Expand All @@ -14,6 +13,7 @@ import {
removeFromDeactivatedWallets,
} from './walletPersistance';
import type { Wallet } from './walletProviders';
import { getInjectedAddress } from './injectedData';

export const useLiFiWalletManagement = () => {
const priorityConnector = usePriorityConnector();
Expand All @@ -33,8 +33,8 @@ export const useLiFiWalletManagement = () => {
const calcWalletData = (connector?: Connector) => {
if (connector) {
const provider = new ethers.providers.Web3Provider(
connector.provider as ExternalProvider,
'any',
(connector.provider as ExternalProvider) || (window as any).ethereum,
'any', // fallback
);
setCurrentProvider(() => provider);
setCurrentConnector(() => connector);
Expand All @@ -44,8 +44,6 @@ export const useLiFiWalletManagement = () => {

const connect = useCallback(
async (wallet?: Wallet) => {
const selectedAddress = (window as any)?.ethereum?.selectedAddress;

try {
if (wallet) {
const { connector } = wallet.web3react;
Expand All @@ -57,6 +55,7 @@ export const useLiFiWalletManagement = () => {
} catch (e) {
console.log(e);
}
const selectedAddress = getInjectedAddress(wallet);
removeFromDeactivatedWallets(selectedAddress);
addToActiveWallets(selectedAddress);
},
Expand All @@ -65,7 +64,7 @@ export const useLiFiWalletManagement = () => {

const disconnect = useCallback(
async (wallet?: Wallet) => {
const selectedAddress = (window as any).ethereum?.selectedAddress;
const selectedAddress = getInjectedAddress(wallet);
removeFromActiveWallets(selectedAddress);
addToDeactivatedWallets(selectedAddress);
if (wallet) {
Expand All @@ -84,11 +83,21 @@ export const useLiFiWalletManagement = () => {

// eager connect
useEffect(() => {
const selectedAddress = (window as any).ethereum?.selectedAddress;
if (!isWalletDeactivated(selectedAddress) && priorityConnector) {
priorityConnector?.connectEagerly!();
}
}, [priorityConnector]);
const eagerConnect = async () => {
const selectedAddress = getInjectedAddress();
if (!isWalletDeactivated(selectedAddress) && priorityConnector) {
priorityConnector.connectEagerly?.();
calcWalletData(priorityConnector);
}
};
eagerConnect();
}, [
priorityConnector,
// eslint-disable-next-line react-hooks/exhaustive-deps
(window as any).ethereum?.selectedAddress,
// eslint-disable-next-line react-hooks/exhaustive-deps
(window as any).tally?.selectedAddress,
]);

// injected wallet listeners
useEffect(() => {
Expand Down
21 changes: 21 additions & 0 deletions packages/wallet-management/src/injectedData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Wallet } from '.';
import { wallets } from './wallets';

export const getInjectedAddress = (wallet?: Wallet) => {
switch (wallet?.name) {
case wallets.metamask.name:
return getMetamaskInjectedAddress();
case wallets.tallyho.name:
return getTallyInjectedAddress();
default:
return getMetamaskInjectedAddress() ?? getTallyInjectedAddress();
}
};

const getMetamaskInjectedAddress = () => {
return (window as any).ethereum?.selectedAddress;
};

const getTallyInjectedAddress = () => {
return (window as any).ethereum?.selectedAddress;
};
18 changes: 8 additions & 10 deletions packages/wallet-management/src/priorityConnector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { getPriorityConnector } from '@web3-react/core';
import { getPriorityConnector, Web3ReactHooks } from '@web3-react/core';
import { Connector } from '@web3-react/types';
import { supportedWallets } from './walletProviders';
// import { eip1193, hooks as eip1193Hooks } from './connectors/eip1193';

const metamaskWallet = supportedWallets.find(
(wallet) => wallet.name === 'MetaMask',
// const metamaskWallet = supportedWallets.find(
// (wallet) => wallet.name === 'MetaMask',
// );
const connectorHookList: [Connector, Web3ReactHooks][] = supportedWallets.map(
(wallet) => [wallet.web3react.connector, wallet.web3react.hooks],
);
export const {
useSelectedStore,
Expand All @@ -25,10 +29,4 @@ export const {
usePriorityProvider,
usePriorityENSNames,
usePriorityENSName,
} = getPriorityConnector(
[metamaskWallet!.web3react.connector, metamaskWallet!.web3react.hooks], // needs to be on top!
// [eip1193, eip1193Hooks],
// [walletConnect, walletConnectHooks],
// [walletLink, walletLinkHooks],
// [network, networkHooks],
);
} = getPriorityConnector(...connectorHookList);
2 changes: 1 addition & 1 deletion packages/wallet-management/src/walletPersistance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const removeFromDeactivatedWallets = (address?: string | null) => {
};

const isWalletDeactivated = (address?: string | null): boolean => {
if (!address) return false;
if (!address) return true;
const lowerCaseAddress = address.toLowerCase();
const deactivatedWallets = readDeactivatedWallets();
const deactivatedAddresses = deactivatedWallets.map((address) =>
Expand Down

0 comments on commit b2b7dde

Please sign in to comment.