@dynamic-labs/wagmi-connector 0.18.24 → 0.18.25

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.
package/CHANGELOG.md CHANGED
@@ -1,4 +1,11 @@
1
1
 
2
+ ### [0.18.25](https://github.com/dynamic-labs/DynamicAuth/compare/v0.18.24...v0.18.25) (2023-10-12)
3
+
4
+
5
+ ### Bug Fixes
6
+
7
+ * **wagmi-connector:** use wallets list to sync wagmi client ([#3563](https://github.com/dynamic-labs/DynamicAuth/issues/3563)) ([#3580](https://github.com/dynamic-labs/DynamicAuth/issues/3580)) ([12e464c](https://github.com/dynamic-labs/DynamicAuth/commit/12e464cca2cd39478475893bc084c5624a034cba))
8
+
2
9
  ### [0.18.24](https://github.com/dynamic-labs/DynamicAuth/compare/v0.18.23...v0.18.24) (2023-10-03)
3
10
 
4
11
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamic-labs/wagmi-connector",
3
- "version": "0.18.24",
3
+ "version": "0.18.25",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/dynamic-labs/DynamicAuth.git",
@@ -11,9 +11,8 @@
11
11
  "react": "^17.0.2 || ^18.0.0",
12
12
  "ethers": "5.7.2",
13
13
  "wagmi": "~0.12.13",
14
- "@dynamic-labs/sdk-react-core": "0.18.24",
15
- "@dynamic-labs/types": "0.18.24",
16
- "eventemitter3": "5.0.1"
14
+ "@dynamic-labs/sdk-react-core": "0.18.25",
15
+ "@dynamic-labs/types": "0.18.25"
17
16
  },
18
17
  "license": "MIT",
19
18
  "main": "./src/index.cjs",
@@ -1,4 +1,4 @@
1
- import React, { useEffect } from 'react';
1
+ import React, { useEffect, useMemo } from 'react';
2
2
  import { configureChains, mainnet, createClient, WagmiConfig } from 'wagmi';
3
3
  import { publicProvider } from 'wagmi/providers/public';
4
4
  import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
@@ -12,14 +12,13 @@ const { provider } = configureChains([mainnet], [publicProvider()]);
12
12
  const client = createClient({
13
13
  provider,
14
14
  });
15
- /**
16
- * we create a "default" (static chain and provider) client to use in the WagmiConfig
17
- * until we've loaded the dynamic chains and providers, at which point we update the client
18
- */
19
- let connector = undefined;
20
15
  const DynamicWagmiConnector = ({ evmNetworks: customerSuppliedEvmNetworks, children, }) => {
21
- var _a;
22
16
  const { networkConfigurations, handleLogOut, connectedWallets, linkedWallets, authMode, sendWagmiSettings, } = useDynamicContext();
17
+ /**
18
+ * Linked wallets are only available on connect-and-sign mode and the connected wallets
19
+ * are only available on connect-only mode, the wallet const represents the current available
20
+ * wallets to be used when creating an wagmi connector
21
+ */
23
22
  const wallets = authMode === 'connect-only' ? connectedWallets : linkedWallets;
24
23
  useEffect(() => {
25
24
  // only send wagmi settings once, when the first time DynamicWagmiConnector renders
@@ -28,22 +27,34 @@ const DynamicWagmiConnector = ({ evmNetworks: customerSuppliedEvmNetworks, child
28
27
  });
29
28
  // eslint-disable-next-line react-hooks/exhaustive-deps
30
29
  }, []);
31
- const evmNetworks = ((_a = customerSuppliedEvmNetworks !== null && customerSuppliedEvmNetworks !== void 0 ? customerSuppliedEvmNetworks : networkConfigurations === null || networkConfigurations === void 0 ? void 0 : networkConfigurations.evm) !== null && _a !== void 0 ? _a : []).map((network) => {
32
- network.chainId = parseInt(network.chainId.toString());
33
- return network;
34
- });
35
- const mappedChains = getWagmiChainsFromDynamicChains(evmNetworks);
36
- const mappedProviders = getWagmiProvidersFromDynamicChains(evmNetworks);
37
- const { chains, provider } = configureChains(mappedChains, mappedProviders);
38
- connector = getConnector(wallets, (wallet) => new Connector({
30
+ const { chains, provider, webSocketProvider } = useMemo(() => {
31
+ var _a;
32
+ const evmNetworks = ((_a = customerSuppliedEvmNetworks !== null && customerSuppliedEvmNetworks !== void 0 ? customerSuppliedEvmNetworks : networkConfigurations === null || networkConfigurations === void 0 ? void 0 : networkConfigurations.evm) !== null && _a !== void 0 ? _a : []).map((network) => {
33
+ network.chainId = parseInt(network.chainId.toString());
34
+ return network;
35
+ });
36
+ const mappedChains = getWagmiChainsFromDynamicChains(evmNetworks);
37
+ const mappedProviders = getWagmiProvidersFromDynamicChains(evmNetworks);
38
+ return configureChains(mappedChains, mappedProviders);
39
+ }, [customerSuppliedEvmNetworks, networkConfigurations === null || networkConfigurations === void 0 ? void 0 : networkConfigurations.evm]);
40
+ const connector = useMemo(() => getConnector(wallets, (wallet) => new Connector({
39
41
  chains,
40
42
  handleLogOut,
41
43
  walletConnector: wallet.connector,
42
- }));
43
- client.config = Object.assign(Object.assign({}, client.config), { autoConnect: true, connectors: connector ? [connector] : [], provider: provider });
44
+ })), [wallets, chains, handleLogOut]);
45
+ /**
46
+ * Updating the wagmi config must be done in a useEffect because
47
+ * when setting the public client and connectors, wagmi will fire
48
+ * an rerender.
49
+ * Keeping the update in a useEffect will prevent an state update
50
+ * while react is in the middle of rendering.
51
+ */
52
+ useEffect(() => {
53
+ client.config = Object.assign(Object.assign({}, client.config), { autoConnect: true, connectors: connector ? [connector] : [], provider: provider, webSocketProvider: webSocketProvider });
54
+ }, [connector, provider, webSocketProvider]);
44
55
  // use React.createElement to prevent bunding react/jsx-runtime,
45
56
  // which is not compatible when bundling apps using React 17
46
- return React.createElement(WagmiConfig, { client: client }, React.createElement(SyncDynamicWagmi, { connector }, children));
57
+ return React.createElement(WagmiConfig, { client: client }, React.createElement(SyncDynamicWagmi, { connector, wallets }, children));
47
58
  };
48
59
 
49
60
  export { DynamicWagmiConnector };
@@ -1,8 +1,10 @@
1
1
  import React from 'react';
2
+ import { Wallet } from '@dynamic-labs/sdk-react-core';
2
3
  import { Connector } from './Connector';
3
4
  export type SyncDynamicWagmiProps = React.PropsWithChildren<{
4
5
  connector: Connector | undefined;
6
+ wallets: Wallet[];
5
7
  }>;
6
- export declare const SyncDynamicWagmi: ({ children, connector, }: SyncDynamicWagmiProps) => React.FunctionComponentElement<{
8
+ export declare const SyncDynamicWagmi: ({ children, connector, wallets, }: SyncDynamicWagmiProps) => React.FunctionComponentElement<{
7
9
  children?: React.ReactNode;
8
10
  }>;
@@ -2,24 +2,34 @@ import React, { useRef, useEffect } from 'react';
2
2
  import { useClient, useConnect, useDisconnect } from 'wagmi';
3
3
  import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
4
4
 
5
- const SyncDynamicWagmi = ({ children, connector, }) => {
5
+ const SyncDynamicWagmi = ({ children, connector, wallets, }) => {
6
6
  const { status: clientStatus } = useClient();
7
7
  const { connect } = useConnect();
8
8
  const { disconnect } = useDisconnect();
9
- const { internalEvents, walletConnector } = useDynamicContext();
9
+ const { walletConnector } = useDynamicContext();
10
10
  const lastConnectedConnectorId = useRef(undefined);
11
11
  useEffect(() => {
12
- const events = internalEvents.current;
13
- const handleDisconnect = () => {
14
- lastConnectedConnectorId.current = undefined;
15
- disconnect();
16
- };
17
- events.on('clearPrimaryWallet', handleDisconnect);
18
- return () => {
19
- events.off('clearPrimaryWallet', handleDisconnect);
20
- };
21
- }, [disconnect, internalEvents]);
22
- useEffect(() => {
12
+ const isConnected = Boolean(lastConnectedConnectorId.current);
13
+ /**
14
+ * Disconnects from Wagmi when no more wallets are available
15
+ * and returns early to prevent re-connecting to the same connector
16
+ */
17
+ if (!wallets.length) {
18
+ /**
19
+ * Check if the connector is already disconnected to prevent
20
+ * disconnecting in a page transition on NextJS with multiple
21
+ * layouts
22
+ */
23
+ if (isConnected) {
24
+ disconnect();
25
+ lastConnectedConnectorId.current = undefined;
26
+ }
27
+ return;
28
+ }
29
+ /**
30
+ * Connects to Wagmi when a connector is available and
31
+ * the connector has changed since the last connection
32
+ */
23
33
  if (walletConnector &&
24
34
  connector &&
25
35
  (connector === null || connector === void 0 ? void 0 : connector.id) !== lastConnectedConnectorId.current) {
@@ -33,6 +43,7 @@ const SyncDynamicWagmi = ({ children, connector, }) => {
33
43
  disconnect,
34
44
  lastConnectedConnectorId,
35
45
  walletConnector,
46
+ wallets,
36
47
  ]);
37
48
  // use React.createElement to prevent bunding react/jsx-runtime,
38
49
  // which is not compatible when bundling apps using React 17