@reown/appkit-core-react-native 0.0.0-feat-multi-social-20250715153550 → 0.0.0-feat-onramp-20250718185718

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