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 --devPackage versions used in this guide:
- Web3.js: ^1.6.0
- EthereumJS-Tx: ^2.1.2
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
- Gas Price: Set at 10 Gwei for Goerli testnet
- Gas Limit: Standard 21,000 units for ETH transfers
- Nonce: Prevents transaction replay attacks
- Value: Amount being transferred (1 ETH in this example)
Handling Different Blockchain Networks
The EthereumJS-Tx package supports several networks by default:
- Mainnet
- Ropsten
- Rinkeby
- Kovan
- Goerli
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:
- The
transactionHashevent returns a transaction ID - Check this ID on a blockchain explorer like Etherscan
- Confirm transaction status (success/failure)
๐ Explore blockchain transaction details
Best Practices for ETH Transfers
- Always test with small amounts first
- Keep private keys secure (never hardcode in production)
- Monitor gas prices for cost-effective transfers
- 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.