Sending Ether from One Account to Another in Web3.js

To send Ether from one account to another using Web3.js, you can use the web3.eth.sendTransaction() method. This method allows you to specify the sender, receiver, and amount of Ether to be sent. Below is a detailed guide on how to perform this operation.

Step-by-Step Guide

  • Install Web3.js: If you haven’t already, install Web3.js using npm:
  • npm install web3
  • Connect to Ethereum Network: Connect to an Ethereum node using Web3.js. If you are using MetaMask, you can access the provider directly.
  • Send Ether: Use the sendTransaction() method to transfer Ether from one account to another.

Sample Code

Here’s a simple example of how to send Ether from one account to another:

const Web3 = require('web3');

// Connect to the Ethereum network
const web3 = new Web3(Web3.givenProvider || "http://localhost:8545");

// Function to send Ether
async function sendEther(sender, receiver, amount, privateKey) {
try {
// Create the transaction object
const transaction = {
from: sender,
to: receiver,
value: web3.utils.toWei(amount.toString(), 'ether'),
gas: 2000000, // Specify a gas limit
};

// Sign the transaction
const signedTransaction = await web3.eth.accounts.signTransaction(transaction, privateKey);

// Send the transaction
const receipt = await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);

console.log(`Transaction successful with hash: ${receipt.transactionHash}`);
} catch (error) {
console.error("Error sending Ether:", error);
}
}

// Replace with your Ethereum account details
const senderAddress = '0xYourSenderAddressHere';
const receiverAddress = '0xYourReceiverAddressHere';
const amountToSend = 0.1; // Amount in Ether
const senderPrivateKey = '0xYourPrivateKeyHere'; // Never expose your private key in production

sendEther(senderAddress, receiverAddress, amountToSend, senderPrivateKey);

Explanation of the Code

  • Web3 Initialization: A new instance of Web3 is created to connect to the Ethereum network.
  • Async Function: The sendEther function is defined as asynchronous to handle the promises involved in signing and sending the transaction.
  • Transaction Object: A transaction object is created that includes the sender's address, receiver's address, amount to send (in Wei), and gas limit.
  • Signing the Transaction: The transaction is signed using the sender's private key, ensuring that only the owner of the account can initiate the transfer.
  • Sending the Transaction: The signed transaction is sent to the Ethereum network, and a receipt is returned upon successful execution.
  • Error Handling: A try-catch block is used to handle any potential errors during the transaction process.

Important Security Note

Never expose your private key in production code. Always use secure methods to manage private keys, such as environment variables or secure vaults.

Conclusion

Sending Ether from one account to another using Web3.js is straightforward. By following the steps outlined above, you can easily implement this functionality in your decentralized application (dApp) or web application.