@reown/appkit-ethers-react-native 0.0.0-feat-onramp-20250409135734 → 0.0.0-feat-multichain-20250513150920

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 (36) hide show
  1. package/lib/commonjs/adapter.js +98 -0
  2. package/lib/commonjs/adapter.js.map +1 -0
  3. package/lib/commonjs/index.js +3 -174
  4. package/lib/commonjs/index.js.map +1 -1
  5. package/lib/module/adapter.js +91 -0
  6. package/lib/module/adapter.js.map +1 -0
  7. package/lib/module/index.js +2 -133
  8. package/lib/module/index.js.map +1 -1
  9. package/lib/typescript/adapter.d.ts +14 -0
  10. package/lib/typescript/adapter.d.ts.map +1 -0
  11. package/lib/typescript/index.d.ts +2 -40
  12. package/lib/typescript/index.d.ts.map +1 -1
  13. package/package.json +6 -6
  14. package/src/adapter.ts +111 -0
  15. package/src/index.tsx +2 -165
  16. package/lib/commonjs/client.js +0 -857
  17. package/lib/commonjs/client.js.map +0 -1
  18. package/lib/commonjs/utils/defaultConfig.js +0 -18
  19. package/lib/commonjs/utils/defaultConfig.js.map +0 -1
  20. package/lib/commonjs/utils/helpers.js +0 -27
  21. package/lib/commonjs/utils/helpers.js.map +0 -1
  22. package/lib/module/client.js +0 -849
  23. package/lib/module/client.js.map +0 -1
  24. package/lib/module/utils/defaultConfig.js +0 -12
  25. package/lib/module/utils/defaultConfig.js.map +0 -1
  26. package/lib/module/utils/helpers.js +0 -20
  27. package/lib/module/utils/helpers.js.map +0 -1
  28. package/lib/typescript/client.d.ts +0 -65
  29. package/lib/typescript/client.d.ts.map +0 -1
  30. package/lib/typescript/utils/defaultConfig.d.ts +0 -7
  31. package/lib/typescript/utils/defaultConfig.d.ts.map +0 -1
  32. package/lib/typescript/utils/helpers.d.ts +0 -10
  33. package/lib/typescript/utils/helpers.d.ts.map +0 -1
  34. package/src/client.ts +0 -1071
  35. package/src/utils/defaultConfig.ts +0 -19
  36. package/src/utils/helpers.ts +0 -27
package/src/adapter.ts ADDED
@@ -0,0 +1,111 @@
1
+ import { formatEther, JsonRpcProvider } from 'ethers';
2
+ import {
3
+ EVMAdapter,
4
+ type AppKitNetwork,
5
+ type CaipAddress,
6
+ type ChainNamespace,
7
+ type GetBalanceParams,
8
+ type GetBalanceResponse
9
+ } from '@reown/appkit-common-react-native';
10
+ import { EthersHelpersUtil } from '@reown/appkit-scaffold-utils-react-native';
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
+ let balance = { amount: '0.00', symbol: network.nativeCurrency.symbol || 'ETH' };
32
+
33
+ if (!balanceAddress) {
34
+ return Promise.resolve(balance);
35
+ }
36
+
37
+ const account = balanceAddress.split(':')[2];
38
+
39
+ try {
40
+ const jsonRpcProvider = new JsonRpcProvider(network.rpcUrls.default.http[0], {
41
+ chainId: Number(network.id),
42
+ name: network.name
43
+ });
44
+
45
+ if (jsonRpcProvider && account) {
46
+ const _balance = await jsonRpcProvider.getBalance(account);
47
+ const formattedBalance = formatEther(_balance);
48
+
49
+ balance = { amount: formattedBalance, symbol: network.nativeCurrency.symbol || 'ETH' };
50
+ }
51
+
52
+ this.emit('balanceChanged', {
53
+ namespace: this.getSupportedNamespace(),
54
+ address: balanceAddress,
55
+ balance
56
+ });
57
+
58
+ return balance;
59
+ } catch (error) {
60
+ return balance;
61
+ }
62
+ }
63
+
64
+ async switchNetwork(network: AppKitNetwork): Promise<void> {
65
+ if (!this.connector) throw new Error('No active connector');
66
+
67
+ const provider = this.connector.getProvider();
68
+ if (!provider) throw new Error('No active provider');
69
+
70
+ try {
71
+ await provider.request(
72
+ {
73
+ method: 'wallet_switchEthereumChain',
74
+ params: [{ chainId: EthersHelpersUtil.numberToHexString(Number(network.id)) }] //TODO: check util
75
+ },
76
+ `${network.chainNamespace ?? 'eip155'}:${network.id}`
77
+ );
78
+ } catch (switchError: any) {
79
+ const message = switchError?.message as string;
80
+ if (/(?<temp1>user rejected)/u.test(message?.toLowerCase())) {
81
+ throw new Error('Chain is not supported');
82
+ }
83
+
84
+ throw switchError;
85
+ }
86
+ }
87
+
88
+ getAccounts(): CaipAddress[] | undefined {
89
+ if (!this.connector) throw new Error('No active connector');
90
+ const namespaces = this.connector.getNamespaces();
91
+
92
+ return namespaces[this.getSupportedNamespace()]?.accounts;
93
+ }
94
+
95
+ disconnect(): Promise<void> {
96
+ if (!this.connector) throw new Error('EthersAdapter:disconnect - No active connector');
97
+
98
+ return this.connector.disconnect();
99
+ }
100
+
101
+ async request(method: string, params?: any[]) {
102
+ if (!this.connector) throw new Error('No active connector');
103
+ const provider = this.connector.getProvider();
104
+
105
+ return provider.request({ method, params });
106
+ }
107
+
108
+ getSupportedNamespace(): ChainNamespace {
109
+ return EthersAdapter.supportedNamespace;
110
+ }
111
+ }
package/src/index.tsx CHANGED
@@ -1,165 +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 type { AppKitOptions } from './client';
17
- import { AppKit } from './client';
18
-
19
- // -- Types -------------------------------------------------------------------
20
- export type { AppKitOptions } from './client';
21
-
22
- type OpenOptions = Parameters<AppKit['open']>[0];
23
-
24
- // -- Setup -------------------------------------------------------------------
25
- let modal: AppKit | undefined;
26
-
27
- export function createAppKit(options: AppKitOptions) {
28
- if (!modal) {
29
- modal = new AppKit({
30
- ...options,
31
- _sdkVersion: `react-native-ethers-${ConstantsUtil.VERSION}`
32
- });
33
- }
34
-
35
- return modal;
36
- }
37
-
38
- // -- Hooks -------------------------------------------------------------------
39
- export function useAppKit() {
40
- if (!modal) {
41
- throw new Error('Please call "createAppKit" before using "useAppKit" hook');
42
- }
43
-
44
- async function open(options?: OpenOptions) {
45
- await modal?.open(options);
46
- }
47
-
48
- async function close() {
49
- await modal?.close();
50
- }
51
-
52
- return { open, close };
53
- }
54
-
55
- export function useAppKitState() {
56
- if (!modal) {
57
- throw new Error('Please call "createAppKit" before using "useAppKitState" hook');
58
- }
59
-
60
- const [state, setState] = useState(modal.getState());
61
-
62
- useEffect(() => {
63
- const unsubscribe = modal?.subscribeState(newState => {
64
- if (newState) setState({ ...newState });
65
- });
66
-
67
- return () => {
68
- unsubscribe?.();
69
- };
70
- }, []);
71
-
72
- return state;
73
- }
74
-
75
- export function useAppKitProvider() {
76
- const { provider, providerType } = useSnapshot(EthersStoreUtil.state);
77
-
78
- const walletProvider = provider as Provider | undefined;
79
- const walletProviderType = providerType;
80
-
81
- return {
82
- walletProvider,
83
- walletProviderType
84
- };
85
- }
86
-
87
- export function useDisconnect() {
88
- async function disconnect() {
89
- await modal?.disconnect();
90
- }
91
-
92
- return {
93
- disconnect
94
- };
95
- }
96
-
97
- export function useAppKitAccount() {
98
- const { address, isConnected, chainId } = useSnapshot(EthersStoreUtil.state);
99
-
100
- return {
101
- address,
102
- isConnected,
103
- chainId
104
- };
105
- }
106
-
107
- export function useWalletInfo() {
108
- if (!modal) {
109
- throw new Error('Please call "createAppKit" before using "useWalletInfo" hook');
110
- }
111
-
112
- const walletInfo = useSyncExternalStore(
113
- modal.subscribeWalletInfo,
114
- modal.getWalletInfo,
115
- modal.getWalletInfo
116
- );
117
-
118
- return { walletInfo };
119
- }
120
-
121
- export function useAppKitError() {
122
- const { error } = useSnapshot(EthersStoreUtil.state);
123
-
124
- return {
125
- error
126
- };
127
- }
128
-
129
- export function useAppKitEvents(callback?: (newEvent: EventsControllerState) => void) {
130
- if (!modal) {
131
- throw new Error('Please call "createAppKit" before using "useAppKitEvents" hook');
132
- }
133
-
134
- const [event, setEvents] = useState(modal.getEvent());
135
-
136
- useEffect(() => {
137
- const unsubscribe = modal?.subscribeEvents(newEvent => {
138
- setEvents({ ...newEvent });
139
- callback?.(newEvent);
140
- });
141
-
142
- return () => {
143
- unsubscribe?.();
144
- };
145
- }, [callback]);
146
-
147
- return event;
148
- }
149
-
150
- export function useAppKitEventSubscription(
151
- event: EventName,
152
- callback: (newEvent: EventsControllerState) => void
153
- ) {
154
- if (!modal) {
155
- throw new Error('Please call "createAppKit" before using "useAppKitEventSubscription" hook');
156
- }
157
-
158
- useEffect(() => {
159
- const unsubscribe = modal?.subscribeEvent(event, callback);
160
-
161
- return () => {
162
- unsubscribe?.();
163
- };
164
- }, [callback, event]);
165
- }
1
+ import { EthersAdapter } from './adapter';
2
+ export { EthersAdapter };