@dynamic-labs/wagmi-connector 3.0.0-alpha.35 → 3.0.0-alpha.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,11 +9,8 @@ const getCreateConnectorFn = ({ connectorId, wallet, preventWagmiSyncFromCalling
9
9
  if (!wallet.connector) {
10
10
  throw new Error('WalletConnector is not defined');
11
11
  }
12
- wallet.connector.on('accountChange', ({ accounts }) => {
13
- const handler = this.onAccountsChanged.bind(this);
14
- handler(accounts);
15
- });
16
- wallet.connector.on('chainChange', ({ chain }) => this.onChainChanged(chain));
12
+ wallet.connector.on('accountChange', this.handleAccountsChange.bind(this));
13
+ wallet.connector.on('chainChange', this.handleChainChange.bind(this));
17
14
  wallet.connector.on('disconnect', this.onDisconnect.bind(this));
18
15
  const accounts = yield this.getAccounts();
19
16
  return {
@@ -28,9 +25,9 @@ const getCreateConnectorFn = ({ connectorId, wallet, preventWagmiSyncFromCalling
28
25
  yield wallet.connector.endSession();
29
26
  yield handleLogOut();
30
27
  }
31
- wallet.connector.off('accountChange');
32
- wallet.connector.off('chainChange');
33
- wallet.connector.off('disconnect');
28
+ wallet.connector.off('accountChange', this.handleAccountsChange.bind(this));
29
+ wallet.connector.off('chainChange', this.handleChainChange.bind(this));
30
+ wallet.connector.off('disconnect', this.onDisconnect.bind(this));
34
31
  /**
35
32
  * In our own Dynamic Wagmi sync component, we will flip the flag to true before calling disconnect,
36
33
  * and here it automatically flips back to false afterwards. This will prevent the Connector from
@@ -74,6 +71,12 @@ const getCreateConnectorFn = ({ connectorId, wallet, preventWagmiSyncFromCalling
74
71
  return wallet.getWalletClient();
75
72
  });
76
73
  },
74
+ handleAccountsChange({ accounts }) {
75
+ this.onAccountsChanged(accounts);
76
+ },
77
+ handleChainChange({ chain }) {
78
+ this.onChainChanged(chain);
79
+ },
77
80
  id: connectorId,
78
81
  isAuthorized() {
79
82
  return __awaiter(this, void 0, void 0, function* () {
@@ -2,20 +2,19 @@
2
2
  import { jsx } from 'react/jsx-runtime';
3
3
  import { useRef, useMemo, useEffect } from 'react';
4
4
  import { useConfig } from 'wagmi';
5
- import { ErrorBoundaryInclude, useDynamicContext, useUserWallets, ErrorBoundaryExclude } from '@dynamic-labs/sdk-react-core';
5
+ import { ErrorBoundaryInclude, useDynamicContext, ErrorBoundaryExclude } from '@dynamic-labs/sdk-react-core';
6
6
  import { getCreateConnectorFn } from './Connector.js';
7
7
  import { SyncDynamicWagmi } from './SyncDynamicWagmi.js';
8
8
  import { useChainMismatchLogger } from './useChainMismatchLogger.js';
9
- import { useFindEvmWallet } from './hooks/useFindEvmWallet/useFindEvmWallet.js';
10
9
  import { useConnectorId } from './hooks/useConnectorId/useConnectorId.js';
11
10
 
12
11
  const DynamicWagmiConnector = ({ suppressChainMismatchError = false, children, }) => (jsx(ErrorBoundaryInclude, { children: jsx(DynamicWagmiConnectorInner, { suppressChainMismatchError: true, children: children }) }));
12
+ const isEvmWallet = (wallet) => (wallet === null || wallet === void 0 ? void 0 : wallet.connector.connectedChain) === 'EVM';
13
13
  const DynamicWagmiConnectorInner = ({ suppressChainMismatchError = false, children }) => {
14
14
  const config = useConfig();
15
15
  const { handleLogOut, primaryWallet } = useDynamicContext();
16
- const wallets = useUserWallets();
17
16
  useChainMismatchLogger({ throwOnMismatch: !suppressChainMismatchError });
18
- const evmWallet = useFindEvmWallet(primaryWallet);
17
+ const evmWallet = isEvmWallet(primaryWallet) ? primaryWallet : null;
19
18
  const getConnectorId = useConnectorId();
20
19
  /**
21
20
  * This will prevent the wagmi Connector from calling handleLogOut when we are the ones calling disconnect
@@ -44,7 +43,7 @@ const DynamicWagmiConnectorInner = ({ suppressChainMismatchError = false, childr
44
43
  useEffect(() => {
45
44
  config._internal.connectors.setState(connector ? [connector] : []);
46
45
  }, [config._internal.connectors, connector]);
47
- return (jsx(SyncDynamicWagmi, { connector: connector, preventWagmiSyncFromCallingLogout: preventWagmiSyncFromCallingLogout, wallets: wallets, children: jsx(ErrorBoundaryExclude, { children: children }) }));
46
+ return (jsx(SyncDynamicWagmi, { connector: connector, preventWagmiSyncFromCallingLogout: preventWagmiSyncFromCallingLogout, wallet: evmWallet, children: jsx(ErrorBoundaryExclude, { children: children }) }));
48
47
  };
49
48
 
50
49
  export { DynamicWagmiConnector, DynamicWagmiConnectorInner };
@@ -3,7 +3,7 @@ import { Connector } from 'wagmi';
3
3
  import { Wallet } from '@dynamic-labs/sdk-react-core';
4
4
  export type SyncDynamicWagmiProps = React.PropsWithChildren<{
5
5
  connector: Connector | undefined;
6
- wallets: Wallet[];
6
+ wallet: Wallet | null;
7
7
  preventWagmiSyncFromCallingLogout: MutableRefObject<boolean>;
8
8
  }>;
9
- export declare const SyncDynamicWagmi: ({ children, connector, wallets, preventWagmiSyncFromCallingLogout, }: SyncDynamicWagmiProps) => JSX.Element;
9
+ export declare const SyncDynamicWagmi: ({ children, connector, wallet, preventWagmiSyncFromCallingLogout, }: SyncDynamicWagmiProps) => JSX.Element;
@@ -1,58 +1,55 @@
1
1
  'use client'
2
+ import { __awaiter } from '../../_virtual/_tslib.js';
2
3
  import { jsx, Fragment } from 'react/jsx-runtime';
3
- import { useRef, useEffect } from 'react';
4
+ import { useRef, useCallback, useEffect } from 'react';
4
5
  import { useConfig, useConnect, useDisconnect } from 'wagmi';
5
- import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
6
+ import { useWalletConnectorEvent } from '@dynamic-labs/sdk-react-core';
6
7
 
7
- const SyncDynamicWagmi = ({ children, connector, wallets, preventWagmiSyncFromCallingLogout, }) => {
8
+ const SyncDynamicWagmi = ({ children, connector, wallet, preventWagmiSyncFromCallingLogout, }) => {
8
9
  const { state: { status: clientStatus }, } = useConfig();
9
10
  const { connect } = useConnect();
10
11
  const { disconnect } = useDisconnect();
11
- const { walletConnector } = useDynamicContext();
12
12
  const lastConnectedConnectorId = useRef(undefined);
13
- useEffect(() => {
14
- const isConnected = Boolean(lastConnectedConnectorId.current);
15
- /**
16
- * Disconnects from Wagmi when no more wallets are available
17
- * and returns early to prevent re-connecting to the same connector
18
- */
19
- if (!wallets.length) {
20
- /**
21
- * Check if the connector is already disconnected to prevent
22
- * disconnecting in a page transition on NextJS with multiple
23
- * layouts
24
- */
25
- if (isConnected) {
26
- /**
27
- * This will prevent the Connector from calling handleLogOut when we are the ones calling disconnect,
28
- * but will preserve the existing behavior for customers who use useDisconnect directly.
29
- */
30
- preventWagmiSyncFromCallingLogout.current = true;
31
- disconnect();
32
- lastConnectedConnectorId.current = undefined;
33
- }
34
- return;
35
- }
13
+ const hasPreviousConnection = Boolean(lastConnectedConnectorId.current);
14
+ const sync = useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
15
+ var _a;
16
+ const isConnected = (_a = (yield (wallet === null || wallet === void 0 ? void 0 : wallet.isConnected()))) !== null && _a !== void 0 ? _a : false;
36
17
  /**
37
18
  * Connects to Wagmi when a connector is available and
38
19
  * the connector has changed since the last connection
39
20
  */
40
- if (walletConnector &&
21
+ if (isConnected &&
41
22
  connector &&
42
23
  (connector === null || connector === void 0 ? void 0 : connector.id) !== lastConnectedConnectorId.current) {
43
24
  lastConnectedConnectorId.current = connector.id;
44
25
  connect({ connector });
26
+ return;
27
+ }
28
+ if (!isConnected && hasPreviousConnection) {
29
+ /**
30
+ * This will prevent the Connector from calling handleLogOut when we are the ones calling disconnect,
31
+ * but will preserve the existing behavior for customers who use useDisconnect directly.
32
+ */
33
+ preventWagmiSyncFromCallingLogout.current = true;
34
+ disconnect();
35
+ lastConnectedConnectorId.current = undefined;
45
36
  }
46
- }, [
47
- clientStatus,
37
+ }), [
48
38
  connect,
49
39
  connector,
50
40
  disconnect,
51
- lastConnectedConnectorId,
41
+ hasPreviousConnection,
52
42
  preventWagmiSyncFromCallingLogout,
53
- walletConnector,
54
- wallets,
43
+ wallet,
55
44
  ]);
45
+ // this is for handling the scenario where the user unlocks the wallet,
46
+ // as well as changes to a linked wallet
47
+ useWalletConnectorEvent(wallet === null || wallet === void 0 ? void 0 : wallet.connector, 'accountChange', () => {
48
+ sync();
49
+ });
50
+ useEffect(() => {
51
+ sync();
52
+ }, [sync, clientStatus]);
56
53
  // eslint-disable-next-line react/jsx-no-useless-fragment
57
54
  return jsx(Fragment, { children: children });
58
55
  };
@@ -1 +0,0 @@
1
- export { useFindEvmWallet } from './useFindEvmWallet';
@@ -1,2 +0,0 @@
1
- import { Wallet } from '@dynamic-labs/sdk-react-core';
2
- export declare const useFindEvmWallet: (primaryWallet: Wallet | null) => Wallet | undefined;
@@ -1,26 +0,0 @@
1
- 'use client'
2
- import { __awaiter } from '../../../../_virtual/_tslib.js';
3
- import { useState, useEffect } from 'react';
4
-
5
- const isEvmWallet = (wallet) => wallet.connector.connectedChain === 'EVM';
6
- const useFindEvmWallet = (primaryWallet) => {
7
- const [wallet, setWallet] = useState(undefined);
8
- const checkForConnectedEvmWallet = (wallet) => __awaiter(void 0, void 0, void 0, function* () {
9
- if (!isEvmWallet(wallet)) {
10
- return;
11
- }
12
- const isConnected = yield wallet.isConnected();
13
- if (isConnected) {
14
- setWallet(wallet);
15
- }
16
- });
17
- useEffect(() => {
18
- if (!primaryWallet) {
19
- return;
20
- }
21
- checkForConnectedEvmWallet(primaryWallet);
22
- }, [primaryWallet]);
23
- return wallet;
24
- };
25
-
26
- export { useFindEvmWallet };