Skip to Content
dApps

トランザクション

トランザクションの準備前にプロバイダーに接続し、ネットワークを取得してください。これらの例は Conflux Core Space 向けです。省略されたアドレスを置き換え、SDK クライアントのメソッドを使う前にクライアントを初期化してください。

Conflux でトランザクションを送信する方法を説明します。


トランザクションを送信

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)

ガスの見積もり付きで送信

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

js-conflux-sdk との併用

セットアップ

npm install js-conflux-sdk

プロバイダーに接続

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

エラー処理

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) } }
最終更新日