@reown/appkit-core-react-native 0.0.0-accounts-canary.1-20251023174733

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 (234) hide show
  1. package/lib/commonjs/controllers/ApiController.js +410 -0
  2. package/lib/commonjs/controllers/ApiController.js.map +1 -0
  3. package/lib/commonjs/controllers/AssetController.js +30 -0
  4. package/lib/commonjs/controllers/AssetController.js.map +1 -0
  5. package/lib/commonjs/controllers/BlockchainApiController.js +384 -0
  6. package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -0
  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 +37 -0
  10. package/lib/commonjs/controllers/EnsController.js.map +1 -0
  11. package/lib/commonjs/controllers/EventsController.js +122 -0
  12. package/lib/commonjs/controllers/EventsController.js.map +1 -0
  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 +68 -0
  16. package/lib/commonjs/controllers/ModalController.js.map +1 -0
  17. package/lib/commonjs/controllers/OnRampController.js +502 -0
  18. package/lib/commonjs/controllers/OnRampController.js.map +1 -0
  19. package/lib/commonjs/controllers/OptionsController.js +96 -0
  20. package/lib/commonjs/controllers/OptionsController.js.map +1 -0
  21. package/lib/commonjs/controllers/PublicStateController.js +33 -0
  22. package/lib/commonjs/controllers/PublicStateController.js.map +1 -0
  23. package/lib/commonjs/controllers/RouterController.js +63 -0
  24. package/lib/commonjs/controllers/RouterController.js.map +1 -0
  25. package/lib/commonjs/controllers/SendController.js +192 -0
  26. package/lib/commonjs/controllers/SendController.js.map +1 -0
  27. package/lib/commonjs/controllers/SnackController.js +83 -0
  28. package/lib/commonjs/controllers/SnackController.js.map +1 -0
  29. package/lib/commonjs/controllers/SwapController.js +674 -0
  30. package/lib/commonjs/controllers/SwapController.js.map +1 -0
  31. package/lib/commonjs/controllers/ThemeController.js +45 -0
  32. package/lib/commonjs/controllers/ThemeController.js.map +1 -0
  33. package/lib/commonjs/controllers/TransactionsController.js +118 -0
  34. package/lib/commonjs/controllers/TransactionsController.js.map +1 -0
  35. package/lib/commonjs/controllers/WcController.js +73 -0
  36. package/lib/commonjs/controllers/WcController.js.map +1 -0
  37. package/lib/commonjs/features/reown-authentication/ReownAuthentication.js +272 -0
  38. package/lib/commonjs/features/reown-authentication/ReownAuthentication.js.map +1 -0
  39. package/lib/commonjs/features/reown-authentication/ReownAuthenticationMessenger.js +48 -0
  40. package/lib/commonjs/features/reown-authentication/ReownAuthenticationMessenger.js.map +1 -0
  41. package/lib/commonjs/features/reown-authentication/index.js +28 -0
  42. package/lib/commonjs/features/reown-authentication/index.js.map +1 -0
  43. package/lib/commonjs/index.js +202 -0
  44. package/lib/commonjs/index.js.map +1 -0
  45. package/lib/commonjs/package.json +1 -0
  46. package/lib/commonjs/utils/ApiUtil.js +43 -0
  47. package/lib/commonjs/utils/ApiUtil.js.map +1 -0
  48. package/lib/commonjs/utils/AssetUtil.js +27 -0
  49. package/lib/commonjs/utils/AssetUtil.js.map +1 -0
  50. package/lib/commonjs/utils/ConstantsUtil.js +226 -0
  51. package/lib/commonjs/utils/ConstantsUtil.js.map +1 -0
  52. package/lib/commonjs/utils/CoreHelperUtil.js +298 -0
  53. package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -0
  54. package/lib/commonjs/utils/EventUtil.js +25 -0
  55. package/lib/commonjs/utils/EventUtil.js.map +1 -0
  56. package/lib/commonjs/utils/FetchUtil.js +158 -0
  57. package/lib/commonjs/utils/FetchUtil.js.map +1 -0
  58. package/lib/commonjs/utils/LogUtils.js +131 -0
  59. package/lib/commonjs/utils/LogUtils.js.map +1 -0
  60. package/lib/commonjs/utils/StorageUtil.js +319 -0
  61. package/lib/commonjs/utils/StorageUtil.js.map +1 -0
  62. package/lib/commonjs/utils/SwapApiUtil.js +75 -0
  63. package/lib/commonjs/utils/SwapApiUtil.js.map +1 -0
  64. package/lib/commonjs/utils/SwapCalculationUtil.js +97 -0
  65. package/lib/commonjs/utils/SwapCalculationUtil.js.map +1 -0
  66. package/lib/commonjs/utils/WalletUtil.js +23 -0
  67. package/lib/commonjs/utils/WalletUtil.js.map +1 -0
  68. package/lib/module/controllers/ApiController.js +407 -0
  69. package/lib/module/controllers/ApiController.js.map +1 -0
  70. package/lib/module/controllers/AssetController.js +27 -0
  71. package/lib/module/controllers/AssetController.js.map +1 -0
  72. package/lib/module/controllers/BlockchainApiController.js +381 -0
  73. package/lib/module/controllers/BlockchainApiController.js.map +1 -0
  74. package/lib/module/controllers/ConnectionsController.js +422 -0
  75. package/lib/module/controllers/ConnectionsController.js.map +1 -0
  76. package/lib/module/controllers/EnsController.js +34 -0
  77. package/lib/module/controllers/EnsController.js.map +1 -0
  78. package/lib/module/controllers/EventsController.js +118 -0
  79. package/lib/module/controllers/EventsController.js.map +1 -0
  80. package/lib/module/controllers/LogController.js +185 -0
  81. package/lib/module/controllers/LogController.js.map +1 -0
  82. package/lib/module/controllers/ModalController.js +65 -0
  83. package/lib/module/controllers/ModalController.js.map +1 -0
  84. package/lib/module/controllers/OnRampController.js +499 -0
  85. package/lib/module/controllers/OnRampController.js.map +1 -0
  86. package/lib/module/controllers/OptionsController.js +93 -0
  87. package/lib/module/controllers/OptionsController.js.map +1 -0
  88. package/lib/module/controllers/PublicStateController.js +30 -0
  89. package/lib/module/controllers/PublicStateController.js.map +1 -0
  90. package/lib/module/controllers/RouterController.js +60 -0
  91. package/lib/module/controllers/RouterController.js.map +1 -0
  92. package/lib/module/controllers/SendController.js +189 -0
  93. package/lib/module/controllers/SendController.js.map +1 -0
  94. package/lib/module/controllers/SnackController.js +80 -0
  95. package/lib/module/controllers/SnackController.js.map +1 -0
  96. package/lib/module/controllers/SwapController.js +671 -0
  97. package/lib/module/controllers/SwapController.js.map +1 -0
  98. package/lib/module/controllers/ThemeController.js +42 -0
  99. package/lib/module/controllers/ThemeController.js.map +1 -0
  100. package/lib/module/controllers/TransactionsController.js +115 -0
  101. package/lib/module/controllers/TransactionsController.js.map +1 -0
  102. package/lib/module/controllers/WcController.js +70 -0
  103. package/lib/module/controllers/WcController.js.map +1 -0
  104. package/lib/module/features/reown-authentication/ReownAuthentication.js +268 -0
  105. package/lib/module/features/reown-authentication/ReownAuthentication.js.map +1 -0
  106. package/lib/module/features/reown-authentication/ReownAuthenticationMessenger.js +43 -0
  107. package/lib/module/features/reown-authentication/ReownAuthenticationMessenger.js.map +1 -0
  108. package/lib/module/features/reown-authentication/index.js +5 -0
  109. package/lib/module/features/reown-authentication/index.js.map +1 -0
  110. package/lib/module/index.js +38 -0
  111. package/lib/module/index.js.map +1 -0
  112. package/lib/module/utils/ApiUtil.js +39 -0
  113. package/lib/module/utils/ApiUtil.js.map +1 -0
  114. package/lib/module/utils/AssetUtil.js +23 -0
  115. package/lib/module/utils/AssetUtil.js.map +1 -0
  116. package/lib/module/utils/ConstantsUtil.js +222 -0
  117. package/lib/module/utils/ConstantsUtil.js.map +1 -0
  118. package/lib/module/utils/CoreHelperUtil.js +294 -0
  119. package/lib/module/utils/CoreHelperUtil.js.map +1 -0
  120. package/lib/module/utils/EventUtil.js +21 -0
  121. package/lib/module/utils/EventUtil.js.map +1 -0
  122. package/lib/module/utils/FetchUtil.js +154 -0
  123. package/lib/module/utils/FetchUtil.js.map +1 -0
  124. package/lib/module/utils/LogUtils.js +121 -0
  125. package/lib/module/utils/LogUtils.js.map +1 -0
  126. package/lib/module/utils/StorageUtil.js +315 -0
  127. package/lib/module/utils/StorageUtil.js.map +1 -0
  128. package/lib/module/utils/SwapApiUtil.js +71 -0
  129. package/lib/module/utils/SwapApiUtil.js.map +1 -0
  130. package/lib/module/utils/SwapCalculationUtil.js +94 -0
  131. package/lib/module/utils/SwapCalculationUtil.js.map +1 -0
  132. package/lib/module/utils/WalletUtil.js +19 -0
  133. package/lib/module/utils/WalletUtil.js.map +1 -0
  134. package/lib/typescript/controllers/ApiController.d.ts +40 -0
  135. package/lib/typescript/controllers/ApiController.d.ts.map +1 -0
  136. package/lib/typescript/controllers/AssetController.d.ts +12 -0
  137. package/lib/typescript/controllers/AssetController.d.ts.map +1 -0
  138. package/lib/typescript/controllers/BlockchainApiController.d.ts +55 -0
  139. package/lib/typescript/controllers/BlockchainApiController.d.ts.map +1 -0
  140. package/lib/typescript/controllers/ConnectionsController.d.ts +47 -0
  141. package/lib/typescript/controllers/ConnectionsController.d.ts.map +1 -0
  142. package/lib/typescript/controllers/EnsController.d.ts +10 -0
  143. package/lib/typescript/controllers/EnsController.d.ts.map +1 -0
  144. package/lib/typescript/controllers/EventsController.d.ts +25 -0
  145. package/lib/typescript/controllers/EventsController.d.ts.map +1 -0
  146. package/lib/typescript/controllers/LogController.d.ts +65 -0
  147. package/lib/typescript/controllers/LogController.d.ts.map +1 -0
  148. package/lib/typescript/controllers/ModalController.d.ts +17 -0
  149. package/lib/typescript/controllers/ModalController.d.ts.map +1 -0
  150. package/lib/typescript/controllers/OnRampController.d.ts +55 -0
  151. package/lib/typescript/controllers/OnRampController.d.ts.map +1 -0
  152. package/lib/typescript/controllers/OptionsController.d.ts +46 -0
  153. package/lib/typescript/controllers/OptionsController.d.ts.map +1 -0
  154. package/lib/typescript/controllers/PublicStateController.d.ts +12 -0
  155. package/lib/typescript/controllers/PublicStateController.d.ts.map +1 -0
  156. package/lib/typescript/controllers/RouterController.d.ts +21 -0
  157. package/lib/typescript/controllers/RouterController.d.ts.map +1 -0
  158. package/lib/typescript/controllers/SendController.d.ts +39 -0
  159. package/lib/typescript/controllers/SendController.d.ts.map +1 -0
  160. package/lib/typescript/controllers/SnackController.d.ts +20 -0
  161. package/lib/typescript/controllers/SnackController.d.ts.map +1 -0
  162. package/lib/typescript/controllers/SwapController.d.ts +116 -0
  163. package/lib/typescript/controllers/SwapController.d.ts.map +1 -0
  164. package/lib/typescript/controllers/ThemeController.d.ts +14 -0
  165. package/lib/typescript/controllers/ThemeController.d.ts.map +1 -0
  166. package/lib/typescript/controllers/TransactionsController.d.ts +21 -0
  167. package/lib/typescript/controllers/TransactionsController.d.ts.map +1 -0
  168. package/lib/typescript/controllers/WcController.d.ts +27 -0
  169. package/lib/typescript/controllers/WcController.d.ts.map +1 -0
  170. package/lib/typescript/features/reown-authentication/ReownAuthentication.d.ts +174 -0
  171. package/lib/typescript/features/reown-authentication/ReownAuthentication.d.ts.map +1 -0
  172. package/lib/typescript/features/reown-authentication/ReownAuthenticationMessenger.d.ts +16 -0
  173. package/lib/typescript/features/reown-authentication/ReownAuthenticationMessenger.d.ts.map +1 -0
  174. package/lib/typescript/features/reown-authentication/index.d.ts +3 -0
  175. package/lib/typescript/features/reown-authentication/index.d.ts.map +1 -0
  176. package/lib/typescript/index.d.ts +29 -0
  177. package/lib/typescript/index.d.ts.map +1 -0
  178. package/lib/typescript/utils/ApiUtil.d.ts +7 -0
  179. package/lib/typescript/utils/ApiUtil.d.ts.map +1 -0
  180. package/lib/typescript/utils/AssetUtil.d.ts +6 -0
  181. package/lib/typescript/utils/AssetUtil.d.ts.map +1 -0
  182. package/lib/typescript/utils/ConstantsUtil.d.ts +79 -0
  183. package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -0
  184. package/lib/typescript/utils/CoreHelperUtil.d.ts +43 -0
  185. package/lib/typescript/utils/CoreHelperUtil.d.ts.map +1 -0
  186. package/lib/typescript/utils/EventUtil.d.ts +5 -0
  187. package/lib/typescript/utils/EventUtil.d.ts.map +1 -0
  188. package/lib/typescript/utils/FetchUtil.d.ts +29 -0
  189. package/lib/typescript/utils/FetchUtil.d.ts.map +1 -0
  190. package/lib/typescript/utils/LogUtils.d.ts +15 -0
  191. package/lib/typescript/utils/LogUtils.d.ts.map +1 -0
  192. package/lib/typescript/utils/StorageUtil.d.ts +36 -0
  193. package/lib/typescript/utils/StorageUtil.d.ts.map +1 -0
  194. package/lib/typescript/utils/SwapApiUtil.d.ts +11 -0
  195. package/lib/typescript/utils/SwapApiUtil.d.ts.map +1 -0
  196. package/lib/typescript/utils/SwapCalculationUtil.d.ts +24 -0
  197. package/lib/typescript/utils/SwapCalculationUtil.d.ts.map +1 -0
  198. package/lib/typescript/utils/WalletUtil.d.ts +5 -0
  199. package/lib/typescript/utils/WalletUtil.d.ts.map +1 -0
  200. package/package.json +57 -0
  201. package/readme.md +9 -0
  202. package/src/controllers/ApiController.ts +454 -0
  203. package/src/controllers/AssetController.ts +32 -0
  204. package/src/controllers/BlockchainApiController.ts +418 -0
  205. package/src/controllers/ConnectionsController.ts +575 -0
  206. package/src/controllers/EnsController.ts +41 -0
  207. package/src/controllers/EventsController.ts +146 -0
  208. package/src/controllers/LogController.ts +250 -0
  209. package/src/controllers/ModalController.ts +68 -0
  210. package/src/controllers/OnRampController.ts +698 -0
  211. package/src/controllers/OptionsController.ts +143 -0
  212. package/src/controllers/PublicStateController.ts +37 -0
  213. package/src/controllers/RouterController.ts +112 -0
  214. package/src/controllers/SendController.ts +270 -0
  215. package/src/controllers/SnackController.ts +95 -0
  216. package/src/controllers/SwapController.ts +863 -0
  217. package/src/controllers/ThemeController.ts +47 -0
  218. package/src/controllers/TransactionsController.ts +150 -0
  219. package/src/controllers/WcController.ts +93 -0
  220. package/src/features/reown-authentication/ReownAuthentication.ts +475 -0
  221. package/src/features/reown-authentication/ReownAuthenticationMessenger.ts +80 -0
  222. package/src/features/reown-authentication/index.ts +2 -0
  223. package/src/index.ts +70 -0
  224. package/src/utils/ApiUtil.ts +50 -0
  225. package/src/utils/AssetUtil.ts +27 -0
  226. package/src/utils/ConstantsUtil.ts +303 -0
  227. package/src/utils/CoreHelperUtil.ts +377 -0
  228. package/src/utils/EventUtil.ts +22 -0
  229. package/src/utils/FetchUtil.ts +164 -0
  230. package/src/utils/LogUtils.ts +179 -0
  231. package/src/utils/StorageUtil.ts +405 -0
  232. package/src/utils/SwapApiUtil.ts +101 -0
  233. package/src/utils/SwapCalculationUtil.ts +145 -0
  234. package/src/utils/WalletUtil.ts +14 -0
@@ -0,0 +1,405 @@
1
+ /* eslint-disable no-console */
2
+ import {
3
+ type OnRampCountry,
4
+ type OnRampFiatCurrency,
5
+ type OnRampFiatLimit,
6
+ type OnRampServiceProvider,
7
+ type OnRampCountryDefaults,
8
+ type WcWallet,
9
+ DateUtil,
10
+ type ConnectorType,
11
+ type ChainNamespace,
12
+ type WalletDeepLink,
13
+ SafeStorageKeys
14
+ } from '@reown/appkit-common-react-native';
15
+ import { OptionsController } from '../controllers/OptionsController';
16
+ import { LogController } from '../controllers/LogController';
17
+
18
+ // -- Utility -----------------------------------------------------------------
19
+ export const StorageUtil = {
20
+ setWalletConnectDeepLink({ href, name }: WalletDeepLink) {
21
+ try {
22
+ OptionsController.getStorage().setItem(SafeStorageKeys.WC_DEEPLINK, {
23
+ href,
24
+ name
25
+ });
26
+ } catch (error) {
27
+ LogController.sendError(error, 'StorageUtil.ts', 'setWalletConnectDeepLink');
28
+ }
29
+ },
30
+
31
+ async getWalletConnectDeepLink() {
32
+ try {
33
+ const deepLink = await OptionsController.getStorage().getItem<WalletDeepLink>(
34
+ SafeStorageKeys.WC_DEEPLINK
35
+ );
36
+ if (deepLink) {
37
+ return deepLink;
38
+ }
39
+ } catch (error) {
40
+ LogController.sendError(error, 'StorageUtil.ts', 'getWalletConnectDeepLink');
41
+ }
42
+
43
+ return undefined;
44
+ },
45
+
46
+ async removeWalletConnectDeepLink() {
47
+ try {
48
+ await OptionsController.getStorage().removeItem(SafeStorageKeys.WC_DEEPLINK);
49
+ } catch (error) {
50
+ LogController.sendError(error, 'StorageUtil.ts', 'removeWalletConnectDeepLink');
51
+ }
52
+ },
53
+
54
+ async addRecentWallet(wallet: WcWallet) {
55
+ try {
56
+ const recentWallets = await StorageUtil.getRecentWallets();
57
+ const recentIndex = recentWallets.findIndex(w => w.id === wallet.id);
58
+
59
+ if (recentIndex > -1) {
60
+ recentWallets.splice(recentIndex, 1);
61
+ }
62
+
63
+ recentWallets.unshift(wallet);
64
+ if (recentWallets.length > 2) {
65
+ recentWallets.pop();
66
+ }
67
+ OptionsController.getStorage().setItem(SafeStorageKeys.RECENT_WALLET, recentWallets);
68
+
69
+ return recentWallets;
70
+ } catch (error) {
71
+ LogController.sendError(error, 'StorageUtil.ts', 'addRecentWallet');
72
+
73
+ return undefined;
74
+ }
75
+ },
76
+
77
+ async setRecentWallets(wallets: WcWallet[]) {
78
+ try {
79
+ await OptionsController.getStorage().setItem(SafeStorageKeys.RECENT_WALLET, wallets);
80
+ } catch (error) {
81
+ LogController.sendError(error, 'StorageUtil.ts', 'setRecentWallets');
82
+ }
83
+ },
84
+
85
+ async getRecentWallets(): Promise<WcWallet[]> {
86
+ try {
87
+ const recent = await OptionsController.getStorage().getItem(SafeStorageKeys.RECENT_WALLET);
88
+
89
+ return recent ?? [];
90
+ } catch (error) {
91
+ LogController.sendError(error, 'StorageUtil.ts', 'getRecentWallets');
92
+ }
93
+
94
+ return [];
95
+ },
96
+
97
+ async setConnectedConnectors({
98
+ type,
99
+ namespaces
100
+ }: {
101
+ type: ConnectorType;
102
+ namespaces: string[];
103
+ }) {
104
+ try {
105
+ const currentConnectors = (await StorageUtil.getConnectedConnectors()) || [];
106
+ // Only add if it doesn't exist already
107
+ if (!currentConnectors.some(c => c.type === type)) {
108
+ const updatedConnectors = [...currentConnectors, { type, namespaces }];
109
+ await OptionsController.getStorage().setItem(
110
+ SafeStorageKeys.CONNECTED_CONNECTORS,
111
+ updatedConnectors
112
+ );
113
+ }
114
+ } catch (error) {
115
+ LogController.sendError(error, 'StorageUtil.ts', 'setConnectedConnectors');
116
+ }
117
+ },
118
+
119
+ async getConnectedConnectors(): Promise<{ type: ConnectorType; namespaces: string[] }[]> {
120
+ try {
121
+ const connectors = await OptionsController.getStorage().getItem<
122
+ { type: ConnectorType; namespaces: string[] }[]
123
+ >(SafeStorageKeys.CONNECTED_CONNECTORS);
124
+
125
+ return connectors ?? [];
126
+ } catch (err) {
127
+ LogController.sendError(err, 'StorageUtil.ts', 'getConnectedConnectors');
128
+ }
129
+
130
+ return [];
131
+ },
132
+
133
+ async removeConnectedConnectors(type: ConnectorType) {
134
+ try {
135
+ const currentConnectors = await StorageUtil.getConnectedConnectors();
136
+ const updatedConnectors = currentConnectors.filter(c => c.type !== type);
137
+ await OptionsController.getStorage().setItem(
138
+ SafeStorageKeys.CONNECTED_CONNECTORS,
139
+ updatedConnectors
140
+ );
141
+ } catch (error) {
142
+ LogController.sendError(error, 'StorageUtil.ts', 'removeConnectedConnectors');
143
+ }
144
+ },
145
+
146
+ async setOnRampPreferredCountry(country: OnRampCountry) {
147
+ try {
148
+ await OptionsController.getStorage().setItem(
149
+ SafeStorageKeys.ONRAMP_PREFERRED_COUNTRY,
150
+ country
151
+ );
152
+ } catch (error) {
153
+ LogController.sendError(error, 'StorageUtil.ts', 'setOnRampPreferredCountry');
154
+ }
155
+ },
156
+
157
+ async getOnRampPreferredCountry() {
158
+ try {
159
+ const country = await OptionsController.getStorage().getItem<OnRampCountry>(
160
+ SafeStorageKeys.ONRAMP_PREFERRED_COUNTRY
161
+ );
162
+
163
+ return country ?? undefined;
164
+ } catch {
165
+ console.info('Unable to get OnRamp Preferred Country');
166
+ }
167
+
168
+ return undefined;
169
+ },
170
+
171
+ async setOnRampPreferredFiatCurrency(currency: OnRampFiatCurrency) {
172
+ try {
173
+ await OptionsController.getStorage().setItem(
174
+ SafeStorageKeys.ONRAMP_PREFERRED_FIAT_CURRENCY,
175
+ currency
176
+ );
177
+ } catch (error) {
178
+ LogController.sendError(error, 'StorageUtil.ts', 'setOnRampPreferredFiatCurrency');
179
+ }
180
+ },
181
+
182
+ async getOnRampPreferredFiatCurrency() {
183
+ try {
184
+ const currency = await OptionsController.getStorage().getItem<OnRampFiatCurrency>(
185
+ SafeStorageKeys.ONRAMP_PREFERRED_FIAT_CURRENCY
186
+ );
187
+
188
+ return currency ?? undefined;
189
+ } catch (error) {
190
+ LogController.sendError(error, 'StorageUtil.ts', 'getOnRampPreferredFiatCurrency');
191
+ }
192
+
193
+ return undefined;
194
+ },
195
+
196
+ async setOnRampCountries(countries: OnRampCountry[]) {
197
+ try {
198
+ await OptionsController.getStorage().setItem(SafeStorageKeys.ONRAMP_COUNTRIES, countries);
199
+ } catch (error) {
200
+ LogController.sendError(error, 'StorageUtil.ts', 'setOnRampCountries');
201
+ }
202
+ },
203
+
204
+ async getOnRampCountries() {
205
+ try {
206
+ const countries = await OptionsController.getStorage().getItem<OnRampCountry[]>(
207
+ SafeStorageKeys.ONRAMP_COUNTRIES
208
+ );
209
+
210
+ return countries ?? [];
211
+ } catch (error) {
212
+ LogController.sendError(error, 'StorageUtil.ts', 'getOnRampCountries');
213
+ }
214
+
215
+ return [];
216
+ },
217
+
218
+ async setOnRampCountriesDefaults(countriesDefaults: OnRampCountryDefaults[]) {
219
+ try {
220
+ const timestamp = Date.now();
221
+
222
+ await OptionsController.getStorage().setItem(SafeStorageKeys.ONRAMP_COUNTRIES_DEFAULTS, {
223
+ data: countriesDefaults,
224
+ timestamp
225
+ });
226
+ } catch (error) {
227
+ LogController.sendError(error, 'StorageUtil.ts', 'setOnRampCountriesDefaults');
228
+ }
229
+ },
230
+
231
+ async getOnRampCountriesDefaults() {
232
+ try {
233
+ const result = await OptionsController.getStorage().getItem(
234
+ SafeStorageKeys.ONRAMP_COUNTRIES_DEFAULTS
235
+ );
236
+
237
+ if (!result) {
238
+ return [];
239
+ }
240
+
241
+ const { data, timestamp } = result;
242
+
243
+ // Cache for 1 week
244
+ if (timestamp && DateUtil.isMoreThanOneWeekAgo(timestamp)) {
245
+ return [];
246
+ }
247
+
248
+ return (data as OnRampCountryDefaults[]) ?? [];
249
+ } catch (error) {
250
+ LogController.sendError(error, 'StorageUtil.ts', 'getOnRampCountriesDefaults');
251
+ }
252
+
253
+ return [];
254
+ },
255
+
256
+ async setOnRampServiceProviders(serviceProviders: OnRampServiceProvider[]) {
257
+ try {
258
+ const timestamp = Date.now();
259
+
260
+ await OptionsController.getStorage().setItem(SafeStorageKeys.ONRAMP_SERVICE_PROVIDERS, {
261
+ data: serviceProviders,
262
+ timestamp
263
+ });
264
+ } catch (error) {
265
+ LogController.sendError(error, 'StorageUtil.ts', 'setOnRampServiceProviders');
266
+ }
267
+ },
268
+
269
+ async getOnRampServiceProviders() {
270
+ try {
271
+ const result = await OptionsController.getStorage().getItem(
272
+ SafeStorageKeys.ONRAMP_SERVICE_PROVIDERS
273
+ );
274
+
275
+ if (!result) {
276
+ return [];
277
+ }
278
+
279
+ const { data, timestamp } = result;
280
+
281
+ // Cache for 1 week
282
+ if (timestamp && DateUtil.isMoreThanOneWeekAgo(timestamp)) {
283
+ return [];
284
+ }
285
+
286
+ return (data as OnRampServiceProvider[]) ?? [];
287
+ } catch (err) {
288
+ LogController.sendError(err, 'StorageUtil.ts', 'getOnRampServiceProviders');
289
+ }
290
+
291
+ return [];
292
+ },
293
+
294
+ async setOnRampFiatLimits(fiatLimits: OnRampFiatLimit[]) {
295
+ try {
296
+ const timestamp = Date.now();
297
+
298
+ await OptionsController.getStorage().setItem(SafeStorageKeys.ONRAMP_FIAT_LIMITS, {
299
+ data: fiatLimits,
300
+ timestamp
301
+ });
302
+ } catch (error) {
303
+ LogController.sendError(error, 'StorageUtil.ts', 'setOnRampFiatLimits');
304
+ }
305
+ },
306
+
307
+ async getOnRampFiatLimits() {
308
+ try {
309
+ const result = await OptionsController.getStorage().getItem(
310
+ SafeStorageKeys.ONRAMP_FIAT_LIMITS
311
+ );
312
+
313
+ if (!result) {
314
+ return [];
315
+ }
316
+
317
+ const { data, timestamp } = result;
318
+
319
+ // Cache for 1 week
320
+ if (timestamp && DateUtil.isMoreThanOneWeekAgo(timestamp)) {
321
+ return [];
322
+ }
323
+
324
+ return (data as OnRampFiatLimit[]) ?? [];
325
+ } catch (error) {
326
+ LogController.sendError(error, 'StorageUtil.ts', 'getOnRampFiatLimits');
327
+ }
328
+
329
+ return [];
330
+ },
331
+
332
+ async setOnRampFiatCurrencies(fiatCurrencies: OnRampFiatCurrency[]) {
333
+ try {
334
+ const timestamp = Date.now();
335
+
336
+ await OptionsController.getStorage().setItem(SafeStorageKeys.ONRAMP_FIAT_CURRENCIES, {
337
+ data: fiatCurrencies,
338
+ timestamp
339
+ });
340
+ } catch (error) {
341
+ LogController.sendError(error, 'StorageUtil.ts', 'setOnRampFiatCurrencies');
342
+ }
343
+ },
344
+
345
+ async getOnRampFiatCurrencies() {
346
+ try {
347
+ const result = await OptionsController.getStorage().getItem(
348
+ SafeStorageKeys.ONRAMP_FIAT_CURRENCIES
349
+ );
350
+
351
+ if (!result) {
352
+ return [];
353
+ }
354
+
355
+ const { data, timestamp } = result;
356
+
357
+ // Cache for 1 week
358
+ if (timestamp && DateUtil.isMoreThanOneWeekAgo(timestamp)) {
359
+ return [];
360
+ }
361
+
362
+ return (data as OnRampFiatCurrency[]) ?? [];
363
+ } catch (error) {
364
+ LogController.sendError(error, 'StorageUtil.ts', 'getOnRampFiatCurrencies');
365
+ }
366
+
367
+ return [];
368
+ },
369
+
370
+ async setActiveNamespace(namespace?: ChainNamespace) {
371
+ try {
372
+ if (!namespace) {
373
+ await OptionsController.getStorage().removeItem(SafeStorageKeys.ACTIVE_NAMESPACE);
374
+
375
+ return;
376
+ }
377
+
378
+ await OptionsController.getStorage().setItem(SafeStorageKeys.ACTIVE_NAMESPACE, namespace);
379
+ } catch (error) {
380
+ LogController.sendError(error, 'StorageUtil.ts', 'setActiveNamespace');
381
+ }
382
+ },
383
+
384
+ async getActiveNamespace() {
385
+ try {
386
+ const namespace = (await OptionsController.getStorage().getItem(
387
+ SafeStorageKeys.ACTIVE_NAMESPACE
388
+ )) as ChainNamespace;
389
+
390
+ return namespace ?? undefined;
391
+ } catch (err) {
392
+ LogController.sendError(err, 'StorageUtil.ts', 'getActiveNamespace');
393
+ }
394
+
395
+ return undefined;
396
+ },
397
+
398
+ async removeActiveNamespace() {
399
+ try {
400
+ await OptionsController.getStorage().removeItem(SafeStorageKeys.ACTIVE_NAMESPACE);
401
+ } catch (error) {
402
+ LogController.sendError(error, 'StorageUtil.ts', 'removeActiveNamespace');
403
+ }
404
+ }
405
+ };
@@ -0,0 +1,101 @@
1
+ import type {
2
+ Balance,
3
+ CaipNetworkId,
4
+ BlockchainApiSwapAllowanceRequest,
5
+ SwapTokenWithBalance
6
+ } from '@reown/appkit-common-react-native';
7
+ import { BlockchainApiController } from '../controllers/BlockchainApiController';
8
+ import { OptionsController } from '../controllers/OptionsController';
9
+ import { ConnectionsController } from '../controllers/ConnectionsController';
10
+ import { ConstantsUtil } from './ConstantsUtil';
11
+
12
+ export const SwapApiUtil = {
13
+ async getTokenList() {
14
+ const chainId: CaipNetworkId =
15
+ ConnectionsController.state.activeNetwork?.caipNetworkId ?? 'eip155:1';
16
+ const response = await BlockchainApiController.fetchSwapTokens({
17
+ projectId: OptionsController.state.projectId,
18
+ chainId
19
+ });
20
+ const tokens =
21
+ response?.tokens?.map(
22
+ token =>
23
+ ({
24
+ ...token,
25
+ eip2612: false,
26
+ quantity: {
27
+ decimals: '0',
28
+ numeric: '0'
29
+ },
30
+ price: 0,
31
+ value: 0
32
+ }) as SwapTokenWithBalance
33
+ ) || [];
34
+
35
+ return tokens;
36
+ },
37
+
38
+ async fetchSwapAllowance({
39
+ tokenAddress,
40
+ userAddress,
41
+ sourceTokenAmount,
42
+ sourceTokenDecimals
43
+ }: Pick<BlockchainApiSwapAllowanceRequest, 'tokenAddress' | 'userAddress'> & {
44
+ sourceTokenAmount: string;
45
+ sourceTokenDecimals: number;
46
+ }) {
47
+ const projectId = OptionsController.state.projectId;
48
+
49
+ const response = await BlockchainApiController.fetchSwapAllowance({
50
+ projectId,
51
+ tokenAddress,
52
+ userAddress
53
+ });
54
+
55
+ if (response?.allowance && sourceTokenAmount && sourceTokenDecimals) {
56
+ const parsedValue =
57
+ ConnectionsController.parseUnits(sourceTokenAmount, sourceTokenDecimals) || 0;
58
+ const hasAllowance = BigInt(response.allowance) >= parsedValue;
59
+
60
+ return hasAllowance;
61
+ }
62
+
63
+ return false;
64
+ },
65
+
66
+ mapBalancesToSwapTokens(balances?: Balance[]) {
67
+ const { activeNamespace, activeCaipNetworkId } = ConnectionsController.state;
68
+ const address = activeNamespace
69
+ ? ConstantsUtil.NATIVE_TOKEN_ADDRESS[activeNamespace]
70
+ : undefined;
71
+
72
+ return (
73
+ balances
74
+ ?.filter(balance => balance?.quantity?.numeric)
75
+ .map(
76
+ token =>
77
+ ({
78
+ ...token,
79
+ address: token?.address ?? `${token?.chainId ?? activeCaipNetworkId}:${address}`,
80
+ decimals: parseInt(token.quantity?.decimals ?? '0', 10),
81
+ logoUri: token.iconUrl,
82
+ eip2612: false
83
+ }) as SwapTokenWithBalance
84
+ ) || []
85
+ );
86
+ },
87
+
88
+ async fetchGasPrice() {
89
+ const projectId = OptionsController.state.projectId;
90
+ const caipNetwork = ConnectionsController.state.activeNetwork;
91
+
92
+ if (!caipNetwork) {
93
+ return null;
94
+ }
95
+
96
+ return await BlockchainApiController.fetchGasPrice({
97
+ projectId,
98
+ chainId: caipNetwork.caipNetworkId
99
+ });
100
+ }
101
+ };
@@ -0,0 +1,145 @@
1
+ // -- Types --------------------------------------------- //
2
+
3
+ import { NumberUtil, type SwapTokenWithBalance } from '@reown/appkit-common-react-native';
4
+
5
+ // -- Util ---------------------------------------- //
6
+ export const SwapCalculationUtil = {
7
+ getGasPriceInEther(gas: bigint, gasPrice: bigint) {
8
+ const totalGasCostInWei = gasPrice * gas;
9
+ const totalGasCostInEther = Number(totalGasCostInWei) / 1e18;
10
+
11
+ return totalGasCostInEther;
12
+ },
13
+
14
+ getGasPriceInUSD(networkPrice: string, gas: bigint, gasPrice: bigint) {
15
+ const totalGasCostInEther = SwapCalculationUtil.getGasPriceInEther(gas, gasPrice);
16
+ const networkPriceInUSD = NumberUtil.bigNumber(networkPrice);
17
+ const gasCostInUSD = networkPriceInUSD.multipliedBy(totalGasCostInEther);
18
+
19
+ return gasCostInUSD.toNumber();
20
+ },
21
+
22
+ getPriceImpact({
23
+ sourceTokenAmount,
24
+ sourceTokenPriceInUSD,
25
+ toTokenPriceInUSD,
26
+ toTokenAmount
27
+ }: {
28
+ sourceTokenAmount: string;
29
+ sourceTokenPriceInUSD: number;
30
+ toTokenPriceInUSD: number;
31
+ toTokenAmount: string;
32
+ }) {
33
+ const inputValue = NumberUtil.bigNumber(sourceTokenAmount).multipliedBy(sourceTokenPriceInUSD);
34
+ const outputValue = NumberUtil.bigNumber(toTokenAmount).multipliedBy(toTokenPriceInUSD);
35
+ const priceImpact = inputValue.minus(outputValue).dividedBy(inputValue).multipliedBy(100);
36
+
37
+ return priceImpact.toNumber();
38
+ },
39
+
40
+ getMaxSlippage(slippage: number, toTokenAmount: string) {
41
+ const slippageToleranceDecimal = NumberUtil.bigNumber(slippage).dividedBy(100);
42
+ const maxSlippageAmount = NumberUtil.multiply(toTokenAmount, slippageToleranceDecimal);
43
+
44
+ return maxSlippageAmount.toNumber();
45
+ },
46
+
47
+ getProviderFee(sourceTokenAmount: string, feePercentage = 0.0085) {
48
+ const providerFee = NumberUtil.bigNumber(sourceTokenAmount).multipliedBy(feePercentage);
49
+
50
+ return providerFee.toString();
51
+ },
52
+
53
+ getProviderFeePrice(
54
+ sourceTokenAmount: string,
55
+ sourceTokenPriceInUSD: number,
56
+ feePercentage = 0.0085
57
+ ) {
58
+ const providerFee = SwapCalculationUtil.getProviderFee(sourceTokenAmount, feePercentage);
59
+ const providerFeePrice = NumberUtil.bigNumber(providerFee).multipliedBy(sourceTokenPriceInUSD);
60
+
61
+ return providerFeePrice.toNumber();
62
+ },
63
+
64
+ isInsufficientNetworkTokenForGas(networkBalanceInUSD: string, gasPriceInUSD: number | undefined) {
65
+ const gasPrice = gasPriceInUSD ?? '0';
66
+
67
+ if (NumberUtil.bigNumber(networkBalanceInUSD).isZero()) {
68
+ return true;
69
+ }
70
+
71
+ return NumberUtil.bigNumber(NumberUtil.bigNumber(gasPrice)).isGreaterThan(networkBalanceInUSD);
72
+ },
73
+
74
+ isInsufficientSourceTokenForSwap(
75
+ sourceTokenAmount: string,
76
+ sourceTokenAddress: string,
77
+ balance: SwapTokenWithBalance[] | undefined
78
+ ) {
79
+ const sourceTokenBalance = balance?.find(token => token.address === sourceTokenAddress)
80
+ ?.quantity?.numeric;
81
+
82
+ const isInSufficientBalance = NumberUtil.bigNumber(sourceTokenBalance ?? '0').isLessThan(
83
+ sourceTokenAmount
84
+ );
85
+
86
+ return isInSufficientBalance;
87
+ },
88
+
89
+ getToTokenAmount({
90
+ sourceToken,
91
+ toToken,
92
+ sourceTokenPrice,
93
+ toTokenPrice,
94
+ sourceTokenAmount
95
+ }: {
96
+ sourceToken: SwapTokenWithBalance | undefined;
97
+ toToken: SwapTokenWithBalance | undefined;
98
+ sourceTokenPrice: number;
99
+ toTokenPrice: number;
100
+ sourceTokenAmount: string;
101
+ }) {
102
+ if (sourceTokenAmount === '0') {
103
+ return '0';
104
+ }
105
+
106
+ if (!sourceToken || !toToken) {
107
+ return '0';
108
+ }
109
+
110
+ const sourceTokenDecimals = sourceToken.decimals;
111
+ const sourceTokenPriceInUSD = sourceTokenPrice;
112
+ const toTokenDecimals = toToken.decimals;
113
+ const toTokenPriceInUSD = toTokenPrice;
114
+
115
+ if (toTokenPriceInUSD <= 0) {
116
+ return '0';
117
+ }
118
+
119
+ // Calculate the provider fee (0.85% of the source token amount)
120
+ const providerFee = NumberUtil.bigNumber(sourceTokenAmount).multipliedBy(0.0085);
121
+
122
+ // Adjust the source token amount by subtracting the provider fee
123
+ const adjustedSourceTokenAmount = NumberUtil.bigNumber(sourceTokenAmount).minus(providerFee);
124
+
125
+ // Proceed with conversion using the adjusted source token amount
126
+ const sourceAmountInSmallestUnit = adjustedSourceTokenAmount.multipliedBy(
127
+ NumberUtil.bigNumber(10).pow(sourceTokenDecimals)
128
+ );
129
+
130
+ const priceRatio = NumberUtil.bigNumber(sourceTokenPriceInUSD).dividedBy(toTokenPriceInUSD);
131
+
132
+ const decimalDifference = sourceTokenDecimals - toTokenDecimals;
133
+ const toTokenAmountInSmallestUnit = sourceAmountInSmallestUnit
134
+ .multipliedBy(priceRatio)
135
+ .dividedBy(NumberUtil.bigNumber(10).pow(decimalDifference));
136
+
137
+ const toTokenAmount = toTokenAmountInSmallestUnit.dividedBy(
138
+ NumberUtil.bigNumber(10).pow(toTokenDecimals)
139
+ );
140
+
141
+ const amount = toTokenAmount.toFixed(toTokenDecimals).toString();
142
+
143
+ return amount;
144
+ }
145
+ };
@@ -0,0 +1,14 @@
1
+ import type { WcWallet } from '@reown/appkit-common-react-native';
2
+ import { ApiController } from '../controllers/ApiController';
3
+ import { OptionsController } from '../controllers/OptionsController';
4
+
5
+ export const WalletUtil = {
6
+ getWallet: (walletId: string): WcWallet | undefined => {
7
+ const { wallets, recommended, featured, installed } = ApiController.state;
8
+ const customWallets = OptionsController.state.customWallets ?? [];
9
+ const allWallets = [...wallets, ...recommended, ...featured, ...installed, ...customWallets];
10
+ const wallet = allWallets.find(w => w.id === walletId);
11
+
12
+ return wallet;
13
+ }
14
+ };