gasLimit: The maximum amount of gas you are willing to spend on the transaction.
to: The address of the recipient.
value: The amount of Ether to send (in wei).
data: Optional data to be included in the transaction.
Sign the Transaction: This line uses the signTransaction method to sign the transaction with your private key. This creates a signed transaction object.
Send the Signed Transaction: This code sends the signed transaction to the Ethereum network using the sendSignedTransaction method. It also includes event listeners to track the transaction's progress and handle any errors.
Important Notes:
Security: Keep your private key extremely secure. Anyone with access to your private key can control your funds.
Gas Fees: Be aware of gas fees, which can vary depending on network congestion.
Error Handling: Implement robust error handling to catch any potential issues during the transaction process.
Testing: Always test your code thoroughly on a testnet before deploying it to the mainnet.
const Web3 = require('web3');
// Replace with your Ethereum node URL
const provider = new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');
const web3 = new Web3(provider);
// Replace with your private key
const privateKey = 'YOUR_PRIVATE_KEY';
// Replace with the transaction details
const toAddress = '0xYOUR_RECIPIENT_ADDRESS';
const amount = '0.1'; // Amount in Ether
// Convert amount to wei
const weiAmount = web3.utils.toWei(amount, 'ether');
// Create transaction object
const transaction = {
to: toAddress,
value: weiAmount,
gas: 21000, // Adjust gas limit as needed
gasPrice: web3.utils.toHex(await web3.eth.getGasPrice()), // Get current gas price
};
// Sign the transaction
const signedTransaction = await web3.eth.accounts.signTransaction(transaction, privateKey);
// Send the signed transaction
web3.eth.sendSignedTransaction(signedTransaction.rawTransaction)
.on('transactionHash', (hash) => {
console.log('Transaction hash:', hash);
})
.on('receipt', (receipt) => {
console.log('Transaction receipt:', receipt);
})
.on('error', (error) => {
console.error('Transaction error:', error);
});
Explanation:
Import Web3: Imports the Web3 library.
Connect to Ethereum Node: Connects to an Ethereum node using Infura. Replace YOUR_INFURA_PROJECT_ID with your actual Infura project ID.
Define Private Key: Replace YOUR_PRIVATE_KEY with your actual private key. Keep it secret!
Transaction Details:
toAddress: Recipient's address.
amount: Amount to send in Ether.
weiAmount: Converts Ether to wei (smallest unit of Ether).
Transaction Object: Creates a transaction object with:
to: Recipient address.
value: Amount in wei.
gas: Estimated gas limit for the transaction.
gasPrice: Current gas price fetched from the network.
Sign Transaction: Signs the transaction using your private key.
Send Transaction: Sends the signed transaction to the network and handles events:
transactionHash: Logs the transaction hash.
receipt: Logs the transaction receipt when it's mined.
error: Logs any errors that occur.
Remember:
Security: Keep your private key extremely secure.
Gas Fees: Adjust gas and gasPrice as needed.
Error Handling: Implement more robust error handling for production use.
Testing: Test thoroughly on a testnet before deploying to mainnet.