@reown/appkit-core-react-native 1.2.3 → 2.0.0-alpha.0

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 (144) hide show
  1. package/lib/commonjs/controllers/ApiController.js +11 -11
  2. package/lib/commonjs/controllers/ApiController.js.map +1 -1
  3. package/lib/commonjs/controllers/BlockchainApiController.js +187 -14
  4. package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -1
  5. package/lib/commonjs/controllers/ConnectionController.js +7 -1
  6. package/lib/commonjs/controllers/ConnectionController.js.map +1 -1
  7. package/lib/commonjs/controllers/ConnectionsController.js +156 -0
  8. package/lib/commonjs/controllers/ConnectionsController.js.map +1 -0
  9. package/lib/commonjs/controllers/ConnectorController.js +1 -0
  10. package/lib/commonjs/controllers/ConnectorController.js.map +1 -1
  11. package/lib/commonjs/controllers/OnRampController.js +421 -0
  12. package/lib/commonjs/controllers/OnRampController.js.map +1 -0
  13. package/lib/commonjs/controllers/OptionsController.js +6 -3
  14. package/lib/commonjs/controllers/OptionsController.js.map +1 -1
  15. package/lib/commonjs/controllers/RouterController.js +3 -2
  16. package/lib/commonjs/controllers/RouterController.js.map +1 -1
  17. package/lib/commonjs/controllers/SwapController.js +13 -5
  18. package/lib/commonjs/controllers/SwapController.js.map +1 -1
  19. package/lib/commonjs/controllers/ThemeController.js +9 -1
  20. package/lib/commonjs/controllers/ThemeController.js.map +1 -1
  21. package/lib/commonjs/controllers/TransactionsController.js +2 -2
  22. package/lib/commonjs/controllers/TransactionsController.js.map +1 -1
  23. package/lib/commonjs/index.js +10 -3
  24. package/lib/commonjs/index.js.map +1 -1
  25. package/lib/commonjs/utils/AssetUtil.js +5 -6
  26. package/lib/commonjs/utils/AssetUtil.js.map +1 -1
  27. package/lib/commonjs/utils/ConstantsUtil.js +492 -7
  28. package/lib/commonjs/utils/ConstantsUtil.js.map +1 -1
  29. package/lib/commonjs/utils/CoreHelperUtil.js +28 -0
  30. package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -1
  31. package/lib/commonjs/utils/FetchUtil.js +15 -4
  32. package/lib/commonjs/utils/FetchUtil.js.map +1 -1
  33. package/lib/commonjs/utils/StorageUtil.js +218 -0
  34. package/lib/commonjs/utils/StorageUtil.js.map +1 -1
  35. package/lib/commonjs/utils/SwapApiUtil.js +9 -4
  36. package/lib/commonjs/utils/SwapApiUtil.js.map +1 -1
  37. package/lib/module/controllers/ApiController.js +11 -11
  38. package/lib/module/controllers/ApiController.js.map +1 -1
  39. package/lib/module/controllers/BlockchainApiController.js +187 -14
  40. package/lib/module/controllers/BlockchainApiController.js.map +1 -1
  41. package/lib/module/controllers/ConnectionController.js +7 -1
  42. package/lib/module/controllers/ConnectionController.js.map +1 -1
  43. package/lib/module/controllers/ConnectionsController.js +151 -0
  44. package/lib/module/controllers/ConnectionsController.js.map +1 -0
  45. package/lib/module/controllers/ConnectorController.js +1 -0
  46. package/lib/module/controllers/ConnectorController.js.map +1 -1
  47. package/lib/module/controllers/OnRampController.js +416 -0
  48. package/lib/module/controllers/OnRampController.js.map +1 -0
  49. package/lib/module/controllers/OptionsController.js +6 -3
  50. package/lib/module/controllers/OptionsController.js.map +1 -1
  51. package/lib/module/controllers/RouterController.js +3 -2
  52. package/lib/module/controllers/RouterController.js.map +1 -1
  53. package/lib/module/controllers/SwapController.js +13 -5
  54. package/lib/module/controllers/SwapController.js.map +1 -1
  55. package/lib/module/controllers/ThemeController.js +9 -1
  56. package/lib/module/controllers/ThemeController.js.map +1 -1
  57. package/lib/module/controllers/TransactionsController.js +2 -2
  58. package/lib/module/controllers/TransactionsController.js.map +1 -1
  59. package/lib/module/index.js +2 -1
  60. package/lib/module/index.js.map +1 -1
  61. package/lib/module/utils/AssetUtil.js +5 -6
  62. package/lib/module/utils/AssetUtil.js.map +1 -1
  63. package/lib/module/utils/ConstantsUtil.js +491 -6
  64. package/lib/module/utils/ConstantsUtil.js.map +1 -1
  65. package/lib/module/utils/CoreHelperUtil.js +26 -0
  66. package/lib/module/utils/CoreHelperUtil.js.map +1 -1
  67. package/lib/module/utils/FetchUtil.js +15 -4
  68. package/lib/module/utils/FetchUtil.js.map +1 -1
  69. package/lib/module/utils/StorageUtil.js +219 -0
  70. package/lib/module/utils/StorageUtil.js.map +1 -1
  71. package/lib/module/utils/SwapApiUtil.js +9 -4
  72. package/lib/module/utils/SwapApiUtil.js.map +1 -1
  73. package/lib/typescript/controllers/AccountController.d.ts +2 -2
  74. package/lib/typescript/controllers/AccountController.d.ts.map +1 -1
  75. package/lib/typescript/controllers/ApiController.d.ts +1 -1
  76. package/lib/typescript/controllers/ApiController.d.ts.map +1 -1
  77. package/lib/typescript/controllers/BlockchainApiController.d.ts +27 -2
  78. package/lib/typescript/controllers/BlockchainApiController.d.ts.map +1 -1
  79. package/lib/typescript/controllers/ConnectionController.d.ts +2 -0
  80. package/lib/typescript/controllers/ConnectionController.d.ts.map +1 -1
  81. package/lib/typescript/controllers/ConnectionsController.d.ts +41 -0
  82. package/lib/typescript/controllers/ConnectionsController.d.ts.map +1 -0
  83. package/lib/typescript/controllers/ConnectorController.d.ts.map +1 -1
  84. package/lib/typescript/controllers/NetworkController.d.ts +1 -1
  85. package/lib/typescript/controllers/NetworkController.d.ts.map +1 -1
  86. package/lib/typescript/controllers/OnRampController.d.ts +54 -0
  87. package/lib/typescript/controllers/OnRampController.d.ts.map +1 -0
  88. package/lib/typescript/controllers/OptionsController.d.ts +5 -2
  89. package/lib/typescript/controllers/OptionsController.d.ts.map +1 -1
  90. package/lib/typescript/controllers/PublicStateController.d.ts +1 -1
  91. package/lib/typescript/controllers/PublicStateController.d.ts.map +1 -1
  92. package/lib/typescript/controllers/RouterController.d.ts +5 -3
  93. package/lib/typescript/controllers/RouterController.d.ts.map +1 -1
  94. package/lib/typescript/controllers/SwapController.d.ts.map +1 -1
  95. package/lib/typescript/controllers/ThemeController.d.ts +1 -1
  96. package/lib/typescript/controllers/ThemeController.d.ts.map +1 -1
  97. package/lib/typescript/index.d.ts +2 -1
  98. package/lib/typescript/index.d.ts.map +1 -1
  99. package/lib/typescript/utils/AssetUtil.d.ts +2 -2
  100. package/lib/typescript/utils/AssetUtil.d.ts.map +1 -1
  101. package/lib/typescript/utils/ConstantsUtil.d.ts +473 -1
  102. package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -1
  103. package/lib/typescript/utils/CoreHelperUtil.d.ts +5 -2
  104. package/lib/typescript/utils/CoreHelperUtil.d.ts.map +1 -1
  105. package/lib/typescript/utils/FetchUtil.d.ts +5 -5
  106. package/lib/typescript/utils/FetchUtil.d.ts.map +1 -1
  107. package/lib/typescript/utils/NetworkUtil.d.ts +1 -1
  108. package/lib/typescript/utils/NetworkUtil.d.ts.map +1 -1
  109. package/lib/typescript/utils/StorageUtil.d.ts +26 -2
  110. package/lib/typescript/utils/StorageUtil.d.ts.map +1 -1
  111. package/lib/typescript/utils/SwapApiUtil.d.ts.map +1 -1
  112. package/lib/typescript/utils/TypeUtil.d.ts +164 -14
  113. package/lib/typescript/utils/TypeUtil.d.ts.map +1 -1
  114. package/package.json +3 -2
  115. package/src/controllers/AccountController.ts +2 -2
  116. package/src/controllers/ApiController.ts +12 -7
  117. package/src/controllers/BlockchainApiController.ts +238 -15
  118. package/src/controllers/ConnectionController.ts +9 -1
  119. package/src/controllers/ConnectionsController.ts +236 -0
  120. package/src/controllers/ConnectorController.ts +1 -0
  121. package/src/controllers/NetworkController.ts +1 -1
  122. package/src/controllers/OnRampController.ts +585 -0
  123. package/src/controllers/OptionsController.ts +12 -6
  124. package/src/controllers/PublicStateController.ts +1 -1
  125. package/src/controllers/RouterController.ts +17 -3
  126. package/src/controllers/SwapController.ts +14 -5
  127. package/src/controllers/ThemeController.ts +11 -2
  128. package/src/controllers/TransactionsController.ts +2 -2
  129. package/src/index.ts +6 -1
  130. package/src/utils/AssetUtil.ts +5 -7
  131. package/src/utils/ConstantsUtil.ts +475 -6
  132. package/src/utils/CoreHelperUtil.ts +38 -3
  133. package/src/utils/FetchUtil.ts +16 -9
  134. package/src/utils/NetworkUtil.ts +1 -1
  135. package/src/utils/StorageUtil.ts +267 -2
  136. package/src/utils/SwapApiUtil.ts +10 -4
  137. package/src/utils/TypeUtil.ts +184 -17
  138. package/lib/commonjs/utils/ConnectionUtil.js +0 -33
  139. package/lib/commonjs/utils/ConnectionUtil.js.map +0 -1
  140. package/lib/module/utils/ConnectionUtil.js +0 -27
  141. package/lib/module/utils/ConnectionUtil.js.map +0 -1
  142. package/lib/typescript/utils/ConnectionUtil.d.ts +0 -4
  143. package/lib/typescript/utils/ConnectionUtil.d.ts.map +0 -1
  144. package/src/utils/ConnectionUtil.ts +0 -27
@@ -1,11 +1,17 @@
1
1
  /* eslint-disable no-bitwise */
2
2
 
3
3
  import { Linking, Platform } from 'react-native';
4
- import { ConstantsUtil as CommonConstants, type Balance } from '@reown/appkit-common-react-native';
4
+ import {
5
+ ConstantsUtil as CommonConstants,
6
+ type Balance,
7
+ type CaipAddress,
8
+ type CaipNetwork
9
+ } from '@reown/appkit-common-react-native';
5
10
 
6
- import { ConstantsUtil } from './ConstantsUtil';
7
- import type { CaipAddress, CaipNetwork, DataWallet, LinkingRecord } from './TypeUtil';
11
+ import * as ct from 'countries-and-timezones';
8
12
 
13
+ import { ConstantsUtil } from './ConstantsUtil';
14
+ import type { DataWallet, LinkingRecord } from './TypeUtil';
9
15
  // -- Helpers -----------------------------------------------------------------
10
16
  async function isAppInstalledIos(deepLink?: string): Promise<boolean> {
11
17
  try {
@@ -172,10 +178,25 @@ export const CoreHelperUtil = {
172
178
  return CommonConstants.BLOCKCHAIN_API_RPC_URL;
173
179
  },
174
180
 
181
+ getBlockchainStagingApiUrl() {
182
+ return CommonConstants.BLOCKCHAIN_API_RPC_URL_STAGING;
183
+ },
184
+
175
185
  getAnalyticsUrl() {
176
186
  return CommonConstants.PULSE_API_URL;
177
187
  },
178
188
 
189
+ getCountryFromTimezone() {
190
+ try {
191
+ const { timeZone } = new Intl.DateTimeFormat().resolvedOptions();
192
+ const country = ct.getCountryForTimezone(timeZone);
193
+
194
+ return country ? country.id : 'US'; // 'id' is the ISO country code (e.g., "GB" for United Kingdom)
195
+ } catch (error) {
196
+ return 'US';
197
+ }
198
+ },
199
+
179
200
  getUUID() {
180
201
  if ((global as any)?.crypto.getRandomValues) {
181
202
  const buffer = new Uint8Array(16);
@@ -287,5 +308,19 @@ export const CoreHelperUtil = {
287
308
  }
288
309
 
289
310
  return requested;
311
+ },
312
+
313
+ debounce<F extends (...args: any[]) => any>(func: F, wait: number) {
314
+ let timeout: ReturnType<typeof setTimeout> | null = null;
315
+
316
+ return function (...args: Parameters<F>) {
317
+ if (timeout) {
318
+ clearTimeout(timeout);
319
+ }
320
+
321
+ timeout = setTimeout(() => {
322
+ func(...args);
323
+ }, wait);
324
+ };
290
325
  }
291
326
  };
@@ -8,7 +8,7 @@ interface Options {
8
8
 
9
9
  interface RequestArguments {
10
10
  path: string;
11
- headers?: HeadersInit_;
11
+ headers?: HeadersInit;
12
12
  params?: Record<string, string | undefined>;
13
13
  cache?: RequestCache;
14
14
  signal?: AbortSignal;
@@ -28,41 +28,44 @@ export class FetchUtil {
28
28
  this.clientId = clientId;
29
29
  }
30
30
 
31
- public async get<T>({ headers, ...args }: RequestArguments) {
31
+ public async get<T>({ headers, signal, ...args }: RequestArguments) {
32
32
  const url = this.createUrl(args);
33
- const response = await fetch(url, { method: 'GET', headers });
33
+ const response = await fetch(url, { method: 'GET', headers, signal });
34
34
 
35
35
  return this.processResponse<T>(response);
36
36
  }
37
37
 
38
- public async post<T>({ body, headers, ...args }: PostArguments) {
38
+ public async post<T>({ body, headers, signal, ...args }: PostArguments) {
39
39
  const url = this.createUrl(args);
40
40
  const response = await fetch(url, {
41
41
  method: 'POST',
42
42
  headers,
43
- body: body ? JSON.stringify(body) : undefined
43
+ body: body ? JSON.stringify(body) : undefined,
44
+ signal
44
45
  });
45
46
 
46
47
  return this.processResponse<T>(response);
47
48
  }
48
49
 
49
- public async put<T>({ body, headers, ...args }: PostArguments) {
50
+ public async put<T>({ body, headers, signal, ...args }: PostArguments) {
50
51
  const url = this.createUrl(args);
51
52
  const response = await fetch(url, {
52
53
  method: 'PUT',
53
54
  headers,
54
- body: body ? JSON.stringify(body) : undefined
55
+ body: body ? JSON.stringify(body) : undefined,
56
+ signal
55
57
  });
56
58
 
57
59
  return this.processResponse<T>(response);
58
60
  }
59
61
 
60
- public async delete<T>({ body, headers, ...args }: PostArguments) {
62
+ public async delete<T>({ body, headers, signal, ...args }: PostArguments) {
61
63
  const url = this.createUrl(args);
62
64
  const response = await fetch(url, {
63
65
  method: 'DELETE',
64
66
  headers,
65
- body: body ? JSON.stringify(body) : undefined
67
+ body: body ? JSON.stringify(body) : undefined,
68
+ signal
66
69
  });
67
70
 
68
71
  return this.processResponse<T>(response);
@@ -103,6 +106,10 @@ export class FetchUtil {
103
106
 
104
107
  private async processResponse<T>(response: Response) {
105
108
  if (!response.ok) {
109
+ if (response.headers.get('content-type')?.includes('application/json')) {
110
+ return Promise.reject((await response.json()) as T);
111
+ }
112
+
106
113
  const errorText = await response.text();
107
114
 
108
115
  return Promise.reject(`Code: ${response.status} - ${response.statusText} - ${errorText}`);
@@ -4,7 +4,7 @@ import { NetworkController } from '../controllers/NetworkController';
4
4
  import { AccountController } from '../controllers/AccountController';
5
5
  import { ConnectorController } from '../controllers/ConnectorController';
6
6
  import { SwapController } from '../controllers/SwapController';
7
- import type { CaipNetwork } from '../utils/TypeUtil';
7
+ import { type CaipNetwork } from '@reown/appkit-common-react-native';
8
8
 
9
9
  export const NetworkUtil = {
10
10
  async handleNetworkSwitch(network: CaipNetwork) {
@@ -1,14 +1,34 @@
1
1
  /* eslint-disable no-console */
2
2
  import AsyncStorage from '@react-native-async-storage/async-storage';
3
- import type { WcWallet } from './TypeUtil';
4
- import type { SocialProvider, ConnectorType } from '@reown/appkit-common-react-native';
3
+ import type {
4
+ OnRampCountry,
5
+ OnRampFiatCurrency,
6
+ OnRampFiatLimit,
7
+ OnRampServiceProvider,
8
+ WcWallet
9
+ } from './TypeUtil';
10
+ import {
11
+ DateUtil,
12
+ type SocialProvider,
13
+ type New_ConnectorType,
14
+ type ConnectorType,
15
+ type ChainNamespace
16
+ } from '@reown/appkit-common-react-native';
5
17
 
6
18
  // -- Helpers -----------------------------------------------------------------
7
19
  const WC_DEEPLINK = 'WALLETCONNECT_DEEPLINK_CHOICE';
8
20
  const RECENT_WALLET = '@w3m/recent';
9
21
  const CONNECTED_WALLET_IMAGE_URL = '@w3m/connected_wallet_image_url';
10
22
  const CONNECTED_CONNECTOR = '@w3m/connected_connector';
23
+ const CONNECTED_CONNECTORS = '@appkit/connected_connectors';
11
24
  const CONNECTED_SOCIAL = '@appkit/connected_social';
25
+ const ONRAMP_PREFERRED_COUNTRY = '@appkit/onramp_preferred_country';
26
+ const ONRAMP_COUNTRIES = '@appkit/onramp_countries';
27
+ const ONRAMP_SERVICE_PROVIDERS = '@appkit/onramp_service_providers';
28
+ const ONRAMP_FIAT_LIMITS = '@appkit/onramp_fiat_limits';
29
+ const ONRAMP_FIAT_CURRENCIES = '@appkit/onramp_fiat_currencies';
30
+ const ONRAMP_PREFERRED_FIAT_CURRENCY = '@appkit/onramp_preferred_fiat_currency';
31
+ const ACTIVE_NAMESPACE = '@appkit/active_namespace';
12
32
 
13
33
  // -- Utility -----------------------------------------------------------------
14
34
  export const StorageUtil = {
@@ -84,6 +104,7 @@ export const StorageUtil = {
84
104
  return [];
85
105
  },
86
106
 
107
+ //TODO: remove this
87
108
  async setConnectedConnector(connectorType: ConnectorType) {
88
109
  try {
89
110
  await AsyncStorage.setItem(CONNECTED_CONNECTOR, JSON.stringify(connectorType));
@@ -112,6 +133,47 @@ export const StorageUtil = {
112
133
  }
113
134
  },
114
135
 
136
+ async setConnectedConnectors({
137
+ type,
138
+ namespaces
139
+ }: {
140
+ type: New_ConnectorType;
141
+ namespaces: string[];
142
+ }) {
143
+ try {
144
+ const currentConnectors = (await StorageUtil.getConnectedConnectors()) || [];
145
+ // Only add if it doesn't exist already
146
+ if (!currentConnectors.some(c => c.type === type)) {
147
+ const updatedConnectors = [...currentConnectors, { type, namespaces }];
148
+ await AsyncStorage.setItem(CONNECTED_CONNECTORS, JSON.stringify(updatedConnectors));
149
+ }
150
+ } catch {
151
+ console.info('Unable to set Connected Connector');
152
+ }
153
+ },
154
+
155
+ async getConnectedConnectors(): Promise<{ type: New_ConnectorType; namespaces: string[] }[]> {
156
+ try {
157
+ const connectors = await AsyncStorage.getItem(CONNECTED_CONNECTORS);
158
+
159
+ return connectors ? JSON.parse(connectors) : [];
160
+ } catch {
161
+ console.info('Unable to get Connected Connector');
162
+ }
163
+
164
+ return [];
165
+ },
166
+
167
+ async removeConnectedConnectors(type: New_ConnectorType) {
168
+ try {
169
+ const currentConnectors = await StorageUtil.getConnectedConnectors();
170
+ const updatedConnectors = currentConnectors.filter(c => c.type !== type);
171
+ await AsyncStorage.setItem(CONNECTED_CONNECTORS, JSON.stringify(updatedConnectors));
172
+ } catch {
173
+ console.info('Unable to remove Connected Connector');
174
+ }
175
+ },
176
+
115
177
  async setConnectedWalletImageUrl(url: string) {
116
178
  try {
117
179
  await AsyncStorage.setItem(CONNECTED_WALLET_IMAGE_URL, url);
@@ -164,5 +226,208 @@ export const StorageUtil = {
164
226
  } catch {
165
227
  console.info('Unable to remove Connected Social Provider');
166
228
  }
229
+ },
230
+
231
+ async setOnRampPreferredCountry(country: OnRampCountry) {
232
+ try {
233
+ await AsyncStorage.setItem(ONRAMP_PREFERRED_COUNTRY, JSON.stringify(country));
234
+ } catch {
235
+ console.info('Unable to set OnRamp Preferred Country');
236
+ }
237
+ },
238
+
239
+ async getOnRampPreferredCountry() {
240
+ try {
241
+ const country = await AsyncStorage.getItem(ONRAMP_PREFERRED_COUNTRY);
242
+
243
+ return country ? (JSON.parse(country) as OnRampCountry) : undefined;
244
+ } catch {
245
+ console.info('Unable to get OnRamp Preferred Country');
246
+ }
247
+
248
+ return undefined;
249
+ },
250
+
251
+ async setOnRampPreferredFiatCurrency(currency: OnRampFiatCurrency) {
252
+ try {
253
+ await AsyncStorage.setItem(ONRAMP_PREFERRED_FIAT_CURRENCY, JSON.stringify(currency));
254
+ } catch {
255
+ console.info('Unable to set OnRamp Preferred Fiat Currency');
256
+ }
257
+ },
258
+
259
+ async getOnRampPreferredFiatCurrency() {
260
+ try {
261
+ const currency = await AsyncStorage.getItem(ONRAMP_PREFERRED_FIAT_CURRENCY);
262
+
263
+ return currency ? (JSON.parse(currency) as OnRampFiatCurrency) : undefined;
264
+ } catch {
265
+ console.info('Unable to get OnRamp Preferred Fiat Currency');
266
+ }
267
+
268
+ return undefined;
269
+ },
270
+
271
+ async setOnRampCountries(countries: OnRampCountry[]) {
272
+ try {
273
+ await AsyncStorage.setItem(ONRAMP_COUNTRIES, JSON.stringify(countries));
274
+ } catch {
275
+ console.info('Unable to set OnRamp Countries');
276
+ }
277
+ },
278
+
279
+ async getOnRampCountries() {
280
+ try {
281
+ const countries = await AsyncStorage.getItem(ONRAMP_COUNTRIES);
282
+
283
+ return countries ? (JSON.parse(countries) as OnRampCountry[]) : [];
284
+ } catch {
285
+ console.info('Unable to get OnRamp Countries');
286
+ }
287
+
288
+ return [];
289
+ },
290
+
291
+ async setOnRampServiceProviders(serviceProviders: OnRampServiceProvider[]) {
292
+ try {
293
+ const timestamp = Date.now();
294
+
295
+ await AsyncStorage.setItem(
296
+ ONRAMP_SERVICE_PROVIDERS,
297
+ JSON.stringify({ data: serviceProviders, timestamp })
298
+ );
299
+ } catch {
300
+ console.info('Unable to set OnRamp Service Providers');
301
+ }
302
+ },
303
+
304
+ async getOnRampServiceProviders() {
305
+ try {
306
+ const result = await AsyncStorage.getItem(ONRAMP_SERVICE_PROVIDERS);
307
+
308
+ if (!result) {
309
+ return [];
310
+ }
311
+
312
+ const { data, timestamp } = JSON.parse(result);
313
+
314
+ // Cache for 1 week
315
+ if (timestamp && DateUtil.isMoreThanOneWeekAgo(timestamp)) {
316
+ return [];
317
+ }
318
+
319
+ return data ? (data as OnRampServiceProvider[]) : [];
320
+ } catch (err) {
321
+ console.error(err);
322
+ console.info('Unable to get OnRamp Service Providers');
323
+ }
324
+
325
+ return [];
326
+ },
327
+
328
+ async setOnRampFiatLimits(fiatLimits: OnRampFiatLimit[]) {
329
+ try {
330
+ const timestamp = Date.now();
331
+
332
+ await AsyncStorage.setItem(
333
+ ONRAMP_FIAT_LIMITS,
334
+ JSON.stringify({ data: fiatLimits, timestamp })
335
+ );
336
+ } catch {
337
+ console.info('Unable to set OnRamp Fiat Limits');
338
+ }
339
+ },
340
+
341
+ async getOnRampFiatLimits() {
342
+ try {
343
+ const result = await AsyncStorage.getItem(ONRAMP_FIAT_LIMITS);
344
+
345
+ if (!result) {
346
+ return [];
347
+ }
348
+
349
+ const { data, timestamp } = JSON.parse(result);
350
+
351
+ // Cache for 1 week
352
+ if (timestamp && DateUtil.isMoreThanOneWeekAgo(timestamp)) {
353
+ return [];
354
+ }
355
+
356
+ return data ? (data as OnRampFiatLimit[]) : [];
357
+ } catch {
358
+ console.info('Unable to get OnRamp Fiat Limits');
359
+ }
360
+
361
+ return [];
362
+ },
363
+
364
+ async setOnRampFiatCurrencies(fiatCurrencies: OnRampFiatCurrency[]) {
365
+ try {
366
+ const timestamp = Date.now();
367
+
368
+ await AsyncStorage.setItem(
369
+ ONRAMP_FIAT_CURRENCIES,
370
+ JSON.stringify({ data: fiatCurrencies, timestamp })
371
+ );
372
+ } catch {
373
+ console.info('Unable to set OnRamp Fiat Currencies');
374
+ }
375
+ },
376
+
377
+ async getOnRampFiatCurrencies() {
378
+ try {
379
+ const result = await AsyncStorage.getItem(ONRAMP_FIAT_CURRENCIES);
380
+
381
+ if (!result) {
382
+ return [];
383
+ }
384
+
385
+ const { data, timestamp } = JSON.parse(result);
386
+
387
+ // Cache for 1 week
388
+ if (timestamp && DateUtil.isMoreThanOneWeekAgo(timestamp)) {
389
+ return [];
390
+ }
391
+
392
+ return data ? (data as OnRampFiatCurrency[]) : [];
393
+ } catch {
394
+ console.info('Unable to get OnRamp Fiat Currencies');
395
+ }
396
+
397
+ return [];
398
+ },
399
+
400
+ async setActiveNamespace(namespace?: ChainNamespace) {
401
+ try {
402
+ if (!namespace) {
403
+ await AsyncStorage.removeItem(ACTIVE_NAMESPACE);
404
+
405
+ return;
406
+ }
407
+
408
+ await AsyncStorage.setItem(ACTIVE_NAMESPACE, namespace);
409
+ } catch {
410
+ console.info('Unable to set Active Namespace');
411
+ }
412
+ },
413
+
414
+ async getActiveNamespace() {
415
+ try {
416
+ const namespace = (await AsyncStorage.getItem(ACTIVE_NAMESPACE)) as ChainNamespace;
417
+
418
+ return namespace ?? undefined;
419
+ } catch (err) {
420
+ console.info('Unable to get Active Namespace');
421
+ }
422
+
423
+ return undefined;
424
+ },
425
+
426
+ async removeActiveNamespace() {
427
+ try {
428
+ await AsyncStorage.removeItem(ACTIVE_NAMESPACE);
429
+ } catch {
430
+ console.info('Unable to remove Active Namespace');
431
+ }
167
432
  }
168
433
  };
@@ -8,12 +8,14 @@ import type {
8
8
  } from './TypeUtil';
9
9
  import { AccountController } from '../controllers/AccountController';
10
10
  import { ConnectionController } from '../controllers/ConnectionController';
11
+ import { ConnectionsController } from '../controllers/ConnectionsController';
11
12
 
12
13
  export const SwapApiUtil = {
13
14
  async getTokenList() {
15
+ const chainId = ConnectionsController.state.activeNetwork?.caipNetworkId ?? 'eip155:1';
14
16
  const response = await BlockchainApiController.fetchSwapTokens({
15
17
  projectId: OptionsController.state.projectId,
16
- chainId: NetworkController.state.caipNetwork?.id
18
+ chainId
17
19
  });
18
20
  const tokens =
19
21
  response?.tokens?.map(
@@ -62,14 +64,18 @@ export const SwapApiUtil = {
62
64
  },
63
65
 
64
66
  async getMyTokensWithBalance(forceUpdate?: string) {
65
- const address = AccountController.state.address;
66
- const chainId = NetworkController.state.caipNetwork?.id;
67
+ const { activeAddress, activeNetwork: network } = ConnectionsController.state;
68
+ const address = activeAddress?.split(':')[2];
67
69
 
68
70
  if (!address) {
69
71
  return [];
70
72
  }
71
73
 
72
- const response = await BlockchainApiController.getBalance(address, chainId, forceUpdate);
74
+ const response = await BlockchainApiController.getBalance(
75
+ address,
76
+ network?.caipNetworkId,
77
+ forceUpdate
78
+ );
73
79
  const balances = response?.balances.filter(balance => balance.quantity.decimals !== '0');
74
80
 
75
81
  AccountController.setTokenBalance(balances);