@reown/appkit-core-react-native 1.2.4 → 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
@@ -19,14 +19,27 @@ import type {
19
19
  BlockchainApiSwapQuoteResponse,
20
20
  BlockchainApiSwapTokensRequest,
21
21
  BlockchainApiSwapTokensResponse,
22
+ BlockchainApiOnRampWidgetResponse,
22
23
  BlockchainApiTokenPriceRequest,
23
24
  BlockchainApiTokenPriceResponse,
24
25
  BlockchainApiTransactionsRequest,
25
- BlockchainApiTransactionsResponse
26
+ BlockchainApiTransactionsResponse,
27
+ OnRampCountry,
28
+ OnRampServiceProvider,
29
+ OnRampPaymentMethod,
30
+ OnRampCryptoCurrency,
31
+ OnRampFiatCurrency,
32
+ OnRampQuote,
33
+ BlockchainApiOnRampWidgetRequest,
34
+ BlockchainApiOnRampQuotesRequest,
35
+ OnRampFiatLimit
26
36
  } from '../utils/TypeUtil';
27
37
  import { OptionsController } from './OptionsController';
28
38
  import { ConstantsUtil } from '../utils/ConstantsUtil';
29
39
  import { ApiUtil } from '../utils/ApiUtil';
40
+ import type { CaipNetworkId } from '@reown/appkit-common-react-native';
41
+ import { ConnectionsController } from './ConnectionsController';
42
+ import { SnackController } from './SnackController';
30
43
 
31
44
  // -- Helpers ------------------------------------------- //
32
45
  const baseUrl = CoreHelperUtil.getBlockchainApiUrl();
@@ -47,19 +60,54 @@ const getHeaders = () => {
47
60
  export interface BlockchainApiControllerState {
48
61
  clientId: string | null;
49
62
  api: FetchUtil;
63
+ supportedChains: { http: CaipNetworkId[]; ws: CaipNetworkId[] };
50
64
  }
51
65
 
52
66
  // -- State --------------------------------------------- //
53
67
  const state = proxy<BlockchainApiControllerState>({
54
68
  clientId: null,
55
- api: new FetchUtil({ baseUrl })
69
+ api: new FetchUtil({ baseUrl }),
70
+ supportedChains: { http: [], ws: [] }
56
71
  });
57
72
 
58
73
  // -- Controller ---------------------------------------- //
59
74
  export const BlockchainApiController = {
60
75
  state,
61
76
 
62
- fetchIdentity({ address }: BlockchainApiIdentityRequest) {
77
+ async isNetworkSupported(networkId?: CaipNetworkId) {
78
+ if (!networkId) {
79
+ return false;
80
+ }
81
+ try {
82
+ if (!state.supportedChains.http.length) {
83
+ await BlockchainApiController.getSupportedNetworks();
84
+ }
85
+ } catch (e) {
86
+ return false;
87
+ }
88
+
89
+ return state.supportedChains.http.includes(networkId);
90
+ },
91
+
92
+ async getSupportedNetworks() {
93
+ const supportedChains = await state.api.get<BlockchainApiControllerState['supportedChains']>({
94
+ path: 'v1/supported-chains'
95
+ });
96
+
97
+ state.supportedChains = supportedChains!;
98
+
99
+ return supportedChains;
100
+ },
101
+
102
+ async fetchIdentity({ address }: BlockchainApiIdentityRequest) {
103
+ const isSupported = await BlockchainApiController.isNetworkSupported(
104
+ ConnectionsController.state.activeCaipNetworkId
105
+ );
106
+
107
+ if (!isSupported) {
108
+ return { avatar: '', name: '' };
109
+ }
110
+
63
111
  return state.api.get<BlockchainApiIdentityResponse>({
64
112
  path: `/v1/identity/${address}`,
65
113
  params: {
@@ -69,29 +117,48 @@ export const BlockchainApiController = {
69
117
  });
70
118
  },
71
119
 
72
- fetchTransactions({
120
+ async fetchTransactions({
73
121
  account,
74
122
  projectId,
75
123
  cursor,
76
124
  onramp,
77
125
  signal,
78
- cache
126
+ cache,
127
+ chainId
79
128
  }: BlockchainApiTransactionsRequest) {
80
- return state.api.get<BlockchainApiTransactionsResponse>({
129
+ const _chainId = chainId ?? ConnectionsController.state.activeCaipNetworkId;
130
+ const isSupported = await BlockchainApiController.isNetworkSupported(_chainId);
131
+
132
+ if (!isSupported) {
133
+ return { data: [], next: undefined };
134
+ }
135
+
136
+ const response = await state.api.get<BlockchainApiTransactionsResponse>({
81
137
  path: `/v1/account/${account}/history`,
82
138
  headers: getHeaders(),
83
139
  params: {
84
140
  projectId,
85
141
  cursor,
86
- onramp
142
+ onramp,
143
+ chainId: _chainId
87
144
  },
88
145
  signal,
89
146
  cache
90
147
  });
148
+
149
+ return response;
91
150
  },
92
151
 
93
- fetchTokenPrice({ projectId, addresses }: BlockchainApiTokenPriceRequest) {
94
- return state.api.post<BlockchainApiTokenPriceResponse>({
152
+ async fetchTokenPrice({ projectId, addresses }: BlockchainApiTokenPriceRequest) {
153
+ const isSupported = await BlockchainApiController.isNetworkSupported(
154
+ ConnectionsController.state.activeCaipNetworkId
155
+ );
156
+
157
+ if (!isSupported) {
158
+ return { fungibles: [] };
159
+ }
160
+
161
+ const response = await state.api.post<BlockchainApiTokenPriceResponse>({
95
162
  path: '/v1/fungible/price',
96
163
  body: {
97
164
  projectId,
@@ -100,9 +167,23 @@ export const BlockchainApiController = {
100
167
  },
101
168
  headers: getHeaders()
102
169
  });
170
+
171
+ return response;
103
172
  },
104
173
 
105
- fetchSwapAllowance({ projectId, tokenAddress, userAddress }: BlockchainApiSwapAllowanceRequest) {
174
+ async fetchSwapAllowance({
175
+ projectId,
176
+ tokenAddress,
177
+ userAddress
178
+ }: BlockchainApiSwapAllowanceRequest) {
179
+ const isSupported = await BlockchainApiController.isNetworkSupported(
180
+ ConnectionsController.state.activeCaipNetworkId
181
+ );
182
+
183
+ if (!isSupported) {
184
+ return { allowance: '0' };
185
+ }
186
+
106
187
  return state.api.get<BlockchainApiSwapAllowanceResponse>({
107
188
  path: `/v1/convert/allowance`,
108
189
  params: {
@@ -114,7 +195,15 @@ export const BlockchainApiController = {
114
195
  });
115
196
  },
116
197
 
117
- fetchGasPrice({ projectId, chainId }: BlockchainApiGasPriceRequest) {
198
+ async fetchGasPrice({ projectId, chainId }: BlockchainApiGasPriceRequest) {
199
+ const isSupported = await BlockchainApiController.isNetworkSupported(
200
+ ConnectionsController.state.activeCaipNetworkId
201
+ );
202
+
203
+ if (!isSupported) {
204
+ throw new Error('Network not supported for Gas Price');
205
+ }
206
+
118
207
  return state.api.get<BlockchainApiGasPriceResponse>({
119
208
  path: `/v1/convert/gas-price`,
120
209
  headers: getHeaders(),
@@ -125,7 +214,7 @@ export const BlockchainApiController = {
125
214
  });
126
215
  },
127
216
 
128
- fetchSwapQuote({
217
+ async fetchSwapQuote({
129
218
  projectId,
130
219
  amount,
131
220
  userAddress,
@@ -133,6 +222,14 @@ export const BlockchainApiController = {
133
222
  to,
134
223
  gasPrice
135
224
  }: BlockchainApiSwapQuoteRequest) {
225
+ const isSupported = await BlockchainApiController.isNetworkSupported(
226
+ ConnectionsController.state.activeCaipNetworkId
227
+ );
228
+
229
+ if (!isSupported) {
230
+ return { quotes: [] };
231
+ }
232
+
136
233
  return state.api.get<BlockchainApiSwapQuoteResponse>({
137
234
  path: `/v1/convert/quotes`,
138
235
  headers: getHeaders(),
@@ -147,7 +244,15 @@ export const BlockchainApiController = {
147
244
  });
148
245
  },
149
246
 
150
- fetchSwapTokens({ projectId, chainId }: BlockchainApiSwapTokensRequest) {
247
+ async fetchSwapTokens({ projectId, chainId }: BlockchainApiSwapTokensRequest) {
248
+ const isSupported = await BlockchainApiController.isNetworkSupported(
249
+ ConnectionsController.state.activeCaipNetworkId
250
+ );
251
+
252
+ if (!isSupported) {
253
+ return { tokens: [] };
254
+ }
255
+
151
256
  return state.api.get<BlockchainApiSwapTokensResponse>({
152
257
  path: `/v1/convert/tokens`,
153
258
  headers: getHeaders(),
@@ -158,13 +263,21 @@ export const BlockchainApiController = {
158
263
  });
159
264
  },
160
265
 
161
- generateSwapCalldata({
266
+ async generateSwapCalldata({
162
267
  amount,
163
268
  from,
164
269
  projectId,
165
270
  to,
166
271
  userAddress
167
272
  }: BlockchainApiGenerateSwapCalldataRequest) {
273
+ const isSupported = await BlockchainApiController.isNetworkSupported(
274
+ ConnectionsController.state.activeCaipNetworkId
275
+ );
276
+
277
+ if (!isSupported) {
278
+ throw new Error('Network not supported for Swaps');
279
+ }
280
+
168
281
  return state.api.post<BlockchainApiGenerateSwapCalldataResponse>({
169
282
  path: '/v1/convert/build-transaction',
170
283
  headers: getHeaders(),
@@ -181,12 +294,20 @@ export const BlockchainApiController = {
181
294
  });
182
295
  },
183
296
 
184
- generateApproveCalldata({
297
+ async generateApproveCalldata({
185
298
  from,
186
299
  projectId,
187
300
  to,
188
301
  userAddress
189
302
  }: BlockchainApiGenerateApproveCalldataRequest) {
303
+ const isSupported = await BlockchainApiController.isNetworkSupported(
304
+ ConnectionsController.state.activeCaipNetworkId
305
+ );
306
+
307
+ if (!isSupported) {
308
+ throw new Error('Network not supported for Swaps');
309
+ }
310
+
190
311
  return state.api.get<BlockchainApiGenerateApproveCalldataResponse>({
191
312
  path: `/v1/convert/build-approve`,
192
313
  headers: getHeaders(),
@@ -200,6 +321,15 @@ export const BlockchainApiController = {
200
321
  },
201
322
 
202
323
  async getBalance(address: string, chainId?: string, forceUpdate?: string) {
324
+ const isSupported = await BlockchainApiController.isNetworkSupported(
325
+ ConnectionsController.state.activeCaipNetworkId
326
+ );
327
+ if (!isSupported) {
328
+ SnackController.showError('Token Balance Unavailable');
329
+
330
+ return { balances: [] };
331
+ }
332
+
203
333
  return state.api.get<BlockchainApiBalanceResponse>({
204
334
  path: `/v1/account/${address}/balance`,
205
335
  headers: getHeaders(),
@@ -223,6 +353,99 @@ export const BlockchainApiController = {
223
353
  });
224
354
  },
225
355
 
356
+ async fetchOnRampCountries() {
357
+ return await state.api.get<OnRampCountry[]>({
358
+ path: '/v1/onramp/providers/properties',
359
+ headers: getHeaders(),
360
+ params: {
361
+ projectId: OptionsController.state.projectId,
362
+ type: 'countries'
363
+ }
364
+ });
365
+ },
366
+
367
+ async fetchOnRampServiceProviders() {
368
+ return await state.api.get<OnRampServiceProvider[]>({
369
+ path: '/v1/onramp/providers',
370
+ headers: getHeaders(),
371
+ params: {
372
+ projectId: OptionsController.state.projectId
373
+ }
374
+ });
375
+ },
376
+
377
+ async fetchOnRampPaymentMethods(params: { countries?: string }) {
378
+ return await state.api.get<OnRampPaymentMethod[]>({
379
+ path: '/v1/onramp/providers/properties',
380
+ headers: getHeaders(),
381
+ params: {
382
+ projectId: OptionsController.state.projectId,
383
+ type: 'payment-methods',
384
+ ...params
385
+ }
386
+ });
387
+ },
388
+
389
+ async fetchOnRampCryptoCurrencies(params: { countries?: string }) {
390
+ return await state.api.get<OnRampCryptoCurrency[]>({
391
+ path: '/v1/onramp/providers/properties',
392
+ headers: getHeaders(),
393
+ params: {
394
+ projectId: OptionsController.state.projectId,
395
+ type: 'crypto-currencies',
396
+ ...params
397
+ }
398
+ });
399
+ },
400
+
401
+ async fetchOnRampFiatCurrencies() {
402
+ return await state.api.get<OnRampFiatCurrency[]>({
403
+ path: '/v1/onramp/providers/properties',
404
+ headers: getHeaders(),
405
+ params: {
406
+ projectId: OptionsController.state.projectId,
407
+ type: 'fiat-currencies'
408
+ }
409
+ });
410
+ },
411
+
412
+ async fetchOnRampFiatLimits() {
413
+ return await state.api.get<OnRampFiatLimit[]>({
414
+ path: '/v1/onramp/providers/properties',
415
+ headers: getHeaders(),
416
+ params: {
417
+ projectId: OptionsController.state.projectId,
418
+ type: 'fiat-purchases-limits'
419
+ }
420
+ });
421
+ },
422
+
423
+ async getOnRampQuotes(body: BlockchainApiOnRampQuotesRequest, signal?: AbortSignal) {
424
+ return await state.api.post<OnRampQuote[]>({
425
+ path: '/v1/onramp/multi/quotes',
426
+ headers: getHeaders(),
427
+ body: {
428
+ projectId: OptionsController.state.projectId,
429
+ ...body
430
+ },
431
+ signal
432
+ });
433
+ },
434
+
435
+ async getOnRampWidget(body: BlockchainApiOnRampWidgetRequest, signal?: AbortSignal) {
436
+ return await state.api.post<BlockchainApiOnRampWidgetResponse>({
437
+ path: '/v1/onramp/widget',
438
+ headers: getHeaders(),
439
+ body: {
440
+ projectId: OptionsController.state.projectId,
441
+ sessionData: {
442
+ ...body
443
+ }
444
+ },
445
+ signal
446
+ });
447
+ },
448
+
226
449
  setClientId(clientId: string | null) {
227
450
  state.clientId = clientId;
228
451
  state.api = new FetchUtil({ baseUrl, clientId });
@@ -120,6 +120,15 @@ export const ConnectionController = {
120
120
  state.pressedWallet = undefined;
121
121
  },
122
122
 
123
+ setWcPromise(wcPromise: ConnectionControllerState['wcPromise']) {
124
+ state.wcPromise = wcPromise;
125
+ },
126
+
127
+ setWcUri(wcUri: ConnectionControllerState['wcUri']) {
128
+ state.wcUri = wcUri;
129
+ state.wcPairingExpiry = CoreHelperUtil.getPairingExpiry();
130
+ },
131
+
123
132
  setRecentWallets(wallets: ConnectionControllerState['recentWallets']) {
124
133
  state.recentWallets = wallets;
125
134
  },
@@ -192,7 +201,6 @@ export const ConnectionController = {
192
201
  },
193
202
 
194
203
  async disconnect() {
195
- await this._getClient().disconnect();
196
204
  this.resetWcConnection();
197
205
  // remove transactions
198
206
  // RouterController.reset('Connect');
@@ -0,0 +1,236 @@
1
+ import { proxy, ref } from 'valtio';
2
+ import { derive } from 'valtio/utils';
3
+ import type {
4
+ AppKitNetwork,
5
+ BlockchainAdapter,
6
+ CaipAddress,
7
+ CaipNetworkId,
8
+ ChainNamespace,
9
+ GetBalanceResponse,
10
+ WalletInfo
11
+ } from '@reown/appkit-common-react-native';
12
+ import { StorageUtil } from '../utils/StorageUtil';
13
+
14
+ // -- Types --------------------------------------------- //
15
+ type Balance = GetBalanceResponse;
16
+
17
+ //TODO: balance could be elsewhere
18
+ interface Connection {
19
+ accounts: CaipAddress[];
20
+ balances: Record<CaipAddress, Balance>; //TODO: make this an array of balances
21
+ adapter: BlockchainAdapter;
22
+ chains: CaipNetworkId[];
23
+ activeChain: CaipNetworkId;
24
+ wallet?: WalletInfo;
25
+ }
26
+
27
+ export interface ConnectionsControllerState {
28
+ activeNamespace?: ChainNamespace;
29
+ connections: Record<string, Connection>;
30
+ networks: AppKitNetwork[];
31
+ }
32
+
33
+ // -- State --------------------------------------------- //
34
+ const baseState = proxy<ConnectionsControllerState>({
35
+ activeNamespace: undefined,
36
+ connections: {},
37
+ networks: []
38
+ });
39
+
40
+ const derivedState = derive(
41
+ {
42
+ activeAddress: (get): CaipAddress | undefined => {
43
+ const snap = get(baseState);
44
+
45
+ if (!snap.activeNamespace) return undefined;
46
+
47
+ const connection = snap.connections[snap.activeNamespace];
48
+
49
+ if (!connection || !connection.accounts || connection.accounts.length === 0) {
50
+ return undefined;
51
+ }
52
+
53
+ //TODO: what happens if there are several accounts on the same chain?
54
+ const activeAccount = connection.accounts.find(account =>
55
+ account.startsWith(connection.activeChain)
56
+ );
57
+
58
+ return activeAccount;
59
+ },
60
+ activeBalance: (get): Balance | undefined => {
61
+ const snap = get(baseState);
62
+
63
+ if (!snap.activeNamespace) return undefined;
64
+ const connection = snap.connections[snap.activeNamespace];
65
+
66
+ if (!connection || !connection.accounts || connection.accounts.length === 0) {
67
+ return undefined;
68
+ }
69
+
70
+ const activeAccount = connection.accounts.find(account =>
71
+ account.startsWith(connection.activeChain)
72
+ );
73
+
74
+ if (
75
+ !connection ||
76
+ !connection.balances ||
77
+ !activeAccount ||
78
+ Object.keys(connection.balances).length === 0
79
+ ) {
80
+ return undefined;
81
+ }
82
+
83
+ return connection.balances[activeAccount];
84
+ },
85
+ activeNetwork: (get): AppKitNetwork | undefined => {
86
+ const snap = get(baseState);
87
+
88
+ if (!snap.activeNamespace) return undefined;
89
+
90
+ const connection = snap.connections[snap.activeNamespace];
91
+
92
+ if (!connection) return undefined;
93
+
94
+ return snap.networks.find(
95
+ network =>
96
+ network.chainNamespace === snap.activeNamespace &&
97
+ network.id?.toString() === connection.activeChain?.split(':')[1]
98
+ );
99
+ },
100
+ activeCaipNetworkId: (get): CaipNetworkId | undefined => {
101
+ const snap = get(baseState);
102
+
103
+ if (!snap.activeNamespace) return undefined;
104
+
105
+ const connection = snap.connections[snap.activeNamespace];
106
+
107
+ if (!connection) return undefined;
108
+
109
+ return connection.activeChain;
110
+ },
111
+ walletInfo: (get): WalletInfo | undefined => {
112
+ const snap = get(baseState);
113
+
114
+ if (!snap.activeNamespace) return undefined;
115
+
116
+ return snap.connections[snap.activeNamespace]?.wallet;
117
+ }
118
+ },
119
+ {
120
+ proxy: baseState // Link derived proxy to the base state proxy
121
+ }
122
+ );
123
+
124
+ // -- Controller ---------------------------------------- //
125
+ export const ConnectionsController = {
126
+ state: derivedState,
127
+
128
+ setActiveNamespace(namespace?: ChainNamespace) {
129
+ baseState.activeNamespace = namespace;
130
+ StorageUtil.setActiveNamespace(namespace);
131
+ },
132
+
133
+ storeConnection({
134
+ namespace,
135
+ adapter,
136
+ accounts,
137
+ chains,
138
+ wallet,
139
+ activeChain
140
+ }: {
141
+ namespace: string;
142
+ adapter: BlockchainAdapter;
143
+ accounts: CaipAddress[];
144
+ chains: CaipNetworkId[];
145
+ wallet?: WalletInfo;
146
+ activeChain?: CaipNetworkId;
147
+ }) {
148
+ baseState.connections[namespace] = {
149
+ balances: {},
150
+ activeChain: activeChain ?? chains[0]!,
151
+ adapter: ref(adapter),
152
+ accounts,
153
+ chains,
154
+ wallet
155
+ };
156
+ },
157
+
158
+ updateAccounts(namespace: string, accounts: CaipAddress[]) {
159
+ const connection = baseState.connections[namespace];
160
+ if (!connection) {
161
+ return;
162
+ }
163
+ connection.accounts = accounts;
164
+ },
165
+
166
+ updateBalance(namespace: string, address: CaipAddress, balance: Balance) {
167
+ const connection = baseState.connections[namespace];
168
+ if (!connection) {
169
+ return;
170
+ }
171
+ connection.balances[address] = balance;
172
+ },
173
+
174
+ setActiveChain(namespace: string, chain: CaipNetworkId) {
175
+ const connection = baseState.connections[namespace];
176
+
177
+ if (!connection) {
178
+ return;
179
+ }
180
+
181
+ connection.activeChain = chain;
182
+ },
183
+
184
+ setNetworks(networks: AppKitNetwork[]) {
185
+ baseState.networks = networks;
186
+ },
187
+
188
+ getConnectedNetworks() {
189
+ return baseState.networks.filter(
190
+ network =>
191
+ baseState.connections[network.chainNamespace]?.chains.includes(network.caipNetworkId)
192
+ );
193
+ },
194
+
195
+ async disconnect(namespace: string, isInternal = true) {
196
+ const connection = baseState.connections[namespace];
197
+ if (!connection) return;
198
+
199
+ // Get the current connector from the adapter
200
+ const connector = connection.adapter.connector;
201
+ if (!connector) return;
202
+
203
+ // Find all namespaces that use the same connector
204
+ const namespacesUsingConnector = Object.keys(baseState.connections).filter(
205
+ ns => baseState.connections[ns]?.adapter.connector === connector
206
+ );
207
+
208
+ // Unsubscribe all event listeners from the adapter
209
+ namespacesUsingConnector.forEach(ns => {
210
+ const _connection = baseState.connections[ns];
211
+ if (_connection?.adapter) {
212
+ _connection.adapter.removeAllListeners();
213
+ }
214
+ });
215
+
216
+ // Disconnect the adapter
217
+ if (isInternal) {
218
+ await connection.adapter.disconnect();
219
+ }
220
+
221
+ // Remove all namespaces that used this connector
222
+ namespacesUsingConnector.forEach(ns => {
223
+ delete baseState.connections[ns];
224
+ });
225
+
226
+ // Remove activeNamespace if it is in the list of namespaces using the connector
227
+ if (
228
+ baseState.activeNamespace &&
229
+ (baseState.activeNamespace === namespace ||
230
+ namespacesUsingConnector.includes(baseState.activeNamespace))
231
+ ) {
232
+ baseState.activeNamespace = undefined;
233
+ StorageUtil.setActiveNamespace(undefined);
234
+ }
235
+ }
236
+ };
@@ -50,6 +50,7 @@ export const ConnectorController = {
50
50
 
51
51
  if (saveStorage) {
52
52
  if (connectorType) {
53
+ //TODO: Check this
53
54
  StorageUtil.setConnectedConnector(connectorType);
54
55
  } else {
55
56
  StorageUtil.removeConnectedConnector();
@@ -1,5 +1,5 @@
1
1
  import { proxy, ref } from 'valtio';
2
- import type { CaipNetwork, CaipNetworkId } from '../utils/TypeUtil';
2
+ import type { CaipNetwork, CaipNetworkId } from '@reown/appkit-common-react-native';
3
3
  import { PublicStateController } from './PublicStateController';
4
4
  import { NetworkUtil } from '@reown/appkit-common-react-native';
5
5
  import { ConstantsUtil } from '../utils/ConstantsUtil';