@tuwaio/satellite-core 1.0.0-fix-packages-alpha.10.d7d336e → 1.0.0-fix-packages-alpha.11.27bf760
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/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/store/satelliteConnectStore.ts"],"names":["createSatelliteConnectStore","adapter","callbackAfterConnected","setAutoFreeze","createStore","set","get","adapterKey","selectAdapterByKey","results","a","accumulator","currentResult","key","value","autoConnect","lastConnectedConnector","lastConnectedConnectorHelpers","delay","isSafeApp","foundAdapter","OrbitAdapter","safeConnectorChainId","error","connectorType","chainId","getAdapterFromConnectorType","connector","state","isContractAddress","updatedConnector","recentConnectedConnectorHelpers","e","connectorToDisconnect","newConnections","newActiveConnection","impersonatedHelpers","currentActive","activeConnection","targetConnectorType","produce","draft","newConnector","targetConnector"],"mappings":"mHA0BO,SAASA,CAAAA,CAAwE,CACtF,QAAAC,CAAAA,CACA,sBAAA,CAAAC,CACF,CAAA,CAAiD,CAC/C,OAAAC,oBAAc,KAAK,CAAA,CAEZC,mBAAAA,EAA0C,CAAE,CAACC,CAAAA,CAAKC,KAAS,CAIhE,UAAA,CAAaC,CAAAA,EAAeC,4BAAAA,CAAmB,CAAE,OAAA,CAAAP,EAAS,UAAA,CAAAM,CAAW,CAAC,CAAA,CAKtE,aAAA,CAAe,IAAM,CACnB,IAAIE,CAAAA,CAEJ,OAAI,KAAA,CAAM,OAAA,CAAQR,CAAO,CAAA,CACvBQ,CAAAA,CAAUR,EAAQ,GAAA,CAAKS,CAAAA,EAAMA,EAAE,aAAA,EAAe,CAAA,CAG9CD,CAAAA,CAAU,CAACR,CAAAA,CAAQ,eAAe,CAAA,CAG7BQ,EAAQ,MAAA,CACb,CAACE,EAAaC,CAAAA,GAAkB,CAC9B,IAAMC,CAA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OAAA,CAAU,MAAA,CAAOA,CAAC,CAAA,CAAG,CAAC,EACtG,CACF,CACF,CAAA,CAUA,uBAAA,CAAyB,IAAM3B,CAAAA,CAAI,CAAE,mBAAoB,MAAU,CAAC,CACtE,CAAA,CAAE,CACJ","file":"index.js","sourcesContent":["import {\n ConnectorType,\n delay,\n getAdapterFromConnectorType,\n impersonatedHelpers,\n isSafeApp,\n lastConnectedConnectorHelpers,\n OrbitAdapter,\n RecentConnectedConnector,\n recentConnectedConnectorHelpers,\n selectAdapterByKey,\n} from '@tuwaio/orbit-core';\nimport { produce, setAutoFreeze } from 'immer';\nimport { createStore } from 'zustand/vanilla';\n\nimport { BaseConnector, Connector, ISatelliteConnectStore, SatelliteConnectStoreInitialParameters } from '../types';\n\n/**\n * Creates a Satellite Connect store instance for managing connector connections and state\n *\n * @param params - Initial parameters for the store\n * @param params.adapter - Blockchain adapter(s) to use\n * @param params.callbackAfterConnected - Optional callback function called after successful connection\n *\n * @returns A Zustand store instance with connection state and methods\n */\nexport function createSatelliteConnectStore<C, W extends BaseConnector = BaseConnector>({\n adapter,\n callbackAfterConnected,\n}: SatelliteConnectStoreInitialParameters<C, W>) {\n setAutoFreeze(false);\n\n return createStore<ISatelliteConnectStore<C, W>>()((set, get) => ({\n /**\n * Returns active adapter\n */\n getAdapter: (adapterKey) => selectAdapterByKey({ adapter, adapterKey }),\n\n /**\n * Get connectors for all configured adapters\n */\n getConnectors: () => {\n let results: { adapter: OrbitAdapter; connectors: C[] }[];\n\n if (Array.isArray(adapter)) {\n results = adapter.map((a) => a.getConnectors());\n } else {\n // Ensure the single adapter result is wrapped in an array for consistent processing\n results = [adapter.getConnectors()];\n }\n\n return results.reduce(\n (accumulator, currentResult) => {\n const key = currentResult.adapter;\n const value = currentResult.connectors;\n return {\n ...accumulator,\n [key]: value,\n };\n },\n {} as Partial<Record<OrbitAdapter, C[]>>,\n );\n },\n\n initializeAutoConnect: async (autoConnect) => {\n if (autoConnect) {\n const lastConnectedConnector = lastConnectedConnectorHelpers.getLastConnectedConnector();\n if (\n lastConnectedConnector &&\n !['impersonatedwallet', 'walletconnect', 'coinbasewallet', 'bitgetwallet'].includes(\n lastConnectedConnector.connectorType.split(':')[1],\n )\n ) {\n await delay(null, 200);\n await get().connect({\n connectorType: lastConnectedConnector.connectorType,\n chainId: lastConnectedConnector.chainId,\n });\n }\n } else if (isSafeApp) {\n await delay(null, 200);\n const foundAdapter = get().getAdapter(OrbitAdapter.EVM);\n if (foundAdapter && foundAdapter.getSafeConnectorChainId) {\n const safeConnectorChainId = await foundAdapter.getSafeConnectorChainId();\n if (safeConnectorChainId) {\n await get().connect({ connectorType: `${OrbitAdapter.EVM}:safewallet`, chainId: safeConnectorChainId });\n }\n }\n }\n },\n\n connecting: false,\n disconnecting: false,\n connectionError: undefined,\n switchNetworkError: undefined,\n activeConnection: undefined,\n connections: {},\n\n setConnectionError: (error) => set({ connectionError: error }),\n\n /**\n * Connects to a connector\n * @param connectorType - Type of connector to connect to\n * @param chainId - Chain ID to connect on\n */\n connect: async ({ connectorType, chainId }) => {\n set({ connecting: true, connectionError: undefined });\n const foundAdapter = get().getAdapter(getAdapterFromConnectorType(connectorType));\n\n if (!foundAdapter) {\n set({\n connecting: false,\n connectionError: `No adapter found for connector type: ${connectorType}`,\n });\n return;\n }\n\n try {\n // 1. Check if connector is already connected\n const existingConnector = get().connections[connectorType as ConnectorType];\n if (existingConnector) {\n return;\n }\n\n const connector = await foundAdapter.connect({\n connectorType,\n chainId,\n });\n\n // 2. Set initial connector state\n set((state) => {\n return {\n activeConnection: connector,\n connections: {\n ...state.connections,\n [connector.connectorType]: connector,\n },\n };\n });\n\n // 3. Check for contract address if the adapter supports it\n if (foundAdapter.checkIsContractAddress) {\n const isContractAddress = await foundAdapter.checkIsContractAddress({\n address: connector.address,\n chainId,\n });\n\n // Update only the isContractAddress property\n get().updateActiveConnection({ ...connector, isContractAddress });\n }\n\n // 4. Run callback if provided\n if (callbackAfterConnected) {\n // Use the latest connector state after potential updates (like isContractAddress)\n const updatedConnector = get().activeConnection;\n if (updatedConnector && updatedConnector.connectorType === connectorType) {\n await callbackAfterConnected(updatedConnector);\n }\n }\n\n // 5. Final state updates\n set({ connecting: false });\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType,\n chainId,\n address: get().activeConnection?.address,\n });\n recentConnectedConnectorHelpers.setRecentConnectedConnector({\n [getAdapterFromConnectorType(connectorType)]: {\n [connectorType.split(':')[1]]: true,\n },\n } as RecentConnectedConnector);\n } catch (e) {\n set({\n connecting: false,\n connectionError: 'Connector connection failed: ' + (e instanceof Error ? e.message : String(e)),\n });\n }\n },\n\n /**\n * Disconnects the currently active wallet or a specific wallet\n */\n /**\n * Disconnects the currently active wallet or a specific wallet\n */\n disconnect: async (connectorType?: string) => {\n // Guard against re-entry\n if (get().disconnecting) return;\n set({ disconnecting: true });\n\n try {\n if (connectorType) {\n // Disconnect specific connector\n const connectorToDisconnect = get().connections[connectorType as ConnectorType];\n\n if (connectorToDisconnect) {\n const foundAdapter = get().getAdapter(getAdapterFromConnectorType(connectorToDisconnect.connectorType));\n await foundAdapter?.disconnect(connectorToDisconnect);\n\n set((state) => {\n const newConnections = { ...state.connections };\n delete newConnections[connectorType as ConnectorType];\n\n // If the disconnected connector was the active one, set activeConnection to undefined\n const newActiveConnection =\n state.activeConnection?.connectorType === connectorType ? undefined : state.activeConnection;\n\n return {\n connections: newConnections,\n activeConnection: newActiveConnection,\n connectionError: undefined,\n switchNetworkError: undefined,\n };\n });\n }\n } else {\n // Disconnect ALL connectors\n await get().disconnectAll();\n }\n\n if (Object.keys(get().connections).length === 0) {\n lastConnectedConnectorHelpers.removeLastConnectedConnector();\n impersonatedHelpers.removeImpersonated();\n } else {\n // Update last connected to the current active one (if any)\n const currentActive = get().activeConnection;\n if (currentActive) {\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: currentActive.connectorType,\n chainId: currentActive.chainId,\n address: currentActive.address,\n });\n }\n }\n } finally {\n set({ disconnecting: false });\n }\n },\n\n disconnectAll: async () => {\n await delay(null, 150);\n\n if (Array.isArray(adapter)) {\n await Promise.allSettled(\n adapter.map(async (a) => {\n try {\n await a.disconnect();\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (e) {\n /* empty */\n }\n }),\n );\n } else {\n try {\n await adapter.disconnect();\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (e) {\n /* empty */\n }\n }\n\n set({\n activeConnection: undefined,\n connections: {},\n connectionError: undefined,\n switchNetworkError: undefined,\n });\n impersonatedHelpers.removeImpersonated();\n },\n\n /**\n * Contains error message if connection failed\n */\n // connectionError is declared above with an initial value\n\n /**\n * Resets any connection errors\n */\n resetConnectionError: () => {\n set({ connectionError: undefined });\n },\n\n /**\n * Updates the active connection's properties\n * @param connector - Partial connector object with properties to update\n */\n updateActiveConnection: (connector: Partial<Connector<W>>) => {\n const activeConnection = get().activeConnection;\n // Determine which connector to update. If connectorType is provided, use it. Otherwise use activeConnection.\n const targetConnectorType = connector.connectorType ?? activeConnection?.connectorType;\n\n if (targetConnectorType) {\n // If chainId is updated, update storage\n if (connector.chainId && targetConnectorType === activeConnection?.connectorType) {\n // Update lastConnectedConnector storage if chainId changes and it's the active connector\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: targetConnectorType,\n chainId: connector.chainId,\n address: connector.address ?? activeConnection?.address,\n });\n }\n\n // Use produce for immutable state update\n set((state) =>\n produce(state, (draft) => {\n if (draft.connections[targetConnectorType as ConnectorType]) {\n draft.connections[targetConnectorType as ConnectorType] = {\n ...draft.connections[targetConnectorType as ConnectorType],\n ...connector,\n } as Connector<W>;\n\n // Also update activeConnection if it matches\n if (draft.activeConnection?.connectorType === targetConnectorType) {\n draft.activeConnection = draft.connections[targetConnectorType as ConnectorType];\n }\n }\n }),\n );\n } else {\n const isConnectorCanChange =\n connector.connectorType !== undefined && connector.chainId !== undefined && connector.address !== undefined;\n\n if (isConnectorCanChange) {\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: connector.connectorType!,\n chainId: connector.chainId!,\n address: connector.address!,\n });\n // It's a new connector or full replacement\n set((state) => {\n const newConnector = connector as Connector<W>;\n return {\n activeConnection: newConnector,\n connections: {\n ...state.connections,\n [newConnector.connectorType]: newConnector,\n },\n };\n });\n } else {\n console.warn('Attempted to set activeConnection with incomplete data while activeConnection was undefined.');\n }\n }\n },\n\n /**\n * Switches active connection from the list of connections\n */\n switchConnection: (connectorType: string) => {\n const targetConnector = get().connections[connectorType as ConnectorType];\n if (targetConnector) {\n set({ activeConnection: targetConnector });\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: targetConnector.connectorType,\n chainId: targetConnector.chainId,\n address: targetConnector.address,\n });\n }\n },\n\n /**\n * Switches the connected connector to a different network\n * @param chainId - Target chain ID to switch to\n * @param connectorType - Optional connector type to switch to. If not provided, will switch to the active connection.\n */\n switchNetwork: async (chainId: string | number, connectorType?: string) => {\n set({ switchNetworkError: undefined });\n const targetConnector = connectorType\n ? get().connections[connectorType as ConnectorType]\n : get().activeConnection;\n\n if (targetConnector) {\n const foundAdapter = get().getAdapter(getAdapterFromConnectorType(targetConnector.connectorType));\n\n if (!foundAdapter) {\n set({ switchNetworkError: `No adapter found for active connector type: ${targetConnector.connectorType}` });\n return;\n }\n\n try {\n // Pass the local updateActiveConnection method from 'get()' to the adapter\n await foundAdapter.checkAndSwitchNetwork(chainId, targetConnector.chainId, get().updateActiveConnection);\n } catch (e) {\n set({ switchNetworkError: 'Switch network failed: ' + (e instanceof Error ? e.message : String(e)) });\n }\n }\n },\n\n /**\n * Contains error message if network switch failed\n */\n // switchNetworkError is declared above with an initial value\n\n /**\n * Resets any network switching errors\n */\n resetSwitchNetworkError: () => set({ switchNetworkError: undefined }),\n }));\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/store/satelliteConnectStore.ts"],"names":["createSatelliteConnectStore","adapter","callbackAfterConnected","setAutoFreeze","createStore","set","get","adapterKey","selectAdapterByKey","results","a","accumulator","currentResult","key","value","autoConnect","lastConnectedConnector","lastConnectedConnectorHelpers","delay","isSafeApp","foundAdapter","OrbitAdapter","safeConnectorChainId","error","connectorType","chainId","getAdapterFromConnectorType","connector","state","isContractAddress","updatedConnector","recentConnectedConnectorHelpers","e","connectorToDisconnect","newConnections","newActiveConnection","impersonatedHelpers","currentActive","activeConnection","targetConnectorType","produce","draft","newConnector","targetConnector"],"mappings":"mHA0BO,SAASA,CAAAA,CAAwE,CACtF,QAAAC,CAAAA,CACA,sBAAA,CAAAC,CACF,CAAA,CAAiD,CAE/C,OAAAC,oBAAc,KAAK,CAAA,CAEZC,mBAAAA,EAA0C,CAAE,CAACC,CAAAA,CAAKC,KAAS,CAIhE,UAAA,CAAaC,CAAAA,EAAeC,4BAAAA,CAAmB,CAAE,OAAA,CAAAP,EAAS,UAAA,CAAAM,CAAW,CAAC,CAAA,CAKtE,aAAA,CAAe,IAAM,CACnB,IAAIE,CAAAA,CAEJ,OAAI,KAAA,CAAM,OAAA,CAAQR,CAAO,CAAA,CACvBQ,CAAAA,CAAUR,EAAQ,GAAA,CAAKS,CAAAA,EAAMA,EAAE,aAAA,EAAe,CAAA,CAG9CD,CAAAA,CAAU,CAACR,CAAAA,CAAQ,eAAe,CAAA,CAG7BQ,EAAQ,MAAA,CACb,CAACE,EAAaC,CAAAA,GAAkB,CAC9B,IAAMC,CAAAA,CAAMD,CAAAA,CAAc,OAAA,CACpBE,EAAQF,CAAAA,CAAc,UAAA,CAC5B,OAAO,CACL,GAAGD,CAAAA,CACH,CAACE,CAAG,EAAGC,CACT,CACF,CAAA,CACA,EACF,CACF,CAAA,CAEA,sBAAuB,MAAOC,CAAAA,EAAgB,CAC5C,GAAIA,CAAAA,CAAa,CACf,IAAMC,CAAAA,CAAyBC,uCAAAA,CAA8B,2BAA0B,CAErFD,CAAAA,EACA,CAAC,CAAC,oBAAA,CAAsB,gBAAiB,gBAAA,CAAkB,cAAc,CAAA,CAAE,QAAA,CACzEA,CA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AAA,CAAgB,aAAA,CAC/B,QAASA,CAAAA,CAAgB,OAAA,CACzB,OAAA,CAASA,CAAAA,CAAgB,OAC3B,CAAC,GAEL,CAAA,CAOA,aAAA,CAAe,MAAOlB,CAAAA,CAA0BD,CAAAA,GAA2B,CACzEnB,CAAAA,CAAI,CAAE,kBAAA,CAAoB,MAAU,CAAC,CAAA,CACrC,IAAMsC,CAAAA,CAAkBnB,CAAAA,CACpBlB,GAAI,CAAE,WAAA,CAAYkB,CAA8B,CAAA,CAChDlB,CAAAA,EAAI,CAAE,gBAAA,CAEV,GAAIqC,CAAAA,CAAiB,CACnB,IAAMvB,CAAAA,CAAed,CAAAA,EAAI,CAAE,UAAA,CAAWoB,qCAAAA,CAA4BiB,EAAgB,aAAa,CAAC,CAAA,CAEhG,GAAI,CAACvB,CAAAA,CAAc,CACjBf,CAAAA,CAAI,CAAE,mBAAoB,CAAA,4CAAA,EAA+CsC,CAAAA,CAAgB,aAAa,CAAA,CAAG,CAAC,CAAA,CAC1G,MACF,CAEA,GAAI,CAEF,MAAMvB,CAAAA,CAAa,qBAAA,CAAsBK,CAAAA,CAASkB,CAAAA,CAAgB,OAAA,CAASrC,GAAI,CAAE,sBAAsB,EACzG,CAAA,MAAS0B,CAAAA,CAAG,CACV3B,EAAI,CAAE,kBAAA,CAAoB,2BAA6B2B,CAAAA,YAAa,KAAA,CAAQA,EAAE,OAAA,CAAU,MAAA,CAAOA,CAAC,CAAA,CAAG,CAAC,EACtG,CACF,CACF,CAAA,CAUA,uBAAA,CAAyB,IAAM3B,CAAAA,CAAI,CAAE,mBAAoB,MAAU,CAAC,CACtE,CAAA,CAAE,CACJ","file":"index.js","sourcesContent":["import {\n ConnectorType,\n delay,\n getAdapterFromConnectorType,\n impersonatedHelpers,\n isSafeApp,\n lastConnectedConnectorHelpers,\n OrbitAdapter,\n RecentConnectedConnector,\n recentConnectedConnectorHelpers,\n selectAdapterByKey,\n} from '@tuwaio/orbit-core';\nimport { produce, setAutoFreeze } from 'immer';\nimport { createStore } from 'zustand/vanilla';\n\nimport { BaseConnector, Connector, ISatelliteConnectStore, SatelliteConnectStoreInitialParameters } from '../types';\n\n/**\n * Creates a Satellite Connect store instance for managing connector connections and state\n *\n * @param params - Initial parameters for the store\n * @param params.adapter - Blockchain adapter(s) to use\n * @param params.callbackAfterConnected - Optional callback function called after successful connection\n *\n * @returns A Zustand store instance with connection state and methods\n */\nexport function createSatelliteConnectStore<C, W extends BaseConnector = BaseConnector>({\n adapter,\n callbackAfterConnected,\n}: SatelliteConnectStoreInitialParameters<C, W>) {\n // Disable autoFreeze for immers in this store, since connectors contain EventEmitter objects that must remain mutable to function correctly\n setAutoFreeze(false);\n\n return createStore<ISatelliteConnectStore<C, W>>()((set, get) => ({\n /**\n * Returns active adapter\n */\n getAdapter: (adapterKey) => selectAdapterByKey({ adapter, adapterKey }),\n\n /**\n * Get connectors for all configured adapters\n */\n getConnectors: () => {\n let results: { adapter: OrbitAdapter; connectors: C[] }[];\n\n if (Array.isArray(adapter)) {\n results = adapter.map((a) => a.getConnectors());\n } else {\n // Ensure the single adapter result is wrapped in an array for consistent processing\n results = [adapter.getConnectors()];\n }\n\n return results.reduce(\n (accumulator, currentResult) => {\n const key = currentResult.adapter;\n const value = currentResult.connectors;\n return {\n ...accumulator,\n [key]: value,\n };\n },\n {} as Partial<Record<OrbitAdapter, C[]>>,\n );\n },\n\n initializeAutoConnect: async (autoConnect) => {\n if (autoConnect) {\n const lastConnectedConnector = lastConnectedConnectorHelpers.getLastConnectedConnector();\n if (\n lastConnectedConnector &&\n !['impersonatedwallet', 'walletconnect', 'coinbasewallet', 'bitgetwallet'].includes(\n lastConnectedConnector.connectorType.split(':')[1],\n )\n ) {\n await delay(null, 200);\n await get().connect({\n connectorType: lastConnectedConnector.connectorType,\n chainId: lastConnectedConnector.chainId,\n });\n }\n } else if (isSafeApp) {\n await delay(null, 200);\n const foundAdapter = get().getAdapter(OrbitAdapter.EVM);\n if (foundAdapter && foundAdapter.getSafeConnectorChainId) {\n const safeConnectorChainId = await foundAdapter.getSafeConnectorChainId();\n if (safeConnectorChainId) {\n await get().connect({ connectorType: `${OrbitAdapter.EVM}:safewallet`, chainId: safeConnectorChainId });\n }\n }\n }\n },\n\n connecting: false,\n disconnecting: false,\n connectionError: undefined,\n switchNetworkError: undefined,\n activeConnection: undefined,\n connections: {},\n\n setConnectionError: (error) => set({ connectionError: error }),\n\n /**\n * Connects to a connector\n * @param connectorType - Type of connector to connect to\n * @param chainId - Chain ID to connect on\n */\n connect: async ({ connectorType, chainId }) => {\n set({ connecting: true, connectionError: undefined });\n const foundAdapter = get().getAdapter(getAdapterFromConnectorType(connectorType));\n\n if (!foundAdapter) {\n set({\n connecting: false,\n connectionError: `No adapter found for connector type: ${connectorType}`,\n });\n return;\n }\n\n try {\n // 1. Check if connector is already connected\n const existingConnector = get().connections[connectorType as ConnectorType];\n if (existingConnector) {\n return;\n }\n\n const connector = await foundAdapter.connect({\n connectorType,\n chainId,\n });\n\n // 2. Set initial connector state\n set((state) => {\n return {\n activeConnection: connector,\n connections: {\n ...state.connections,\n [connector.connectorType]: connector,\n },\n };\n });\n\n // 3. Check for contract address if the adapter supports it\n if (foundAdapter.checkIsContractAddress) {\n const isContractAddress = await foundAdapter.checkIsContractAddress({\n address: connector.address,\n chainId,\n });\n\n // Update only the isContractAddress property\n get().updateActiveConnection({ ...connector, isContractAddress });\n }\n\n // 4. Run callback if provided\n if (callbackAfterConnected) {\n // Use the latest connector state after potential updates (like isContractAddress)\n const updatedConnector = get().activeConnection;\n if (updatedConnector && updatedConnector.connectorType === connectorType) {\n await callbackAfterConnected(updatedConnector);\n }\n }\n\n // 5. Final state updates\n set({ connecting: false });\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType,\n chainId,\n address: get().activeConnection?.address,\n });\n recentConnectedConnectorHelpers.setRecentConnectedConnector({\n [getAdapterFromConnectorType(connectorType)]: {\n [connectorType.split(':')[1]]: true,\n },\n } as RecentConnectedConnector);\n } catch (e) {\n set({\n connecting: false,\n connectionError: 'Connector connection failed: ' + (e instanceof Error ? e.message : String(e)),\n });\n }\n },\n\n /**\n * Disconnects the currently active wallet or a specific wallet\n */\n /**\n * Disconnects the currently active wallet or a specific wallet\n */\n disconnect: async (connectorType?: string) => {\n // Guard against re-entry\n if (get().disconnecting) return;\n set({ disconnecting: true });\n\n try {\n if (connectorType) {\n // Disconnect specific connector\n const connectorToDisconnect = get().connections[connectorType as ConnectorType];\n\n if (connectorToDisconnect) {\n const foundAdapter = get().getAdapter(getAdapterFromConnectorType(connectorToDisconnect.connectorType));\n await foundAdapter?.disconnect(connectorToDisconnect);\n\n set((state) => {\n const newConnections = { ...state.connections };\n delete newConnections[connectorType as ConnectorType];\n\n // If the disconnected connector was the active one, set activeConnection to undefined\n const newActiveConnection =\n state.activeConnection?.connectorType === connectorType ? undefined : state.activeConnection;\n\n return {\n connections: newConnections,\n activeConnection: newActiveConnection,\n connectionError: undefined,\n switchNetworkError: undefined,\n };\n });\n }\n } else {\n // Disconnect ALL connectors\n await get().disconnectAll();\n }\n\n if (Object.keys(get().connections).length === 0) {\n lastConnectedConnectorHelpers.removeLastConnectedConnector();\n impersonatedHelpers.removeImpersonated();\n } else {\n // Update last connected to the current active one (if any)\n const currentActive = get().activeConnection;\n if (currentActive) {\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: currentActive.connectorType,\n chainId: currentActive.chainId,\n address: currentActive.address,\n });\n }\n }\n } finally {\n set({ disconnecting: false });\n }\n },\n\n disconnectAll: async () => {\n await delay(null, 150);\n\n if (Array.isArray(adapter)) {\n await Promise.allSettled(\n adapter.map(async (a) => {\n try {\n await a.disconnect();\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (e) {\n /* empty */\n }\n }),\n );\n } else {\n try {\n await adapter.disconnect();\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (e) {\n /* empty */\n }\n }\n\n set({\n activeConnection: undefined,\n connections: {},\n connectionError: undefined,\n switchNetworkError: undefined,\n });\n impersonatedHelpers.removeImpersonated();\n },\n\n /**\n * Contains error message if connection failed\n */\n // connectionError is declared above with an initial value\n\n /**\n * Resets any connection errors\n */\n resetConnectionError: () => {\n set({ connectionError: undefined });\n },\n\n /**\n * Updates the active connection's properties\n * @param connector - Partial connector object with properties to update\n */\n updateActiveConnection: (connector: Partial<Connector<W>>) => {\n const activeConnection = get().activeConnection;\n // Determine which connector to update. If connectorType is provided, use it. Otherwise use activeConnection.\n const targetConnectorType = connector.connectorType ?? activeConnection?.connectorType;\n\n if (targetConnectorType) {\n // If chainId is updated, update storage\n if (connector.chainId && targetConnectorType === activeConnection?.connectorType) {\n // Update lastConnectedConnector storage if chainId changes and it's the active connector\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: targetConnectorType,\n chainId: connector.chainId,\n address: connector.address ?? activeConnection?.address,\n });\n }\n\n // Use produce for immutable state update\n set((state) =>\n produce(state, (draft) => {\n if (draft.connections[targetConnectorType as ConnectorType]) {\n draft.connections[targetConnectorType as ConnectorType] = {\n ...draft.connections[targetConnectorType as ConnectorType],\n ...connector,\n } as Connector<W>;\n\n // Also update activeConnection if it matches\n if (draft.activeConnection?.connectorType === targetConnectorType) {\n draft.activeConnection = draft.connections[targetConnectorType as ConnectorType];\n }\n }\n }),\n );\n } else {\n const isConnectorCanChange =\n connector.connectorType !== undefined && connector.chainId !== undefined && connector.address !== undefined;\n\n if (isConnectorCanChange) {\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: connector.connectorType!,\n chainId: connector.chainId!,\n address: connector.address!,\n });\n // It's a new connector or full replacement\n set((state) => {\n const newConnector = connector as Connector<W>;\n return {\n activeConnection: newConnector,\n connections: {\n ...state.connections,\n [newConnector.connectorType]: newConnector,\n },\n };\n });\n } else {\n console.warn('Attempted to set activeConnection with incomplete data while activeConnection was undefined.');\n }\n }\n },\n\n /**\n * Switches active connection from the list of connections\n */\n switchConnection: (connectorType: string) => {\n const targetConnector = get().connections[connectorType as ConnectorType];\n if (targetConnector) {\n set({ activeConnection: targetConnector });\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: targetConnector.connectorType,\n chainId: targetConnector.chainId,\n address: targetConnector.address,\n });\n }\n },\n\n /**\n * Switches the connected connector to a different network\n * @param chainId - Target chain ID to switch to\n * @param connectorType - Optional connector type to switch to. If not provided, will switch to the active connection.\n */\n switchNetwork: async (chainId: string | number, connectorType?: string) => {\n set({ switchNetworkError: undefined });\n const targetConnector = connectorType\n ? get().connections[connectorType as ConnectorType]\n : get().activeConnection;\n\n if (targetConnector) {\n const foundAdapter = get().getAdapter(getAdapterFromConnectorType(targetConnector.connectorType));\n\n if (!foundAdapter) {\n set({ switchNetworkError: `No adapter found for active connector type: ${targetConnector.connectorType}` });\n return;\n }\n\n try {\n // Pass the local updateActiveConnection method from 'get()' to the adapter\n await foundAdapter.checkAndSwitchNetwork(chainId, targetConnector.chainId, get().updateActiveConnection);\n } catch (e) {\n set({ switchNetworkError: 'Switch network failed: ' + (e instanceof Error ? e.message : String(e)) });\n }\n }\n },\n\n /**\n * Contains error message if network switch failed\n */\n // switchNetworkError is declared above with an initial value\n\n /**\n * Resets any network switching errors\n */\n resetSwitchNetworkError: () => set({ switchNetworkError: undefined }),\n }));\n}\n"]}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/store/satelliteConnectStore.ts"],"names":["createSatelliteConnectStore","adapter","callbackAfterConnected","setAutoFreeze","createStore","set","get","adapterKey","selectAdapterByKey","results","a","accumulator","currentResult","key","value","autoConnect","lastConnectedConnector","lastConnectedConnectorHelpers","delay","isSafeApp","foundAdapter","OrbitAdapter","safeConnectorChainId","error","connectorType","chainId","getAdapterFromConnectorType","connector","state","isContractAddress","updatedConnector","recentConnectedConnectorHelpers","e","connectorToDisconnect","newConnections","newActiveConnection","impersonatedHelpers","currentActive","activeConnection","targetConnectorType","produce","draft","newConnector","targetConnector"],"mappings":"mRA0B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AE,WAAA,CAAYkB,CAA8B,CAAA,CAChDlB,CAAAA,EAAI,CAAE,gBAAA,CAEV,GAAIqC,CAAAA,CAAiB,CACnB,IAAMvB,CAAAA,CAAed,CAAAA,EAAI,CAAE,UAAA,CAAWoB,2BAAAA,CAA4BiB,EAAgB,aAAa,CAAC,CAAA,CAEhG,GAAI,CAACvB,CAAAA,CAAc,CACjBf,CAAAA,CAAI,CAAE,mBAAoB,CAAA,4CAAA,EAA+CsC,CAAAA,CAAgB,aAAa,CAAA,CAAG,CAAC,CAAA,CAC1G,MACF,CAEA,GAAI,CAEF,MAAMvB,CAAAA,CAAa,qBAAA,CAAsBK,CAAAA,CAASkB,CAAAA,CAAgB,OAAA,CAASrC,GAAI,CAAE,sBAAsB,EACzG,CAAA,MAAS0B,CAAAA,CAAG,CACV3B,EAAI,CAAE,kBAAA,CAAoB,2BAA6B2B,CAAAA,YAAa,KAAA,CAAQA,EAAE,OAAA,CAAU,MAAA,CAAOA,CAAC,CAAA,CAAG,CAAC,EACtG,CACF,CACF,CAAA,CAUA,uBAAA,CAAyB,IAAM3B,CAAAA,CAAI,CAAE,mBAAoB,MAAU,CAAC,CACtE,CAAA,CAAE,CACJ","file":"index.mjs","sourcesContent":["import {\n ConnectorType,\n delay,\n getAdapterFromConnectorType,\n impersonatedHelpers,\n isSafeApp,\n lastConnectedConnectorHelpers,\n OrbitAdapter,\n RecentConnectedConnector,\n recentConnectedConnectorHelpers,\n selectAdapterByKey,\n} from '@tuwaio/orbit-core';\nimport { produce, setAutoFreeze } from 'immer';\nimport { createStore } from 'zustand/vanilla';\n\nimport { BaseConnector, Connector, ISatelliteConnectStore, SatelliteConnectStoreInitialParameters } from '../types';\n\n/**\n * Creates a Satellite Connect store instance for managing connector connections and state\n *\n * @param params - Initial parameters for the store\n * @param params.adapter - Blockchain adapter(s) to use\n * @param params.callbackAfterConnected - Optional callback function called after successful connection\n *\n * @returns A Zustand store instance with connection state and methods\n */\nexport function createSatelliteConnectStore<C, W extends BaseConnector = BaseConnector>({\n adapter,\n callbackAfterConnected,\n}: SatelliteConnectStoreInitialParameters<C, W>) {\n setAutoFreeze(false);\n\n return createStore<ISatelliteConnectStore<C, W>>()((set, get) => ({\n /**\n * Returns active adapter\n */\n getAdapter: (adapterKey) => selectAdapterByKey({ adapter, adapterKey }),\n\n /**\n * Get connectors for all configured adapters\n */\n getConnectors: () => {\n let results: { adapter: OrbitAdapter; connectors: C[] }[];\n\n if (Array.isArray(adapter)) {\n results = adapter.map((a) => a.getConnectors());\n } else {\n // Ensure the single adapter result is wrapped in an array for consistent processing\n results = [adapter.getConnectors()];\n }\n\n return results.reduce(\n (accumulator, currentResult) => {\n const key = currentResult.adapter;\n const value = currentResult.connectors;\n return {\n ...accumulator,\n [key]: value,\n };\n },\n {} as Partial<Record<OrbitAdapter, C[]>>,\n );\n },\n\n initializeAutoConnect: async (autoConnect) => {\n if (autoConnect) {\n const lastConnectedConnector = lastConnectedConnectorHelpers.getLastConnectedConnector();\n if (\n lastConnectedConnector &&\n !['impersonatedwallet', 'walletconnect', 'coinbasewallet', 'bitgetwallet'].includes(\n lastConnectedConnector.connectorType.split(':')[1],\n )\n ) {\n await delay(null, 200);\n await get().connect({\n connectorType: lastConnectedConnector.connectorType,\n chainId: lastConnectedConnector.chainId,\n });\n }\n } else if (isSafeApp) {\n await delay(null, 200);\n const foundAdapter = get().getAdapter(OrbitAdapter.EVM);\n if (foundAdapter && foundAdapter.getSafeConnectorChainId) {\n const safeConnectorChainId = await foundAdapter.getSafeConnectorChainId();\n if (safeConnectorChainId) {\n await get().connect({ connectorType: `${OrbitAdapter.EVM}:safewallet`, chainId: safeConnectorChainId });\n }\n }\n }\n },\n\n connecting: false,\n disconnecting: false,\n connectionError: undefined,\n switchNetworkError: undefined,\n activeConnection: undefined,\n connections: {},\n\n setConnectionError: (error) => set({ connectionError: error }),\n\n /**\n * Connects to a connector\n * @param connectorType - Type of connector to connect to\n * @param chainId - Chain ID to connect on\n */\n connect: async ({ connectorType, chainId }) => {\n set({ connecting: true, connectionError: undefined });\n const foundAdapter = get().getAdapter(getAdapterFromConnectorType(connectorType));\n\n if (!foundAdapter) {\n set({\n connecting: false,\n connectionError: `No adapter found for connector type: ${connectorType}`,\n });\n return;\n }\n\n try {\n // 1. Check if connector is already connected\n const existingConnector = get().connections[connectorType as ConnectorType];\n if (existingConnector) {\n return;\n }\n\n const connector = await foundAdapter.connect({\n connectorType,\n chainId,\n });\n\n // 2. Set initial connector state\n set((state) => {\n return {\n activeConnection: connector,\n connections: {\n ...state.connections,\n [connector.connectorType]: connector,\n },\n };\n });\n\n // 3. Check for contract address if the adapter supports it\n if (foundAdapter.checkIsContractAddress) {\n const isContractAddress = await foundAdapter.checkIsContractAddress({\n address: connector.address,\n chainId,\n });\n\n // Update only the isContractAddress property\n get().updateActiveConnection({ ...connector, isContractAddress });\n }\n\n // 4. Run callback if provided\n if (callbackAfterConnected) {\n // Use the latest connector state after potential updates (like isContractAddress)\n const updatedConnector = get().activeConnection;\n if (updatedConnector && updatedConnector.connectorType === connectorType) {\n await callbackAfterConnected(updatedConnector);\n }\n }\n\n // 5. Final state updates\n set({ connecting: false });\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType,\n chainId,\n address: get().activeConnection?.address,\n });\n recentConnectedConnectorHelpers.setRecentConnectedConnector({\n [getAdapterFromConnectorType(connectorType)]: {\n [connectorType.split(':')[1]]: true,\n },\n } as RecentConnectedConnector);\n } catch (e) {\n set({\n connecting: false,\n connectionError: 'Connector connection failed: ' + (e instanceof Error ? e.message : String(e)),\n });\n }\n },\n\n /**\n * Disconnects the currently active wallet or a specific wallet\n */\n /**\n * Disconnects the currently active wallet or a specific wallet\n */\n disconnect: async (connectorType?: string) => {\n // Guard against re-entry\n if (get().disconnecting) return;\n set({ disconnecting: true });\n\n try {\n if (connectorType) {\n // Disconnect specific connector\n const connectorToDisconnect = get().connections[connectorType as ConnectorType];\n\n if (connectorToDisconnect) {\n const foundAdapter = get().getAdapter(getAdapterFromConnectorType(connectorToDisconnect.connectorType));\n await foundAdapter?.disconnect(connectorToDisconnect);\n\n set((state) => {\n const newConnections = { ...state.connections };\n delete newConnections[connectorType as ConnectorType];\n\n // If the disconnected connector was the active one, set activeConnection to undefined\n const newActiveConnection =\n state.activeConnection?.connectorType === connectorType ? undefined : state.activeConnection;\n\n return {\n connections: newConnections,\n activeConnection: newActiveConnection,\n connectionError: undefined,\n switchNetworkError: undefined,\n };\n });\n }\n } else {\n // Disconnect ALL connectors\n await get().disconnectAll();\n }\n\n if (Object.keys(get().connections).length === 0) {\n lastConnectedConnectorHelpers.removeLastConnectedConnector();\n impersonatedHelpers.removeImpersonated();\n } else {\n // Update last connected to the current active one (if any)\n const currentActive = get().activeConnection;\n if (currentActive) {\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: currentActive.connectorType,\n chainId: currentActive.chainId,\n address: currentActive.address,\n });\n }\n }\n } finally {\n set({ disconnecting: false });\n }\n },\n\n disconnectAll: async () => {\n await delay(null, 150);\n\n if (Array.isArray(adapter)) {\n await Promise.allSettled(\n adapter.map(async (a) => {\n try {\n await a.disconnect();\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (e) {\n /* empty */\n }\n }),\n );\n } else {\n try {\n await adapter.disconnect();\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (e) {\n /* empty */\n }\n }\n\n set({\n activeConnection: undefined,\n connections: {},\n connectionError: undefined,\n switchNetworkError: undefined,\n });\n impersonatedHelpers.removeImpersonated();\n },\n\n /**\n * Contains error message if connection failed\n */\n // connectionError is declared above with an initial value\n\n /**\n * Resets any connection errors\n */\n resetConnectionError: () => {\n set({ connectionError: undefined });\n },\n\n /**\n * Updates the active connection's properties\n * @param connector - Partial connector object with properties to update\n */\n updateActiveConnection: (connector: Partial<Connector<W>>) => {\n const activeConnection = get().activeConnection;\n // Determine which connector to update. If connectorType is provided, use it. Otherwise use activeConnection.\n const targetConnectorType = connector.connectorType ?? activeConnection?.connectorType;\n\n if (targetConnectorType) {\n // If chainId is updated, update storage\n if (connector.chainId && targetConnectorType === activeConnection?.connectorType) {\n // Update lastConnectedConnector storage if chainId changes and it's the active connector\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: targetConnectorType,\n chainId: connector.chainId,\n address: connector.address ?? activeConnection?.address,\n });\n }\n\n // Use produce for immutable state update\n set((state) =>\n produce(state, (draft) => {\n if (draft.connections[targetConnectorType as ConnectorType]) {\n draft.connections[targetConnectorType as ConnectorType] = {\n ...draft.connections[targetConnectorType as ConnectorType],\n ...connector,\n } as Connector<W>;\n\n // Also update activeConnection if it matches\n if (draft.activeConnection?.connectorType === targetConnectorType) {\n draft.activeConnection = draft.connections[targetConnectorType as ConnectorType];\n }\n }\n }),\n );\n } else {\n const isConnectorCanChange =\n connector.connectorType !== undefined && connector.chainId !== undefined && connector.address !== undefined;\n\n if (isConnectorCanChange) {\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: connector.connectorType!,\n chainId: connector.chainId!,\n address: connector.address!,\n });\n // It's a new connector or full replacement\n set((state) => {\n const newConnector = connector as Connector<W>;\n return {\n activeConnection: newConnector,\n connections: {\n ...state.connections,\n [newConnector.connectorType]: newConnector,\n },\n };\n });\n } else {\n console.warn('Attempted to set activeConnection with incomplete data while activeConnection was undefined.');\n }\n }\n },\n\n /**\n * Switches active connection from the list of connections\n */\n switchConnection: (connectorType: string) => {\n const targetConnector = get().connections[connectorType as ConnectorType];\n if (targetConnector) {\n set({ activeConnection: targetConnector });\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: targetConnector.connectorType,\n chainId: targetConnector.chainId,\n address: targetConnector.address,\n });\n }\n },\n\n /**\n * Switches the connected connector to a different network\n * @param chainId - Target chain ID to switch to\n * @param connectorType - Optional connector type to switch to. If not provided, will switch to the active connection.\n */\n switchNetwork: async (chainId: string | number, connectorType?: string) => {\n set({ switchNetworkError: undefined });\n const targetConnector = connectorType\n ? get().connections[connectorType as ConnectorType]\n : get().activeConnection;\n\n if (targetConnector) {\n const foundAdapter = get().getAdapter(getAdapterFromConnectorType(targetConnector.connectorType));\n\n if (!foundAdapter) {\n set({ switchNetworkError: `No adapter found for active connector type: ${targetConnector.connectorType}` });\n return;\n }\n\n try {\n // Pass the local updateActiveConnection method from 'get()' to the adapter\n await foundAdapter.checkAndSwitchNetwork(chainId, targetConnector.chainId, get().updateActiveConnection);\n } catch (e) {\n set({ switchNetworkError: 'Switch network failed: ' + (e instanceof Error ? e.message : String(e)) });\n }\n }\n },\n\n /**\n * Contains error message if network switch failed\n */\n // switchNetworkError is declared above with an initial value\n\n /**\n * Resets any network switching errors\n */\n resetSwitchNetworkError: () => set({ switchNetworkError: undefined }),\n }));\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/store/satelliteConnectStore.ts"],"names":["createSatelliteConnectStore","adapter","callbackAfterConnected","setAutoFreeze","createStore","set","get","adapterKey","selectAdapterByKey","results","a","accumulator","currentResult","key","value","autoConnect","lastConnectedConnector","lastConnectedConnectorHelpers","delay","isSafeApp","foundAdapter","OrbitAdapter","safeConnectorChainId","error","connectorType","chainId","getAdapterFromConnectorType","connector","state","isContractAddress","updatedConnector","recentConnectedConnectorHelpers","e","connectorToDisconnect","newConnections","newActiveConnection","impersonatedHelpers","currentActive","activeConnection","targetConnectorType","produce","draft","newConnector","targetConnector"],"mappings":"mRA0BO,SAASA,CAAAA,CAAwE,CACtF,QAAAC,CAAAA,CACA,sBAAA,CAAAC,CACF,CAAA,CAAiD,CAE/C,OAAAC,cAAc,KAAK,CAAA,CAEZC,WAAAA,EAA0C,CAAE,CAACC,CAAAA,CAAKC,KAAS,CAIhE,UAAA,CAAaC,CAAAA,EAAeC,kBAAAA,CAAmB,CAAE,OAAA,CAAAP,EAAS,UAAA,CAAAM,CAAW,CAAC,CAAA,CAKtE,aAAA,CAAe,IAAM,CACnB,IAAIE,CAAAA,CAEJ,OAAI,KAAA,CAAM,OAAA,CAAQR,CAAO,CAAA,CACvBQ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AA,CAAa,qBAAA,CAAsBK,CAAAA,CAASkB,CAAAA,CAAgB,OAAA,CAASrC,GAAI,CAAE,sBAAsB,EACzG,CAAA,MAAS0B,CAAAA,CAAG,CACV3B,EAAI,CAAE,kBAAA,CAAoB,2BAA6B2B,CAAAA,YAAa,KAAA,CAAQA,EAAE,OAAA,CAAU,MAAA,CAAOA,CAAC,CAAA,CAAG,CAAC,EACtG,CACF,CACF,CAAA,CAUA,uBAAA,CAAyB,IAAM3B,CAAAA,CAAI,CAAE,mBAAoB,MAAU,CAAC,CACtE,CAAA,CAAE,CACJ","file":"index.mjs","sourcesContent":["import {\n ConnectorType,\n delay,\n getAdapterFromConnectorType,\n impersonatedHelpers,\n isSafeApp,\n lastConnectedConnectorHelpers,\n OrbitAdapter,\n RecentConnectedConnector,\n recentConnectedConnectorHelpers,\n selectAdapterByKey,\n} from '@tuwaio/orbit-core';\nimport { produce, setAutoFreeze } from 'immer';\nimport { createStore } from 'zustand/vanilla';\n\nimport { BaseConnector, Connector, ISatelliteConnectStore, SatelliteConnectStoreInitialParameters } from '../types';\n\n/**\n * Creates a Satellite Connect store instance for managing connector connections and state\n *\n * @param params - Initial parameters for the store\n * @param params.adapter - Blockchain adapter(s) to use\n * @param params.callbackAfterConnected - Optional callback function called after successful connection\n *\n * @returns A Zustand store instance with connection state and methods\n */\nexport function createSatelliteConnectStore<C, W extends BaseConnector = BaseConnector>({\n adapter,\n callbackAfterConnected,\n}: SatelliteConnectStoreInitialParameters<C, W>) {\n // Disable autoFreeze for immers in this store, since connectors contain EventEmitter objects that must remain mutable to function correctly\n setAutoFreeze(false);\n\n return createStore<ISatelliteConnectStore<C, W>>()((set, get) => ({\n /**\n * Returns active adapter\n */\n getAdapter: (adapterKey) => selectAdapterByKey({ adapter, adapterKey }),\n\n /**\n * Get connectors for all configured adapters\n */\n getConnectors: () => {\n let results: { adapter: OrbitAdapter; connectors: C[] }[];\n\n if (Array.isArray(adapter)) {\n results = adapter.map((a) => a.getConnectors());\n } else {\n // Ensure the single adapter result is wrapped in an array for consistent processing\n results = [adapter.getConnectors()];\n }\n\n return results.reduce(\n (accumulator, currentResult) => {\n const key = currentResult.adapter;\n const value = currentResult.connectors;\n return {\n ...accumulator,\n [key]: value,\n };\n },\n {} as Partial<Record<OrbitAdapter, C[]>>,\n );\n },\n\n initializeAutoConnect: async (autoConnect) => {\n if (autoConnect) {\n const lastConnectedConnector = lastConnectedConnectorHelpers.getLastConnectedConnector();\n if (\n lastConnectedConnector &&\n !['impersonatedwallet', 'walletconnect', 'coinbasewallet', 'bitgetwallet'].includes(\n lastConnectedConnector.connectorType.split(':')[1],\n )\n ) {\n await delay(null, 200);\n await get().connect({\n connectorType: lastConnectedConnector.connectorType,\n chainId: lastConnectedConnector.chainId,\n });\n }\n } else if (isSafeApp) {\n await delay(null, 200);\n const foundAdapter = get().getAdapter(OrbitAdapter.EVM);\n if (foundAdapter && foundAdapter.getSafeConnectorChainId) {\n const safeConnectorChainId = await foundAdapter.getSafeConnectorChainId();\n if (safeConnectorChainId) {\n await get().connect({ connectorType: `${OrbitAdapter.EVM}:safewallet`, chainId: safeConnectorChainId });\n }\n }\n }\n },\n\n connecting: false,\n disconnecting: false,\n connectionError: undefined,\n switchNetworkError: undefined,\n activeConnection: undefined,\n connections: {},\n\n setConnectionError: (error) => set({ connectionError: error }),\n\n /**\n * Connects to a connector\n * @param connectorType - Type of connector to connect to\n * @param chainId - Chain ID to connect on\n */\n connect: async ({ connectorType, chainId }) => {\n set({ connecting: true, connectionError: undefined });\n const foundAdapter = get().getAdapter(getAdapterFromConnectorType(connectorType));\n\n if (!foundAdapter) {\n set({\n connecting: false,\n connectionError: `No adapter found for connector type: ${connectorType}`,\n });\n return;\n }\n\n try {\n // 1. Check if connector is already connected\n const existingConnector = get().connections[connectorType as ConnectorType];\n if (existingConnector) {\n return;\n }\n\n const connector = await foundAdapter.connect({\n connectorType,\n chainId,\n });\n\n // 2. Set initial connector state\n set((state) => {\n return {\n activeConnection: connector,\n connections: {\n ...state.connections,\n [connector.connectorType]: connector,\n },\n };\n });\n\n // 3. Check for contract address if the adapter supports it\n if (foundAdapter.checkIsContractAddress) {\n const isContractAddress = await foundAdapter.checkIsContractAddress({\n address: connector.address,\n chainId,\n });\n\n // Update only the isContractAddress property\n get().updateActiveConnection({ ...connector, isContractAddress });\n }\n\n // 4. Run callback if provided\n if (callbackAfterConnected) {\n // Use the latest connector state after potential updates (like isContractAddress)\n const updatedConnector = get().activeConnection;\n if (updatedConnector && updatedConnector.connectorType === connectorType) {\n await callbackAfterConnected(updatedConnector);\n }\n }\n\n // 5. Final state updates\n set({ connecting: false });\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType,\n chainId,\n address: get().activeConnection?.address,\n });\n recentConnectedConnectorHelpers.setRecentConnectedConnector({\n [getAdapterFromConnectorType(connectorType)]: {\n [connectorType.split(':')[1]]: true,\n },\n } as RecentConnectedConnector);\n } catch (e) {\n set({\n connecting: false,\n connectionError: 'Connector connection failed: ' + (e instanceof Error ? e.message : String(e)),\n });\n }\n },\n\n /**\n * Disconnects the currently active wallet or a specific wallet\n */\n /**\n * Disconnects the currently active wallet or a specific wallet\n */\n disconnect: async (connectorType?: string) => {\n // Guard against re-entry\n if (get().disconnecting) return;\n set({ disconnecting: true });\n\n try {\n if (connectorType) {\n // Disconnect specific connector\n const connectorToDisconnect = get().connections[connectorType as ConnectorType];\n\n if (connectorToDisconnect) {\n const foundAdapter = get().getAdapter(getAdapterFromConnectorType(connectorToDisconnect.connectorType));\n await foundAdapter?.disconnect(connectorToDisconnect);\n\n set((state) => {\n const newConnections = { ...state.connections };\n delete newConnections[connectorType as ConnectorType];\n\n // If the disconnected connector was the active one, set activeConnection to undefined\n const newActiveConnection =\n state.activeConnection?.connectorType === connectorType ? undefined : state.activeConnection;\n\n return {\n connections: newConnections,\n activeConnection: newActiveConnection,\n connectionError: undefined,\n switchNetworkError: undefined,\n };\n });\n }\n } else {\n // Disconnect ALL connectors\n await get().disconnectAll();\n }\n\n if (Object.keys(get().connections).length === 0) {\n lastConnectedConnectorHelpers.removeLastConnectedConnector();\n impersonatedHelpers.removeImpersonated();\n } else {\n // Update last connected to the current active one (if any)\n const currentActive = get().activeConnection;\n if (currentActive) {\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: currentActive.connectorType,\n chainId: currentActive.chainId,\n address: currentActive.address,\n });\n }\n }\n } finally {\n set({ disconnecting: false });\n }\n },\n\n disconnectAll: async () => {\n await delay(null, 150);\n\n if (Array.isArray(adapter)) {\n await Promise.allSettled(\n adapter.map(async (a) => {\n try {\n await a.disconnect();\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (e) {\n /* empty */\n }\n }),\n );\n } else {\n try {\n await adapter.disconnect();\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (e) {\n /* empty */\n }\n }\n\n set({\n activeConnection: undefined,\n connections: {},\n connectionError: undefined,\n switchNetworkError: undefined,\n });\n impersonatedHelpers.removeImpersonated();\n },\n\n /**\n * Contains error message if connection failed\n */\n // connectionError is declared above with an initial value\n\n /**\n * Resets any connection errors\n */\n resetConnectionError: () => {\n set({ connectionError: undefined });\n },\n\n /**\n * Updates the active connection's properties\n * @param connector - Partial connector object with properties to update\n */\n updateActiveConnection: (connector: Partial<Connector<W>>) => {\n const activeConnection = get().activeConnection;\n // Determine which connector to update. If connectorType is provided, use it. Otherwise use activeConnection.\n const targetConnectorType = connector.connectorType ?? activeConnection?.connectorType;\n\n if (targetConnectorType) {\n // If chainId is updated, update storage\n if (connector.chainId && targetConnectorType === activeConnection?.connectorType) {\n // Update lastConnectedConnector storage if chainId changes and it's the active connector\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: targetConnectorType,\n chainId: connector.chainId,\n address: connector.address ?? activeConnection?.address,\n });\n }\n\n // Use produce for immutable state update\n set((state) =>\n produce(state, (draft) => {\n if (draft.connections[targetConnectorType as ConnectorType]) {\n draft.connections[targetConnectorType as ConnectorType] = {\n ...draft.connections[targetConnectorType as ConnectorType],\n ...connector,\n } as Connector<W>;\n\n // Also update activeConnection if it matches\n if (draft.activeConnection?.connectorType === targetConnectorType) {\n draft.activeConnection = draft.connections[targetConnectorType as ConnectorType];\n }\n }\n }),\n );\n } else {\n const isConnectorCanChange =\n connector.connectorType !== undefined && connector.chainId !== undefined && connector.address !== undefined;\n\n if (isConnectorCanChange) {\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: connector.connectorType!,\n chainId: connector.chainId!,\n address: connector.address!,\n });\n // It's a new connector or full replacement\n set((state) => {\n const newConnector = connector as Connector<W>;\n return {\n activeConnection: newConnector,\n connections: {\n ...state.connections,\n [newConnector.connectorType]: newConnector,\n },\n };\n });\n } else {\n console.warn('Attempted to set activeConnection with incomplete data while activeConnection was undefined.');\n }\n }\n },\n\n /**\n * Switches active connection from the list of connections\n */\n switchConnection: (connectorType: string) => {\n const targetConnector = get().connections[connectorType as ConnectorType];\n if (targetConnector) {\n set({ activeConnection: targetConnector });\n lastConnectedConnectorHelpers.setLastConnectedConnector({\n connectorType: targetConnector.connectorType,\n chainId: targetConnector.chainId,\n address: targetConnector.address,\n });\n }\n },\n\n /**\n * Switches the connected connector to a different network\n * @param chainId - Target chain ID to switch to\n * @param connectorType - Optional connector type to switch to. If not provided, will switch to the active connection.\n */\n switchNetwork: async (chainId: string | number, connectorType?: string) => {\n set({ switchNetworkError: undefined });\n const targetConnector = connectorType\n ? get().connections[connectorType as ConnectorType]\n : get().activeConnection;\n\n if (targetConnector) {\n const foundAdapter = get().getAdapter(getAdapterFromConnectorType(targetConnector.connectorType));\n\n if (!foundAdapter) {\n set({ switchNetworkError: `No adapter found for active connector type: ${targetConnector.connectorType}` });\n return;\n }\n\n try {\n // Pass the local updateActiveConnection method from 'get()' to the adapter\n await foundAdapter.checkAndSwitchNetwork(chainId, targetConnector.chainId, get().updateActiveConnection);\n } catch (e) {\n set({ switchNetworkError: 'Switch network failed: ' + (e instanceof Error ? e.message : String(e)) });\n }\n }\n },\n\n /**\n * Contains error message if network switch failed\n */\n // switchNetworkError is declared above with an initial value\n\n /**\n * Resets any network switching errors\n */\n resetSwitchNetworkError: () => set({ switchNetworkError: undefined }),\n }));\n}\n"]}
|