On this page
Transactions
Connect the provider and read its network before preparing transactions. These examples are for Conflux Core Space. Replace abbreviated addresses and initialize any SDK client before using its methods.
Learn how to send transactions on Conflux.
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)Using with js-conflux-sdk
Setup
npm install js-conflux-sdkConnect 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 = providerSend 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()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)
}
}Last updated on