On this page
Ethereum / EVM
Use the injected EIP-1193 provider to build EVM dApps with OneKey. All EVM-compatible chains (Ethereum, Polygon, BSC, Arbitrum, Optimism, Base, etc.) share the same provider.
Provider Detection
EIP-6963 (Recommended)
Modern multi-wallet detection that avoids conflicts:
let onekeyProvider = null
const onAnnounce = (event) => {
if (event.detail.info.rdns === 'so.onekey.app.wallet') {
onekeyProvider = event.detail.provider
}
}
window.addEventListener('eip6963:announceProvider', onAnnounce)
window.dispatchEvent(new Event('eip6963:requestProvider'))
// Call from your Connect button after the user selects OneKey.
async function connectOneKey() {
if (!onekeyProvider) throw new Error('OneKey has not announced a provider yet')
return onekeyProvider.request({ method: 'eth_requestAccounts' })
}
// On component cleanup:
// window.removeEventListener('eip6963:announceProvider', onAnnounce)Direct Access (Fallback)
const provider = window.$onekey?.ethereum
if (!provider) throw new Error('OneKey not detected')
const accounts = await provider.request({ method: 'eth_requestAccounts' })Events
// Account changed
provider.on('accountsChanged', (accounts) => {
console.log('Account:', accounts[0])
})
// Network changed
provider.on('chainChanged', (chainId) => {
console.log('Chain:', chainId)
window.location.reload() // Recommended
})
// Connected
provider.on('connect', (info) => {
console.log('Connected to chain:', info.chainId)
})Error Codes
| Code | Description | Solution |
|---|---|---|
| 4001 | User rejected | Handle rejection gracefully |
| 4902 | Unknown chain | Add chain with wallet_addEthereumChain |
| -32602 | Invalid params | Use 0x-prefixed hex strings |
| -32603 | Internal error | Check request format |
Chain IDs
| Network | Chain ID | Hex |
|---|---|---|
| Ethereum Mainnet | 1 | 0x1 |
| Polygon | 137 | 0x89 |
| BSC | 56 | 0x38 |
| Arbitrum One | 42161 | 0xa4b1 |
| Optimism | 10 | 0xa |
| Base | 8453 | 0x2105 |
See chainlist.org for more chains.
For quick integration with UI components, use RainbowKit or Web3Modal.
Last updated on