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

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 (44) hide show
  1. package/lib/commonjs/controllers/ConnectionsController.js +75 -26
  2. package/lib/commonjs/controllers/ConnectionsController.js.map +1 -1
  3. package/lib/commonjs/controllers/OptionsController.js +5 -0
  4. package/lib/commonjs/controllers/OptionsController.js.map +1 -1
  5. package/lib/commonjs/controllers/SendController.js +5 -5
  6. package/lib/commonjs/controllers/SendController.js.map +1 -1
  7. package/lib/commonjs/controllers/SwapController.js +39 -36
  8. package/lib/commonjs/controllers/SwapController.js.map +1 -1
  9. package/lib/commonjs/utils/ConstantsUtil.js +109 -1
  10. package/lib/commonjs/utils/ConstantsUtil.js.map +1 -1
  11. package/lib/commonjs/utils/SwapApiUtil.js +10 -6
  12. package/lib/commonjs/utils/SwapApiUtil.js.map +1 -1
  13. package/lib/module/controllers/ConnectionsController.js +75 -26
  14. package/lib/module/controllers/ConnectionsController.js.map +1 -1
  15. package/lib/module/controllers/OptionsController.js +5 -0
  16. package/lib/module/controllers/OptionsController.js.map +1 -1
  17. package/lib/module/controllers/SendController.js +5 -5
  18. package/lib/module/controllers/SendController.js.map +1 -1
  19. package/lib/module/controllers/SwapController.js +39 -36
  20. package/lib/module/controllers/SwapController.js.map +1 -1
  21. package/lib/module/utils/ConstantsUtil.js +109 -1
  22. package/lib/module/utils/ConstantsUtil.js.map +1 -1
  23. package/lib/module/utils/SwapApiUtil.js +10 -6
  24. package/lib/module/utils/SwapApiUtil.js.map +1 -1
  25. package/lib/typescript/controllers/ConnectionsController.d.ts +13 -11
  26. package/lib/typescript/controllers/ConnectionsController.d.ts.map +1 -1
  27. package/lib/typescript/controllers/OptionsController.d.ts +4 -2
  28. package/lib/typescript/controllers/OptionsController.d.ts.map +1 -1
  29. package/lib/typescript/controllers/SwapController.d.ts +1 -1
  30. package/lib/typescript/controllers/SwapController.d.ts.map +1 -1
  31. package/lib/typescript/utils/ConstantsUtil.d.ts +1 -0
  32. package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -1
  33. package/lib/typescript/utils/SwapApiUtil.d.ts.map +1 -1
  34. package/lib/typescript/utils/TypeUtil.d.ts +1 -12
  35. package/lib/typescript/utils/TypeUtil.d.ts.map +1 -1
  36. package/package.json +2 -2
  37. package/src/controllers/ConnectionController.ts +1 -1
  38. package/src/controllers/ConnectionsController.ts +98 -43
  39. package/src/controllers/OptionsController.ts +9 -9
  40. package/src/controllers/SendController.ts +5 -5
  41. package/src/controllers/SwapController.ts +47 -46
  42. package/src/utils/ConstantsUtil.ts +111 -1
  43. package/src/utils/SwapApiUtil.ts +13 -7
  44. package/src/utils/TypeUtil.ts +2 -13
@@ -1,10 +1,9 @@
1
1
  import { subscribeKey as subKey } from 'valtio/utils';
2
2
  import { proxy, subscribe as sub } from 'valtio';
3
- import { NumberUtil } from '@reown/appkit-common-react-native';
3
+ import { NumberUtil, type CaipAddress } from '@reown/appkit-common-react-native';
4
4
 
5
5
  import { ConstantsUtil } from '../utils/ConstantsUtil';
6
6
  import { SwapApiUtil } from '../utils/SwapApiUtil';
7
- import { NetworkController } from './NetworkController';
8
7
  import { BlockchainApiController } from './BlockchainApiController';
9
8
  import { OptionsController } from './OptionsController';
10
9
  import { SwapCalculationUtil } from '../utils/SwapCalculationUtil';
@@ -14,7 +13,6 @@ import type { SwapInputTarget, SwapTokenWithBalance } from '../utils/TypeUtil';
14
13
  import { ConnectorController } from './ConnectorController';
15
14
  import { AccountController } from './AccountController';
16
15
  import { CoreHelperUtil } from '../utils/CoreHelperUtil';
17
- import { ConnectionController } from './ConnectionController';
18
16
  import { TransactionsController } from './TransactionsController';
19
17
  import { EventsController } from './EventsController';
20
18
  import { ConnectionsController } from './ConnectionsController';
@@ -166,9 +164,7 @@ export const SwapController = {
166
164
  throw new Error('No active namespace or network found to swap the tokens from.');
167
165
  }
168
166
 
169
- const networkAddress = `${activeNetwork.caipNetworkId ?? 'eip155:1'}:${
170
- ConstantsUtil.NATIVE_TOKEN_ADDRESS[activeNamespace]
171
- }`;
167
+ const networkAddress: CaipAddress = `${activeNetwork.caipNetworkId}:${ConstantsUtil.NATIVE_TOKEN_ADDRESS[activeNamespace]}`;
172
168
 
173
169
  const type = ConnectorController.state.connectedConnector;
174
170
 
@@ -291,7 +287,6 @@ export const SwapController = {
291
287
 
292
288
  async getMyTokensWithBalance(forceUpdate?: string) {
293
289
  const balances = await SwapApiUtil.getMyTokensWithBalance(forceUpdate);
294
-
295
290
  if (!balances) {
296
291
  return;
297
292
  }
@@ -410,7 +405,7 @@ export const SwapController = {
410
405
 
411
406
  setBalances(balances: SwapTokenWithBalance[]) {
412
407
  const { networkAddress } = this.getParams();
413
- const caipNetwork = NetworkController.state.caipNetwork;
408
+ const caipNetwork = ConnectionsController.state.activeNetwork;
414
409
 
415
410
  if (!caipNetwork) {
416
411
  return;
@@ -422,7 +417,9 @@ export const SwapController = {
422
417
  state.tokensPriceMap[token.address] = token.price || 0;
423
418
  });
424
419
 
425
- state.myTokensWithBalance = balances.filter(token => token.address?.startsWith(caipNetwork.id));
420
+ state.myTokensWithBalance = balances.filter(
421
+ token => token.address?.startsWith(caipNetwork.caipNetworkId)
422
+ );
426
423
 
427
424
  state.networkBalanceInUSD = networkToken
428
425
  ? NumberUtil.multiply(networkToken.quantity.numeric, networkToken.price).toString()
@@ -473,12 +470,12 @@ export const SwapController = {
473
470
 
474
471
  // -- Swap ---------------------------------------------- //
475
472
  async swapTokens() {
476
- const address = AccountController.state.address as `${string}:${string}:${string}`;
473
+ const address = ConnectionsController.state.activeAddress;
477
474
  const sourceToken = state.sourceToken;
478
475
  const toToken = state.toToken;
479
476
  const haveSourceTokenAmount = NumberUtil.bigNumber(state.sourceTokenAmount).isGreaterThan(0);
480
477
 
481
- if (!toToken || !sourceToken || state.loadingPrices || !haveSourceTokenAmount) {
478
+ if (!toToken || !sourceToken || state.loadingPrices || !haveSourceTokenAmount || !address) {
482
479
  return;
483
480
  }
484
481
 
@@ -488,39 +485,44 @@ export const SwapController = {
488
485
  .multipliedBy(10 ** sourceToken.decimals)
489
486
  .integerValue();
490
487
 
491
- const quoteResponse = await BlockchainApiController.fetchSwapQuote({
492
- userAddress: address,
493
- projectId: OptionsController.state.projectId,
494
- from: sourceToken.address,
495
- to: toToken.address,
496
- gasPrice: state.gasFee,
497
- amount: amountDecimal.toString()
498
- });
488
+ try {
489
+ const quoteResponse = await BlockchainApiController.fetchSwapQuote({
490
+ userAddress: address,
491
+ projectId: OptionsController.state.projectId,
492
+ from: sourceToken.address,
493
+ to: toToken.address,
494
+ gasPrice: state.gasFee,
495
+ amount: amountDecimal.toString()
496
+ });
499
497
 
500
- state.loadingQuote = false;
498
+ state.loadingQuote = false;
501
499
 
502
- const quoteToAmount = quoteResponse?.quotes?.[0]?.toAmount;
500
+ const quoteToAmount = quoteResponse?.quotes?.[0]?.toAmount;
503
501
 
504
- if (!quoteToAmount) {
505
- return;
506
- }
502
+ if (!quoteToAmount) {
503
+ return;
504
+ }
507
505
 
508
- const toTokenAmount = NumberUtil.bigNumber(quoteToAmount)
509
- .dividedBy(10 ** toToken.decimals)
510
- .toString();
506
+ const toTokenAmount = NumberUtil.bigNumber(quoteToAmount)
507
+ .dividedBy(10 ** toToken.decimals)
508
+ .toString();
511
509
 
512
- this.setToTokenAmount(toTokenAmount);
510
+ this.setToTokenAmount(toTokenAmount);
513
511
 
514
- const isInsufficientToken = this.hasInsufficientToken(
515
- state.sourceTokenAmount,
516
- sourceToken.address
517
- );
512
+ const isInsufficientToken = this.hasInsufficientToken(
513
+ state.sourceTokenAmount,
514
+ sourceToken.address
515
+ );
518
516
 
519
- if (isInsufficientToken) {
520
- state.inputError = 'Insufficient balance';
521
- } else {
522
- state.inputError = undefined;
523
- this.setTransactionDetails();
517
+ if (isInsufficientToken) {
518
+ state.inputError = 'Insufficient balance';
519
+ } else {
520
+ state.inputError = undefined;
521
+ this.setTransactionDetails();
522
+ }
523
+ } catch (error) {
524
+ SnackController.showError('Failed to get swap quote');
525
+ state.loadingQuote = false;
524
526
  }
525
527
  },
526
528
 
@@ -589,7 +591,7 @@ export const SwapController = {
589
591
  if (!response) {
590
592
  throw new Error('createAllowanceTransaction - No response from generateApproveCalldata');
591
593
  }
592
- const gasLimit = await ConnectionController.estimateGas({
594
+ const gasLimit = await ConnectionsController.estimateGas({
593
595
  address: fromAddress as `0x${string}`,
594
596
  to: CoreHelperUtil.getPlainAddress(response.tx.to) as `0x${string}`,
595
597
  data: response.tx.data
@@ -642,7 +644,7 @@ export const SwapController = {
642
644
  return undefined;
643
645
  }
644
646
 
645
- const amount = ConnectionController.parseUnits(
647
+ const amount = ConnectionsController.parseUnits(
646
648
  sourceTokenAmount,
647
649
  sourceToken.decimals
648
650
  )?.toString();
@@ -710,13 +712,13 @@ export const SwapController = {
710
712
  }
711
713
 
712
714
  try {
713
- await ConnectionController.sendTransaction({
715
+ await ConnectionsController.sendTransaction({
714
716
  address: fromAddress as `0x${string}`,
715
717
  to: data.to as `0x${string}`,
716
718
  data: data.data as `0x${string}`,
717
719
  value: BigInt(data.value),
718
720
  gasPrice: BigInt(data.gasPrice),
719
- chainNamespace: 'eip155'
721
+ chainNamespace: ConnectionsController.state.activeNamespace
720
722
  });
721
723
 
722
724
  await this.swapTokens();
@@ -735,7 +737,6 @@ export const SwapController = {
735
737
  if (!data) {
736
738
  return undefined;
737
739
  }
738
-
739
740
  const { fromAddress, toTokenAmount, isAuthConnector } = this.getParams();
740
741
 
741
742
  state.loadingTransaction = true;
@@ -762,14 +763,14 @@ export const SwapController = {
762
763
 
763
764
  try {
764
765
  const forceUpdateAddresses = [state.sourceToken?.address, state.toToken?.address].join(',');
765
- const transactionHash = await ConnectionController.sendTransaction({
766
+ const transactionHash = await ConnectionsController.sendTransaction({
766
767
  address: fromAddress as `0x${string}`,
767
768
  to: data.to as `0x${string}`,
768
769
  data: data.data as `0x${string}`,
769
770
  gas: data.gas,
770
771
  gasPrice: BigInt(data.gasPrice),
771
772
  value: data.value,
772
- chainNamespace: 'eip155'
773
+ chainNamespace: ConnectionsController.state.activeNamespace
773
774
  });
774
775
 
775
776
  state.loadingTransaction = false;
@@ -778,7 +779,7 @@ export const SwapController = {
778
779
  type: 'track',
779
780
  event: 'SWAP_SUCCESS',
780
781
  properties: {
781
- network: NetworkController.state.caipNetwork?.id || '',
782
+ network: ConnectionsController.state.activeNetwork?.caipNetworkId || '',
782
783
  swapFromToken: this.state.sourceToken?.symbol || '',
783
784
  swapToToken: this.state.toToken?.symbol || '',
784
785
  swapFromAmount: this.state.sourceTokenAmount || '',
@@ -810,7 +811,7 @@ export const SwapController = {
810
811
  event: 'SWAP_ERROR',
811
812
  properties: {
812
813
  message: error?.shortMessage ?? error?.message ?? 'Unknown',
813
- network: NetworkController.state.caipNetwork?.id || '',
814
+ network: ConnectionsController.state.activeNetwork?.caipNetworkId || '',
814
815
  swapFromToken: this.state.sourceToken?.symbol || '',
815
816
  swapToToken: this.state.toToken?.symbol || '',
816
817
  swapFromAmount: this.state.sourceTokenAmount || '',
@@ -3,7 +3,7 @@ import type { Features } from './TypeUtil';
3
3
 
4
4
  //TODO: enable this again after implemented
5
5
  const defaultFeatures: Features = {
6
- swaps: false,
6
+ swaps: true,
7
7
  onramp: false,
8
8
  email: false,
9
9
  emailShowWallets: false,
@@ -135,6 +135,116 @@ export const ConstantsUtil = {
135
135
  'WNT'
136
136
  ],
137
137
 
138
+ //TODO: replace with supported chains from backend
139
+ ACTIVITY_SUPPORTED_CHAINS: [
140
+ // Arbitrum
141
+ 'eip155:42161',
142
+ // BNB Chain
143
+ 'eip155:56',
144
+ // Ethereum
145
+ 'eip155:1',
146
+ // Blast
147
+ 'eip155:81457',
148
+ // Ape Chain
149
+ 'eip155:99999',
150
+ // Avalanche
151
+ 'eip155:43114',
152
+ // Abstract
153
+ 'eip155:900',
154
+ // opBNB
155
+ 'eip155:204',
156
+ // Astar zkEVM
157
+ 'eip155:3776',
158
+ // ZKsync Era
159
+ 'eip155:324',
160
+ // Berachain
161
+ 'eip155:80085',
162
+ // BOB
163
+ 'eip155:60808',
164
+ // Cyber
165
+ 'eip155:7560',
166
+ // Degen Chain
167
+ 'eip155:666666666',
168
+ // Fraxtal
169
+ 'eip155:252',
170
+ // Gravity Alpha
171
+ 'eip155:10003',
172
+ // Ink
173
+ 'eip155:999',
174
+ // Lens
175
+ 'eip155:1348',
176
+ // Lisk
177
+ 'eip155:113',
178
+ // Mode
179
+ 'eip155:34443',
180
+ // Base
181
+ 'eip155:8453',
182
+ // Mantle
183
+ 'eip155:5000',
184
+ // Optimism
185
+ 'eip155:10',
186
+ // Polygon
187
+ 'eip155:137',
188
+ // Celo
189
+ 'eip155:42220',
190
+ // Manta Pacific
191
+ 'eip155:169',
192
+ // Gnosis Chain
193
+ 'eip155:100',
194
+ // Fantom
195
+ 'eip155:250',
196
+ // Ronin
197
+ 'eip155:2020',
198
+ // Linea
199
+ 'eip155:59144',
200
+ // Metis Andromeda
201
+ 'eip155:1088',
202
+ // Aurora
203
+ 'eip155:1313161554',
204
+ // XDC
205
+ 'eip155:50',
206
+ // Cronos zkEVM
207
+ 'eip155:1030',
208
+ // Polygon zkEVM
209
+ 'eip155:1101',
210
+ // Polynomial
211
+ 'eip155:80001',
212
+ // Rari
213
+ 'eip155:1380012617',
214
+ // Redstone
215
+ 'eip155:690',
216
+ // Scroll
217
+ 'eip155:534352',
218
+ // Sei
219
+ 'eip155:1329',
220
+ // Soneium
221
+ 'eip155:1499',
222
+ // Sonic
223
+ 'eip155:7007',
224
+ // Swellchain
225
+ 'eip155:7777777',
226
+ // Taiko
227
+ 'eip155:167000',
228
+ // Viction
229
+ 'eip155:88',
230
+ // Unichain
231
+ 'eip155:12345',
232
+ // Wonder
233
+ 'eip155:8787',
234
+ // X Layer
235
+ 'eip155:196',
236
+ // World Chain
237
+ 'eip155:2008',
238
+ // ZERϴ
239
+ 'eip155:77777',
240
+ // ZkLink Nova
241
+ 'eip155:810180',
242
+ // re.al
243
+ 'eip155:666',
244
+ // Zora
245
+ 'eip155:7777777'
246
+ ],
247
+
138
248
  SWAP_SUPPORTED_NETWORKS: [
139
249
  // Ethereum'
140
250
  'eip155:1',
@@ -1,18 +1,19 @@
1
1
  import { BlockchainApiController } from '../controllers/BlockchainApiController';
2
2
  import { OptionsController } from '../controllers/OptionsController';
3
- import { NetworkController } from '../controllers/NetworkController';
4
3
  import type {
5
4
  BlockchainApiBalanceResponse,
6
5
  BlockchainApiSwapAllowanceRequest,
7
6
  SwapTokenWithBalance
8
7
  } from './TypeUtil';
9
8
  import { AccountController } from '../controllers/AccountController';
10
- import { ConnectionController } from '../controllers/ConnectionController';
11
9
  import { ConnectionsController } from '../controllers/ConnectionsController';
10
+ import type { CaipNetworkId } from '@reown/appkit-common-react-native';
11
+ import { ConstantsUtil } from './ConstantsUtil';
12
12
 
13
13
  export const SwapApiUtil = {
14
14
  async getTokenList() {
15
- const chainId = ConnectionsController.state.activeNetwork?.caipNetworkId ?? 'eip155:1';
15
+ const chainId: CaipNetworkId =
16
+ ConnectionsController.state.activeNetwork?.caipNetworkId ?? 'eip155:1';
16
17
  const response = await BlockchainApiController.fetchSwapTokens({
17
18
  projectId: OptionsController.state.projectId,
18
19
  chainId
@@ -54,7 +55,7 @@ export const SwapApiUtil = {
54
55
 
55
56
  if (response?.allowance && sourceTokenAmount && sourceTokenDecimals) {
56
57
  const parsedValue =
57
- ConnectionController.parseUnits(sourceTokenAmount, sourceTokenDecimals) || 0;
58
+ ConnectionsController.parseUnits(sourceTokenAmount, sourceTokenDecimals) || 0;
58
59
  const hasAllowance = BigInt(response.allowance) >= parsedValue;
59
60
 
60
61
  return hasAllowance;
@@ -84,12 +85,17 @@ export const SwapApiUtil = {
84
85
  },
85
86
 
86
87
  mapBalancesToSwapTokens(balances?: BlockchainApiBalanceResponse['balances']) {
88
+ const { activeNamespace, activeCaipNetworkId } = ConnectionsController.state;
89
+ const address = activeNamespace
90
+ ? ConstantsUtil.NATIVE_TOKEN_ADDRESS[activeNamespace]
91
+ : undefined;
92
+
87
93
  return (
88
94
  balances?.map(
89
95
  token =>
90
96
  ({
91
97
  ...token,
92
- address: token?.address || NetworkController.getActiveNetworkTokenAddress(),
98
+ address: token?.address ?? `${token?.chainId ?? activeCaipNetworkId}:${address}`,
93
99
  decimals: parseInt(token.quantity.decimals, 10),
94
100
  logoUri: token.iconUrl,
95
101
  eip2612: false
@@ -100,7 +106,7 @@ export const SwapApiUtil = {
100
106
 
101
107
  async fetchGasPrice() {
102
108
  const projectId = OptionsController.state.projectId;
103
- const caipNetwork = NetworkController.state.caipNetwork;
109
+ const caipNetwork = ConnectionsController.state.activeNetwork;
104
110
 
105
111
  if (!caipNetwork) {
106
112
  return null;
@@ -108,7 +114,7 @@ export const SwapApiUtil = {
108
114
 
109
115
  return await BlockchainApiController.fetchGasPrice({
110
116
  projectId,
111
- chainId: caipNetwork.id
117
+ chainId: caipNetwork.caipNetworkId
112
118
  });
113
119
  }
114
120
  };
@@ -6,7 +6,8 @@ import type {
6
6
  SocialProvider,
7
7
  ThemeMode,
8
8
  Transaction,
9
- ConnectorType
9
+ ConnectorType,
10
+ Metadata
10
11
  } from '@reown/appkit-common-react-native';
11
12
 
12
13
  import { OnRampErrorType } from './ConstantsUtil';
@@ -339,18 +340,6 @@ export type BlockchainApiOnRampWidgetResponse = {
339
340
  };
340
341
 
341
342
  // -- OptionsController Types ---------------------------------------------------
342
- export type Metadata = {
343
- name: string;
344
- description: string;
345
- url: string;
346
- icons: string[];
347
- redirect?: {
348
- native?: string;
349
- universal?: string;
350
- linkMode?: boolean;
351
- };
352
- };
353
-
354
343
  export type CustomWallet = Pick<
355
344
  WcWallet,
356
345
  | 'id'