What is a Transaction?

In the context of Ethers.js and the Ethereum blockchain, a transaction is a signed data package that is sent from one address to another. It can involve transferring Ether (ETH), sending tokens, or interacting with smart contracts. Each transaction is recorded on the blockchain, making it immutable and publicly accessible.

A transaction typically includes several important pieces of information:

  • Nonce: A unique number that ensures transactions are processed in order.
  • Gas Price: The fee paid to miners for processing the transaction.
  • Gas Limit: The maximum amount of gas units that can be used for the transaction.
  • To: The recipient's address.
  • Value: The amount of Ether to be sent.
  • Data: Optional data to be sent along with the transaction, often used for contract interactions.

Creating and Sending a Transaction

Below is an example of how to create and send a transaction using Ethers.js.


<div class="example">
<label for="privateKey">Sender Private Key:</label>
<input type="text" id="privateKey" placeholder="Your private key">
<label for="recipientAddress">Recipient Address:</label>
<input type="text" id="recipientAddress" placeholder="0x...">
<label for="amount">Amount (in ETH):</label>
<input type="text" id="amount" placeholder="0.01">
<button id="sendTransaction">Send Transaction</button>
<h3>Status: <span id="transactionStatus">Not sent</span></h3>
</div>

<script>
const provider = new ethers.providers.getDefaultProvider('homestead');

document.getElementById('sendTransaction').onclick = async () => {
const privateKey = document.getElementById('privateKey').value;
const recipient = document.getElementById('recipientAddress').value;
const amount = document.getElementById('amount').value;

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

// Create a transaction object
const tx = {
to: recipient,
value: ethers.utils.parseEther(amount),
gasLimit: ethers.utils.hexlify(21000), // Standard gas limit for ETH transfer
gasPrice: await provider.getGasPrice() // Fetch current gas price
};

try {
// Send the transaction
const transactionResponse = await wallet.sendTransaction(tx);
document.getElementById('transactionStatus').innerText = `Transaction sent: ${transactionResponse.hash}`;
} catch (error) {
console.error(error);
document.getElementById('transactionStatus').innerText = 'Transaction failed';
}
};
</script>

Transaction Confirmation

After sending a transaction, it may take some time to be confirmed by the network. You can monitor the transaction status using the transaction hash returned after sending it. This hash can be used to look up the transaction on a blockchain explorer.

Conclusion

Transactions are fundamental to the Ethereum blockchain, enabling the transfer of value and interaction with smart contracts. Ethers.js provides a straightforward way to create and send transactions, making it accessible for developers to integrate Ethereum functionality into their applications. Understanding how to work with transactions is essential for anyone looking to build on the Ethereum platform.