Skip to main content

Transfers

This walkthrough covers how to initiate transactions, estimate gas, read-only call contracts through UKey Wallet Conflux Provider, and complete more complex on-chain interactions with js-conflux-sdk.


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

// Note: First estimate the 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);

Used 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,
});

// 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();

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