Best Practice Notes
The following rules can help DApps connect to UKey Wallet more stably in a multi-wallet environment and keep the interface recoverable when users reject, switch networks, or disconnect.
1. Prioritize the use of EIP-6963
Don't think of window.ethereum as the only entry point. EIP-6963 can discover multiple wallets in the same browser, allowing users to explicitly select UKey Wallet to avoid requests being taken over by other wallets.
2. Handle user rejection normally
Rejecting connections, refusing to sign, and canceling transactions are all normal user behaviors. The app should retain the current page state, display understandable prompts, and allow the user to reinitiate the action.
try {
await provider.request({ method: 'eth_requestAccounts' });
} catch (err) {
if (err.code === 4001) {
// Note: EIP-1193 User rejected request error
console.log('Please connect the wallet before continuing.');
} else {
console.error('Caught provider error:', err);
}
}
3. Respond promptly to network changes
Users may switch networks within the wallet. After receiving chainChanged, the chain-related status should be refreshed; if the application status is complex, refreshing the page is usually the safest way to handle it.
4. Avoid polling
Do not use setInterval to repeatedly read account or chain information. Prioritize monitoring of accountsChanged, chainChanged and other events, and then trigger status refresh as needed.