Overview

In Ethereum, sending Ether from one address to another is a fundamental operation. Ethers.js provides an easy and efficient way to perform this task, allowing you to interact with the Ethereum blockchain seamlessly. This operation involves creating a transaction, signing it, and sending it to the network.

Prerequisites

Before you can send Ether, you need the following:

  • A wallet with sufficient Ether to cover the transaction amount and gas fees.
  • A provider to connect to the Ethereum network (e.g., Infura, Alchemy, or a local node).
  • The private key of the sender's wallet (for signing the transaction). Note: Be careful with handling private keys in production environments.

Sample Code

Below is a complete HTML example that demonstrates how to send Ether from one address to another using Ethers.js:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ethers.js Ether Transfer Example</title>
<script src="https://cdn.jsdelivr.net/npm/ethers@5.7.0/dist/ethers.umd.min.js"></script>
</head>
<body>
<h1>Send Ether</h1>
<input type="text" id="recipientInput" placeholder="Enter recipient address" />
<input type="number" id="amountInput" placeholder="Enter amount in ETH" />
<input type="text" id="privateKeyInput" placeholder="Enter your private key" />
<button id="sendButton">Send Ether</button>
<pre id="transactionInfo"></pre>

<script>
async function sendEther() {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const recipient = document.getElementById('recipientInput').value;
const amount = document.getElementById('amountInput').value;
const privateKey = document.getElementById('privateKeyInput').value;

// Create a wallet instance
const wallet = new ethers.Wallet(privateKey, provider);

// Create a transaction object
const tx = {
to: recipient,
value: ethers.utils.parseEther(amount) // Convert amount to Wei
};

try {
// Send the transaction
const transactionResponse = await wallet.sendTransaction(tx);
document.getElementById('transactionInfo').innerText = "Transaction Sent: " + transactionResponse.hash;

// Wait for the transaction to be mined
await transactionResponse.wait();
document.getElementById('transactionInfo').innerText += "\\nTransaction Mined: " + transactionResponse.hash;
} catch (error) {
document.getElementById('transactionInfo').innerText = "Error: " + error.message;
}
}

document.getElementById('sendButton').onclick = sendEther;
</script>
</body>
</html>

How It Works

In the example above:

  1. We create a simple HTML interface that allows the user to input the recipient's address, the amount of Ether to send, and the sender's private key.
  2. The sendEther function initializes a connection to the Ethereum network using a Web3 provider.
  3. A wallet instance is created using the provided private key and the provider.
  4. A transaction object is constructed with the recipient's address and the amount of Ether to send, converted from Ether to Wei using ethers.utils.parseEther.
  5. The transaction is sent using the wallet.sendTransaction method, and the transaction hash is displayed on the webpage.
  6. Finally, the code waits for the transaction to be mined and updates the displayed information accordingly.

Conclusion

Sending Ether from one address to another using Ethers.js is a straightforward process that allows developers to interact with the Ethereum blockchain easily. By following the steps outlined above, you can implement Ether transfers in your decentralized applications, enabling seamless transactions between users.