@reown/appkit-core-react-native 0.0.0-chore-canary-20250722145858 → 0.0.0-chore-bump-builder-20250728194329

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.
Files changed (209) hide show
  1. package/lib/commonjs/controllers/ApiController.js +27 -16
  2. package/lib/commonjs/controllers/ApiController.js.map +1 -1
  3. package/lib/commonjs/controllers/BlockchainApiController.js +173 -103
  4. package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -1
  5. package/lib/commonjs/controllers/ConnectionController.js +8 -11
  6. package/lib/commonjs/controllers/ConnectionController.js.map +1 -1
  7. package/lib/commonjs/controllers/ConnectionsController.js +417 -0
  8. package/lib/commonjs/controllers/ConnectionsController.js.map +1 -0
  9. package/lib/commonjs/controllers/ModalController.js +6 -7
  10. package/lib/commonjs/controllers/ModalController.js.map +1 -1
  11. package/lib/commonjs/controllers/OnRampController.js +58 -42
  12. package/lib/commonjs/controllers/OnRampController.js.map +1 -1
  13. package/lib/commonjs/controllers/OptionsController.js +17 -3
  14. package/lib/commonjs/controllers/OptionsController.js.map +1 -1
  15. package/lib/commonjs/controllers/RouterController.js +1 -22
  16. package/lib/commonjs/controllers/RouterController.js.map +1 -1
  17. package/lib/commonjs/controllers/SendController.js +96 -96
  18. package/lib/commonjs/controllers/SendController.js.map +1 -1
  19. package/lib/commonjs/controllers/SnackController.js +29 -5
  20. package/lib/commonjs/controllers/SnackController.js.map +1 -1
  21. package/lib/commonjs/controllers/SwapController.js +151 -150
  22. package/lib/commonjs/controllers/SwapController.js.map +1 -1
  23. package/lib/commonjs/controllers/ThemeController.js +9 -1
  24. package/lib/commonjs/controllers/ThemeController.js.map +1 -1
  25. package/lib/commonjs/controllers/TransactionsController.js +22 -18
  26. package/lib/commonjs/controllers/TransactionsController.js.map +1 -1
  27. package/lib/commonjs/index.js +3 -38
  28. package/lib/commonjs/index.js.map +1 -1
  29. package/lib/commonjs/package.json +1 -0
  30. package/lib/commonjs/utils/AssetUtil.js +3 -15
  31. package/lib/commonjs/utils/AssetUtil.js.map +1 -1
  32. package/lib/commonjs/utils/ConstantsUtil.js +122 -5
  33. package/lib/commonjs/utils/ConstantsUtil.js.map +1 -1
  34. package/lib/commonjs/utils/CoreHelperUtil.js +11 -5
  35. package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -1
  36. package/lib/commonjs/utils/FetchUtil.js +7 -2
  37. package/lib/commonjs/utils/FetchUtil.js.map +1 -1
  38. package/lib/commonjs/utils/StorageUtil.js +99 -71
  39. package/lib/commonjs/utils/StorageUtil.js.map +1 -1
  40. package/lib/commonjs/utils/SwapApiUtil.js +15 -21
  41. package/lib/commonjs/utils/SwapApiUtil.js.map +1 -1
  42. package/lib/commonjs/utils/TypeUtil.js +25 -0
  43. package/lib/commonjs/utils/TypeUtil.js.map +1 -1
  44. package/lib/module/controllers/ApiController.js +28 -16
  45. package/lib/module/controllers/ApiController.js.map +1 -1
  46. package/lib/module/controllers/AssetController.js +2 -0
  47. package/lib/module/controllers/AssetController.js.map +1 -1
  48. package/lib/module/controllers/BlockchainApiController.js +174 -102
  49. package/lib/module/controllers/BlockchainApiController.js.map +1 -1
  50. package/lib/module/controllers/ConnectionController.js +10 -11
  51. package/lib/module/controllers/ConnectionController.js.map +1 -1
  52. package/lib/module/controllers/ConnectionsController.js +414 -0
  53. package/lib/module/controllers/ConnectionsController.js.map +1 -0
  54. package/lib/module/controllers/EnsController.js +2 -0
  55. package/lib/module/controllers/EnsController.js.map +1 -1
  56. package/lib/module/controllers/EventsController.js +2 -0
  57. package/lib/module/controllers/EventsController.js.map +1 -1
  58. package/lib/module/controllers/ModalController.js +8 -7
  59. package/lib/module/controllers/ModalController.js.map +1 -1
  60. package/lib/module/controllers/OnRampController.js +59 -41
  61. package/lib/module/controllers/OnRampController.js.map +1 -1
  62. package/lib/module/controllers/OptionsController.js +19 -3
  63. package/lib/module/controllers/OptionsController.js.map +1 -1
  64. package/lib/module/controllers/PublicStateController.js +2 -0
  65. package/lib/module/controllers/PublicStateController.js.map +1 -1
  66. package/lib/module/controllers/RouterController.js +3 -22
  67. package/lib/module/controllers/RouterController.js.map +1 -1
  68. package/lib/module/controllers/SendController.js +98 -96
  69. package/lib/module/controllers/SendController.js.map +1 -1
  70. package/lib/module/controllers/SnackController.js +31 -5
  71. package/lib/module/controllers/SnackController.js.map +1 -1
  72. package/lib/module/controllers/SwapController.js +153 -150
  73. package/lib/module/controllers/SwapController.js.map +1 -1
  74. package/lib/module/controllers/ThemeController.js +11 -1
  75. package/lib/module/controllers/ThemeController.js.map +1 -1
  76. package/lib/module/controllers/TransactionsController.js +24 -18
  77. package/lib/module/controllers/TransactionsController.js.map +1 -1
  78. package/lib/module/index.js +3 -6
  79. package/lib/module/index.js.map +1 -1
  80. package/lib/module/utils/ApiUtil.js +2 -0
  81. package/lib/module/utils/ApiUtil.js.map +1 -1
  82. package/lib/module/utils/AssetUtil.js +5 -15
  83. package/lib/module/utils/AssetUtil.js.map +1 -1
  84. package/lib/module/utils/ConstantsUtil.js +124 -5
  85. package/lib/module/utils/ConstantsUtil.js.map +1 -1
  86. package/lib/module/utils/CoreHelperUtil.js +13 -5
  87. package/lib/module/utils/CoreHelperUtil.js.map +1 -1
  88. package/lib/module/utils/EventUtil.js +2 -0
  89. package/lib/module/utils/EventUtil.js.map +1 -1
  90. package/lib/module/utils/FetchUtil.js +9 -2
  91. package/lib/module/utils/FetchUtil.js.map +1 -1
  92. package/lib/module/utils/RouterUtil.js +2 -0
  93. package/lib/module/utils/RouterUtil.js.map +1 -1
  94. package/lib/module/utils/StorageUtil.js +103 -71
  95. package/lib/module/utils/StorageUtil.js.map +1 -1
  96. package/lib/module/utils/SwapApiUtil.js +17 -21
  97. package/lib/module/utils/SwapApiUtil.js.map +1 -1
  98. package/lib/module/utils/SwapCalculationUtil.js +2 -0
  99. package/lib/module/utils/SwapCalculationUtil.js.map +1 -1
  100. package/lib/module/utils/TypeUtil.js +25 -1
  101. package/lib/module/utils/TypeUtil.js.map +1 -1
  102. package/lib/typescript/controllers/ApiController.d.ts +3 -2
  103. package/lib/typescript/controllers/ApiController.d.ts.map +1 -1
  104. package/lib/typescript/controllers/BlockchainApiController.d.ts +30 -11
  105. package/lib/typescript/controllers/BlockchainApiController.d.ts.map +1 -1
  106. package/lib/typescript/controllers/ConnectionController.d.ts +5 -16
  107. package/lib/typescript/controllers/ConnectionController.d.ts.map +1 -1
  108. package/lib/typescript/controllers/ConnectionsController.d.ts +45 -0
  109. package/lib/typescript/controllers/ConnectionsController.d.ts.map +1 -0
  110. package/lib/typescript/controllers/ModalController.d.ts.map +1 -1
  111. package/lib/typescript/controllers/OnRampController.d.ts +1 -2
  112. package/lib/typescript/controllers/OnRampController.d.ts.map +1 -1
  113. package/lib/typescript/controllers/OptionsController.d.ts +8 -2
  114. package/lib/typescript/controllers/OptionsController.d.ts.map +1 -1
  115. package/lib/typescript/controllers/PublicStateController.d.ts +1 -1
  116. package/lib/typescript/controllers/PublicStateController.d.ts.map +1 -1
  117. package/lib/typescript/controllers/RouterController.d.ts +4 -16
  118. package/lib/typescript/controllers/RouterController.d.ts.map +1 -1
  119. package/lib/typescript/controllers/SendController.d.ts +5 -7
  120. package/lib/typescript/controllers/SendController.d.ts.map +1 -1
  121. package/lib/typescript/controllers/SnackController.d.ts +3 -3
  122. package/lib/typescript/controllers/SnackController.d.ts.map +1 -1
  123. package/lib/typescript/controllers/SwapController.d.ts +12 -11
  124. package/lib/typescript/controllers/SwapController.d.ts.map +1 -1
  125. package/lib/typescript/controllers/ThemeController.d.ts +1 -1
  126. package/lib/typescript/controllers/ThemeController.d.ts.map +1 -1
  127. package/lib/typescript/controllers/TransactionsController.d.ts +3 -3
  128. package/lib/typescript/controllers/TransactionsController.d.ts.map +1 -1
  129. package/lib/typescript/index.d.ts +1 -6
  130. package/lib/typescript/index.d.ts.map +1 -1
  131. package/lib/typescript/utils/AssetUtil.d.ts +2 -3
  132. package/lib/typescript/utils/AssetUtil.d.ts.map +1 -1
  133. package/lib/typescript/utils/ConstantsUtil.d.ts +11 -1
  134. package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -1
  135. package/lib/typescript/utils/CoreHelperUtil.d.ts +5 -4
  136. package/lib/typescript/utils/CoreHelperUtil.d.ts.map +1 -1
  137. package/lib/typescript/utils/FetchUtil.d.ts +1 -1
  138. package/lib/typescript/utils/FetchUtil.d.ts.map +1 -1
  139. package/lib/typescript/utils/StorageUtil.d.ts +18 -12
  140. package/lib/typescript/utils/StorageUtil.d.ts.map +1 -1
  141. package/lib/typescript/utils/SwapApiUtil.d.ts +3 -3
  142. package/lib/typescript/utils/SwapApiUtil.d.ts.map +1 -1
  143. package/lib/typescript/utils/TypeUtil.d.ts +44 -184
  144. package/lib/typescript/utils/TypeUtil.d.ts.map +1 -1
  145. package/package.json +7 -7
  146. package/src/controllers/ApiController.ts +28 -10
  147. package/src/controllers/BlockchainApiController.ts +152 -89
  148. package/src/controllers/ConnectionController.ts +14 -29
  149. package/src/controllers/ConnectionsController.ts +543 -0
  150. package/src/controllers/ModalController.ts +6 -7
  151. package/src/controllers/OnRampController.ts +91 -59
  152. package/src/controllers/OptionsController.ts +27 -13
  153. package/src/controllers/PublicStateController.ts +1 -1
  154. package/src/controllers/RouterController.ts +5 -54
  155. package/src/controllers/SendController.ts +122 -111
  156. package/src/controllers/SnackController.ts +31 -5
  157. package/src/controllers/SwapController.ts +174 -181
  158. package/src/controllers/ThemeController.ts +11 -2
  159. package/src/controllers/TransactionsController.ts +25 -19
  160. package/src/index.ts +3 -14
  161. package/src/utils/AssetUtil.ts +4 -20
  162. package/src/utils/ConstantsUtil.ts +126 -5
  163. package/src/utils/CoreHelperUtil.ts +34 -7
  164. package/src/utils/FetchUtil.ts +9 -3
  165. package/src/utils/StorageUtil.ts +180 -78
  166. package/src/utils/SwapApiUtil.ts +27 -38
  167. package/src/utils/TypeUtil.ts +54 -182
  168. package/lib/commonjs/controllers/AccountController.js +0 -93
  169. package/lib/commonjs/controllers/AccountController.js.map +0 -1
  170. package/lib/commonjs/controllers/ConnectorController.js +0 -49
  171. package/lib/commonjs/controllers/ConnectorController.js.map +0 -1
  172. package/lib/commonjs/controllers/NetworkController.js +0 -90
  173. package/lib/commonjs/controllers/NetworkController.js.map +0 -1
  174. package/lib/commonjs/controllers/WebviewController.js +0 -52
  175. package/lib/commonjs/controllers/WebviewController.js.map +0 -1
  176. package/lib/commonjs/utils/ConnectionUtil.js +0 -33
  177. package/lib/commonjs/utils/ConnectionUtil.js.map +0 -1
  178. package/lib/commonjs/utils/NetworkUtil.js +0 -46
  179. package/lib/commonjs/utils/NetworkUtil.js.map +0 -1
  180. package/lib/module/controllers/AccountController.js +0 -88
  181. package/lib/module/controllers/AccountController.js.map +0 -1
  182. package/lib/module/controllers/ConnectorController.js +0 -44
  183. package/lib/module/controllers/ConnectorController.js.map +0 -1
  184. package/lib/module/controllers/NetworkController.js +0 -85
  185. package/lib/module/controllers/NetworkController.js.map +0 -1
  186. package/lib/module/controllers/WebviewController.js +0 -47
  187. package/lib/module/controllers/WebviewController.js.map +0 -1
  188. package/lib/module/utils/ConnectionUtil.js +0 -27
  189. package/lib/module/utils/ConnectionUtil.js.map +0 -1
  190. package/lib/module/utils/NetworkUtil.js +0 -40
  191. package/lib/module/utils/NetworkUtil.js.map +0 -1
  192. package/lib/typescript/controllers/AccountController.d.ts +0 -33
  193. package/lib/typescript/controllers/AccountController.d.ts.map +0 -1
  194. package/lib/typescript/controllers/ConnectorController.d.ts +0 -18
  195. package/lib/typescript/controllers/ConnectorController.d.ts.map +0 -1
  196. package/lib/typescript/controllers/NetworkController.d.ts +0 -34
  197. package/lib/typescript/controllers/NetworkController.d.ts.map +0 -1
  198. package/lib/typescript/controllers/WebviewController.d.ts +0 -21
  199. package/lib/typescript/controllers/WebviewController.d.ts.map +0 -1
  200. package/lib/typescript/utils/ConnectionUtil.d.ts +0 -4
  201. package/lib/typescript/utils/ConnectionUtil.d.ts.map +0 -1
  202. package/lib/typescript/utils/NetworkUtil.d.ts +0 -8
  203. package/lib/typescript/utils/NetworkUtil.d.ts.map +0 -1
  204. package/src/controllers/AccountController.ts +0 -128
  205. package/src/controllers/ConnectorController.ts +0 -63
  206. package/src/controllers/NetworkController.ts +0 -120
  207. package/src/controllers/WebviewController.ts +0 -63
  208. package/src/utils/ConnectionUtil.ts +0 -27
  209. package/src/utils/NetworkUtil.ts +0 -33
@@ -0,0 +1,543 @@
1
+ import { proxy, ref } from 'valtio';
2
+ import { derive } from 'derive-valtio';
3
+ import {
4
+ EVMAdapter,
5
+ type AppKitNetwork,
6
+ type BlockchainAdapter,
7
+ type CaipAddress,
8
+ type CaipNetworkId,
9
+ type ChainNamespace,
10
+ type Balance,
11
+ type WalletInfo,
12
+ type ConnectionProperties,
13
+ type AccountType,
14
+ type Connection,
15
+ SolanaBaseAdapter,
16
+ type Identity
17
+ } from '@reown/appkit-common-react-native';
18
+ import { StorageUtil } from '../utils/StorageUtil';
19
+ import { BlockchainApiController } from './BlockchainApiController';
20
+ import { SnackController } from './SnackController';
21
+ import { OptionsController } from './OptionsController';
22
+ import { CoreHelperUtil } from '../utils/CoreHelperUtil';
23
+
24
+ // -- Types --------------------------------------------- //
25
+ export interface ConnectionsControllerState {
26
+ activeNamespace?: ChainNamespace;
27
+ connections: Map<ChainNamespace, Connection>;
28
+ networks: AppKitNetwork[];
29
+ }
30
+
31
+ // -- State --------------------------------------------- //
32
+ const baseState = proxy<ConnectionsControllerState>({
33
+ activeNamespace: undefined,
34
+ connections: new Map<ChainNamespace, Connection>(),
35
+ networks: []
36
+ });
37
+
38
+ // -- Helper Functions --------------------------------------------- //
39
+ const getActiveConnection = (snap: ConnectionsControllerState): Connection | undefined => {
40
+ if (!snap.activeNamespace) return undefined;
41
+
42
+ return snap.connections.get(snap.activeNamespace);
43
+ };
44
+
45
+ const hasValidAccounts = (connection: Connection): boolean => {
46
+ return connection?.accounts && connection.accounts.length > 0;
47
+ };
48
+
49
+ const findSmartAccountForNetwork = (connection: Connection): CaipAddress | undefined => {
50
+ return connection.properties?.smartAccounts?.find(account =>
51
+ account.startsWith(connection.caipNetwork)
52
+ );
53
+ };
54
+
55
+ const findEOAForNetwork = (connection: Connection): CaipAddress | undefined => {
56
+ const smartAccounts = connection.properties?.smartAccounts || [];
57
+
58
+ return connection.accounts.find(
59
+ account => account.startsWith(connection.caipNetwork) && !smartAccounts.includes(account)
60
+ );
61
+ };
62
+
63
+ const getActiveAddress = (connection: Connection): CaipAddress | undefined => {
64
+ if (!hasValidAccounts(connection)) {
65
+ return undefined;
66
+ }
67
+
68
+ // For smart accounts, prioritize smart account addresses
69
+ if (connection.type === 'smartAccount') {
70
+ const smartAccount = findSmartAccountForNetwork(connection);
71
+ if (smartAccount) {
72
+ return smartAccount;
73
+ }
74
+ }
75
+
76
+ // Fall back to EOA or any account that matches the network
77
+ return findEOAForNetwork(connection);
78
+ };
79
+
80
+ const updateConnection = (
81
+ namespace: ChainNamespace,
82
+ connection: Connection,
83
+ updates: Partial<Connection>
84
+ ) => {
85
+ if (!connection) return;
86
+ const newConnectionsMap = new Map(baseState.connections);
87
+ newConnectionsMap.set(namespace, { ...connection, ...updates });
88
+ baseState.connections = newConnectionsMap;
89
+ };
90
+
91
+ const getActiveIdentity = (connection: Connection): Identity | undefined => {
92
+ const activeAddress = getActiveAddress(connection);
93
+ if (!activeAddress) return undefined;
94
+
95
+ return connection.identities?.get(activeAddress);
96
+ };
97
+
98
+ const derivedState = derive(
99
+ {
100
+ isConnected: (get): boolean => {
101
+ const snap = get(baseState);
102
+
103
+ return !!snap.activeNamespace && !!snap.connections.size;
104
+ },
105
+ activeAddress: (get): CaipAddress | undefined => {
106
+ const snap = get(baseState);
107
+ const connection = getActiveConnection(snap);
108
+
109
+ return connection ? getActiveAddress(connection) : undefined;
110
+ },
111
+ identity: (get): Identity | undefined => {
112
+ const snap = get(baseState);
113
+ const connection = getActiveConnection(snap);
114
+
115
+ return connection ? getActiveIdentity(connection) : undefined;
116
+ },
117
+ activeBalance: (get): Balance | undefined => {
118
+ const snap = get(baseState);
119
+ const connection = getActiveConnection(snap);
120
+
121
+ if (!connection) {
122
+ return undefined;
123
+ }
124
+
125
+ const activeAddress = getActiveAddress(connection);
126
+ if (!activeAddress || !connection.balances || connection.balances.size === 0) {
127
+ return undefined;
128
+ }
129
+
130
+ const addressBalances = connection.balances.get(activeAddress);
131
+ if (!addressBalances || addressBalances.length === 0) {
132
+ return undefined;
133
+ }
134
+
135
+ // Check if there's a specific token configured in OptionsController
136
+ const configuredTokens = OptionsController.state.tokens;
137
+ const activeNetwork = snap.networks.find(
138
+ network =>
139
+ network.chainNamespace === snap.activeNamespace &&
140
+ network.id?.toString() === connection.caipNetwork?.split(':')[1]
141
+ );
142
+
143
+ if (configuredTokens && activeNetwork) {
144
+ const configuredToken = configuredTokens[activeNetwork.caipNetworkId];
145
+ if (configuredToken) {
146
+ // Find the configured token in the balances
147
+ const specificToken = addressBalances.find(
148
+ balance => balance.address === configuredToken.address
149
+ );
150
+ if (specificToken) {
151
+ return specificToken;
152
+ }
153
+ }
154
+ }
155
+
156
+ // Return the native token (first balance without address)
157
+ const nativeToken = addressBalances.find(balance => !balance.address);
158
+ if (nativeToken) {
159
+ return nativeToken;
160
+ }
161
+
162
+ // Fallback to first available balance
163
+ return addressBalances[0];
164
+ },
165
+ activeNetwork: (get): AppKitNetwork | undefined => {
166
+ const snap = get(baseState);
167
+ const connection = getActiveConnection(snap);
168
+
169
+ if (!connection) return undefined;
170
+
171
+ return snap.networks.find(
172
+ network =>
173
+ network.chainNamespace === snap.activeNamespace &&
174
+ network.id?.toString() === connection.caipNetwork?.split(':')[1]
175
+ );
176
+ },
177
+ activeCaipNetworkId: (get): CaipNetworkId | undefined => {
178
+ const snap = get(baseState);
179
+ const connection = getActiveConnection(snap);
180
+
181
+ return connection?.caipNetwork;
182
+ },
183
+ accountType: (get): AccountType | undefined => {
184
+ const snap = get(baseState);
185
+ const connection = getActiveConnection(snap);
186
+
187
+ return connection?.type;
188
+ },
189
+ connection: (get): Connection | undefined => {
190
+ const snap = get(baseState);
191
+
192
+ return getActiveConnection(snap);
193
+ },
194
+ balances: (get): Balance[] | undefined => {
195
+ const snap = get(baseState);
196
+
197
+ const _connection = getActiveConnection(snap);
198
+
199
+ if (!_connection) {
200
+ return undefined;
201
+ }
202
+
203
+ const _activeAddress = getActiveAddress(_connection);
204
+
205
+ if (!_activeAddress) return [];
206
+
207
+ return (
208
+ _connection?.balances
209
+ .get(_activeAddress)
210
+ // Filter out tokens with no quantity
211
+ ?.filter(balance => balance?.quantity?.numeric)
212
+ );
213
+ },
214
+ walletInfo: (get): WalletInfo | undefined => {
215
+ const snap = get(baseState);
216
+ const connection = getActiveConnection(snap);
217
+
218
+ return connection?.wallet;
219
+ }
220
+ },
221
+ {
222
+ proxy: baseState // Link derived proxy to the base state proxy
223
+ }
224
+ );
225
+
226
+ // -- Controller ---------------------------------------- //
227
+ export const ConnectionsController = {
228
+ state: derivedState,
229
+
230
+ setActiveNamespace(namespace?: ChainNamespace) {
231
+ baseState.activeNamespace = namespace;
232
+ StorageUtil.setActiveNamespace(namespace);
233
+ },
234
+
235
+ setConnection({
236
+ accounts,
237
+ adapter,
238
+ caipNetwork,
239
+ namespace,
240
+ properties,
241
+ wallet
242
+ }: {
243
+ accounts: CaipAddress[];
244
+ adapter: BlockchainAdapter;
245
+ caipNetwork: CaipNetworkId;
246
+ namespace: ChainNamespace;
247
+ properties?: ConnectionProperties;
248
+ wallet?: WalletInfo;
249
+ }) {
250
+ const type: AccountType =
251
+ properties?.smartAccounts?.length &&
252
+ properties.smartAccounts.find(account => account.startsWith(caipNetwork))
253
+ ? 'smartAccount'
254
+ : 'eoa';
255
+
256
+ const newConnectionEntry: Connection = {
257
+ balances: new Map<CaipAddress, Balance[]>(),
258
+ caipNetwork,
259
+ adapter: ref(adapter),
260
+ accounts,
261
+ wallet,
262
+ properties,
263
+ type
264
+ };
265
+
266
+ // Create a new Map to ensure Valtio detects the change
267
+ const newConnectionsMap = new Map(baseState.connections);
268
+ newConnectionsMap.set(namespace, newConnectionEntry);
269
+ baseState.connections = newConnectionsMap;
270
+ },
271
+
272
+ updateAccounts(namespace: ChainNamespace, accounts: CaipAddress[]) {
273
+ const connection = baseState.connections.get(namespace);
274
+ if (!connection) {
275
+ return;
276
+ }
277
+
278
+ const newConnectionsMap = new Map(baseState.connections);
279
+ const updatedConnection = { ...connection, accounts };
280
+ newConnectionsMap.set(namespace, updatedConnection);
281
+ baseState.connections = newConnectionsMap;
282
+ },
283
+
284
+ updateBalance(namespace: ChainNamespace, address: CaipAddress, balance: Balance) {
285
+ const connection = baseState.connections.get(namespace);
286
+ if (!connection) {
287
+ console.warn(`No connection found for namespace: ${namespace}`);
288
+
289
+ return;
290
+ }
291
+ const newBalances = new Map(connection.balances);
292
+ const existingBalances = connection.balances.get(address) || [];
293
+ // Check if this token already exists by contract address or symbol
294
+ const existingIndex = existingBalances.findIndex(existingBalance => {
295
+ if (balance.address) {
296
+ return existingBalance.address === balance.address;
297
+ }
298
+
299
+ return existingBalance.symbol === balance.symbol;
300
+ });
301
+ let updatedBalances: Balance[];
302
+ if (existingIndex >= 0) {
303
+ // Update existing token
304
+ updatedBalances = [...existingBalances];
305
+ updatedBalances[existingIndex] = {
306
+ ...updatedBalances[existingIndex],
307
+ ...balance
308
+ };
309
+ } else {
310
+ // Add new token
311
+ updatedBalances = [...existingBalances, balance];
312
+ }
313
+ newBalances.set(address, updatedBalances);
314
+ updateConnection(namespace, connection, { balances: newBalances });
315
+ },
316
+
317
+ updateIdentity(
318
+ namespace: ChainNamespace,
319
+ connection: Connection,
320
+ plainAddress: string,
321
+ identity: Identity
322
+ ) {
323
+ const accounts = connection.accounts.filter(
324
+ account => CoreHelperUtil.getPlainAddress(account) === plainAddress
325
+ );
326
+
327
+ if (accounts.length > 0) {
328
+ const newIdentities = new Map(connection.identities || []);
329
+ let hasChanges = false;
330
+
331
+ accounts.forEach(account => {
332
+ const existingIdentity = newIdentities.get(account);
333
+ if (!existingIdentity || existingIdentity.name !== identity.name) {
334
+ newIdentities.set(account, identity);
335
+ hasChanges = true;
336
+ }
337
+ });
338
+
339
+ if (hasChanges) {
340
+ updateConnection(namespace, connection, { identities: newIdentities });
341
+ }
342
+ }
343
+ },
344
+
345
+ setActiveNetwork(namespace: ChainNamespace, networkId: CaipNetworkId) {
346
+ const connection = baseState.connections.get(namespace);
347
+
348
+ if (!connection) {
349
+ return;
350
+ }
351
+
352
+ baseState.connections.set(namespace, {
353
+ ...connection,
354
+ caipNetwork: networkId
355
+ });
356
+ },
357
+
358
+ setNetworks(networks: AppKitNetwork[]) {
359
+ baseState.networks = networks;
360
+ },
361
+
362
+ getConnectedNetworks() {
363
+ return baseState.networks.filter(
364
+ network =>
365
+ baseState.connections
366
+ .get(network.chainNamespace)
367
+ ?.accounts.some(account => account.startsWith(network.caipNetworkId))
368
+ );
369
+ },
370
+
371
+ setAccountType(namespace: ChainNamespace, type: AccountType) {
372
+ const connection = baseState.connections.get(namespace);
373
+ if (!connection) return;
374
+
375
+ const newConnectionsMap = new Map(baseState.connections);
376
+ const newConnection = { ...connection, type };
377
+ newConnectionsMap.set(namespace, newConnection);
378
+ baseState.connections = newConnectionsMap;
379
+ },
380
+
381
+ async disconnect(namespace: ChainNamespace, isInternal = true) {
382
+ const connection = baseState.connections.get(namespace);
383
+ if (!connection) return;
384
+
385
+ // Get the current connector from the adapter
386
+ const connector = connection.adapter.connector;
387
+ if (!connector) return;
388
+
389
+ // Find all namespaces that use the same connector
390
+ const namespacesUsingConnector = Array.from(baseState.connections.keys()).filter(
391
+ ns => baseState.connections.get(ns)?.adapter.connector === connector
392
+ );
393
+
394
+ // Unsubscribe all event listeners from the adapter
395
+ namespacesUsingConnector.forEach(ns => {
396
+ const _connection = baseState.connections.get(ns);
397
+ if (_connection?.adapter) {
398
+ _connection.adapter.removeAllListeners();
399
+ }
400
+ });
401
+
402
+ // Disconnect the adapter
403
+ if (isInternal) {
404
+ await connection.adapter.disconnect();
405
+ }
406
+
407
+ // Remove all namespaces that used this connector
408
+ const newConnectionsMap = new Map(baseState.connections);
409
+ namespacesUsingConnector.forEach(ns => {
410
+ newConnectionsMap.delete(ns);
411
+ });
412
+ baseState.connections = newConnectionsMap;
413
+
414
+ // Remove activeNamespace if it is in the list of namespaces using the connector
415
+ if (
416
+ baseState.activeNamespace &&
417
+ (baseState.activeNamespace === namespace ||
418
+ namespacesUsingConnector.includes(baseState.activeNamespace))
419
+ ) {
420
+ baseState.activeNamespace = undefined;
421
+ StorageUtil.setActiveNamespace(undefined);
422
+ }
423
+ },
424
+
425
+ parseUnits(value: string, decimals: number) {
426
+ if (!baseState.activeNamespace) return undefined;
427
+
428
+ return baseState.connections
429
+ .get(baseState.activeNamespace)
430
+ ?.adapter.parseUnits(value, decimals);
431
+ },
432
+
433
+ async signMessage(address: CaipAddress, message: string) {
434
+ if (!baseState.activeNamespace) return undefined;
435
+
436
+ const [namespace, chainId, plainAddress] = address.split(':');
437
+
438
+ if (!namespace || namespace !== baseState.activeNamespace || !chainId || !plainAddress) {
439
+ return undefined;
440
+ }
441
+
442
+ const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
443
+
444
+ if (adapter instanceof EVMAdapter && plainAddress && chainId) {
445
+ return adapter.signMessage(plainAddress, message, chainId);
446
+ }
447
+
448
+ return undefined;
449
+ },
450
+
451
+ async sendTransaction(args: any) {
452
+ if (!baseState.activeNamespace) return undefined;
453
+
454
+ const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
455
+ if (adapter instanceof EVMAdapter || adapter instanceof SolanaBaseAdapter) {
456
+ return adapter.sendTransaction(args);
457
+ }
458
+
459
+ return undefined;
460
+ },
461
+
462
+ async estimateGas(args: any) {
463
+ if (!baseState.activeNamespace || baseState.activeNamespace !== 'eip155') return undefined;
464
+
465
+ const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
466
+ if (adapter instanceof EVMAdapter) {
467
+ return adapter.estimateGas(args);
468
+ }
469
+
470
+ return undefined;
471
+ },
472
+
473
+ async writeContract(args: any) {
474
+ if (!baseState.activeNamespace) return undefined;
475
+
476
+ const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
477
+ if (adapter instanceof EVMAdapter) {
478
+ return adapter.writeContract(args);
479
+ }
480
+
481
+ return undefined;
482
+ },
483
+
484
+ async fetchBalance(forceUpdateAddresses?: CaipAddress[]) {
485
+ const connection = getActiveConnection(baseState);
486
+ if (!connection) {
487
+ throw new Error('No active connection found for balance fetch');
488
+ }
489
+
490
+ const chainId = connection.caipNetwork;
491
+ const address = getActiveAddress(connection);
492
+ const namespace = baseState.activeNamespace;
493
+ if (!namespace || !address || !chainId) {
494
+ throw new Error('Missing required data for balance fetch');
495
+ }
496
+
497
+ try {
498
+ const response = await BlockchainApiController.getBalance(address, forceUpdateAddresses);
499
+ if (!response) {
500
+ throw new Error('Failed to fetch token balance');
501
+ }
502
+ // Update balances for each token in the response
503
+ response.balances.forEach(balance => {
504
+ this.updateBalance(namespace, address, {
505
+ name: balance.name,
506
+ symbol: balance.symbol,
507
+ amount: balance.quantity.numeric,
508
+ address: balance.address,
509
+ quantity: balance.quantity,
510
+ price: balance.price,
511
+ value: balance.value,
512
+ iconUrl: balance.iconUrl
513
+ });
514
+ });
515
+ } catch (error) {
516
+ SnackController.showError('Failed to get account balance');
517
+ }
518
+ },
519
+
520
+ getSmartAccountEnabledNetworks(): AppKitNetwork[] {
521
+ const activeConnection = getActiveConnection(baseState);
522
+ if (!activeConnection) {
523
+ return [];
524
+ }
525
+ if (!activeConnection.properties?.smartAccounts?.length) {
526
+ return [];
527
+ }
528
+ const smartAccountNetworks = new Set<CaipNetworkId>();
529
+ activeConnection.properties.smartAccounts.forEach(smartAccount => {
530
+ const parts = smartAccount.split(':');
531
+ if (parts.length >= 2) {
532
+ const networkId: CaipNetworkId = `${parts[0]}:${parts[1]}`;
533
+ smartAccountNetworks.add(networkId);
534
+ }
535
+ });
536
+
537
+ return baseState.networks.filter(network => {
538
+ const networkId: CaipNetworkId = `${network.chainNamespace}:${network.id}`;
539
+
540
+ return smartAccountNetworks.has(networkId);
541
+ });
542
+ }
543
+ };
@@ -1,10 +1,9 @@
1
1
  import { proxy } from 'valtio';
2
- import { AccountController } from './AccountController';
3
2
  import { RouterController, type RouterControllerState } from './RouterController';
4
3
  import { PublicStateController } from './PublicStateController';
5
4
  import { EventsController } from './EventsController';
6
5
  import { ApiController } from './ApiController';
7
- import { ConnectorController } from './ConnectorController';
6
+ import { ConnectionsController } from './ConnectionsController';
8
7
 
9
8
  // -- Types --------------------------------------------- //
10
9
  export interface ModalControllerState {
@@ -30,11 +29,11 @@ export const ModalController = {
30
29
 
31
30
  async open(options?: ModalControllerArguments['open']) {
32
31
  await ApiController.state.prefetchPromise;
33
- const connected = AccountController.state.isConnected;
32
+ const isConnected = ConnectionsController.state.isConnected;
34
33
  if (options?.view) {
35
34
  RouterController.reset(options.view);
36
- } else if (AccountController.state.isConnected) {
37
- const isUniversalWallet = ConnectorController.state.connectedConnector === 'AUTH';
35
+ } else if (isConnected) {
36
+ const isUniversalWallet = !!ConnectionsController.state.connection?.properties?.provider;
38
37
  RouterController.reset(isUniversalWallet ? 'Account' : 'AccountDefault');
39
38
  } else {
40
39
  RouterController.reset('Connect');
@@ -44,12 +43,12 @@ export const ModalController = {
44
43
  EventsController.sendEvent({
45
44
  type: 'track',
46
45
  event: 'MODAL_OPEN',
47
- properties: { connected }
46
+ properties: { connected: isConnected }
48
47
  });
49
48
  },
50
49
 
51
50
  close() {
52
- const connected = AccountController.state.isConnected;
51
+ const connected = ConnectionsController.state.isConnected;
53
52
  state.open = false;
54
53
  PublicStateController.set({ open: false });
55
54
  EventsController.sendEvent({