Skip to Content
dAppsConflux

Conflux

Integrate Conflux blockchain using OneKey’s Fluent-compatible provider. Access via window.$onekey.conflux.

OneKey’s Conflux provider is compatible with both ConfluxPortal and Fluent wallet interfaces.



Provider Detection

// Detect OneKey Conflux provider const provider = window.$onekey?.conflux if (!provider) { throw new Error('OneKey Conflux provider not detected') } // Check compatibility flags console.log('isConfluxPortal:', provider.isConfluxPortal) // true console.log('isFluent:', provider.isFluent) // true console.log('isOneKey:', provider.isOneKey) // true

Quick Start

Request Accounts

const accounts = await provider.request({ method: 'cfx_requestAccounts' }) console.log('Connected:', accounts[0])

Check Connection

if (provider.isConnected()) { const accounts = await provider.request({ method: 'cfx_accounts' }) console.log('Accounts:', accounts) }

Account and Network

Get Chain ID

const chainId = await provider.request({ method: 'cfx_chainId' }) console.log('Chain ID:', chainId) // e.g., '0x405' for mainnet

Get Network Version

const networkId = await provider.request({ method: 'net_version' }) console.log('Network ID:', networkId) // e.g., '1029' for mainnet

Get Balance

const balance = await provider.request({ method: 'cfx_getBalance', params: [address, 'latest_state'] }) console.log('Balance:', balance) // In Drip (1 CFX = 10^18 Drip)

Transactions

Send Transaction

const txHash = await provider.request({ method: 'cfx_sendTransaction', params: [{ from: senderAddress, to: recipientAddress, value: '0xDE0B6B3A7640000', // 1 CFX in Drip (hex) gas: '0x5208', // 21000 }] }) console.log('Transaction hash:', txHash)

Send with Gas Estimation

// Estimate gas first const gasEstimate = await provider.request({ method: 'cfx_estimateGasAndCollateral', params: [{ from: senderAddress, to: recipientAddress, value: '0xDE0B6B3A7640000', }] }) // Send with estimated values const txHash = await provider.request({ method: 'cfx_sendTransaction', params: [{ from: senderAddress, to: recipientAddress, value: '0xDE0B6B3A7640000', gas: gasEstimate.gasLimit, storageLimit: gasEstimate.storageCollateralized, }] })

Call Contract

const result = await provider.request({ method: 'cfx_call', params: [{ to: contractAddress, data: encodedFunctionCall, }, 'latest_state'] }) console.log('Result:', result)

Event Handling

Listen for Account Changes

provider.on('accountsChanged', (accounts) => { if (accounts.length === 0) { console.log('Disconnected') } else { console.log('Account changed:', accounts[0]) } })

Listen for Chain Changes

provider.on('chainChanged', (chainId) => { console.log('Chain changed:', chainId) // Recommend reloading the page window.location.reload() })

Listen for Connection

provider.on('connect', ({ chainId, networkId }) => { console.log('Connected to:', chainId, networkId) }) provider.on('disconnect', () => { console.log('Disconnected') })

Using with js-conflux-sdk

Setup

npm install js-conflux-sdk

Connect Provider

import { Conflux } from 'js-conflux-sdk' const conflux = new Conflux({ url: 'https://main.confluxrpc.com', networkId: 1029, }) // Use OneKey provider for signing conflux.provider = provider

Send Transaction with SDK

// Create and send transaction const tx = conflux.cfx.sendTransaction({ from: senderAddress, to: recipientAddress, value: Conflux.Drip.fromCFX(1), // 1 CFX }) console.log('Transaction:', tx) // Wait for confirmation const receipt = await tx.executed() console.log('Receipt:', receipt)

Contract Interaction

const contract = conflux.Contract({ abi: contractABI, address: contractAddress, }) // Read const result = await contract.someViewFunction().call() // Write const tx = contract.someWriteFunction(param1, param2).sendTransaction({ from: senderAddress, }) const receipt = await tx.executed()

API Reference

Methods

MethodDescription
request({ method, params })Send JSON-RPC request
isConnected()Check connection status
on(event, callback)Subscribe to events
off(event, callback)Unsubscribe from events

RPC Methods

MethodDescription
cfx_requestAccountsRequest connection
cfx_accountsGet connected accounts
cfx_chainIdGet chain ID
cfx_sendTransactionSend transaction
cfx_callCall contract (read-only)
cfx_estimateGasAndCollateralEstimate gas
cfx_getBalanceGet account balance
cfx_getTransactionByHashGet transaction
cfx_getTransactionReceiptGet receipt
net_versionGet network ID

Events

EventParametersDescription
connect{ chainId, networkId }Connected to network
disconnect-Disconnected
chainChangedchainIdNetwork changed
accountsChangedaccounts[]Accounts changed

Networks

NetworkChain ID (hex)Network ID
Mainnet0x4051029
Testnet0x11

Deprecated Methods

These methods still work but are deprecated:

// Deprecated: use request({ method: 'cfx_requestAccounts' }) await provider.enable() // Deprecated: use request() await provider.send({ method: 'cfx_accounts' }) await provider.sendAsync({ method: 'cfx_accounts' }, callback) // Deprecated: use request({ method: 'cfx_chainId' }) provider.chainId // Deprecated: use request({ method: 'net_version' }) provider.networkVersion // Deprecated: use request({ method: 'cfx_accounts' }) provider.selectedAddress

Error Handling

try { const accounts = await provider.request({ method: 'cfx_requestAccounts' }) } catch (error) { if (error.code === 4001) { console.log('User rejected the request') } else if (error.code === -32603) { console.log('Internal error') } else { console.error('Error:', error.message) } }

Common Error Codes

CodeDescription
4001User rejected request
-32700Parse error
-32600Invalid request
-32601Method not found
-32602Invalid params
-32603Internal error

Address Format

Conflux uses base32 addresses (CIP-37):

// Mainnet: cfx:... // Testnet: cfxtest:... // Example mainnet address const address = 'cfx:aak2rra2njvd77ezwjvx04kkds9fzagfe6ku8scz91' // Convert between formats using js-conflux-sdk import { format } from 'js-conflux-sdk' const hexAddress = format.hexAddress(address) const base32Address = format.address(hexAddress, 1029) // networkId
Last updated on