Skip to content

Commit

Permalink
fix: remove attemptEagerConnect
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Jul 21, 2022
1 parent eadbd7a commit 61506cf
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 38 deletions.
7 changes: 0 additions & 7 deletions packages/widget/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useEffect } from 'react';
import { Route, Routes } from 'react-router-dom';
import { AppProps, AppProvider } from './AppProvider';
import { AppContainer } from './components/AppContainer';
Expand All @@ -11,7 +10,6 @@ import { SelectWalletPage } from './pages/SelectWalletPage';
import { SettingsPage } from './pages/SettingsPage';
import { SwapPage } from './pages/SwapPage';
import { SwapRoutesPage } from './pages/SwapRoutesPage';
import { useWallet } from './providers/WalletProvider';
import { navigationRoutes } from './utils';

export const App: React.FC<AppProps> = ({ config }) => {
Expand All @@ -23,11 +21,6 @@ export const App: React.FC<AppProps> = ({ config }) => {
};

export const AppDefault = () => {
const { attemptEagerConnect } = useWallet();
useEffect(() => {
attemptEagerConnect();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<AppContainer>
<Header />
Expand Down
30 changes: 4 additions & 26 deletions packages/widget/src/providers/WalletProvider/WalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const initialContext: WalletContextProps = {
switchChain: stub,
addChain: stub,
addToken: stub,
attemptEagerConnect: stub,
account: {},
};

Expand Down Expand Up @@ -72,7 +71,7 @@ export const WalletProvider: FC<PropsWithChildren<{}>> = ({ children }) => {
// only for injected wallets
const switchChain = useCallback(
async (chainId: number) => {
if (config.walletManagement) {
if (config.walletManagement?.switchChain) {
const signer = await config.walletManagement.switchChain(chainId);
const account = await extractAccountFromSigner(signer);
setAccount(account);
Expand All @@ -84,7 +83,7 @@ export const WalletProvider: FC<PropsWithChildren<{}>> = ({ children }) => {

const addChain = useCallback(
async (chainId: number) => {
if (config.walletManagement) {
if (config.walletManagement?.addChain) {
return config.walletManagement.addChain(chainId);
}
return walletAddChain(chainId);
Expand All @@ -94,26 +93,14 @@ export const WalletProvider: FC<PropsWithChildren<{}>> = ({ children }) => {

const addToken = useCallback(
async (chainId: number, token: Token) => {
if (config.walletManagement) {
if (config.walletManagement?.addToken) {
return config.walletManagement.addToken(token, chainId);
}
return switchChainAndAddToken(chainId, token);
},
[config.walletManagement],
);

const attemptEagerConnect = useCallback(async () => {
if (config.walletManagement) {
try {
const signer = await config.walletManagement.getSigner();
const account = await extractAccountFromSigner(signer);
setAccount(() => ({ ...account }));
} catch (e) {
console.warn('WalletProvider: attempted eager connect.', e);
}
}
}, [config.walletManagement]);

// keep account information up to date
useEffect(() => {
const updateAccount = async () => {
Expand All @@ -137,18 +124,9 @@ export const WalletProvider: FC<PropsWithChildren<{}>> = ({ children }) => {
switchChain,
addChain,
addToken,
attemptEagerConnect,
account,
}),
[
account,
addChain,
addToken,
connect,
disconnect,
switchChain,
attemptEagerConnect,
],
[account, addChain, addToken, connect, disconnect, switchChain],
);

return (
Expand Down
1 change: 0 additions & 1 deletion packages/widget/src/providers/WalletProvider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export interface WalletContextProps {
disconnect(): void;
switchChain(chainId: number): Promise<boolean>;
connect(wallet?: Wallet | undefined): Promise<void>;
attemptEagerConnect(): Promise<void>;
}

export interface WalletAccount {
Expand Down
7 changes: 3 additions & 4 deletions packages/widget/src/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ export type ThemeConfig = {
export interface WidgetWalletManagement {
connect(): Promise<Signer>;
disconnect(): Promise<void>;
getSigner(): Promise<Signer | undefined>;
switchChain(reqChainId: number): Promise<Signer>;
addToken(token: Token, chainId: number): Promise<void>;
addChain(chainId: number): Promise<boolean>;
switchChain?(chainId: number): Promise<Signer>;
addToken?(token: Token, chainId: number): Promise<void>;
addChain?(chainId: number): Promise<boolean>;
signer?: Signer;
}

Expand Down

0 comments on commit 61506cf

Please sign in to comment.