@dynamic-labs/wagmi-connector 2.0.0-alpha.2 → 2.0.0-alpha.21

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.
@@ -1,146 +1,128 @@
1
1
  import { __awaiter } from '../../_virtual/_tslib.js';
2
- import { Connector as Connector$1 } from 'wagmi';
3
- import { getAddress, createWalletClient, custom, toHex } from 'viem';
2
+ import { createConnector } from '@wagmi/core';
3
+ import { getAddress, createWalletClient, custom } from 'viem';
4
4
 
5
- class Connector extends Connector$1 {
6
- constructor({ chains, handleLogOut, walletConnector, preventWagmiSyncFromCallingLogout, }) {
7
- super({ chains, options: undefined });
8
- // wagmi properties
9
- this.id = 'dynamic';
10
- this.name = 'Dynamic';
11
- // also a wagmi prop, but is only used for auto-connect
12
- // which we have implemented ourselves, so this value
13
- // doesn't matter
14
- this.ready = false;
15
- this.onAccountsChanged = (accounts) => __awaiter(this, void 0, void 0, function* () {
16
- this.emit('change', {
17
- account: getAddress(accounts[0]),
18
- });
19
- });
20
- this.onChainChanged = (chain) => __awaiter(this, void 0, void 0, function* () {
21
- this.emit('change', {
22
- chain: { id: Number(chain), unsupported: false },
23
- });
24
- });
25
- this.onDisconnect = () => __awaiter(this, void 0, void 0, function* () { });
26
- this.handleLogOut = handleLogOut;
27
- this.walletConnector = walletConnector;
28
- this.id = `dynamic-${walletConnector.key}`;
29
- this.preventWagmiSyncFromCallingLogout = preventWagmiSyncFromCallingLogout;
30
- }
31
- _onAccountsChanged({ accounts }) {
32
- this.onAccountsChanged(accounts);
33
- }
34
- _onChainChanged({ chain }) {
35
- this.onChainChanged(chain);
36
- }
37
- _onDisconnect() {
38
- this.onDisconnect();
39
- }
40
- setupEventListeners() {
41
- this.walletConnector.on('accountChange', this._onAccountsChanged, this);
42
- this.walletConnector.on('chainChange', this._onChainChanged, this);
43
- this.walletConnector.on('disconnect', this._onDisconnect, this);
44
- }
45
- connect(config) {
5
+ const getCreateConnectorFn = (parameters) => createConnector((config) => ({
6
+ connect() {
46
7
  return __awaiter(this, void 0, void 0, function* () {
47
- if (!this.walletConnector) {
8
+ if (!parameters.walletConnector) {
48
9
  throw new Error('WalletConnector is not defined');
49
10
  }
50
- const account = yield this.getAccount();
51
- this.setupEventListeners();
52
- this.emit('message', { type: 'connecting' });
11
+ parameters.walletConnector.on('accountChange', ({ accounts }) => {
12
+ const handler = this.onAccountsChanged.bind(this);
13
+ handler(accounts);
14
+ });
15
+ parameters.walletConnector.on('chainChange', ({ chain }) => this.onChainChanged(chain));
16
+ parameters.walletConnector.on('disconnect', this.onDisconnect.bind(this));
17
+ const accounts = yield this.getAccounts();
53
18
  return {
54
- account,
55
- chain: { id: yield this.getChainId(), unsupported: false },
19
+ accounts,
20
+ chainId: yield this.getChainId(),
56
21
  };
57
22
  });
58
- }
23
+ },
59
24
  disconnect() {
60
25
  return __awaiter(this, void 0, void 0, function* () {
61
- this.walletConnector.removeListener('accountChange', this._onAccountsChanged, this);
62
- this.walletConnector.removeListener('chainChange', this._onChainChanged, this);
63
- this.walletConnector.removeListener('disconnect', this._onDisconnect, this);
64
- if (!this.preventWagmiSyncFromCallingLogout.current) {
65
- yield this.walletConnector.endSession();
66
- yield this.handleLogOut();
26
+ if (!parameters.preventWagmiSyncFromCallingLogout.current) {
27
+ yield parameters.walletConnector.endSession();
28
+ yield parameters.handleLogOut();
67
29
  }
30
+ parameters.walletConnector.off('accountChange');
31
+ parameters.walletConnector.off('chainChange');
32
+ parameters.walletConnector.off('disconnect');
68
33
  /**
69
34
  * In our own Dynamic Wagmi sync component, we will flip the flag to true before calling disconnect,
70
35
  * and here it automatically flips back to false afterwards. This will prevent the Connector from
71
36
  * calling handleLogOut when we are the ones calling disconnect, but will preserve the existing behavior
72
37
  * for customers who use useDisconnect directly.
73
38
  */
74
- this.preventWagmiSyncFromCallingLogout.current = false;
39
+ parameters.preventWagmiSyncFromCallingLogout.current = false;
75
40
  });
76
- }
77
- getAccount() {
41
+ },
42
+ getAccounts() {
78
43
  var _a;
79
44
  return __awaiter(this, void 0, void 0, function* () {
80
- const address = yield ((_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.fetchPublicAddress());
45
+ const address = yield ((_a = parameters.walletConnector) === null || _a === void 0 ? void 0 : _a.getAddress());
81
46
  if (!address) {
82
47
  throw new Error('Not connected');
83
48
  }
84
- return getAddress(address);
49
+ return [getAddress(address)];
85
50
  });
86
- }
51
+ },
87
52
  getChainId() {
88
53
  return __awaiter(this, void 0, void 0, function* () {
89
- if (!this.walletConnector) {
54
+ if (!parameters.walletConnector) {
90
55
  throw new Error('WalletConnector is not defined');
91
56
  }
92
- const network = yield this.walletConnector.getNetwork();
57
+ const network = yield parameters.walletConnector.getNetwork();
93
58
  if (!network) {
94
59
  throw new Error('Network is not defined');
95
60
  }
96
61
  return Number(network);
97
62
  });
98
- }
99
- getProvider(config) {
100
- var _a;
101
- return __awaiter(this, void 0, void 0, function* () {
102
- return (_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.getWalletClient();
103
- });
104
- }
105
- getWalletClient(config) {
106
- var _a;
63
+ },
64
+ getClient(args) {
65
+ var _a, _b;
107
66
  return __awaiter(this, void 0, void 0, function* () {
108
- const signer = yield this.walletConnector.getSigner();
67
+ const signer = yield parameters.walletConnector.getSigner();
109
68
  if (signer.account && signer.chain) {
110
69
  return signer;
111
70
  }
112
71
  const walletClient = createWalletClient({
113
- account: yield this.getAccount(),
114
- chain: (_a = this.chains.find((chain) => chain.id === (config === null || config === void 0 ? void 0 : config.chainId))) !== null && _a !== void 0 ? _a : this.chains[0],
72
+ account: (_a = (yield this.getAccounts())) === null || _a === void 0 ? void 0 : _a[0],
73
+ 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],
115
74
  transport: custom(signer),
116
75
  });
117
76
  return walletClient;
118
77
  });
119
- }
78
+ },
79
+ getProvider() {
80
+ var _a;
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ return (_a = parameters.walletConnector) === null || _a === void 0 ? void 0 : _a.getWalletClient();
83
+ });
84
+ },
85
+ id: `dynamic-${parameters.walletConnector.key}`,
120
86
  isAuthorized() {
121
87
  var _a, _b;
122
88
  return __awaiter(this, void 0, void 0, function* () {
123
- const accounts = (_b = (yield ((_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.getConnectedAccounts()))) !== null && _b !== void 0 ? _b : [];
89
+ const accounts = (_b = (yield ((_a = parameters.walletConnector) === null || _a === void 0 ? void 0 : _a.getConnectedAccounts()))) !== null && _b !== void 0 ? _b : [];
124
90
  return accounts.length > 0;
125
91
  });
126
- }
127
- switchChain(chainId) {
128
- var _a, _b;
92
+ },
93
+ name: 'Dynamic',
94
+ onAccountsChanged(accounts) {
95
+ config.emitter.emit('change', {
96
+ accounts: [getAddress(accounts[0])],
97
+ });
98
+ },
99
+ onChainChanged(chainId) {
100
+ config.emitter.emit('change', {
101
+ chainId: Number(chainId),
102
+ });
103
+ },
104
+ onDisconnect() { },
105
+ switchChain({ chainId }) {
106
+ var _a;
129
107
  return __awaiter(this, void 0, void 0, function* () {
130
- yield ((_a = this.walletConnector) === null || _a === void 0 ? void 0 : _a.switchNetwork({ networkChainId: chainId }));
131
- const id = toHex(chainId);
132
- return ((_b = this.chains.find((x) => x.id === chainId)) !== null && _b !== void 0 ? _b : {
133
- id: chainId,
134
- name: `Chain ${id}`,
135
- nativeCurrency: { decimals: 18, name: 'Ether', symbol: 'ETH' },
136
- network: `${id}`,
137
- rpcUrls: { default: { http: [''] }, public: { http: [''] } },
138
- });
108
+ const chain = config.chains.find((x) => x.id === chainId);
109
+ if (!chain) {
110
+ throw new Error(`Chain ${chainId} is not supported`);
111
+ }
112
+ yield ((_a = parameters.walletConnector) === null || _a === void 0 ? void 0 : _a.switchNetwork({
113
+ networkChainId: chainId,
114
+ }));
115
+ return chain;
139
116
  });
117
+ },
118
+ type: 'dynamic',
119
+ }));
120
+ const createConnectorForFirstConnectedEvmWallet = (wallets, createConnector) => {
121
+ for (const wallet of wallets) {
122
+ if (wallet.connected && wallet.connector.connectedChain === 'EVM') {
123
+ return createConnector(wallet);
124
+ }
140
125
  }
141
- watchAsset(asset) {
142
- throw new Error('Method not implemented.');
143
- }
144
- }
126
+ };
145
127
 
146
- export { Connector };
128
+ export { createConnectorForFirstConnectedEvmWallet, getCreateConnectorFn };
@@ -1,8 +1,6 @@
1
- import { ReactNode } from 'react';
2
- import { WagmiEvmNetworks } from './types';
1
+ import React, { ReactNode } from 'react';
3
2
  type DynamicWagmiConnectorProps = {
4
3
  children: ReactNode;
5
- evmNetworks?: WagmiEvmNetworks[];
6
4
  };
7
- export declare const DynamicWagmiConnector: ({ evmNetworks: customerSuppliedEvmNetworks, children, }: DynamicWagmiConnectorProps) => JSX.Element;
5
+ export declare const DynamicWagmiConnector: React.FC<DynamicWagmiConnectorProps>;
8
6
  export {};
@@ -1,22 +1,16 @@
1
1
  import { jsx } from 'react/jsx-runtime';
2
- import React, { useRef, useMemo, useEffect } from 'react';
3
- import { configureChains, mainnet, createConfig, WagmiConfig } from 'wagmi';
4
- import { publicProvider } from 'wagmi/providers/public';
2
+ import { useRef, useMemo, useEffect } from 'react';
3
+ import { useConfig } from 'wagmi';
5
4
  import { useDynamicContext, useUserWallets } from '@dynamic-labs/sdk-react-core';
6
- import { Connector } from './Connector.js';
7
- import { getConnector } from './getConnector.js';
8
- import { getWagmiChainsFromDynamicChains } from './getWagmiChainsFromDynamicChains.js';
9
- import { getWagmiProvidersFromDynamicChains } from './getWagmiProvidersFromDynamicChains.js';
10
- import { SyncDynamicWagmi } from './SyncDynamicWagmi.js';
11
5
  import { findAndOrderEvmWallets } from './utils/findAndOrderEvmWallets/findAndOrderEvmWallets.js';
6
+ import { createConnectorForFirstConnectedEvmWallet, getCreateConnectorFn } from './Connector.js';
7
+ import { SyncDynamicWagmi } from './SyncDynamicWagmi.js';
8
+ import { useChainMismatchLogger } from './useChainMismatchLogger.js';
12
9
 
13
- const { publicClient } = configureChains([mainnet], [publicProvider()]);
14
- const config = createConfig({
15
- autoConnect: true,
16
- publicClient,
17
- });
18
- const DynamicWagmiConnector = ({ evmNetworks: customerSuppliedEvmNetworks, children, }) => {
19
- const { networkConfigurations, handleLogOut, sendWagmiSettings, primaryWallet, } = useDynamicContext();
10
+ const DynamicWagmiConnector = ({ children, }) => {
11
+ const config = useConfig();
12
+ const { handleLogOut, primaryWallet } = useDynamicContext();
13
+ useChainMismatchLogger();
20
14
  /**
21
15
  * This will prevent the wagmi Connector from calling handleLogOut when we are the ones calling disconnect
22
16
  * as a result of a handleLogOut call (see SyncDynamicWagmi), but will preserve the existing behavior for customers.
@@ -25,29 +19,14 @@ const DynamicWagmiConnector = ({ evmNetworks: customerSuppliedEvmNetworks, child
25
19
  // Sort places the primary wallet as the first wallet to attempt to connect with
26
20
  const wallets = useUserWallets().sort((wallet) => wallet.address === (primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.address) ? -1 : 1);
27
21
  const evmWallets = useMemo(() => findAndOrderEvmWallets(primaryWallet, wallets), [primaryWallet, wallets]);
28
- useEffect(() => {
29
- // only send wagmi settings once, when the first time DynamicWagmiConnector renders
30
- sendWagmiSettings({
31
- dynamicWagmiSettings: customerSuppliedEvmNetworks,
32
- });
33
- // eslint-disable-next-line react-hooks/exhaustive-deps
34
- }, []);
35
- const { chains, publicClient, webSocketPublicClient } = useMemo(() => {
36
- var _a;
37
- 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) => {
38
- network.chainId = parseInt(network.chainId.toString());
39
- return network;
40
- });
41
- const mappedChains = getWagmiChainsFromDynamicChains(evmNetworks);
42
- const mappedProviders = getWagmiProvidersFromDynamicChains(evmNetworks);
43
- return configureChains(mappedChains, mappedProviders);
44
- }, [customerSuppliedEvmNetworks, networkConfigurations === null || networkConfigurations === void 0 ? void 0 : networkConfigurations.evm]);
45
- const connector = useMemo(() => getConnector(evmWallets, (wallet) => new Connector({
46
- chains,
47
- handleLogOut,
48
- preventWagmiSyncFromCallingLogout,
49
- walletConnector: wallet.connector,
50
- })), [evmWallets, chains, handleLogOut, preventWagmiSyncFromCallingLogout]);
22
+ const connector = useMemo(() => createConnectorForFirstConnectedEvmWallet(evmWallets, (wallet) => {
23
+ const connector = config._internal.connectors.setup(getCreateConnectorFn({
24
+ handleLogOut,
25
+ preventWagmiSyncFromCallingLogout,
26
+ walletConnector: wallet.connector,
27
+ }));
28
+ return connector;
29
+ }), [config._internal.connectors, evmWallets, handleLogOut]);
51
30
  /**
52
31
  * Updating the wagmi config must be done in a useEffect because
53
32
  * when setting the public client and connectors, wagmi will fire
@@ -56,16 +35,9 @@ const DynamicWagmiConnector = ({ evmNetworks: customerSuppliedEvmNetworks, child
56
35
  * while react is in the middle of rendering.
57
36
  */
58
37
  useEffect(() => {
59
- config.setPublicClient(publicClient);
60
- config.setWebSocketPublicClient(webSocketPublicClient);
61
- config.setConnectors(connector ? [connector] : []);
62
- }, [connector, publicClient, webSocketPublicClient]);
63
- if (React.version.startsWith('18')) {
64
- return (jsx(WagmiConfig, { config: config, children: jsx(SyncDynamicWagmi, { connector: connector, preventWagmiSyncFromCallingLogout: preventWagmiSyncFromCallingLogout, wallets: wallets, children: children }) }));
65
- }
66
- // use React.createElement to prevent bunding react/jsx-runtime,
67
- // which is not compatible when bundling apps using React 17
68
- return React.createElement(WagmiConfig, { config: config }, React.createElement(SyncDynamicWagmi, { connector, preventWagmiSyncFromCallingLogout, wallets }, children));
38
+ config._internal.connectors.setState(connector ? [connector] : []);
39
+ }, [config._internal.connectors, connector]);
40
+ return (jsx(SyncDynamicWagmi, { connector: connector, preventWagmiSyncFromCallingLogout: preventWagmiSyncFromCallingLogout, wallets: wallets, children: children }));
69
41
  };
70
42
 
71
43
  export { DynamicWagmiConnector };
@@ -1,6 +1,6 @@
1
1
  import React, { MutableRefObject } from 'react';
2
+ import { Connector } from 'wagmi';
2
3
  import { Wallet } from '@dynamic-labs/sdk-react-core';
3
- import { Connector } from './Connector';
4
4
  export type SyncDynamicWagmiProps = React.PropsWithChildren<{
5
5
  connector: Connector | undefined;
6
6
  wallets: Wallet[];
@@ -1,10 +1,10 @@
1
1
  import { jsx, Fragment } from 'react/jsx-runtime';
2
- import React, { useRef, useEffect } from 'react';
2
+ import { useRef, useEffect } from 'react';
3
3
  import { useConfig, useConnect, useDisconnect } from 'wagmi';
4
4
  import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
5
5
 
6
6
  const SyncDynamicWagmi = ({ children, connector, wallets, preventWagmiSyncFromCallingLogout, }) => {
7
- const { status: clientStatus } = useConfig();
7
+ const { state: { status: clientStatus }, } = useConfig();
8
8
  const { connect } = useConnect();
9
9
  const { disconnect } = useDisconnect();
10
10
  const { walletConnector } = useDynamicContext();
@@ -52,13 +52,8 @@ const SyncDynamicWagmi = ({ children, connector, wallets, preventWagmiSyncFromCa
52
52
  walletConnector,
53
53
  wallets,
54
54
  ]);
55
- if (React.version.startsWith('18')) {
56
- // eslint-disable-next-line react/jsx-no-useless-fragment
57
- return jsx(Fragment, { children: children });
58
- }
59
- // use React.createElement to prevent bunding react/jsx-runtime,
60
- // which is not compatible when bundling apps using React 17
61
- return React.createElement(React.Fragment, null, children);
55
+ // eslint-disable-next-line react/jsx-no-useless-fragment
56
+ return jsx(Fragment, { children: children });
62
57
  };
63
58
 
64
59
  export { SyncDynamicWagmi };
@@ -0,0 +1 @@
1
+ export declare const useChainMismatchLogger: () => void;
@@ -0,0 +1,30 @@
1
+ import { useEffect } from 'react';
2
+ import { useConfig } from 'wagmi';
3
+ import { Logger } from '@dynamic-labs/logger';
4
+ import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
5
+
6
+ const logger = new Logger('DynamicWagmiConnector');
7
+ const useChainMismatchLogger = () => {
8
+ const config = useConfig();
9
+ const { networkConfigurations } = useDynamicContext();
10
+ useEffect(() => {
11
+ if (!(networkConfigurations === null || networkConfigurations === void 0 ? void 0 : networkConfigurations.evm))
12
+ return;
13
+ const wagmiChains = config.chains;
14
+ const wagmiChainIds = wagmiChains.map((chain) => chain.id);
15
+ const dynamicChains = networkConfigurations.evm;
16
+ const dynamicChainIds = dynamicChains.map((chain) => chain.chainId);
17
+ wagmiChains.forEach((chain) => {
18
+ if (!dynamicChainIds.includes(chain.id)) {
19
+ logger.warn(`Chain (id: ${chain.id} name: ${chain.name}) is present in the Wagmi config, but is not present in the Dynamic configuration.`, 'Please make sure to enable the chain in your Dynamic Dashboard, or add it to the evmNetworks prop on DynamicContextProvider.', 'For more information, see: https://docs.dynamic.xyz/guides/frameworks/wagmi-v2#chain-configuration');
20
+ }
21
+ });
22
+ dynamicChains.forEach((chain) => {
23
+ if (!wagmiChainIds.includes(Number(chain.chainId))) {
24
+ logger.warn(`Chain (id: ${chain.chainId} name: ${chain.name}) is present in the Dynamic config, but is not present in the Wagmi configuration.`, 'Please make sure to add the chain to the chains prop on the Wagmi createConfig function.', 'For more information, see: https://docs.dynamic.xyz/guides/frameworks/wagmi-v2#chain-configuration');
25
+ }
26
+ });
27
+ }, [config.chains, networkConfigurations]);
28
+ };
29
+
30
+ export { useChainMismatchLogger };
@@ -1,2 +1 @@
1
- export * from './resolveRpcUrlFromEvmNetworks';
2
1
  export * from './findAndOrderEvmWallets';
@@ -1,3 +0,0 @@
1
- import { Wallet } from '@dynamic-labs/sdk-react-core';
2
- import { Connector } from './Connector';
3
- export declare const getConnector: (wallets: Wallet[], createConnector: (wallet: Wallet) => Connector) => Connector | undefined;
@@ -1,9 +0,0 @@
1
- const getConnector = (wallets, createConnector) => {
2
- for (const wallet of wallets) {
3
- if (wallet.connected && wallet.connector.connectedChain === 'EVM') {
4
- return createConnector(wallet);
5
- }
6
- }
7
- };
8
-
9
- export { getConnector };
@@ -1,3 +0,0 @@
1
- import { Chain } from 'viem/chains';
2
- import { WagmiEvmNetworks } from './types';
3
- export declare const getWagmiChainsFromDynamicChains: (dynamicChains: WagmiEvmNetworks[]) => Chain[];
@@ -1,9 +0,0 @@
1
- import { mainnet } from 'viem/chains';
2
- import { mapDynamicChainToWagmi } from './mappers/chain.js';
3
-
4
- const getWagmiChainsFromDynamicChains = (dynamicChains) => {
5
- const mappedChains = dynamicChains.map(mapDynamicChainToWagmi);
6
- return mappedChains.length > 0 ? mappedChains : [mainnet];
7
- };
8
-
9
- export { getWagmiChainsFromDynamicChains };
@@ -1,3 +0,0 @@
1
- import { Chain, ChainProviderFn } from 'wagmi';
2
- import { WagmiEvmNetworks } from './types';
3
- export declare const getWagmiProvidersFromDynamicChains: (dynamicChains: WagmiEvmNetworks[]) => ChainProviderFn<Chain>[];
@@ -1,37 +0,0 @@
1
- import { publicProvider } from 'wagmi/providers/public';
2
- import { jsonRpcProvider } from 'wagmi/providers/jsonRpc';
3
-
4
- /* eslint-disable prefer-destructuring */
5
- const getProvider = (dynamicChain, rpcUrl) => {
6
- let http;
7
- let webSocket;
8
- if (typeof rpcUrl !== 'string') {
9
- http = rpcUrl.http;
10
- webSocket = rpcUrl.webSocket;
11
- }
12
- else {
13
- http = rpcUrl;
14
- }
15
- return (chain) => {
16
- if (chain.id !== dynamicChain.chainId)
17
- return null;
18
- return jsonRpcProvider({
19
- rpc: () => ({
20
- http,
21
- webSocket,
22
- }),
23
- })(chain);
24
- };
25
- };
26
- const getWagmiProvidersFromDynamicChains = (dynamicChains) => {
27
- const wagmiProviders = [];
28
- dynamicChains.forEach((dynamicChain) => {
29
- var _a;
30
- ((_a = dynamicChain.privateCustomerRpcUrls) !== null && _a !== void 0 ? _a : []).forEach((rpcUrl) => {
31
- wagmiProviders.push(getProvider(dynamicChain, rpcUrl));
32
- });
33
- });
34
- return [...wagmiProviders, publicProvider()];
35
- };
36
-
37
- export { getWagmiProvidersFromDynamicChains };
@@ -1,4 +0,0 @@
1
- import { Chain } from 'wagmi';
2
- import { WagmiEvmNetworks } from '../types';
3
- export declare const mapDynamicChainToWagmi: (dynamicChain: WagmiEvmNetworks) => Chain;
4
- export declare const overrideWagmiChainWithDynamicSettings: (wagmiChain: Chain | undefined, dynamicChain: WagmiEvmNetworks) => Chain;
@@ -1,50 +0,0 @@
1
- import * as allChains from 'wagmi/chains';
2
- import { resolveRpcUrlFromEvmNetworks } from '../utils/resolveRpcUrlFromEvmNetworks/resolveRpcUrlFromEvmNetworks.js';
3
-
4
- // eslint-disable-next-line import/no-namespace
5
- const updateRpcProvider = (rpcUrls, privateCustomerRpcUrls, name) => {
6
- const findRpcUrl = privateCustomerRpcUrls === null || privateCustomerRpcUrls === void 0 ? void 0 : privateCustomerRpcUrls.find((rpcUrl) => {
7
- if (typeof rpcUrl === 'string') {
8
- return rpcUrl.includes(name);
9
- }
10
- else if (typeof rpcUrl.http === 'string') {
11
- return rpcUrl.http.includes(name);
12
- }
13
- else {
14
- return false;
15
- }
16
- });
17
- if (typeof findRpcUrl === 'string') {
18
- rpcUrls[name] = { http: [findRpcUrl] };
19
- }
20
- else if (typeof (findRpcUrl === null || findRpcUrl === void 0 ? void 0 : findRpcUrl.http) === 'string') {
21
- rpcUrls[name] = { http: [findRpcUrl.http] };
22
- }
23
- };
24
- const mapDynamicChainToWagmi = (dynamicChain) => {
25
- /**
26
- * Find the chain in Wagmi that matches the chainId of the dynamic chain.
27
- */
28
- const wagmiChain = Object.values(allChains).find((chain) => chain.id === dynamicChain.chainId);
29
- return overrideWagmiChainWithDynamicSettings(wagmiChain, dynamicChain);
30
- };
31
- const generateRpcUrls = (dynamicChain) => {
32
- const rpcUrlResolved = resolveRpcUrlFromEvmNetworks(dynamicChain);
33
- const rpcUrls = {
34
- default: {
35
- http: rpcUrlResolved,
36
- },
37
- public: {
38
- http: rpcUrlResolved,
39
- },
40
- };
41
- const rpcUrlProviders = ['alchemy', 'infura'];
42
- rpcUrlProviders.forEach((name) => updateRpcProvider(rpcUrls, dynamicChain.privateCustomerRpcUrls, name));
43
- return rpcUrls;
44
- };
45
- const overrideWagmiChainWithDynamicSettings = (wagmiChain, dynamicChain) => {
46
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
47
- return (Object.assign(Object.assign({}, wagmiChain), { blockExplorers: Object.assign(Object.assign({}, wagmiChain === null || wagmiChain === void 0 ? void 0 : wagmiChain.blockExplorers), { default: Object.assign(Object.assign({}, (_a = wagmiChain === null || wagmiChain === void 0 ? void 0 : wagmiChain.blockExplorers) === null || _a === void 0 ? void 0 : _a.default), { name: '', url: (_c = (_b = dynamicChain.blockExplorerUrls) === null || _b === void 0 ? void 0 : _b[0]) !== null && _c !== void 0 ? _c : '' }) }), contracts: (_d = dynamicChain.contracts) !== null && _d !== void 0 ? _d : wagmiChain === null || wagmiChain === void 0 ? void 0 : wagmiChain.contracts, id: (_e = wagmiChain === null || wagmiChain === void 0 ? void 0 : wagmiChain.id) !== null && _e !== void 0 ? _e : dynamicChain.chainId, name: (_g = (_f = dynamicChain.vanityName) !== null && _f !== void 0 ? _f : dynamicChain.name) !== null && _g !== void 0 ? _g : wagmiChain === null || wagmiChain === void 0 ? void 0 : wagmiChain.name, nativeCurrency: (_h = dynamicChain.nativeCurrency) !== null && _h !== void 0 ? _h : wagmiChain === null || wagmiChain === void 0 ? void 0 : wagmiChain.nativeCurrency, network: (_k = (_j = wagmiChain === null || wagmiChain === void 0 ? void 0 : wagmiChain.network) !== null && _j !== void 0 ? _j : dynamicChain.chainName) !== null && _k !== void 0 ? _k : '', rpcUrls: generateRpcUrls(dynamicChain) }));
48
- };
49
-
50
- export { mapDynamicChainToWagmi, overrideWagmiChainWithDynamicSettings };
@@ -1 +0,0 @@
1
- export { resolveRpcUrlFromEvmNetworks } from './resolveRpcUrlFromEvmNetworks';
@@ -1,2 +0,0 @@
1
- import { WagmiEvmNetworks } from '../../types';
2
- export declare const resolveRpcUrlFromEvmNetworks: (dynamicChain: WagmiEvmNetworks) => string[];
@@ -1,9 +0,0 @@
1
- const mapCustomProviderOptionsToString = (rpcUrl) => (typeof rpcUrl === 'string' ? rpcUrl : rpcUrl.http);
2
- const resolveRpcUrlFromEvmNetworks = (dynamicChain) => {
3
- var _a;
4
- const privateCustomerRpcUrls = (_a = dynamicChain.privateCustomerRpcUrls) !== null && _a !== void 0 ? _a : [];
5
- const customerRpcUrls = privateCustomerRpcUrls.map(mapCustomProviderOptionsToString);
6
- return customerRpcUrls.length ? customerRpcUrls : dynamicChain.rpcUrls;
7
- };
8
-
9
- export { resolveRpcUrlFromEvmNetworks };