@dynamic-labs/wagmi-connector 3.0.0-alpha.51 → 3.0.0-alpha.53

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.
@@ -3,7 +3,8 @@ import { __awaiter } from '../../_virtual/_tslib.js';
3
3
  import { createConnector } from '@wagmi/core';
4
4
  import { getAddress, createWalletClient, custom } from 'viem';
5
5
 
6
- const getCreateConnectorFn = ({ connectorId, wallet, preventWagmiSyncFromCallingLogout, handleLogOut, }) => createConnector((config) => ({
6
+ /* eslint-disable prefer-arrow/prefer-arrow-functions */
7
+ const getCreateConnectorFn = ({ connectorId, wallet, }) => createConnector((config) => ({
7
8
  connect() {
8
9
  return __awaiter(this, void 0, void 0, function* () {
9
10
  if (!wallet.connector) {
@@ -21,20 +22,9 @@ const getCreateConnectorFn = ({ connectorId, wallet, preventWagmiSyncFromCalling
21
22
  },
22
23
  disconnect() {
23
24
  return __awaiter(this, void 0, void 0, function* () {
24
- if (!preventWagmiSyncFromCallingLogout.current) {
25
- yield wallet.connector.endSession();
26
- yield handleLogOut();
27
- }
28
25
  wallet.connector.off('accountChange', this.handleAccountsChange.bind(this));
29
26
  wallet.connector.off('chainChange', this.handleChainChange.bind(this));
30
27
  wallet.connector.off('disconnect', this.onDisconnect.bind(this));
31
- /**
32
- * In our own Dynamic Wagmi sync component, we will flip the flag to true before calling disconnect,
33
- * and here it automatically flips back to false afterwards. This will prevent the Connector from
34
- * calling handleLogOut when we are the ones calling disconnect, but will preserve the existing behavior
35
- * for customers who use useDisconnect directly.
36
- */
37
- preventWagmiSyncFromCallingLogout.current = false;
38
28
  });
39
29
  },
40
30
  getAccounts() {
@@ -44,7 +34,7 @@ const getCreateConnectorFn = ({ connectorId, wallet, preventWagmiSyncFromCalling
44
34
  },
45
35
  getChainId() {
46
36
  return __awaiter(this, void 0, void 0, function* () {
47
- const network = yield wallet.getNetwork();
37
+ const network = yield wallet.connector.getNetwork();
48
38
  if (!network) {
49
39
  throw new Error('Network is not defined');
50
40
  }
@@ -54,16 +44,17 @@ const getCreateConnectorFn = ({ connectorId, wallet, preventWagmiSyncFromCalling
54
44
  getClient(args) {
55
45
  return __awaiter(this, void 0, void 0, function* () {
56
46
  var _a, _b;
57
- const signer = yield wallet.getSigner();
58
- if (signer.account && signer.chain) {
59
- return signer;
47
+ const walletClient = yield wallet.getWalletClient();
48
+ if (walletClient.account && walletClient.chain) {
49
+ return walletClient;
60
50
  }
61
- const walletClient = createWalletClient({
62
- account: (_a = (yield this.getAccounts())) === null || _a === void 0 ? void 0 : _a[0],
63
- chain: (_b = config.chains.find((chain) => chain.id === (args === null || args === void 0 ? void 0 : args.chainId))) !== null && _b !== void 0 ? _b : config.chains[0],
64
- transport: custom(signer),
51
+ const client = createWalletClient({
52
+ account: walletClient.account || ((_a = (yield this.getAccounts())) === null || _a === void 0 ? void 0 : _a[0]),
53
+ chain: (_b = (walletClient.chain ||
54
+ config.chains.find((chain) => chain.id === (args === null || args === void 0 ? void 0 : args.chainId)))) !== null && _b !== void 0 ? _b : config.chains[0],
55
+ transport: custom(walletClient),
65
56
  });
66
- return walletClient;
57
+ return client;
67
58
  });
68
59
  },
69
60
  getProvider() {
@@ -82,14 +73,14 @@ const getCreateConnectorFn = ({ connectorId, wallet, preventWagmiSyncFromCalling
82
73
  return __awaiter(this, void 0, void 0, function* () {
83
74
  var _a, _b;
84
75
  const accounts = (_b = (yield ((_a = wallet.connector) === null || _a === void 0 ? void 0 : _a.getConnectedAccounts()))) !== null && _b !== void 0 ? _b : [];
85
- return accounts.length > 0;
76
+ return accounts.includes(wallet.address);
86
77
  });
87
78
  },
88
79
  name: 'Dynamic',
89
- onAccountsChanged(accounts) {
90
- config.emitter.emit('change', {
91
- accounts: [getAddress(accounts[0])],
92
- });
80
+ onAccountsChanged(_) {
81
+ // this changes the active account on wagmi, but we don't want to do that
82
+ // we want the active account to always match the primary wallet
83
+ // so we should not emit this event on the wagmi config
93
84
  },
94
85
  onChainChanged(chainId) {
95
86
  config.emitter.emit('change', {
@@ -1,26 +1,27 @@
1
1
  'use client'
2
2
  import { jsx } from 'react/jsx-runtime';
3
- import { useRef, useMemo, useEffect } from 'react';
3
+ import { useMemo, useEffect } from 'react';
4
4
  import { useConfig } from 'wagmi';
5
5
  import { ErrorBoundaryInclude, useDynamicContext, ErrorBoundaryExclude } from '@dynamic-labs/sdk-react-core';
6
+ import { isEthereumWallet } from '@dynamic-labs/ethereum-core';
6
7
  import { getCreateConnectorFn } from './Connector.js';
7
8
  import { SyncDynamicWagmi } from './SyncDynamicWagmi.js';
8
9
  import { useChainMismatchLogger } from './useChainMismatchLogger.js';
9
10
  import { useConnectorId } from './hooks/useConnectorId/useConnectorId.js';
10
11
 
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';
12
+ const DynamicWagmiConnector = ({ suppressChainMismatchError = false, children, }) => (jsx(ErrorBoundaryInclude, { children: jsx(DynamicWagmiConnectorInner, { suppressChainMismatchError: suppressChainMismatchError, children: children }) }));
13
+ const getEvmWallet = (wallet) => {
14
+ if (!wallet || !isEthereumWallet(wallet)) {
15
+ return null;
16
+ }
17
+ return wallet;
18
+ };
13
19
  const DynamicWagmiConnectorInner = ({ suppressChainMismatchError = false, children }) => {
14
20
  const config = useConfig();
15
- const { handleLogOut, primaryWallet } = useDynamicContext();
21
+ const { primaryWallet } = useDynamicContext();
16
22
  useChainMismatchLogger({ throwOnMismatch: !suppressChainMismatchError });
17
- const evmWallet = isEvmWallet(primaryWallet) ? primaryWallet : null;
23
+ const evmWallet = getEvmWallet(primaryWallet);
18
24
  const getConnectorId = useConnectorId();
19
- /**
20
- * This will prevent the wagmi Connector from calling handleLogOut when we are the ones calling disconnect
21
- * as a result of a handleLogOut call (see SyncDynamicWagmi), but will preserve the existing behavior for customers.
22
- */
23
- const preventWagmiSyncFromCallingLogout = useRef(false);
24
25
  const connector = useMemo(() => {
25
26
  if (!evmWallet) {
26
27
  return;
@@ -28,11 +29,9 @@ const DynamicWagmiConnectorInner = ({ suppressChainMismatchError = false, childr
28
29
  const connectorId = getConnectorId(evmWallet.connector);
29
30
  return config._internal.connectors.setup(getCreateConnectorFn({
30
31
  connectorId,
31
- handleLogOut,
32
- preventWagmiSyncFromCallingLogout,
33
32
  wallet: evmWallet,
34
33
  }));
35
- }, [config._internal.connectors, getConnectorId, handleLogOut, evmWallet]);
34
+ }, [config._internal.connectors, getConnectorId, evmWallet]);
36
35
  /**
37
36
  * Updating the wagmi config must be done in a useEffect because
38
37
  * when setting the public client and connectors, wagmi will fire
@@ -43,7 +42,7 @@ const DynamicWagmiConnectorInner = ({ suppressChainMismatchError = false, childr
43
42
  useEffect(() => {
44
43
  config._internal.connectors.setState(connector ? [connector] : []);
45
44
  }, [config._internal.connectors, connector]);
46
- return (jsx(SyncDynamicWagmi, { connector: connector, preventWagmiSyncFromCallingLogout: preventWagmiSyncFromCallingLogout, wallet: evmWallet, children: jsx(ErrorBoundaryExclude, { children: children }) }));
45
+ return (jsx(SyncDynamicWagmi, { connector: connector, wallet: evmWallet, children: jsx(ErrorBoundaryExclude, { children: children }) }));
47
46
  };
48
47
 
49
48
  export { DynamicWagmiConnector, DynamicWagmiConnectorInner };
@@ -1,9 +1,8 @@
1
- import React, { MutableRefObject } from 'react';
1
+ import React from 'react';
2
2
  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
6
  wallet: Wallet | null;
7
- preventWagmiSyncFromCallingLogout: MutableRefObject<boolean>;
8
7
  }>;
9
- export declare const SyncDynamicWagmi: ({ children, connector, wallet, preventWagmiSyncFromCallingLogout, }: SyncDynamicWagmiProps) => JSX.Element;
8
+ export declare const SyncDynamicWagmi: ({ children, connector, wallet, }: SyncDynamicWagmiProps) => JSX.Element;
@@ -4,43 +4,55 @@ import { jsx, Fragment } from 'react/jsx-runtime';
4
4
  import { useRef, useCallback, useEffect } from 'react';
5
5
  import { useConfig, useConnect, useDisconnect } from 'wagmi';
6
6
  import { useWalletConnectorEvent } from '@dynamic-labs/sdk-react-core';
7
+ import { logger } from '@dynamic-labs/wallet-connector-core';
7
8
 
8
- const SyncDynamicWagmi = ({ children, connector, wallet, preventWagmiSyncFromCallingLogout, }) => {
9
+ const SyncDynamicWagmi = ({ children, connector, wallet, }) => {
9
10
  const { state: { status: clientStatus }, } = useConfig();
10
11
  const { connect } = useConnect();
11
12
  const { disconnect } = useDisconnect();
12
- const lastConnectedConnectorId = useRef(undefined);
13
- const hasPreviousConnection = Boolean(lastConnectedConnectorId.current);
13
+ const lastConnectedWalletId = useRef(undefined);
14
+ const hasPreviousConnection = Boolean(lastConnectedWalletId.current);
15
+ const disconnectWagmi = useCallback(() => {
16
+ logger.debug('[SyncDynamicWagmi] disconnecting wagmi - previous status: ', clientStatus);
17
+ if (clientStatus === 'disconnected') {
18
+ return;
19
+ }
20
+ disconnect();
21
+ lastConnectedWalletId.current = undefined;
22
+ }, [disconnect, clientStatus]);
23
+ const connectWagmi = useCallback((connector, newId, forceConnect) => {
24
+ logger.debug('[SyncDynamicWagmi] connecting wagmi - previous status: ', clientStatus);
25
+ if (clientStatus !== 'disconnected' && !forceConnect) {
26
+ return;
27
+ }
28
+ lastConnectedWalletId.current = newId;
29
+ connect({ connector });
30
+ }, [connect, clientStatus]);
14
31
  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;
17
- /**
18
- * Connects to Wagmi when a connector is available and
19
- * the connector has changed since the last connection
20
- */
21
- if (isConnected &&
22
- connector &&
23
- (connector === null || connector === void 0 ? void 0 : connector.id) !== lastConnectedConnectorId.current) {
24
- lastConnectedConnectorId.current = connector.id;
25
- connect({ connector });
32
+ // if there's no connector, disconnect wagmi
33
+ if (!connector || !(wallet === null || wallet === void 0 ? void 0 : wallet.address)) {
34
+ disconnectWagmi();
35
+ return;
36
+ }
37
+ const newId = `${connector.id}-${wallet.address}`;
38
+ // if the connector hasn't changed since the last connection, do nothing
39
+ if (newId === lastConnectedWalletId.current) {
26
40
  return;
27
41
  }
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;
42
+ // if has a previous connection, disconnect wagmi
43
+ if (hasPreviousConnection) {
44
+ disconnectWagmi();
36
45
  }
46
+ // and then (re)connect wagmi
47
+ // force reconnect if has disconnected in the preivous step
48
+ // in case the clientStatus takes a while to update
49
+ connectWagmi(connector, newId, hasPreviousConnection);
37
50
  }), [
38
- connect,
51
+ connectWagmi,
39
52
  connector,
40
- disconnect,
53
+ disconnectWagmi,
41
54
  hasPreviousConnection,
42
- preventWagmiSyncFromCallingLogout,
43
- wallet,
55
+ wallet === null || wallet === void 0 ? void 0 : wallet.address,
44
56
  ]);
45
57
  // this is for handling the scenario where the user unlocks the wallet,
46
58
  // as well as changes to a linked wallet