Skip to main content

Conflux

UKey Wallet Conflux Provider is used to connect Conflux accounts, read network status, send transactions and call contracts in web applications. The page is accessible via window.$ukey.conflux.

ℹ️

UKey Wallet Conflux Provider is compatible with both ConfluxPortal and Fluent wallet interfaces, making it easy to migrate existing Conflux DApps.



Find Provider

// Detect the UKey Wallet Conflux provider
const provider = window.$ukey?.conflux;

if (!provider) {
throw new Error("UKey Wallet Conflux provider not detected");
}

// Inspect compatibility flags
console.log("isConfluxPortal:", provider.isConfluxPortal); // Reference value: true
console.log("isFluent:", provider.isFluent); // Reference value: true
console.log("isUKey:", provider.isUKey); // Reference value: true

Get Going

Ask Access

const accounts = await provider.request({
method: "cfx_requestAccounts",
});

console.log("Connection established:", accounts[0]);

Check connection

if (provider.isConnected()) {
const accounts = await provider.request({ method: "cfx_accounts" });
console.log("Selected account:", accounts);
}

Accounts and networks

Read chain ID

const chainId = await provider.request({ method: "cfx_chainId" });
console.log("Reported chain ID:", chainId); // For instance, the main network is '0x405'

Read network version

const networkId = await provider.request({ method: "net_version" });
console.log("Reported network ID:", networkId); // For instance, the main network is '1029'

Read balance

const balance = await provider.request({
method: "cfx_getBalance",
requestParams: [address, "latest_state"],
});

console.log("Current balance:", balance); // Unit Drip (1 CFX = 10^18 Drip)

Transfers

Send tx

const txHash = await provider.request({
method: "cfx_sendTransaction",
requestParams: [
{
from: senderAddress,
to: recipientAddress,
value: "0x2386f26fc10000", // sample CFX amount
gas: "0x5208", // Reference value: 21000
},
],
});

console.log("Tx hash:", txHash);

Send after estimating Gas

// Start by estimating gas
const gasEstimate = await provider.request({
method: "cfx_estimateGasAndCollateral",
requestParams: [
{
from: senderAddress,
to: recipientAddress,
value: "0x2386f26fc10000",
},
],
});

// Send using the estimate
const txHash = await provider.request({
method: "cfx_sendTransaction",
requestParams: [
{
from: senderAddress,
to: recipientAddress,
value: "0x2386f26fc10000",
gas: gasEstimate.gasLimit,
storageLimit: gasEstimate.storageCollateralized,
},
],
});

Call contract

const callResult = await provider.request({
method: "cfx_call",
requestParams: [
{
to: contractAddress,
data: encodedFunctionCall,
},
"latest_state",
],
});

console.log("Call callResult:", callResult);

Event Flow

Watch Accounts

provider.on("accountsChanged", (accounts) => {
if (accounts.length === 0) {
console.log("Connection closed");
} else {
console.log("Active account changed:", accounts[0]);
}
});

Listening for chain changes

provider.on("chainChanged", (chainId) => {
console.log("Chain switched:", chainId);
// Reloading the page is preferred
window.location.reload();
});

Listen for connections

provider.on("connect", ({ chainId, networkId }) => {
console.log("Connected using:", chainId, networkId);
});

provider.on("disconnect", () => {
console.log("Connection closed");
});

Used with js-conflux-sdk

set up

npm install js-conflux-sdk

Connect Provider

import { Conflux } from "js-conflux-sdk";

const conflux = new Conflux({
url: "https://main.confluxrpc.com",
networkId: 1029,
});

// Sign with the UKey Wallet provider
conflux.provider = provider;

Send transactions using the SDK

// Build and submit transactions
const tx = await conflux.cfx.sendTransaction({
from: senderAddress,
to: recipientAddress,
value: Conflux.Drip.fromCFX(1), // one CFX
});

console.log("Built transaction:", tx);

// Wait for confirmation
const receipt = await tx.executed();
console.log("Transaction receipt:", receipt);

Contract interaction

const contract = conflux.Contract({
abi: contractABI,
address: contractAddress,
});

// Read call
const callResult = await contract.someViewFunction().call();

// Write call
const tx = await contract.someWriteFunction(param1, param2).sendTransaction({
from: senderAddress,
});

const receipt = await tx.executed();

API Notes

Methods

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

RPC method

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

Events

EventInputsDetails
connect{ chainId, networkId }Connected to the network
disconnect-Connection closed
chainChangedchainIdNetwork updated
accountsChangedaccounts[]Selected account changed

network

NetworkChain ID (hex)Network ID
Mainnet0x4051029
Testnet0x11

Deprecated method

The following compatible methods will still work, but new code should use request instead:

// 已废弃,改用 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;

Handle Errors

try {
const accounts = await provider.request({
method: "cfx_requestAccounts",
});
} catch (error) {
if (error.code === 4001) {
console.log("The user declined the request");
} else if (error.code === -32603) {
console.log("Internal provider error");
} else {
console.error("Operation error:", error.message);
}
}

Common error codes

CodeDescription
4001User rejected the request
-32700Failed to parse the request
-32600Request shape is invalid
-32601Requested method is unknown
-32602Parameters failed validation
-32603Provider internal failure

Addr Format

Conflux uses CIP-37 base32 addresses. Please keep the network prefix correct when displaying it to the user:

// 主网地址形态: cfx:...
// 测试网地址形态: cfxtest:...

// Example mainnet address
const address = "cfx:aak2rra2njvd77ezwjvx04kkds9fzagfe6ku8scz91";

// Convert between formats with js-conflux-sdk
import { format } from "js-conflux-sdk";

const hexAddress = format.hexAddress(address);
const base32Address = format.address(hexAddress, 1029); // Network ID