Skip to Content
dApps

Transaktionen

Verbinden Sie den Provider und lesen Sie sein Netzwerk, bevor Sie Transaktionen vorbereiten. Diese Beispiele gelten für Conflux Core Space. Ersetzen Sie verkürzte Adressen und initialisieren Sie jeden SDK-Client, bevor Sie dessen Methoden verwenden.

Erfahren Sie, wie Sie Transaktionen auf Conflux senden.


Transaktion senden

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)

Mit Gasschätzung senden

// 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, }] })

Vertrag aufrufen

const result = await provider.request({ method: 'cfx_call', params: [{ to: contractAddress, data: encodedFunctionCall, }, 'latest_state'] }) console.log('Result:', result)

Mit js-conflux-sdk verwenden

Einrichtung

npm install js-conflux-sdk

Provider verbinden

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

Transaktion mit dem SDK senden

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

Vertragsinteraktion

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

Fehlerbehandlung

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) } }
Zuletzt aktualisiert am