@reown/appkit-ethers-react-native 0.0.0-canary-20240912195440
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/client.js +659 -0
- package/lib/commonjs/client.js.map +1 -0
- package/lib/commonjs/index.js +170 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/utils/defaultConfig.js +18 -0
- package/lib/commonjs/utils/defaultConfig.js.map +1 -0
- package/lib/commonjs/utils/helpers.js +27 -0
- package/lib/commonjs/utils/helpers.js.map +1 -0
- package/lib/module/client.js +651 -0
- package/lib/module/client.js.map +1 -0
- package/lib/module/index.js +121 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/utils/defaultConfig.js +12 -0
- package/lib/module/utils/defaultConfig.js.map +1 -0
- package/lib/module/utils/helpers.js +20 -0
- package/lib/module/utils/helpers.js.map +1 -0
- package/lib/typescript/client.d.ts +61 -0
- package/lib/typescript/client.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +39 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/utils/defaultConfig.d.ts +7 -0
- package/lib/typescript/utils/defaultConfig.d.ts.map +1 -0
- package/lib/typescript/utils/helpers.d.ts +10 -0
- package/lib/typescript/utils/helpers.d.ts.map +1 -0
- package/package.json +60 -0
- package/readme.md +9 -0
- package/src/client.ts +827 -0
- package/src/index.tsx +150 -0
- package/src/utils/defaultConfig.ts +19 -0
- package/src/utils/helpers.ts +27 -0
package/src/index.tsx
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { useEffect, useState, useSyncExternalStore } from 'react';
|
|
2
|
+
import { useSnapshot } from 'valtio';
|
|
3
|
+
import {
|
|
4
|
+
ConstantsUtil,
|
|
5
|
+
EthersStoreUtil,
|
|
6
|
+
type Provider
|
|
7
|
+
} from '@reown/appkit-scaffold-utils-react-native';
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
AccountButton,
|
|
11
|
+
AppKitButton,
|
|
12
|
+
ConnectButton,
|
|
13
|
+
NetworkButton,
|
|
14
|
+
AppKit
|
|
15
|
+
} from '@reown/appkit-scaffold-react-native';
|
|
16
|
+
|
|
17
|
+
export { defaultConfig } from './utils/defaultConfig';
|
|
18
|
+
|
|
19
|
+
import type { AppKitOptions } from './client';
|
|
20
|
+
import { AppKit } from './client';
|
|
21
|
+
|
|
22
|
+
// -- Types -------------------------------------------------------------------
|
|
23
|
+
export type { AppKitOptions } from './client';
|
|
24
|
+
|
|
25
|
+
type OpenOptions = Parameters<AppKit['open']>[0];
|
|
26
|
+
|
|
27
|
+
// -- Setup -------------------------------------------------------------------
|
|
28
|
+
let modal: AppKit | undefined;
|
|
29
|
+
|
|
30
|
+
export function createAppKit(options: AppKitOptions) {
|
|
31
|
+
if (!modal) {
|
|
32
|
+
modal = new AppKit({
|
|
33
|
+
...options,
|
|
34
|
+
_sdkVersion: `react-native-ethers-${ConstantsUtil.VERSION}`
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return modal;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// -- Hooks -------------------------------------------------------------------
|
|
42
|
+
export function useAppKit() {
|
|
43
|
+
if (!modal) {
|
|
44
|
+
throw new Error('Please call "createAppKit" before using "useAppKit" hook');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function open(options?: OpenOptions) {
|
|
48
|
+
await modal?.open(options);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function close() {
|
|
52
|
+
await modal?.close();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return { open, close };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function useAppKitState() {
|
|
59
|
+
if (!modal) {
|
|
60
|
+
throw new Error('Please call "createAppKit" before using "useAppKitState" hook');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const [state, setState] = useState(modal.getState());
|
|
64
|
+
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
const unsubscribe = modal?.subscribeState(newState => {
|
|
67
|
+
if (newState) setState({ ...newState });
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return () => {
|
|
71
|
+
unsubscribe?.();
|
|
72
|
+
};
|
|
73
|
+
}, []);
|
|
74
|
+
|
|
75
|
+
return state;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function useAppKitProvider() {
|
|
79
|
+
const { provider, providerType } = useSnapshot(EthersStoreUtil.state);
|
|
80
|
+
|
|
81
|
+
const walletProvider = provider as Provider | undefined;
|
|
82
|
+
const walletProviderType = providerType;
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
walletProvider,
|
|
86
|
+
walletProviderType
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function useDisconnect() {
|
|
91
|
+
async function disconnect() {
|
|
92
|
+
await modal?.disconnect();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
disconnect
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function useAppKitAccount() {
|
|
101
|
+
const { address, isConnected, chainId } = useSnapshot(EthersStoreUtil.state);
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
address,
|
|
105
|
+
isConnected,
|
|
106
|
+
chainId
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function useWalletInfo() {
|
|
111
|
+
if (!modal) {
|
|
112
|
+
throw new Error('Please call "createAppKit" before using "useWalletInfo" hook');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const walletInfo = useSyncExternalStore(
|
|
116
|
+
modal.subscribeWalletInfo,
|
|
117
|
+
modal.getWalletInfo,
|
|
118
|
+
modal.getWalletInfo
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
return { walletInfo };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function useAppKitError() {
|
|
125
|
+
const { error } = useSnapshot(EthersStoreUtil.state);
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
error
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function useAppKitEvents() {
|
|
133
|
+
if (!modal) {
|
|
134
|
+
throw new Error('Please call "createAppKit" before using "useAppKitEvents" hook');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const [event, setEvents] = useState(modal.getEvent());
|
|
138
|
+
|
|
139
|
+
useEffect(() => {
|
|
140
|
+
const unsubscribe = modal?.subscribeEvents(newEvent => {
|
|
141
|
+
setEvents({ ...newEvent });
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
return () => {
|
|
145
|
+
unsubscribe?.();
|
|
146
|
+
};
|
|
147
|
+
}, []);
|
|
148
|
+
|
|
149
|
+
return event;
|
|
150
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Metadata,
|
|
3
|
+
Provider,
|
|
4
|
+
ProviderType,
|
|
5
|
+
AppKitFrameProvider
|
|
6
|
+
} from '@reown/appkit-scaffold-utils-react-native';
|
|
7
|
+
|
|
8
|
+
export interface ConfigOptions {
|
|
9
|
+
metadata: Metadata;
|
|
10
|
+
extraConnectors?: (Provider | AppKitFrameProvider)[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function defaultConfig(options: ConfigOptions) {
|
|
14
|
+
const { metadata, extraConnectors } = options;
|
|
15
|
+
|
|
16
|
+
let providers: ProviderType = { metadata, extraConnectors };
|
|
17
|
+
|
|
18
|
+
return providers;
|
|
19
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { CaipNetworkId } from '@reown/appkit-scaffold-react-native';
|
|
2
|
+
import { PresetsUtil, ConstantsUtil } from '@reown/appkit-scaffold-utils-react-native';
|
|
3
|
+
import EthereumProvider from '@walletconnect/ethereum-provider';
|
|
4
|
+
|
|
5
|
+
export async function getWalletConnectCaipNetworks(provider?: EthereumProvider) {
|
|
6
|
+
if (!provider) {
|
|
7
|
+
throw new Error('networkControllerClient:getApprovedCaipNetworks - provider is undefined');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const ns = provider.signer?.session?.namespaces;
|
|
11
|
+
const nsMethods = ns?.[ConstantsUtil.EIP155]?.methods;
|
|
12
|
+
const nsChains = ns?.[ConstantsUtil.EIP155]?.chains as CaipNetworkId[];
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
supportsAllNetworks: Boolean(nsMethods?.includes(ConstantsUtil.ADD_CHAIN_METHOD)),
|
|
16
|
+
approvedCaipNetworkIds: nsChains
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function getAuthCaipNetworks() {
|
|
21
|
+
return {
|
|
22
|
+
supportsAllNetworks: false,
|
|
23
|
+
approvedCaipNetworkIds: PresetsUtil.RpcChainIds.map(
|
|
24
|
+
id => `${ConstantsUtil.EIP155}:${id}`
|
|
25
|
+
) as CaipNetworkId[]
|
|
26
|
+
};
|
|
27
|
+
}
|