@reown/appkit-ethers-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.
- package/lib/commonjs/adapter.js +80 -0
- package/lib/commonjs/adapter.js.map +1 -0
- package/lib/commonjs/helpers.js +33 -0
- package/lib/commonjs/helpers.js.map +1 -0
- package/lib/commonjs/index.js +3 -174
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/adapter.js +73 -0
- package/lib/module/adapter.js.map +1 -0
- package/lib/module/helpers.js +25 -0
- package/lib/module/helpers.js.map +1 -0
- package/lib/module/index.js +2 -133
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/adapter.d.ts +13 -0
- package/lib/typescript/adapter.d.ts.map +1 -0
- package/lib/typescript/helpers.d.ts +3 -0
- package/lib/typescript/helpers.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +2 -40
- package/lib/typescript/index.d.ts.map +1 -1
- package/package.json +6 -10
- package/src/adapter.ts +98 -0
- package/src/helpers.ts +25 -0
- package/src/index.tsx +2 -165
- package/lib/commonjs/client.js +0 -857
- package/lib/commonjs/client.js.map +0 -1
- package/lib/commonjs/utils/defaultConfig.js +0 -18
- package/lib/commonjs/utils/defaultConfig.js.map +0 -1
- package/lib/commonjs/utils/helpers.js +0 -27
- package/lib/commonjs/utils/helpers.js.map +0 -1
- package/lib/module/client.js +0 -849
- package/lib/module/client.js.map +0 -1
- package/lib/module/utils/defaultConfig.js +0 -12
- package/lib/module/utils/defaultConfig.js.map +0 -1
- package/lib/module/utils/helpers.js +0 -20
- package/lib/module/utils/helpers.js.map +0 -1
- package/lib/typescript/client.d.ts +0 -65
- package/lib/typescript/client.d.ts.map +0 -1
- package/lib/typescript/utils/defaultConfig.d.ts +0 -7
- package/lib/typescript/utils/defaultConfig.d.ts.map +0 -1
- package/lib/typescript/utils/helpers.d.ts +0 -10
- package/lib/typescript/utils/helpers.d.ts.map +0 -1
- package/src/client.ts +0 -1071
- package/src/utils/defaultConfig.ts +0 -19
- 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": "
|
|
3
|
+
"version": "2.0.0-alpha.0",
|
|
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": "
|
|
42
|
-
"@reown/appkit-
|
|
43
|
-
"@reown/appkit-scaffold-utils-react-native": "
|
|
44
|
-
"@reown/appkit-siwe-react-native": "
|
|
45
|
-
"@walletconnect/ethereum-provider": "2.
|
|
41
|
+
"@reown/appkit-common-react-native": "2.0.0-alpha.0",
|
|
42
|
+
"@reown/appkit-react-native": "2.0.0-alpha.0",
|
|
43
|
+
"@reown/appkit-scaffold-utils-react-native": "2.0.0-alpha.0",
|
|
44
|
+
"@reown/appkit-siwe-react-native": "2.0.0-alpha.0",
|
|
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,98 @@
|
|
|
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', {
|
|
47
|
+
namespace: this.getSupportedNamespace(),
|
|
48
|
+
address: balanceAddress,
|
|
49
|
+
balance
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return balance;
|
|
53
|
+
} catch {
|
|
54
|
+
return balance;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async switchNetwork(network: AppKitNetwork): Promise<void> {
|
|
59
|
+
if (!this.connector) throw new Error('No active connector');
|
|
60
|
+
|
|
61
|
+
const provider = this.connector.getProvider();
|
|
62
|
+
if (!provider) throw new Error('No active provider');
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
await provider.request(
|
|
66
|
+
{
|
|
67
|
+
method: 'wallet_switchEthereumChain',
|
|
68
|
+
params: [{ chainId: EthersHelpersUtil.numberToHexString(Number(network.id)) }] //TODO: check util
|
|
69
|
+
},
|
|
70
|
+
`${EthersAdapter.supportedNamespace}:${network.id}`
|
|
71
|
+
);
|
|
72
|
+
} catch (switchError: any) {
|
|
73
|
+
const message = switchError?.message as string;
|
|
74
|
+
if (/(?<temp1>user rejected)/u.test(message?.toLowerCase())) {
|
|
75
|
+
throw new Error('Chain is not supported');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
throw switchError;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
getAccounts(): CaipAddress[] | undefined {
|
|
83
|
+
if (!this.connector) throw new Error('No active connector');
|
|
84
|
+
const namespaces = this.connector.getNamespaces();
|
|
85
|
+
|
|
86
|
+
return namespaces[this.getSupportedNamespace()]?.accounts;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
disconnect(): Promise<void> {
|
|
90
|
+
if (!this.connector) throw new Error('EthersAdapter:disconnect - No active connector');
|
|
91
|
+
|
|
92
|
+
return this.connector.disconnect();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
getSupportedNamespace(): ChainNamespace {
|
|
96
|
+
return EthersAdapter.supportedNamespace;
|
|
97
|
+
}
|
|
98
|
+
}
|
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,165 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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 };
|