Skip to Content

Conflux

使用 OneKey 的 Fluent 兼容 Provider 集成 Conflux 区块链。通过 window.$onekey.conflux 访问。

OneKey 的 Conflux Provider 同时兼容 ConfluxPortal 和 Fluent 钱包接口。


快速链接


Provider 检测

// 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

快速开始

请求账户

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

检查连接

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

账户和网络

获取链 ID

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

获取网络版本

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

获取余额

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

交易

发送交易

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)

带 Gas 估算的发送

// 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, }] })

调用合约

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

事件处理

监听账户变化

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

监听链变化

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

监听连接

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

配合 js-conflux-sdk 使用

设置

npm install js-conflux-sdk

连接 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

使用 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)

合约交互

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 参考

方法

方法说明
request({ method, params })发送 JSON-RPC 请求
isConnected()检查连接状态
on(event, callback)订阅事件
off(event, callback)取消订阅事件

RPC 方法

方法说明
cfx_requestAccounts请求连接
cfx_accounts获取已连接账户
cfx_chainId获取链 ID
cfx_sendTransaction发送交易
cfx_call调用合约(只读)
cfx_estimateGasAndCollateral估算 gas
cfx_getBalance获取账户余额
cfx_getTransactionByHash获取交易
cfx_getTransactionReceipt获取回执
net_version获取网络 ID

事件

事件参数说明
connect{ chainId, networkId }已连接到网络
disconnect-已断开连接
chainChangedchainId网络已变更
accountsChangedaccounts[]账户已变更

网络

网络链 ID (十六进制)网络 ID
主网0x4051029
测试网0x11

已弃用方法

这些方法仍然可用但已弃用:

// 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

错误处理

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) } }

常见错误码

错误码说明
4001用户拒绝请求
-32700解析错误
-32600无效请求
-32601方法未找到
-32602无效参数
-32603内部错误

地址格式

Conflux 使用 base32 地址 (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
最后更新于