@reown/appkit-ethers-react-native 0.0.0-feat-onramp-20250602154313 → 0.0.0-feat-multichain-20250604171123

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 (43) hide show
  1. package/lib/commonjs/adapter.js +79 -0
  2. package/lib/commonjs/adapter.js.map +1 -0
  3. package/lib/commonjs/helpers.js +33 -0
  4. package/lib/commonjs/helpers.js.map +1 -0
  5. package/lib/commonjs/index.js +3 -174
  6. package/lib/commonjs/index.js.map +1 -1
  7. package/lib/module/adapter.js +72 -0
  8. package/lib/module/adapter.js.map +1 -0
  9. package/lib/module/helpers.js +25 -0
  10. package/lib/module/helpers.js.map +1 -0
  11. package/lib/module/index.js +2 -133
  12. package/lib/module/index.js.map +1 -1
  13. package/lib/typescript/adapter.d.ts +13 -0
  14. package/lib/typescript/adapter.d.ts.map +1 -0
  15. package/lib/typescript/helpers.d.ts +3 -0
  16. package/lib/typescript/helpers.d.ts.map +1 -0
  17. package/lib/typescript/index.d.ts +2 -39
  18. package/lib/typescript/index.d.ts.map +1 -1
  19. package/package.json +6 -10
  20. package/src/adapter.ts +94 -0
  21. package/src/helpers.ts +25 -0
  22. package/src/index.tsx +2 -164
  23. package/lib/commonjs/client.js +0 -861
  24. package/lib/commonjs/client.js.map +0 -1
  25. package/lib/commonjs/utils/defaultConfig.js +0 -18
  26. package/lib/commonjs/utils/defaultConfig.js.map +0 -1
  27. package/lib/commonjs/utils/helpers.js +0 -27
  28. package/lib/commonjs/utils/helpers.js.map +0 -1
  29. package/lib/module/client.js +0 -853
  30. package/lib/module/client.js.map +0 -1
  31. package/lib/module/utils/defaultConfig.js +0 -12
  32. package/lib/module/utils/defaultConfig.js.map +0 -1
  33. package/lib/module/utils/helpers.js +0 -20
  34. package/lib/module/utils/helpers.js.map +0 -1
  35. package/lib/typescript/client.d.ts +0 -65
  36. package/lib/typescript/client.d.ts.map +0 -1
  37. package/lib/typescript/utils/defaultConfig.d.ts +0 -7
  38. package/lib/typescript/utils/defaultConfig.d.ts.map +0 -1
  39. package/lib/typescript/utils/helpers.d.ts +0 -10
  40. package/lib/typescript/utils/helpers.d.ts.map +0 -1
  41. package/src/client.ts +0 -1077
  42. package/src/utils/defaultConfig.ts +0 -19
  43. package/src/utils/helpers.ts +0 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reown/appkit-ethers-react-native",
3
- "version": "0.0.0-feat-onramp-20250602154313",
3
+ "version": "0.0.0-feat-multichain-20250604171123",
4
4
  "main": "lib/commonjs/index.js",
5
5
  "types": "lib/typescript/index.d.ts",
6
6
  "module": "lib/module/index.js",
@@ -38,23 +38,19 @@
38
38
  "access": "public"
39
39
  },
40
40
  "dependencies": {
41
- "@reown/appkit-common-react-native": "0.0.0-feat-onramp-20250602154313",
42
- "@reown/appkit-scaffold-react-native": "0.0.0-feat-onramp-20250602154313",
43
- "@reown/appkit-scaffold-utils-react-native": "0.0.0-feat-onramp-20250602154313",
44
- "@reown/appkit-siwe-react-native": "0.0.0-feat-onramp-20250602154313",
45
- "@walletconnect/ethereum-provider": "2.21.0"
41
+ "@reown/appkit-common-react-native": "0.0.0-feat-multichain-20250604171123",
42
+ "@reown/appkit-react-native": "0.0.0-feat-multichain-20250604171123",
43
+ "@reown/appkit-scaffold-utils-react-native": "0.0.0-feat-multichain-20250604171123",
44
+ "@reown/appkit-siwe-react-native": "0.0.0-feat-multichain-20250604171123",
45
+ "@walletconnect/ethereum-provider": "2.20.2"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "@react-native-async-storage/async-storage": ">=1.17.0",
49
49
  "@react-native-community/netinfo": "*",
50
50
  "@walletconnect/react-native-compat": ">=2.13.1",
51
- "ethers": ">=6.0.0",
52
51
  "react": ">=17",
53
52
  "react-native": ">=0.68.5",
54
53
  "react-native-get-random-values": "*"
55
54
  },
56
- "devDependencies": {
57
- "ethers": "6.10.0"
58
- },
59
55
  "react-native": "src/index.tsx"
60
56
  }
package/src/adapter.ts ADDED
@@ -0,0 +1,94 @@
1
+ import {
2
+ EVMAdapter,
3
+ type AppKitNetwork,
4
+ type CaipAddress,
5
+ type ChainNamespace,
6
+ type GetBalanceParams,
7
+ type GetBalanceResponse
8
+ } from '@reown/appkit-common-react-native';
9
+ import { EthersHelpersUtil } from '@reown/appkit-scaffold-utils-react-native';
10
+ import { formatEther, getEthBalance } from './helpers';
11
+
12
+ export class EthersAdapter extends EVMAdapter {
13
+ private static supportedNamespace: ChainNamespace = 'eip155';
14
+
15
+ constructor(configParams: { projectId: string }) {
16
+ super({
17
+ projectId: configParams.projectId,
18
+ supportedNamespace: EthersAdapter.supportedNamespace
19
+ });
20
+ }
21
+
22
+ async getBalance(params: GetBalanceParams): Promise<GetBalanceResponse> {
23
+ const { network, address } = params;
24
+
25
+ if (!this.connector) throw new Error('No active connector');
26
+ if (!network) throw new Error('No network provided');
27
+
28
+ const balanceAddress =
29
+ address || this.getAccounts()?.find(account => account.includes(network.id.toString()));
30
+
31
+ const balance: GetBalanceResponse = {
32
+ amount: '0.00',
33
+ symbol: network.nativeCurrency.symbol || 'ETH'
34
+ };
35
+
36
+ if (!balanceAddress) return balance;
37
+
38
+ const account = balanceAddress.split(':')[2];
39
+ const rpcUrl = network.rpcUrls.default.http?.[0];
40
+ if (!rpcUrl || !account) return balance;
41
+
42
+ try {
43
+ const wei = await getEthBalance(rpcUrl, account);
44
+ balance.amount = formatEther(wei);
45
+
46
+ this.emit('balanceChanged', { address: balanceAddress, balance });
47
+
48
+ return balance;
49
+ } catch {
50
+ return balance;
51
+ }
52
+ }
53
+
54
+ async switchNetwork(network: AppKitNetwork): Promise<void> {
55
+ if (!this.connector) throw new Error('No active connector');
56
+
57
+ const provider = this.connector.getProvider();
58
+ if (!provider) throw new Error('No active provider');
59
+
60
+ try {
61
+ await provider.request(
62
+ {
63
+ method: 'wallet_switchEthereumChain',
64
+ params: [{ chainId: EthersHelpersUtil.numberToHexString(Number(network.id)) }] //TODO: check util
65
+ },
66
+ `${EthersAdapter.supportedNamespace}:${network.id}`
67
+ );
68
+ } catch (switchError: any) {
69
+ const message = switchError?.message as string;
70
+ if (/(?<temp1>user rejected)/u.test(message?.toLowerCase())) {
71
+ throw new Error('Chain is not supported');
72
+ }
73
+
74
+ throw switchError;
75
+ }
76
+ }
77
+
78
+ getAccounts(): CaipAddress[] | undefined {
79
+ if (!this.connector) throw new Error('No active connector');
80
+ const namespaces = this.connector.getNamespaces();
81
+
82
+ return namespaces[this.getSupportedNamespace()]?.accounts;
83
+ }
84
+
85
+ disconnect(): Promise<void> {
86
+ if (!this.connector) throw new Error('EthersAdapter:disconnect - No active connector');
87
+
88
+ return this.connector.disconnect();
89
+ }
90
+
91
+ getSupportedNamespace(): ChainNamespace {
92
+ return EthersAdapter.supportedNamespace;
93
+ }
94
+ }
package/src/helpers.ts ADDED
@@ -0,0 +1,25 @@
1
+ // Helper to convert Wei (as string or bigint) to ETH
2
+ export const formatEther = (wei: bigint): string => {
3
+ return (Number(wei) / 1e18).toString();
4
+ };
5
+
6
+ // Raw JSON-RPC for balance lookup
7
+ export async function getEthBalance(rpcUrl: string, address: string): Promise<bigint> {
8
+ const body = {
9
+ jsonrpc: '2.0',
10
+ method: 'eth_getBalance',
11
+ params: [address, 'latest'],
12
+ id: 1
13
+ };
14
+
15
+ const response = await fetch(rpcUrl, {
16
+ method: 'POST',
17
+ headers: { 'Content-Type': 'application/json' },
18
+ body: JSON.stringify(body)
19
+ });
20
+
21
+ const json = await response.json();
22
+ if (json.error) throw new Error(json.error.message);
23
+
24
+ return BigInt(json.result); // result is hex string
25
+ }
package/src/index.tsx CHANGED
@@ -1,164 +1,2 @@
1
- import { useEffect, useState, useSyncExternalStore } from 'react';
2
- import { useSnapshot } from 'valtio';
3
- import { EthersStoreUtil, type Provider } from '@reown/appkit-scaffold-utils-react-native';
4
-
5
- export {
6
- AccountButton,
7
- AppKitButton,
8
- ConnectButton,
9
- NetworkButton,
10
- AppKit
11
- } from '@reown/appkit-scaffold-react-native';
12
- import type { EventName, EventsControllerState } from '@reown/appkit-scaffold-react-native';
13
- import { ConstantsUtil } from '@reown/appkit-common-react-native';
14
- export { defaultConfig } from './utils/defaultConfig';
15
-
16
- import { AppKit, type AppKitOptions } from './client';
17
-
18
- // -- Types -------------------------------------------------------------------
19
- export type { AppKitOptions } from './client';
20
-
21
- type OpenOptions = Parameters<AppKit['open']>[0];
22
-
23
- // -- Setup -------------------------------------------------------------------
24
- let modal: AppKit | undefined;
25
-
26
- export function createAppKit(options: AppKitOptions) {
27
- if (!modal) {
28
- modal = new AppKit({
29
- ...options,
30
- _sdkVersion: `react-native-ethers-${ConstantsUtil.VERSION}`
31
- });
32
- }
33
-
34
- return modal;
35
- }
36
-
37
- // -- Hooks -------------------------------------------------------------------
38
- export function useAppKit() {
39
- if (!modal) {
40
- throw new Error('Please call "createAppKit" before using "useAppKit" hook');
41
- }
42
-
43
- async function open(options?: OpenOptions) {
44
- await modal?.open(options);
45
- }
46
-
47
- async function close() {
48
- await modal?.close();
49
- }
50
-
51
- return { open, close };
52
- }
53
-
54
- export function useAppKitState() {
55
- if (!modal) {
56
- throw new Error('Please call "createAppKit" before using "useAppKitState" hook');
57
- }
58
-
59
- const [state, setState] = useState(modal.getState());
60
-
61
- useEffect(() => {
62
- const unsubscribe = modal?.subscribeState(newState => {
63
- if (newState) setState({ ...newState });
64
- });
65
-
66
- return () => {
67
- unsubscribe?.();
68
- };
69
- }, []);
70
-
71
- return state;
72
- }
73
-
74
- export function useAppKitProvider() {
75
- const { provider, providerType } = useSnapshot(EthersStoreUtil.state);
76
-
77
- const walletProvider = provider as Provider | undefined;
78
- const walletProviderType = providerType;
79
-
80
- return {
81
- walletProvider,
82
- walletProviderType
83
- };
84
- }
85
-
86
- export function useDisconnect() {
87
- async function disconnect() {
88
- await modal?.disconnect();
89
- }
90
-
91
- return {
92
- disconnect
93
- };
94
- }
95
-
96
- export function useAppKitAccount() {
97
- const { address, isConnected, chainId } = useSnapshot(EthersStoreUtil.state);
98
-
99
- return {
100
- address,
101
- isConnected,
102
- chainId
103
- };
104
- }
105
-
106
- export function useWalletInfo() {
107
- if (!modal) {
108
- throw new Error('Please call "createAppKit" before using "useWalletInfo" hook');
109
- }
110
-
111
- const walletInfo = useSyncExternalStore(
112
- modal.subscribeWalletInfo,
113
- modal.getWalletInfo,
114
- modal.getWalletInfo
115
- );
116
-
117
- return { walletInfo };
118
- }
119
-
120
- export function useAppKitError() {
121
- const { error } = useSnapshot(EthersStoreUtil.state);
122
-
123
- return {
124
- error
125
- };
126
- }
127
-
128
- export function useAppKitEvents(callback?: (newEvent: EventsControllerState) => void) {
129
- if (!modal) {
130
- throw new Error('Please call "createAppKit" before using "useAppKitEvents" hook');
131
- }
132
-
133
- const [event, setEvents] = useState(modal.getEvent());
134
-
135
- useEffect(() => {
136
- const unsubscribe = modal?.subscribeEvents(newEvent => {
137
- setEvents({ ...newEvent });
138
- callback?.(newEvent);
139
- });
140
-
141
- return () => {
142
- unsubscribe?.();
143
- };
144
- }, [callback]);
145
-
146
- return event;
147
- }
148
-
149
- export function useAppKitEventSubscription(
150
- event: EventName,
151
- callback: (newEvent: EventsControllerState) => void
152
- ) {
153
- if (!modal) {
154
- throw new Error('Please call "createAppKit" before using "useAppKitEventSubscription" hook');
155
- }
156
-
157
- useEffect(() => {
158
- const unsubscribe = modal?.subscribeEvent(event, callback);
159
-
160
- return () => {
161
- unsubscribe?.();
162
- };
163
- }, [callback, event]);
164
- }
1
+ import { EthersAdapter } from './adapter';
2
+ export { EthersAdapter };