Web3 by Example: Send Raw Transaction

An Ethereum transaction refers to an action initiated by an externally-owned account, in other words an account managed by a human, not a contract. For example, if Bob sends Alice 1 ETH, Bob's account must be debited and Alice's must be credited. This state-changing action takes place within a transaction.

As usual, we need provider, private key and wallet to send a transaction.

Create a raw transaction object that is ready to be signed and sent.

A transaction object needs to be signed using the sender's private key. This proves that the transaction could only have come from the sender and was not sent fraudulently. We use wallet.signTransaction() to sign the transaction. The signedTx is the signed transaction in Recursive Length Prefix (RLP) encoded form.

Submit transaction to the network to be mined. The transaction must be signed, and be valid (i.e. the nonce is correct and the account has sufficient balance to pay for the transaction).

Eureka! We can reduce the code using wallet.sendTransaction(). This method helps us do a lot of work, such as populating all fields in a transaction, signing it and sending it to the network.


const ethers = require("ethers");
(async () => {
const provider = new ethers.providers.AlchemyProvider("goerli");
const privateKey = "your-private-key";
const wallet = new ethers.Wallet(privateKey, provider);
const rawTx = {
to: "0xddB51f100672Cb252C67D516eb79931bf27cE3E6",
value: ethers.utils.parseEther("0.1"),
data: ethers.utils.toUtf8Bytes("web3byexample"),
const tx = await wallet.sendTransaction(rawTx);
console.log("txhash", tx.hash);


➜ node send-transaction.js
txhash 0x5fec073dcffa88a30b4f74c2f61baaf9aae0183e36148fbf2a5b414dbde96870

Next example: Smart Contract Compilation & ABI