@reown/appkit-wagmi-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 +144 -0
- package/lib/commonjs/adapter.js.map +1 -0
- package/lib/commonjs/connectors/UniversalConnector.js +214 -0
- package/lib/commonjs/connectors/UniversalConnector.js.map +1 -0
- package/lib/commonjs/index.js +8 -121
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/helpers.js +16 -44
- package/lib/commonjs/utils/helpers.js.map +1 -1
- package/lib/module/adapter.js +137 -0
- package/lib/module/adapter.js.map +1 -0
- package/lib/module/connectors/UniversalConnector.js +208 -0
- package/lib/module/connectors/UniversalConnector.js.map +1 -0
- package/lib/module/index.js +3 -92
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/helpers.js +13 -39
- package/lib/module/utils/helpers.js.map +1 -1
- package/lib/typescript/adapter.d.ts +24 -0
- package/lib/typescript/adapter.d.ts.map +1 -0
- package/lib/typescript/connectors/UniversalConnector.d.ts +5 -0
- package/lib/typescript/connectors/UniversalConnector.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +3 -25
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/helpers.d.ts +3 -13
- package/lib/typescript/utils/helpers.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/adapter.ts +184 -0
- package/src/connectors/UniversalConnector.ts +228 -0
- package/src/index.tsx +3 -122
- package/src/utils/helpers.ts +17 -56
- package/lib/commonjs/client.js +0 -516
- package/lib/commonjs/client.js.map +0 -1
- package/lib/commonjs/connectors/WalletConnectConnector.js +0 -353
- package/lib/commonjs/connectors/WalletConnectConnector.js.map +0 -1
- package/lib/commonjs/utils/defaultWagmiConfig.js +0 -46
- package/lib/commonjs/utils/defaultWagmiConfig.js.map +0 -1
- package/lib/module/client.js +0 -510
- package/lib/module/client.js.map +0 -1
- package/lib/module/connectors/WalletConnectConnector.js +0 -348
- package/lib/module/connectors/WalletConnectConnector.js.map +0 -1
- package/lib/module/utils/defaultWagmiConfig.js +0 -40
- package/lib/module/utils/defaultWagmiConfig.js.map +0 -1
- package/lib/typescript/client.d.ts +0 -40
- package/lib/typescript/client.d.ts.map +0 -1
- package/lib/typescript/connectors/WalletConnectConnector.d.ts +0 -74
- package/lib/typescript/connectors/WalletConnectConnector.d.ts.map +0 -1
- package/lib/typescript/utils/defaultWagmiConfig.d.ts +0 -11
- package/lib/typescript/utils/defaultWagmiConfig.d.ts.map +0 -1
- package/src/client.ts +0 -644
- package/src/connectors/WalletConnectConnector.ts +0 -469
- package/src/utils/defaultWagmiConfig.ts +0 -53
|
@@ -1,18 +1,8 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import type { Connector } from '@wagmi/core';
|
|
3
|
-
import type { AppKitClientOptions } from '../client';
|
|
4
|
-
export declare function getCaipDefaultChain(chain?: AppKitClientOptions['defaultChain']): CaipNetwork | undefined;
|
|
5
|
-
export declare function getWalletConnectCaipNetworks(connector?: Connector): Promise<{
|
|
6
|
-
supportsAllNetworks: boolean;
|
|
7
|
-
approvedCaipNetworkIds: `${string}:${string}`[];
|
|
8
|
-
}>;
|
|
9
|
-
export declare function getAuthCaipNetworks(): {
|
|
10
|
-
supportsAllNetworks: boolean;
|
|
11
|
-
approvedCaipNetworkIds: `${string}:${string}`[];
|
|
12
|
-
};
|
|
1
|
+
import { type AppKitNetwork, type Network } from '@reown/appkit-common-react-native';
|
|
13
2
|
export declare function getTransport({ chainId, projectId }: {
|
|
14
3
|
chainId: number;
|
|
15
4
|
projectId: string;
|
|
16
5
|
}): import("viem").HttpTransport<undefined, false>;
|
|
17
|
-
export declare function
|
|
6
|
+
export declare function formatNetwork(network: Network): AppKitNetwork;
|
|
7
|
+
export declare function formatNetworks(networks: Network[]): AppKitNetwork[];
|
|
18
8
|
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/utils/helpers.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/utils/helpers.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,aAAa,EAClB,KAAK,OAAO,EACb,MAAM,mCAAmC,CAAC;AAG3C,wBAAgB,YAAY,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,kDAQ1F;AAED,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,aAAa,CAO7D;AAED,wBAAgB,cAAc,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,aAAa,EAAE,CAEnE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reown/appkit-wagmi-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",
|
|
@@ -39,10 +39,10 @@
|
|
|
39
39
|
"access": "public"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@reown/appkit-common-react-native": "
|
|
43
|
-
"@reown/appkit-
|
|
44
|
-
"@reown/appkit-scaffold-utils-react-native": "
|
|
45
|
-
"@reown/appkit-siwe-react-native": "
|
|
42
|
+
"@reown/appkit-common-react-native": "2.0.0-alpha.0",
|
|
43
|
+
"@reown/appkit-react-native": "2.0.0-alpha.0",
|
|
44
|
+
"@reown/appkit-scaffold-utils-react-native": "2.0.0-alpha.0",
|
|
45
|
+
"@reown/appkit-siwe-react-native": "2.0.0-alpha.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"@react-native-async-storage/async-storage": ">=1.17.0",
|
package/src/adapter.ts
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EVMAdapter,
|
|
3
|
+
WalletConnector,
|
|
4
|
+
type AppKitNetwork,
|
|
5
|
+
type CaipAddress,
|
|
6
|
+
type ChainNamespace,
|
|
7
|
+
type GetBalanceParams,
|
|
8
|
+
type GetBalanceResponse
|
|
9
|
+
} from '@reown/appkit-common-react-native';
|
|
10
|
+
import {
|
|
11
|
+
type Config,
|
|
12
|
+
type CreateConfigParameters,
|
|
13
|
+
createConfig,
|
|
14
|
+
getBalance as getBalanceWagmi,
|
|
15
|
+
switchChain as switchChainWagmi,
|
|
16
|
+
disconnect as disconnectWagmiCore,
|
|
17
|
+
connect as connectWagmi,
|
|
18
|
+
type Connector
|
|
19
|
+
} from '@wagmi/core';
|
|
20
|
+
import type { Chain } from 'wagmi/chains';
|
|
21
|
+
import { getTransport } from './utils/helpers';
|
|
22
|
+
import { formatUnits, type Hex } from 'viem';
|
|
23
|
+
import { UniversalConnector } from './connectors/UniversalConnector';
|
|
24
|
+
|
|
25
|
+
type ConfigParams = Partial<CreateConfigParameters> & {
|
|
26
|
+
networks: readonly [Chain, ...Chain[]];
|
|
27
|
+
projectId: string;
|
|
28
|
+
connectors?: Connector[];
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export class WagmiAdapter extends EVMAdapter {
|
|
32
|
+
private static supportedNamespace: ChainNamespace = 'eip155';
|
|
33
|
+
public wagmiChains: readonly Chain[] | undefined;
|
|
34
|
+
public wagmiConfig!: Config;
|
|
35
|
+
private wagmiConfigConnector?: Connector;
|
|
36
|
+
|
|
37
|
+
constructor(configParams: ConfigParams) {
|
|
38
|
+
super({
|
|
39
|
+
projectId: configParams.projectId,
|
|
40
|
+
supportedNamespace: WagmiAdapter.supportedNamespace
|
|
41
|
+
});
|
|
42
|
+
this.wagmiChains = configParams.networks;
|
|
43
|
+
this.wagmiConfig = this.createWagmiInternalConfig(configParams);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private createWagmiInternalConfig(configParams: ConfigParams): Config {
|
|
47
|
+
// Connectors are typically added via wagmiConfig.connectors, but here AppKit manages the connection.
|
|
48
|
+
// We'll use the `connect` action with our dynamically created connector instance.
|
|
49
|
+
// So, the `connectors` array for createConfig can be empty and is added later.
|
|
50
|
+
const initialConnectors: (() => Connector)[] = [];
|
|
51
|
+
|
|
52
|
+
const transportsArr = configParams.networks.map(chain => [
|
|
53
|
+
chain.id,
|
|
54
|
+
getTransport({ chainId: chain.id, projectId: configParams.projectId })
|
|
55
|
+
]);
|
|
56
|
+
const transports = Object.fromEntries(transportsArr);
|
|
57
|
+
|
|
58
|
+
return createConfig({
|
|
59
|
+
chains: configParams.networks,
|
|
60
|
+
connectors: initialConnectors, // Empty, as we connect programmatically
|
|
61
|
+
transports,
|
|
62
|
+
multiInjectedProviderDiscovery: false
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async switchNetwork(network: AppKitNetwork): Promise<void> {
|
|
67
|
+
if (!this.wagmiConfigConnector) {
|
|
68
|
+
throw new Error('WagmiAdapter: AppKit connector not set or not connected via Wagmi.');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
await switchChainWagmi(this.wagmiConfig, {
|
|
72
|
+
chainId: network.id as number,
|
|
73
|
+
connector: this.wagmiConfigConnector
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async getBalance(params: GetBalanceParams): Promise<GetBalanceResponse> {
|
|
78
|
+
const { network, address, tokens } = params;
|
|
79
|
+
|
|
80
|
+
if (!this.connector) throw new Error('No active AppKit connector (EVMAdapter.connector)');
|
|
81
|
+
if (!network) throw new Error('No network provided');
|
|
82
|
+
|
|
83
|
+
if (!this.wagmiConfigConnector) {
|
|
84
|
+
throw new Error('WagmiAdapter: AppKit connector not properly configured with Wagmi.');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const balanceAddress =
|
|
88
|
+
address ||
|
|
89
|
+
this.getAccounts()?.find((acc: CaipAddress) => acc.includes(network.id.toString()));
|
|
90
|
+
|
|
91
|
+
if (!balanceAddress) {
|
|
92
|
+
return Promise.resolve({ amount: '0.00', symbol: network.nativeCurrency.symbol || 'ETH' });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const accountHex = balanceAddress.split(':')[2] as Hex;
|
|
96
|
+
|
|
97
|
+
const token = network?.caipNetworkId && (tokens?.[network.caipNetworkId]?.address as Hex);
|
|
98
|
+
|
|
99
|
+
const balance = await getBalanceWagmi(this.wagmiConfig, {
|
|
100
|
+
address: accountHex,
|
|
101
|
+
chainId: network.id as number,
|
|
102
|
+
token
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const formattedBalance = {
|
|
106
|
+
amount: formatUnits(balance.value, balance.decimals),
|
|
107
|
+
symbol: balance.symbol,
|
|
108
|
+
contractAddress: token ? (`${network.caipNetworkId}:${token}` as CaipAddress) : undefined
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
this.emit('balanceChanged', {
|
|
112
|
+
namespace: this.getSupportedNamespace(),
|
|
113
|
+
address: balanceAddress,
|
|
114
|
+
balance: formattedBalance
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
return Promise.resolve(formattedBalance);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
getAccounts(): CaipAddress[] | undefined {
|
|
121
|
+
if (!this.connector) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const namespaces = this.connector.getNamespaces();
|
|
126
|
+
if (!namespaces) {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const supportedNamespaceKey = this.getSupportedNamespace();
|
|
131
|
+
const accountsForNamespace = namespaces[supportedNamespaceKey];
|
|
132
|
+
|
|
133
|
+
return accountsForNamespace?.accounts;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async disconnect(): Promise<void> {
|
|
137
|
+
if (this.wagmiConfigConnector) {
|
|
138
|
+
await disconnectWagmiCore(this.wagmiConfig, { connector: this.wagmiConfigConnector });
|
|
139
|
+
this.wagmiConfigConnector = undefined;
|
|
140
|
+
} else if (this.connector) {
|
|
141
|
+
await this.connector.disconnect();
|
|
142
|
+
this.onDisconnect();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const evmAdapterInstance = this as any;
|
|
146
|
+
if ('connector' in evmAdapterInstance) {
|
|
147
|
+
evmAdapterInstance.connector = undefined;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
getSupportedNamespace(): ChainNamespace {
|
|
152
|
+
return WagmiAdapter.supportedNamespace;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
override setConnector(_connector: WalletConnector): void {
|
|
156
|
+
super.setConnector(_connector);
|
|
157
|
+
|
|
158
|
+
if (_connector && this.wagmiChains) {
|
|
159
|
+
if (!this.wagmiConfigConnector) {
|
|
160
|
+
// Manually add the connector to the wagmiConfig
|
|
161
|
+
const connectorInstance = this.wagmiConfig._internal.connectors.setup(
|
|
162
|
+
UniversalConnector(_connector)
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
this.wagmiConfig._internal.connectors.setState(prev => [...prev, connectorInstance]);
|
|
166
|
+
this.wagmiConfigConnector = connectorInstance;
|
|
167
|
+
|
|
168
|
+
connectorInstance.emitter.on('message', ({ type }: { type: string }) => {
|
|
169
|
+
if (type === 'externalDisconnect') {
|
|
170
|
+
this.onDisconnect();
|
|
171
|
+
|
|
172
|
+
this.wagmiConfigConnector = undefined;
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
connectWagmi(this.wagmiConfig, { connector: connectorInstance });
|
|
178
|
+
} catch (error) {
|
|
179
|
+
this.wagmiConfigConnector = undefined;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Provider,
|
|
3
|
+
RequestArguments,
|
|
4
|
+
WalletConnector
|
|
5
|
+
} from '@reown/appkit-common-react-native';
|
|
6
|
+
import {
|
|
7
|
+
getAddress,
|
|
8
|
+
numberToHex,
|
|
9
|
+
SwitchChainError,
|
|
10
|
+
UserRejectedRequestError,
|
|
11
|
+
type Hex
|
|
12
|
+
} from 'viem';
|
|
13
|
+
import { ChainNotConfiguredError, createConnector, ProviderNotFoundError } from 'wagmi';
|
|
14
|
+
import { formatNetwork } from '../utils/helpers';
|
|
15
|
+
|
|
16
|
+
export function UniversalConnector(appKitProvidedConnector: WalletConnector) {
|
|
17
|
+
let provider: Provider | undefined;
|
|
18
|
+
|
|
19
|
+
let accountsChangedHandler: ((accounts: string[]) => void) | undefined;
|
|
20
|
+
let chainChangedHandler: ((chainId: string | number) => void) | undefined;
|
|
21
|
+
let disconnectHandler: ((error?: Error) => void) | undefined;
|
|
22
|
+
|
|
23
|
+
type AppKitConnectorProperties = { ready: boolean };
|
|
24
|
+
|
|
25
|
+
return createConnector<Provider, AppKitConnectorProperties>(config => ({
|
|
26
|
+
id: 'walletconnect',
|
|
27
|
+
name: 'WalletConnect',
|
|
28
|
+
type: 'walletconnect' as const,
|
|
29
|
+
ready: !!appKitProvidedConnector.getProvider(),
|
|
30
|
+
|
|
31
|
+
async setup() {
|
|
32
|
+
provider = appKitProvidedConnector.getProvider();
|
|
33
|
+
if (provider?.on) {
|
|
34
|
+
accountsChangedHandler = (accounts: string[]) => {
|
|
35
|
+
const hexAccounts = accounts.map(acc => getAddress(acc));
|
|
36
|
+
config.emitter.emit('change', { accounts: hexAccounts });
|
|
37
|
+
if (hexAccounts.length === 0) {
|
|
38
|
+
config.emitter.emit('disconnect');
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
chainChangedHandler = (chainId: string | number) => {
|
|
42
|
+
const newChainId = typeof chainId === 'string' ? parseInt(chainId, 10) : chainId;
|
|
43
|
+
config.emitter.emit('change', { chainId: newChainId });
|
|
44
|
+
};
|
|
45
|
+
disconnectHandler = (error?: Error) => {
|
|
46
|
+
config.emitter.emit('disconnect');
|
|
47
|
+
if (error) config.emitter.emit('error', { error });
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
if (accountsChangedHandler) provider.on('accountsChanged', accountsChangedHandler);
|
|
51
|
+
if (chainChangedHandler) provider.on('chainChanged', chainChangedHandler);
|
|
52
|
+
if (disconnectHandler) provider.on('disconnect', disconnectHandler);
|
|
53
|
+
if (disconnectHandler) provider.on('session_delete', disconnectHandler);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
async connect({ chainId } = {}) {
|
|
58
|
+
try {
|
|
59
|
+
const _provider = await this.getProvider();
|
|
60
|
+
if (!_provider) throw new ProviderNotFoundError();
|
|
61
|
+
|
|
62
|
+
// AppKit connector is already connected or handles its own connection.
|
|
63
|
+
// We just need to sync its state with Wagmi.
|
|
64
|
+
const accountAddresses = await this.getAccounts();
|
|
65
|
+
if (!accountAddresses || accountAddresses.length === 0) {
|
|
66
|
+
throw new UserRejectedRequestError(
|
|
67
|
+
new Error('No accounts found or user rejected connection via AppKit.')
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
let currentChainId = await this.getChainId();
|
|
72
|
+
|
|
73
|
+
// Handle chain switching if requested and different
|
|
74
|
+
if (chainId && currentChainId !== chainId) {
|
|
75
|
+
await this.switchChain?.({ chainId });
|
|
76
|
+
currentChainId = chainId;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
this.ready = true;
|
|
80
|
+
|
|
81
|
+
return { accounts: accountAddresses, chainId: currentChainId };
|
|
82
|
+
} catch (error) {
|
|
83
|
+
if (error instanceof UserRejectedRequestError) throw error;
|
|
84
|
+
throw new UserRejectedRequestError(error as Error); // Generalize other errors as user rejection for simplicity
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
async disconnect() {
|
|
89
|
+
await appKitProvidedConnector.disconnect();
|
|
90
|
+
config.emitter.emit('message', { type: 'externalDisconnect' });
|
|
91
|
+
if (provider?.off && accountsChangedHandler && chainChangedHandler && disconnectHandler) {
|
|
92
|
+
provider.off('accountsChanged', accountsChangedHandler);
|
|
93
|
+
provider.off('chainChanged', chainChangedHandler);
|
|
94
|
+
provider.off('disconnect', disconnectHandler);
|
|
95
|
+
provider.off('session_delete', disconnectHandler);
|
|
96
|
+
accountsChangedHandler = undefined;
|
|
97
|
+
chainChangedHandler = undefined;
|
|
98
|
+
disconnectHandler = undefined;
|
|
99
|
+
}
|
|
100
|
+
this.ready = false;
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
async getAccounts() {
|
|
104
|
+
const namespaces = appKitProvidedConnector.getNamespaces();
|
|
105
|
+
// @ts-ignore
|
|
106
|
+
const eip155Accounts = namespaces?.eip155?.accounts;
|
|
107
|
+
if (!eip155Accounts) return [] as readonly Hex[];
|
|
108
|
+
|
|
109
|
+
return eip155Accounts
|
|
110
|
+
.map((caipAddr: string) => {
|
|
111
|
+
const parts = caipAddr.split(':');
|
|
112
|
+
|
|
113
|
+
return parts.length === 3 ? parts[2] : null;
|
|
114
|
+
})
|
|
115
|
+
.filter((addrPart): addrPart is string => !!addrPart)
|
|
116
|
+
.map((addrPart: string) => getAddress(addrPart)) as readonly Hex[];
|
|
117
|
+
},
|
|
118
|
+
|
|
119
|
+
async getChainId() {
|
|
120
|
+
const chainId = appKitProvidedConnector.getChainId('eip155')?.split(':')[1];
|
|
121
|
+
|
|
122
|
+
if (chainId) return parseInt(chainId, 10);
|
|
123
|
+
|
|
124
|
+
// Fallback: Try to get from CAIP accounts if available
|
|
125
|
+
const namespaces = appKitProvidedConnector.getNamespaces();
|
|
126
|
+
// @ts-ignore
|
|
127
|
+
const eip155Accounts = namespaces?.eip155?.accounts;
|
|
128
|
+
if (eip155Accounts && eip155Accounts.length > 0) {
|
|
129
|
+
const parts = eip155Accounts[0]?.split(':');
|
|
130
|
+
if (parts && parts.length > 1 && typeof parts[1] === 'string') {
|
|
131
|
+
const chainIdNum = parseInt(parts[1], 10);
|
|
132
|
+
if (!isNaN(chainIdNum)) {
|
|
133
|
+
return chainIdNum;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (config.chains && config.chains.length > 0) return config.chains[0].id;
|
|
138
|
+
throw new Error('Unable to determine chainId.');
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
async getProvider() {
|
|
142
|
+
if (!provider) {
|
|
143
|
+
provider = appKitProvidedConnector.getProvider();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const chainId = await this.getChainId();
|
|
147
|
+
|
|
148
|
+
//TODO: Review this with gancho
|
|
149
|
+
const _provider = {
|
|
150
|
+
...provider,
|
|
151
|
+
request: (args: RequestArguments) => {
|
|
152
|
+
return provider?.request(args, `eip155:${chainId}`);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
return Promise.resolve(_provider as Provider);
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
async isAuthorized() {
|
|
160
|
+
try {
|
|
161
|
+
const accounts = await this.getAccounts();
|
|
162
|
+
|
|
163
|
+
return !!(accounts && accounts.length > 0);
|
|
164
|
+
} catch {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
|
|
169
|
+
async switchChain({ chainId }) {
|
|
170
|
+
const _provider = await this.getProvider();
|
|
171
|
+
if (!_provider) throw new Error('Provider not available for switching chain.');
|
|
172
|
+
const newChain = config.chains.find(c => c.id === chainId);
|
|
173
|
+
|
|
174
|
+
if (!newChain) throw new SwitchChainError(new ChainNotConfiguredError());
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
await _provider.request({
|
|
178
|
+
method: 'wallet_switchEthereumChain',
|
|
179
|
+
params: [{ chainId: numberToHex(chainId) }]
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
config.emitter.emit('change', { chainId });
|
|
183
|
+
|
|
184
|
+
return newChain;
|
|
185
|
+
} catch (error) {
|
|
186
|
+
// Try to add chain if switch failed (common pattern)
|
|
187
|
+
//4902 in MetaMask: Unrecognized chain ID
|
|
188
|
+
if ((error as any)?.code === 4902 || (error as any)?.data?.originalError?.code === 4902) {
|
|
189
|
+
try {
|
|
190
|
+
await _provider.request({
|
|
191
|
+
method: 'wallet_addEthereumChain',
|
|
192
|
+
params: [
|
|
193
|
+
{
|
|
194
|
+
chainId: numberToHex(chainId),
|
|
195
|
+
chainName: newChain.name,
|
|
196
|
+
nativeCurrency: newChain.nativeCurrency,
|
|
197
|
+
rpcUrls: [newChain.rpcUrls.default?.http[0] ?? ''], // Take first default HTTP RPC URL
|
|
198
|
+
blockExplorerUrls: [newChain.blockExplorers?.default?.url]
|
|
199
|
+
}
|
|
200
|
+
]
|
|
201
|
+
});
|
|
202
|
+
await appKitProvidedConnector.switchNetwork(formatNetwork(newChain));
|
|
203
|
+
config.emitter.emit('change', { chainId });
|
|
204
|
+
|
|
205
|
+
return newChain;
|
|
206
|
+
} catch (addError) {
|
|
207
|
+
throw new UserRejectedRequestError(addError as Error);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
throw new SwitchChainError(error as Error);
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
|
|
214
|
+
onAccountsChanged(accounts: string[]) {
|
|
215
|
+
if (accounts.length === 0) this.onDisconnect();
|
|
216
|
+
else config.emitter.emit('change', { accounts: accounts.map(x => getAddress(x)) });
|
|
217
|
+
},
|
|
218
|
+
|
|
219
|
+
onChainChanged(chain: string) {
|
|
220
|
+
const chainId = Number(chain);
|
|
221
|
+
config.emitter.emit('change', { chainId });
|
|
222
|
+
},
|
|
223
|
+
|
|
224
|
+
onDisconnect: () => {
|
|
225
|
+
config.emitter.emit('disconnect');
|
|
226
|
+
}
|
|
227
|
+
}));
|
|
228
|
+
}
|
package/src/index.tsx
CHANGED
|
@@ -1,123 +1,4 @@
|
|
|
1
|
-
import '
|
|
2
|
-
import { useEffect, useState, useSyncExternalStore } from 'react';
|
|
3
|
-
export {
|
|
4
|
-
AccountButton,
|
|
5
|
-
AppKitButton,
|
|
6
|
-
ConnectButton,
|
|
7
|
-
NetworkButton,
|
|
8
|
-
AppKit
|
|
9
|
-
} from '@reown/appkit-scaffold-react-native';
|
|
10
|
-
import type { EventName, EventsControllerState } from '@reown/appkit-scaffold-react-native';
|
|
11
|
-
import { ConstantsUtil } from '@reown/appkit-common-react-native';
|
|
1
|
+
import { WagmiAdapter } from './adapter';
|
|
12
2
|
|
|
13
|
-
export {
|
|
14
|
-
|
|
15
|
-
import { AppKit } from './client';
|
|
16
|
-
|
|
17
|
-
// -- Types -------------------------------------------------------------------
|
|
18
|
-
export type { AppKitOptions } from './client';
|
|
19
|
-
|
|
20
|
-
type OpenOptions = Parameters<AppKit['open']>[0];
|
|
21
|
-
|
|
22
|
-
// -- Setup -------------------------------------------------------------------
|
|
23
|
-
let modal: AppKit | undefined;
|
|
24
|
-
|
|
25
|
-
export function createAppKit(options: AppKitOptions) {
|
|
26
|
-
if (!modal) {
|
|
27
|
-
modal = new AppKit({
|
|
28
|
-
...options,
|
|
29
|
-
_sdkVersion: `react-native-wagmi-${ConstantsUtil.VERSION}`
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return modal;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// -- Hooks -------------------------------------------------------------------
|
|
37
|
-
export function useAppKit() {
|
|
38
|
-
if (!modal) {
|
|
39
|
-
throw new Error('Please call "createAppKit" before using "useAppKit" hook');
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async function open(options?: OpenOptions) {
|
|
43
|
-
await modal?.open(options);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async function close() {
|
|
47
|
-
await modal?.close();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return { open, close };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function useAppKitState() {
|
|
54
|
-
if (!modal) {
|
|
55
|
-
throw new Error('Please call "createAppKit" before using "useAppKitState" hook');
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const [state, setState] = useState(modal.getState());
|
|
59
|
-
|
|
60
|
-
useEffect(() => {
|
|
61
|
-
const unsubscribe = modal?.subscribeState(newState => {
|
|
62
|
-
if (newState) setState({ ...newState });
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
return () => {
|
|
66
|
-
unsubscribe?.();
|
|
67
|
-
};
|
|
68
|
-
}, []);
|
|
69
|
-
|
|
70
|
-
return state;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export function useWalletInfo() {
|
|
74
|
-
if (!modal) {
|
|
75
|
-
throw new Error('Please call "createAppKit" before using "useWalletInfo" hook');
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const walletInfo = useSyncExternalStore(
|
|
79
|
-
modal.subscribeWalletInfo,
|
|
80
|
-
modal.getWalletInfo,
|
|
81
|
-
modal.getWalletInfo
|
|
82
|
-
);
|
|
83
|
-
|
|
84
|
-
return { walletInfo };
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export function useAppKitEvents(callback?: (newEvent: EventsControllerState) => void) {
|
|
88
|
-
if (!modal) {
|
|
89
|
-
throw new Error('Please call "createAppKit" before using "useAppKitEvents" hook');
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const [event, setEvents] = useState(modal.getEvent());
|
|
93
|
-
|
|
94
|
-
useEffect(() => {
|
|
95
|
-
const unsubscribe = modal?.subscribeEvents(newEvent => {
|
|
96
|
-
setEvents({ ...newEvent });
|
|
97
|
-
callback?.(newEvent);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
return () => {
|
|
101
|
-
unsubscribe?.();
|
|
102
|
-
};
|
|
103
|
-
}, [callback]);
|
|
104
|
-
|
|
105
|
-
return event;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function useAppKitEventSubscription(
|
|
109
|
-
event: EventName,
|
|
110
|
-
callback: (newEvent: EventsControllerState) => void
|
|
111
|
-
) {
|
|
112
|
-
if (!modal) {
|
|
113
|
-
throw new Error('Please call "createAppKit" before using "useAppKitEventSubscription" hook');
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
useEffect(() => {
|
|
117
|
-
const unsubscribe = modal?.subscribeEvent(event, callback);
|
|
118
|
-
|
|
119
|
-
return () => {
|
|
120
|
-
unsubscribe?.();
|
|
121
|
-
};
|
|
122
|
-
}, [callback, event]);
|
|
123
|
-
}
|
|
3
|
+
export { WagmiAdapter };
|
|
4
|
+
export { formatNetworks, formatNetwork } from './utils/helpers';
|
package/src/utils/helpers.ts
CHANGED
|
@@ -1,51 +1,11 @@
|
|
|
1
|
+
import { CoreHelperUtil } from '@reown/appkit-react-native';
|
|
1
2
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import
|
|
8
|
-
import { EthereumProvider } from '@walletconnect/ethereum-provider';
|
|
9
|
-
import type { AppKitClientOptions } from '../client';
|
|
10
|
-
import { http, type Hex } from 'viem';
|
|
11
|
-
|
|
12
|
-
export function getCaipDefaultChain(chain?: AppKitClientOptions['defaultChain']) {
|
|
13
|
-
if (!chain) {
|
|
14
|
-
return undefined;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return {
|
|
18
|
-
id: `${ConstantsUtil.EIP155}:${chain.id}`,
|
|
19
|
-
name: chain.name,
|
|
20
|
-
imageId: PresetsUtil.EIP155NetworkImageIds[chain.id]
|
|
21
|
-
} as CaipNetwork;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export async function getWalletConnectCaipNetworks(connector?: Connector) {
|
|
25
|
-
if (!connector) {
|
|
26
|
-
throw new Error('networkControllerClient:getApprovedCaipNetworks - connector is undefined');
|
|
27
|
-
}
|
|
28
|
-
const provider = (await connector?.getProvider()) as Awaited<
|
|
29
|
-
ReturnType<(typeof EthereumProvider)['init']>
|
|
30
|
-
>;
|
|
31
|
-
const ns = provider?.signer?.session?.namespaces;
|
|
32
|
-
const nsMethods = ns?.[ConstantsUtil.EIP155]?.methods;
|
|
33
|
-
const nsChains = ns?.[ConstantsUtil.EIP155]?.chains as CaipNetworkId[];
|
|
34
|
-
|
|
35
|
-
return {
|
|
36
|
-
supportsAllNetworks: Boolean(nsMethods?.includes(ConstantsUtil.ADD_CHAIN_METHOD)),
|
|
37
|
-
approvedCaipNetworkIds: nsChains
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function getAuthCaipNetworks() {
|
|
42
|
-
return {
|
|
43
|
-
supportsAllNetworks: false,
|
|
44
|
-
approvedCaipNetworkIds: PresetsUtil.RpcChainIds.map(
|
|
45
|
-
id => `${ConstantsUtil.EIP155}:${id}`
|
|
46
|
-
) as CaipNetworkId[]
|
|
47
|
-
};
|
|
48
|
-
}
|
|
3
|
+
PresetsUtil,
|
|
4
|
+
ConstantsUtil,
|
|
5
|
+
type AppKitNetwork,
|
|
6
|
+
type Network
|
|
7
|
+
} from '@reown/appkit-common-react-native';
|
|
8
|
+
import { http } from 'viem';
|
|
49
9
|
|
|
50
10
|
export function getTransport({ chainId, projectId }: { chainId: number; projectId: string }) {
|
|
51
11
|
const RPC_URL = CoreHelperUtil.getBlockchainApiUrl();
|
|
@@ -57,14 +17,15 @@ export function getTransport({ chainId, projectId }: { chainId: number; projectI
|
|
|
57
17
|
return http(`${RPC_URL}/v1/?chainId=${ConstantsUtil.EIP155}:${chainId}&projectId=${projectId}`);
|
|
58
18
|
}
|
|
59
19
|
|
|
60
|
-
export function
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
20
|
+
export function formatNetwork(network: Network): AppKitNetwork {
|
|
21
|
+
return {
|
|
22
|
+
...network,
|
|
23
|
+
rpcUrls: { ...network.rpcUrls },
|
|
24
|
+
chainNamespace: 'eip155',
|
|
25
|
+
caipNetworkId: `eip155:${network.id}`
|
|
26
|
+
};
|
|
27
|
+
}
|
|
68
28
|
|
|
69
|
-
|
|
29
|
+
export function formatNetworks(networks: Network[]): AppKitNetwork[] {
|
|
30
|
+
return networks.map(formatNetwork);
|
|
70
31
|
}
|