@reown/appkit-core-react-native 2.0.0-alpha.1 → 2.0.0-alpha.2

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 (207) hide show
  1. package/lib/commonjs/controllers/ApiController.js +16 -5
  2. package/lib/commonjs/controllers/ApiController.js.map +1 -1
  3. package/lib/commonjs/controllers/AssetController.js.map +1 -1
  4. package/lib/commonjs/controllers/BlockchainApiController.js +103 -111
  5. package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -1
  6. package/lib/commonjs/controllers/ConnectionController.js +1 -10
  7. package/lib/commonjs/controllers/ConnectionController.js.map +1 -1
  8. package/lib/commonjs/controllers/ConnectionsController.js +263 -51
  9. package/lib/commonjs/controllers/ConnectionsController.js.map +1 -1
  10. package/lib/commonjs/controllers/EnsController.js +4 -4
  11. package/lib/commonjs/controllers/EnsController.js.map +1 -1
  12. package/lib/commonjs/controllers/EventsController.js +4 -4
  13. package/lib/commonjs/controllers/EventsController.js.map +1 -1
  14. package/lib/commonjs/controllers/ModalController.js +6 -7
  15. package/lib/commonjs/controllers/ModalController.js.map +1 -1
  16. package/lib/commonjs/controllers/OnRampController.js +117 -53
  17. package/lib/commonjs/controllers/OnRampController.js.map +1 -1
  18. package/lib/commonjs/controllers/OptionsController.js +9 -0
  19. package/lib/commonjs/controllers/OptionsController.js.map +1 -1
  20. package/lib/commonjs/controllers/PublicStateController.js.map +1 -1
  21. package/lib/commonjs/controllers/RouterController.js +1 -22
  22. package/lib/commonjs/controllers/RouterController.js.map +1 -1
  23. package/lib/commonjs/controllers/SendController.js +100 -100
  24. package/lib/commonjs/controllers/SendController.js.map +1 -1
  25. package/lib/commonjs/controllers/SnackController.js +29 -5
  26. package/lib/commonjs/controllers/SnackController.js.map +1 -1
  27. package/lib/commonjs/controllers/SwapController.js +106 -116
  28. package/lib/commonjs/controllers/SwapController.js.map +1 -1
  29. package/lib/commonjs/controllers/ThemeController.js.map +1 -1
  30. package/lib/commonjs/controllers/TransactionsController.js +23 -19
  31. package/lib/commonjs/controllers/TransactionsController.js.map +1 -1
  32. package/lib/commonjs/index.js +0 -35
  33. package/lib/commonjs/index.js.map +1 -1
  34. package/lib/commonjs/package.json +1 -0
  35. package/lib/commonjs/utils/ApiUtil.js.map +1 -1
  36. package/lib/commonjs/utils/AssetUtil.js +3 -14
  37. package/lib/commonjs/utils/AssetUtil.js.map +1 -1
  38. package/lib/commonjs/utils/ConstantsUtil.js +13 -425
  39. package/lib/commonjs/utils/ConstantsUtil.js.map +1 -1
  40. package/lib/commonjs/utils/CoreHelperUtil.js +13 -8
  41. package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -1
  42. package/lib/commonjs/utils/EventUtil.js.map +1 -1
  43. package/lib/commonjs/utils/FetchUtil.js +34 -10
  44. package/lib/commonjs/utils/FetchUtil.js.map +1 -1
  45. package/lib/commonjs/utils/RouterUtil.js.map +1 -1
  46. package/lib/commonjs/utils/StorageUtil.js +86 -92
  47. package/lib/commonjs/utils/StorageUtil.js.map +1 -1
  48. package/lib/commonjs/utils/SwapApiUtil.js +2 -17
  49. package/lib/commonjs/utils/SwapApiUtil.js.map +1 -1
  50. package/lib/commonjs/utils/SwapCalculationUtil.js.map +1 -1
  51. package/lib/commonjs/utils/TypeUtil.js +25 -0
  52. package/lib/commonjs/utils/TypeUtil.js.map +1 -1
  53. package/lib/module/controllers/ApiController.js +18 -6
  54. package/lib/module/controllers/ApiController.js.map +1 -1
  55. package/lib/module/controllers/AssetController.js +2 -0
  56. package/lib/module/controllers/AssetController.js.map +1 -1
  57. package/lib/module/controllers/BlockchainApiController.js +104 -110
  58. package/lib/module/controllers/BlockchainApiController.js.map +1 -1
  59. package/lib/module/controllers/ConnectionController.js +3 -10
  60. package/lib/module/controllers/ConnectionController.js.map +1 -1
  61. package/lib/module/controllers/ConnectionsController.js +265 -51
  62. package/lib/module/controllers/ConnectionsController.js.map +1 -1
  63. package/lib/module/controllers/EnsController.js +4 -2
  64. package/lib/module/controllers/EnsController.js.map +1 -1
  65. package/lib/module/controllers/EventsController.js +3 -1
  66. package/lib/module/controllers/EventsController.js.map +1 -1
  67. package/lib/module/controllers/ModalController.js +8 -7
  68. package/lib/module/controllers/ModalController.js.map +1 -1
  69. package/lib/module/controllers/OnRampController.js +118 -52
  70. package/lib/module/controllers/OnRampController.js.map +1 -1
  71. package/lib/module/controllers/OptionsController.js +11 -0
  72. package/lib/module/controllers/OptionsController.js.map +1 -1
  73. package/lib/module/controllers/PublicStateController.js +2 -0
  74. package/lib/module/controllers/PublicStateController.js.map +1 -1
  75. package/lib/module/controllers/RouterController.js +3 -22
  76. package/lib/module/controllers/RouterController.js.map +1 -1
  77. package/lib/module/controllers/SendController.js +99 -97
  78. package/lib/module/controllers/SendController.js.map +1 -1
  79. package/lib/module/controllers/SnackController.js +31 -5
  80. package/lib/module/controllers/SnackController.js.map +1 -1
  81. package/lib/module/controllers/SwapController.js +108 -116
  82. package/lib/module/controllers/SwapController.js.map +1 -1
  83. package/lib/module/controllers/ThemeController.js +2 -0
  84. package/lib/module/controllers/ThemeController.js.map +1 -1
  85. package/lib/module/controllers/TransactionsController.js +23 -17
  86. package/lib/module/controllers/TransactionsController.js.map +1 -1
  87. package/lib/module/index.js +2 -5
  88. package/lib/module/index.js.map +1 -1
  89. package/lib/module/utils/ApiUtil.js +2 -0
  90. package/lib/module/utils/ApiUtil.js.map +1 -1
  91. package/lib/module/utils/AssetUtil.js +5 -14
  92. package/lib/module/utils/AssetUtil.js.map +1 -1
  93. package/lib/module/utils/ConstantsUtil.js +15 -425
  94. package/lib/module/utils/ConstantsUtil.js.map +1 -1
  95. package/lib/module/utils/CoreHelperUtil.js +14 -6
  96. package/lib/module/utils/CoreHelperUtil.js.map +1 -1
  97. package/lib/module/utils/EventUtil.js +2 -0
  98. package/lib/module/utils/EventUtil.js.map +1 -1
  99. package/lib/module/utils/FetchUtil.js +36 -10
  100. package/lib/module/utils/FetchUtil.js.map +1 -1
  101. package/lib/module/utils/RouterUtil.js +2 -0
  102. package/lib/module/utils/RouterUtil.js.map +1 -1
  103. package/lib/module/utils/StorageUtil.js +89 -91
  104. package/lib/module/utils/StorageUtil.js.map +1 -1
  105. package/lib/module/utils/SwapApiUtil.js +4 -17
  106. package/lib/module/utils/SwapApiUtil.js.map +1 -1
  107. package/lib/module/utils/SwapCalculationUtil.js +2 -0
  108. package/lib/module/utils/SwapCalculationUtil.js.map +1 -1
  109. package/lib/module/utils/TypeUtil.js +25 -1
  110. package/lib/module/utils/TypeUtil.js.map +1 -1
  111. package/lib/typescript/controllers/ApiController.d.ts +2 -1
  112. package/lib/typescript/controllers/ApiController.d.ts.map +1 -1
  113. package/lib/typescript/controllers/BlockchainApiController.d.ts +20 -13
  114. package/lib/typescript/controllers/BlockchainApiController.d.ts.map +1 -1
  115. package/lib/typescript/controllers/ConnectionController.d.ts +3 -16
  116. package/lib/typescript/controllers/ConnectionController.d.ts.map +1 -1
  117. package/lib/typescript/controllers/ConnectionsController.d.ts +19 -17
  118. package/lib/typescript/controllers/ConnectionsController.d.ts.map +1 -1
  119. package/lib/typescript/controllers/ModalController.d.ts +1 -1
  120. package/lib/typescript/controllers/ModalController.d.ts.map +1 -1
  121. package/lib/typescript/controllers/OnRampController.d.ts +3 -2
  122. package/lib/typescript/controllers/OnRampController.d.ts.map +1 -1
  123. package/lib/typescript/controllers/OptionsController.d.ts +4 -1
  124. package/lib/typescript/controllers/OptionsController.d.ts.map +1 -1
  125. package/lib/typescript/controllers/RouterController.d.ts +4 -17
  126. package/lib/typescript/controllers/RouterController.d.ts.map +1 -1
  127. package/lib/typescript/controllers/SendController.d.ts +5 -7
  128. package/lib/typescript/controllers/SendController.d.ts.map +1 -1
  129. package/lib/typescript/controllers/SnackController.d.ts +3 -3
  130. package/lib/typescript/controllers/SnackController.d.ts.map +1 -1
  131. package/lib/typescript/controllers/SwapController.d.ts +11 -10
  132. package/lib/typescript/controllers/SwapController.d.ts.map +1 -1
  133. package/lib/typescript/controllers/TransactionsController.d.ts +3 -3
  134. package/lib/typescript/controllers/TransactionsController.d.ts.map +1 -1
  135. package/lib/typescript/index.d.ts +0 -5
  136. package/lib/typescript/index.d.ts.map +1 -1
  137. package/lib/typescript/utils/AssetUtil.d.ts +2 -3
  138. package/lib/typescript/utils/AssetUtil.d.ts.map +1 -1
  139. package/lib/typescript/utils/ConstantsUtil.d.ts +6 -414
  140. package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -1
  141. package/lib/typescript/utils/CoreHelperUtil.d.ts +5 -4
  142. package/lib/typescript/utils/CoreHelperUtil.d.ts.map +1 -1
  143. package/lib/typescript/utils/FetchUtil.d.ts.map +1 -1
  144. package/lib/typescript/utils/StorageUtil.d.ts +11 -15
  145. package/lib/typescript/utils/StorageUtil.d.ts.map +1 -1
  146. package/lib/typescript/utils/SwapApiUtil.d.ts +3 -3
  147. package/lib/typescript/utils/SwapApiUtil.d.ts.map +1 -1
  148. package/lib/typescript/utils/TypeUtil.d.ts +54 -166
  149. package/lib/typescript/utils/TypeUtil.d.ts.map +1 -1
  150. package/package.json +9 -23
  151. package/src/controllers/ApiController.ts +17 -4
  152. package/src/controllers/BlockchainApiController.ts +74 -119
  153. package/src/controllers/ConnectionController.ts +4 -27
  154. package/src/controllers/ConnectionsController.ts +329 -77
  155. package/src/controllers/EnsController.ts +2 -2
  156. package/src/controllers/EventsController.ts +1 -1
  157. package/src/controllers/ModalController.ts +7 -9
  158. package/src/controllers/OnRampController.ts +178 -81
  159. package/src/controllers/OptionsController.ts +14 -1
  160. package/src/controllers/RouterController.ts +4 -54
  161. package/src/controllers/SendController.ts +123 -112
  162. package/src/controllers/SnackController.ts +31 -5
  163. package/src/controllers/SwapController.ts +127 -144
  164. package/src/controllers/TransactionsController.ts +24 -18
  165. package/src/index.ts +0 -15
  166. package/src/utils/AssetUtil.ts +4 -18
  167. package/src/utils/ConstantsUtil.ts +14 -427
  168. package/src/utils/CoreHelperUtil.ts +30 -8
  169. package/src/utils/FetchUtil.ts +37 -10
  170. package/src/utils/StorageUtil.ts +165 -106
  171. package/src/utils/SwapApiUtil.ts +15 -38
  172. package/src/utils/TypeUtil.ts +65 -161
  173. package/lib/commonjs/controllers/AccountController.js +0 -93
  174. package/lib/commonjs/controllers/AccountController.js.map +0 -1
  175. package/lib/commonjs/controllers/ConnectorController.js +0 -50
  176. package/lib/commonjs/controllers/ConnectorController.js.map +0 -1
  177. package/lib/commonjs/controllers/NetworkController.js +0 -90
  178. package/lib/commonjs/controllers/NetworkController.js.map +0 -1
  179. package/lib/commonjs/controllers/WebviewController.js +0 -52
  180. package/lib/commonjs/controllers/WebviewController.js.map +0 -1
  181. package/lib/commonjs/utils/NetworkUtil.js +0 -46
  182. package/lib/commonjs/utils/NetworkUtil.js.map +0 -1
  183. package/lib/module/controllers/AccountController.js +0 -88
  184. package/lib/module/controllers/AccountController.js.map +0 -1
  185. package/lib/module/controllers/ConnectorController.js +0 -45
  186. package/lib/module/controllers/ConnectorController.js.map +0 -1
  187. package/lib/module/controllers/NetworkController.js +0 -85
  188. package/lib/module/controllers/NetworkController.js.map +0 -1
  189. package/lib/module/controllers/WebviewController.js +0 -47
  190. package/lib/module/controllers/WebviewController.js.map +0 -1
  191. package/lib/module/utils/NetworkUtil.js +0 -40
  192. package/lib/module/utils/NetworkUtil.js.map +0 -1
  193. package/lib/typescript/controllers/AccountController.d.ts +0 -33
  194. package/lib/typescript/controllers/AccountController.d.ts.map +0 -1
  195. package/lib/typescript/controllers/ConnectorController.d.ts +0 -18
  196. package/lib/typescript/controllers/ConnectorController.d.ts.map +0 -1
  197. package/lib/typescript/controllers/NetworkController.d.ts +0 -34
  198. package/lib/typescript/controllers/NetworkController.d.ts.map +0 -1
  199. package/lib/typescript/controllers/WebviewController.d.ts +0 -21
  200. package/lib/typescript/controllers/WebviewController.d.ts.map +0 -1
  201. package/lib/typescript/utils/NetworkUtil.d.ts +0 -8
  202. package/lib/typescript/utils/NetworkUtil.d.ts.map +0 -1
  203. package/src/controllers/AccountController.ts +0 -128
  204. package/src/controllers/ConnectorController.ts +0 -64
  205. package/src/controllers/NetworkController.ts +0 -120
  206. package/src/controllers/WebviewController.ts +0 -63
  207. package/src/utils/NetworkUtil.ts +0 -33
@@ -1,5 +1,5 @@
1
1
  import { proxy, ref } from 'valtio';
2
- import { derive } from 'valtio/utils';
2
+ import { derive } from 'derive-valtio';
3
3
  import {
4
4
  EVMAdapter,
5
5
  type AppKitNetwork,
@@ -7,23 +7,21 @@ import {
7
7
  type CaipAddress,
8
8
  type CaipNetworkId,
9
9
  type ChainNamespace,
10
- type GetBalanceResponse,
11
- type WalletInfo
10
+ type Balance,
11
+ type WalletInfo,
12
+ type ConnectionProperties,
13
+ type AccountType,
14
+ type Connection,
15
+ SolanaBaseAdapter,
16
+ type Identity
12
17
  } from '@reown/appkit-common-react-native';
13
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';
14
23
 
15
24
  // -- Types --------------------------------------------- //
16
- type Balance = GetBalanceResponse;
17
-
18
- //TODO: balance could be elsewhere
19
- interface Connection {
20
- accounts: CaipAddress[];
21
- balances: Record<CaipAddress, Balance>; //TODO: make this an array of balances
22
- adapter: BlockchainAdapter;
23
- caipNetwork: CaipNetworkId;
24
- wallet?: WalletInfo;
25
- }
26
-
27
25
  export interface ConnectionsControllerState {
28
26
  activeNamespace?: ChainNamespace;
29
27
  connections: Map<ChainNamespace, Connection>;
@@ -37,59 +35,136 @@ const baseState = proxy<ConnectionsControllerState>({
37
35
  networks: []
38
36
  });
39
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
+
40
98
  const derivedState = derive(
41
99
  {
42
- activeAddress: (get): CaipAddress | undefined => {
100
+ isConnected: (get): boolean => {
43
101
  const snap = get(baseState);
44
102
 
45
- if (!snap.activeNamespace) {
46
- return undefined;
47
- }
48
-
49
- const connection = snap.connections.get(snap.activeNamespace);
50
-
51
- if (!connection || !connection.accounts || connection.accounts.length === 0) {
52
- return undefined;
53
- }
103
+ return !!snap.activeNamespace && !!snap.connections.size;
104
+ },
105
+ activeAddress: (get): CaipAddress | undefined => {
106
+ const snap = get(baseState);
107
+ const connection = getActiveConnection(snap);
54
108
 
55
- //TODO: what happens if there are several accounts on the same chain?
56
- const activeAccount = connection.accounts.find(account =>
57
- account.startsWith(connection.caipNetwork)
58
- );
109
+ return connection ? getActiveAddress(connection) : undefined;
110
+ },
111
+ identity: (get): Identity | undefined => {
112
+ const snap = get(baseState);
113
+ const connection = getActiveConnection(snap);
59
114
 
60
- return activeAccount;
115
+ return connection ? getActiveIdentity(connection) : undefined;
61
116
  },
62
117
  activeBalance: (get): Balance | undefined => {
63
118
  const snap = get(baseState);
119
+ const connection = getActiveConnection(snap);
64
120
 
65
- if (!snap.activeNamespace) return undefined;
66
- const connection = snap.connections.get(snap.activeNamespace);
121
+ if (!connection) {
122
+ return undefined;
123
+ }
67
124
 
68
- if (!connection || !connection.accounts || connection.accounts.length === 0) {
125
+ const activeAddress = getActiveAddress(connection);
126
+ if (!activeAddress || !connection.balances || connection.balances.size === 0) {
69
127
  return undefined;
70
128
  }
71
129
 
72
- const activeAccount = connection.accounts.find(account =>
73
- account.startsWith(connection.caipNetwork)
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]
74
141
  );
75
142
 
76
- if (
77
- !connection ||
78
- !connection.balances ||
79
- !activeAccount ||
80
- Object.keys(connection.balances).length === 0
81
- ) {
82
- return undefined;
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;
83
160
  }
84
161
 
85
- return connection.balances[activeAccount];
162
+ // Fallback to first available balance
163
+ return addressBalances[0];
86
164
  },
87
165
  activeNetwork: (get): AppKitNetwork | undefined => {
88
166
  const snap = get(baseState);
89
-
90
- if (!snap.activeNamespace) return undefined;
91
-
92
- const connection = snap.connections.get(snap.activeNamespace);
167
+ const connection = getActiveConnection(snap);
93
168
 
94
169
  if (!connection) return undefined;
95
170
 
@@ -101,21 +176,46 @@ const derivedState = derive(
101
176
  },
102
177
  activeCaipNetworkId: (get): CaipNetworkId | undefined => {
103
178
  const snap = get(baseState);
179
+ const connection = getActiveConnection(snap);
104
180
 
105
- if (!snap.activeNamespace) return undefined;
181
+ return connection?.caipNetwork;
182
+ },
183
+ accountType: (get): AccountType | undefined => {
184
+ const snap = get(baseState);
185
+ const connection = getActiveConnection(snap);
106
186
 
107
- const connection = snap.connections.get(snap.activeNamespace);
187
+ return connection?.type;
188
+ },
189
+ connection: (get): Connection | undefined => {
190
+ const snap = get(baseState);
108
191
 
109
- if (!connection) return undefined;
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
+ }
110
202
 
111
- return connection.caipNetwork;
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
+ );
112
213
  },
113
214
  walletInfo: (get): WalletInfo | undefined => {
114
215
  const snap = get(baseState);
216
+ const connection = getActiveConnection(snap);
115
217
 
116
- if (!snap.activeNamespace) return undefined;
117
-
118
- return snap.connections.get(snap.activeNamespace)?.wallet;
218
+ return connection?.wallet;
119
219
  }
120
220
  },
121
221
  {
@@ -132,28 +232,35 @@ export const ConnectionsController = {
132
232
  StorageUtil.setActiveNamespace(namespace);
133
233
  },
134
234
 
135
- storeConnection({
136
- namespace,
137
- adapter,
235
+ setConnection({
138
236
  accounts,
139
- chains,
140
- wallet,
141
- caipNetwork
237
+ adapter,
238
+ caipNetwork,
239
+ namespace,
240
+ properties,
241
+ wallet
142
242
  }: {
143
- namespace: ChainNamespace;
144
- adapter: BlockchainAdapter;
145
243
  accounts: CaipAddress[];
146
- chains: CaipNetworkId[];
244
+ adapter: BlockchainAdapter;
245
+ caipNetwork: CaipNetworkId;
246
+ namespace: ChainNamespace;
247
+ properties?: ConnectionProperties;
147
248
  wallet?: WalletInfo;
148
- caipNetwork?: CaipNetworkId;
149
249
  }) {
150
- const newConnectionEntry = {
151
- balances: {},
152
- caipNetwork: caipNetwork ?? chains[0]!,
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,
153
259
  adapter: ref(adapter),
154
260
  accounts,
155
- chains,
156
- wallet
261
+ wallet,
262
+ properties,
263
+ type
157
264
  };
158
265
 
159
266
  // Create a new Map to ensure Valtio detects the change
@@ -177,14 +284,62 @@ export const ConnectionsController = {
177
284
  updateBalance(namespace: ChainNamespace, address: CaipAddress, balance: Balance) {
178
285
  const connection = baseState.connections.get(namespace);
179
286
  if (!connection) {
287
+ console.warn(`No connection found for namespace: ${namespace}`);
288
+
180
289
  return;
181
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
+ }
182
298
 
183
- const newBalances = { ...connection.balances, [address]: balance };
184
- const updatedConnection = { ...connection, balances: newBalances };
185
- const newConnectionsMap = new Map(baseState.connections);
186
- newConnectionsMap.set(namespace, updatedConnection);
187
- baseState.connections = newConnectionsMap;
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
+ }
188
343
  },
189
344
 
190
345
  setActiveNetwork(namespace: ChainNamespace, networkId: CaipNetworkId) {
@@ -213,6 +368,16 @@ export const ConnectionsController = {
213
368
  );
214
369
  },
215
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
+
216
381
  async disconnect(namespace: ChainNamespace, isInternal = true) {
217
382
  const connection = baseState.connections.get(namespace);
218
383
  if (!connection) return;
@@ -265,27 +430,114 @@ export const ConnectionsController = {
265
430
  ?.adapter.parseUnits(value, decimals);
266
431
  },
267
432
 
268
- async sendTransaction(args: any) {
433
+ async signMessage(address: CaipAddress, message: string) {
269
434
  if (!baseState.activeNamespace) return undefined;
270
435
 
436
+ const [namespace, chainId, plainAddress] = address.split(':');
437
+
438
+ if (!namespace || namespace !== baseState.activeNamespace || !chainId || !plainAddress) {
439
+ return undefined;
440
+ }
441
+
271
442
  const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
272
443
 
273
- if (adapter instanceof EVMAdapter) {
274
- return adapter.sendTransaction(args);
444
+ if (adapter instanceof EVMAdapter && plainAddress && chainId) {
445
+ return adapter.signMessage(plainAddress, message, chainId);
275
446
  }
276
447
 
277
448
  return undefined;
278
449
  },
279
450
 
280
- async estimateGas(args: any) {
451
+ async sendTransaction(args: any) {
281
452
  if (!baseState.activeNamespace) return undefined;
282
453
 
283
454
  const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
455
+ if (adapter instanceof EVMAdapter || adapter instanceof SolanaBaseAdapter) {
456
+ return adapter.sendTransaction(args);
457
+ }
284
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;
285
466
  if (adapter instanceof EVMAdapter) {
286
467
  return adapter.estimateGas(args);
287
468
  }
288
469
 
289
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
+ });
290
542
  }
291
543
  };
@@ -1,5 +1,5 @@
1
- import { subscribeKey as subKey } from 'valtio/vanilla/utils';
2
- import { proxy, subscribe as sub } from 'valtio/vanilla';
1
+ import { subscribeKey as subKey } from 'valtio/utils';
2
+ import { proxy, subscribe as sub } from 'valtio';
3
3
  import { BlockchainApiController } from './BlockchainApiController';
4
4
  import type { BlockchainApiEnsError } from '../utils/TypeUtil';
5
5
 
@@ -1,4 +1,4 @@
1
- import { proxy, subscribe as sub } from 'valtio/vanilla';
1
+ import { proxy, subscribe as sub } from 'valtio';
2
2
  import { ApiController } from './ApiController';
3
3
  import { OptionsController } from './OptionsController';
4
4
  import { CoreHelperUtil } from '../utils/CoreHelperUtil';
@@ -1,11 +1,9 @@
1
1
  import { proxy } from 'valtio';
2
- import { AccountController } from './AccountController';
3
- import type { RouterControllerState } from './RouterController';
4
- import { RouterController } from './RouterController';
2
+ import { RouterController, type RouterControllerState } from './RouterController';
5
3
  import { PublicStateController } from './PublicStateController';
6
4
  import { EventsController } from './EventsController';
7
5
  import { ApiController } from './ApiController';
8
- import { ConnectorController } from './ConnectorController';
6
+ import { ConnectionsController } from './ConnectionsController';
9
7
 
10
8
  // -- Types --------------------------------------------- //
11
9
  export interface ModalControllerState {
@@ -31,11 +29,11 @@ export const ModalController = {
31
29
 
32
30
  async open(options?: ModalControllerArguments['open']) {
33
31
  await ApiController.state.prefetchPromise;
34
- const connected = AccountController.state.isConnected;
32
+ const isConnected = ConnectionsController.state.isConnected;
35
33
  if (options?.view) {
36
34
  RouterController.reset(options.view);
37
- } else if (AccountController.state.isConnected) {
38
- const isUniversalWallet = ConnectorController.state.connectedConnector === 'AUTH';
35
+ } else if (isConnected) {
36
+ const isUniversalWallet = !!ConnectionsController.state.connection?.properties?.provider;
39
37
  RouterController.reset(isUniversalWallet ? 'Account' : 'AccountDefault');
40
38
  } else {
41
39
  RouterController.reset('Connect');
@@ -45,12 +43,12 @@ export const ModalController = {
45
43
  EventsController.sendEvent({
46
44
  type: 'track',
47
45
  event: 'MODAL_OPEN',
48
- properties: { connected }
46
+ properties: { connected: isConnected }
49
47
  });
50
48
  },
51
49
 
52
50
  close() {
53
- const connected = AccountController.state.isConnected;
51
+ const connected = ConnectionsController.state.isConnected;
54
52
  state.open = false;
55
53
  PublicStateController.set({ open: false });
56
54
  EventsController.sendEvent({