Node.js ETH Transfer Guide: A Step-by-Step Tutorial

ยท

Introduction to ETH Transfers with Node.js

Building on previous Web3.js explorations, this tutorial focuses on using Node.js to execute Ethereum (ETH) transfers. We'll walk through the entire process from setting up dependencies to confirming transactions on the blockchain.

Prerequisites and Setup

Installing Required Dependencies

For basic ETH transfers, you'll need just two packages:

yarn add web3 ethereumjs-tx --dev

Package versions used in this guide:

Initializing Web3 Provider

First, obtain a Goerli testnet URL from Infura. Here's how to initialize Web3:

const Web3 = require('web3')
const rpcUrl = "https://goerli.infura.io/v3/YOUR_INFURA_PROJECT_ID"
const web3Provider = new Web3.providers.HttpProvider(rpcUrl)
const web3 = new Web3(web3Provider)

Executing an ETH Transfer

Core Transaction Code

const EthereumTx = require('ethereumjs-tx').Transaction
const currentAddress = '0x3EcAa09DD6B8828607bba4B1d7055Ea1143f8B94'

const startTransfer = async () => {
  // Check account balance first
  const balance = await web3.eth.getBalance(currentAddress)
  console.log(`Account Balance: ${balance}`)

  const recipientAddress = '0xe208D2fB37df02061B78848B83F02b4AD33540e1'
  const privateKey = Buffer.from('YOUR_PRIVATE_KEY', 'hex')
  const amount = web3.utils.toHex(web3.utils.toWei('1', 'ether'))
  const nonce = await web3.eth.getTransactionCount(currentAddress)

  const transactionParams = {
    from: currentAddress,
    to: recipientAddress,
    gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
    gasLimit: web3.utils.toHex(21000),
    value: amount,
    nonce: web3.utils.toHex(nonce)
  }

  const transaction = new EthereumTx(transactionParams, {
    chain: 'goerli'
  })

  transaction.sign(privateKey)

  web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
    .on('transactionHash', console.log)
    .catch(err => console.error('Transaction Error:', err))
}

startTransfer()

๐Ÿ‘‰ Learn more about secure transaction practices

Transaction Parameters Explained

  1. Gas Price: Set at 10 Gwei for Goerli testnet
  2. Gas Limit: Standard 21,000 units for ETH transfers
  3. Nonce: Prevents transaction replay attacks
  4. Value: Amount being transferred (1 ETH in this example)

Handling Different Blockchain Networks

The EthereumJS-Tx package supports several networks by default:

For custom chains, use ethereumjs-common:

const Common = require('ethereumjs-common').default
const customChain = Common.forCustomChain(
  'mainnet',
  {
    name: 'Fantom Opera',
    networkId: 250,
    chainId: 250
  },
  'petersburg'
)

const customTx = new EthereumTx(txParams, {
  common: customChain
})
customTx.sign(privateKey)

Verifying Transactions

After execution:

  1. The transactionHash event returns a transaction ID
  2. Check this ID on a blockchain explorer like Etherscan
  3. Confirm transaction status (success/failure)

๐Ÿ‘‰ Explore blockchain transaction details

Best Practices for ETH Transfers

  1. Always test with small amounts first
  2. Keep private keys secure (never hardcode in production)
  3. Monitor gas prices for cost-effective transfers
  4. Implement proper error handling

FAQ: Node.js ETH Transfers

Q: What's the minimum gas limit for ETH transfers?

A: 21,000 units is the standard minimum for simple ETH transfers.

Q: How do I convert between Ether and Wei?

A: Use web3.utils.toWei() and web3.utils.fromWei() for conversions.

Q: Why am I getting "invalid sender" errors?

A: This often occurs when the chain parameter isn't properly specified for testnets.

Q: Is it safe to use my private key this way?

A: For development/testing only. In production, use secure wallet solutions.

Q: How long do testnet transactions take?

A: Typically a few seconds to minutes, depending on network congestion.

Q: Can I use this for ERC-20 token transfers?

A: No, token transfers require different contract interaction methods.

Conclusion

This guide has covered the complete process for executing ETH transfers via Node.js. By following these steps, you can programmatically interact with the Ethereum blockchain for various use cases.

๐Ÿ‘‰ Discover advanced blockchain development techniques