@reown/appkit-core-react-native 0.0.0-develop-20250728153935 → 0.0.0-develop-20251008155354

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