Conflux
UKey Wallet Conflux Provider 用于在 Web 应用中连接 Conflux 账户、读取网络状态、发送交易和调用合约。页面中可通过 window.$ukey.conflux 访问。
ℹ️
UKey Wallet Conflux Provider 同时兼容 ConfluxPortal 和 Fluent 钱包接口,方便现有 Conflux DApp 迁移。
快捷链接
找 Provider
// 检查 UKey Wallet Conflux provider
const provider = window.$ukey?.conflux;
if (!provider) {
throw new Error("未检测到 UKey Wallet Conflux provider");
}
// 确认兼容性标记
console.log("isConfluxPortal:", provider.isConfluxPortal); // 布尔参考片段:true
console.log("isFluent:", provider.isFluent); // 布尔参考片段:true
console.log("isUKey:", provider.isUKey); // 布尔参考片段:true
快上手
请求授权
const accounts = await provider.request({
method: "cfx_requestAccounts",
});
console.log("连接结果:", accounts[0]);
检查连接
if (provider.isConnected()) {
const accounts = await provider.request({ method: "cfx_accounts" });
console.log("当前账户信息:", accounts);
}
账户和网络
读取链 ID
const chainId = await provider.request({ method: "cfx_chainId" });
console.log("返回的链 ID:", chainId); // 例如主网为 '0x405'
读取网络版本
const networkId = await provider.request({ method: "net_version" });
console.log("返回的网络 ID:", networkId); // 例如主网为 '1029'
读取余额
const balance = await provider.request({
method: "cfx_getBalance",
requestParams: [address, "latest_state"],
});
console.log("当前余额:", balance); // 单位 Drip (1 CFX = 10^18 Drip)
交易项
发交易
const txHash = await provider.request({
method: "cfx_sendTransaction",
requestParams: [
{
from: senderAddress,
to: recipientAddress,
value: "0x2386f26fc10000", // 示例 CFX 金额
gas: "0x5208", // 数值参考片段:21000
},
],
});
console.log("交易哈希:", txHash);
估算 Gas 后发送
// 先做一遍 gas 估算
const gasEstimate = await provider.request({
method: "cfx_estimateGasAndCollateral",
requestParams: [
{
from: senderAddress,
to: recipientAddress,
value: "0x2386f26fc10000",
},
],
});
// 按估算值发起发送
const txHash = await provider.request({
method: "cfx_sendTransaction",
requestParams: [
{
from: senderAddress,
to: recipientAddress,
value: "0x2386f26fc10000",
gas: gasEstimate.gasLimit,
storageLimit: gasEstimate.storageCollateralized,
},
],
});
调用合约
const callResult = await provider.request({
method: "cfx_call",
requestParams: [
{
to: contractAddress,
data: encodedFunctionCall,
},
"latest_state",
],
});
console.log("调用结果:", callResult);
事件流
看账户变化
provider.on("accountsChanged", (accounts) => {
if (accounts.length === 0) {
console.log("连接已关闭");
} else {
console.log("当前账户已切换为:", accounts[0]);
}
});
监听链变化
provider.on("chainChanged", (chainId) => {
console.log("链已切换为:", chainId);
// 建议刷新页面
window.location.reload();
});
监听连接
provider.on("connect", ({ chainId, networkId }) => {
console.log("当前连接参数:", chainId, networkId);
});
provider.on("disconnect", () => {
console.log("连接已关闭");
});
配合 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,
});
// 调用方式如下 UKey Wallet provider 执行签名
conflux.provider = provider;
使用 SDK 发送交易
// 构建并发送交易
const tx = await conflux.cfx.sendTransaction({
from: senderAddress,
to: recipientAddress,
value: Conflux.Drip.fromCFX(1), // one CFX
});
console.log("构造出的交易:", tx);
// 等待链上确认
const receipt = await tx.executed();
console.log("交易回执:", receipt);
合约交互
const contract = conflux.Contract({
abi: contractABI,
address: contractAddress,
});
// 执行读取
const callResult = await contract.someViewFunction().call();
// 执行写入
const tx = await contract.someWriteFunction(param1, param2).sendTransaction({
from: senderAddress,
});
const receipt = await tx.executed();
API说明
方法集
| 方法 | 说明 |
|---|---|
request({ action, 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 | - | 已断开连接 |
chainChanged | chainId | 网络已更新 |
accountsChanged | accounts[] | 当前账户已切换 |
网络
| 网络 | 链 ID (十六进制) | 网络 ID |
|---|---|---|
| 主网 | 0x405 | 1029 |
| 测试网 | 0x1 | 1 |
已弃用方法
下面这些兼容方法仍可运行,但新代码应改用 request:
// 这个写法已弃用,请改成 request({ method: 'cfx_requestAccounts' })
await provider.enable();
// 这个写法已弃用,请改成 request()
await provider.send({ method: "cfx_accounts" });
await provider.sendAsync({ method: "cfx_accounts" }, callback);
// 这个写法已弃用,请改成 request({ method: 'cfx_chainId' })
provider.chainId;
// 这个写法已弃用,请改成 request({ method: 'net_version' })
provider.networkVersion;
// 这个写法已弃用,请改成 request({ method: 'cfx_accounts' })
provider.selectedAddress;
处理异常
try {
const accounts = await provider.request({
method: "cfx_requestAccounts",
});
} catch (error) {
if (error.code === 4001) {
console.log("用户已拒绝本次请求");
} else if (error.code === -32603) {
console.log("Provider 内部错误");
} else {
console.error("执行报错:", error.message);
}
}
常见错误码
| 错误码 | 说明 |
|---|---|
| 4001 | 用户取消了本次请求 |
| -32700 | 解析错误 |
| -32600 | 无效请求 |
| -32601 | 方法未找到 |
| -32602 | 无效参数 |
| -32603 | 内部处理异常 |
地址样式
Conflux 使用 CIP-37 base32 地址。展示给用户时请保持网络前缀正确:
// 主网参考片段:cfx:...
// 测试网参考片段:cfxtest:...
// 主网地址参考值
const address = "cfx:aak2rra2njvd77ezwjvx04kkds9fzagfe6ku8scz91";
// 通过 js-conflux-sdk 在不同格式间转换
import { format } from "js-conflux-sdk";
const hexAddress = format.hexAddress(address);
const base32Address = format.address(hexAddress, 1029); // Network ID