Introduction
The web3-eth package offers a robust set of tools for interacting with the Ethereum blockchain and smart contracts. This tutorial provides a step-by-step guide to leveraging Web3.js version 4's web3-eth package, using TypeScript for clarity.
Step 1: Environment Setup
Prerequisites
- Ganache: Local blockchain for Ethereum development (Download)
- Node.js: JavaScript runtime (Download)
- npm/yarn: Package managers (npm Guide | Yarn Guide)
Step 2: Project Initialization
Create a project directory:
mkdir smart-contract-tutorial && cd smart-contract-tutorialInitialize Node.js:
npm init -yInstall TypeScript:
npm install typescript @types/node
Step 3: Web3.js Configuration
Install Web3.js:
npm install web3Connect to Ganache:
import { Web3 } from 'web3'; const web3 = new Web3('http://localhost:7545'); async function checkConnection() { const block = await web3.eth.getBlockNumber(); console.log('Last block:', block); // Example output: Last block: 4975299n }
👉 Learn more about Web3.js configurations
Step 4: Smart Contract Deployment
Example: Sending a Transaction
async function sendTransaction() {
const accounts = await web3.eth.getAccounts();
const tx = {
from: accounts[0],
to: accounts[1],
value: web3.utils.toWei('1', 'ether'),
gas: 21000
};
const receipt = await web3.eth.sendTransaction(tx);
console.log('Receipt:', receipt);
}Gas Estimation
async function estimateGas() {
const accounts = await web3.eth.getAccounts();
const estimatedGas = await contract.deploy().estimateGas({ from: accounts[0] });
console.log('Estimated gas:', estimatedGas); // Example output: 140648n
}Step 5: Transaction Types
Traditional Transactions
const tx = {
from: senderAddress,
to: receiverAddress,
value: web3.utils.toWei('1', 'ether'),
type: 0 // Traditional transaction type
};EIP-1559 Transactions
const tx = {
from: senderAddress,
to: receiverAddress,
value: web3.utils.toWei('1', 'ether'),
type: 2 // EIP-1559 transaction type
};👉 Explore advanced transaction types
Step 6: Direct Imports (Optimizing Bundle Size)
import { Web3Eth } from 'web3-eth';
const eth = new Web3Eth('http://localhost:7545');Conclusion
This guide covered:
- Environment setup with Ganache
- Project initialization
- Web3.js configuration
- Smart contract deployment
- Transaction types (Traditional, EIP-2930, EIP-1559)
- Optimizing imports
FAQ
Q1: How do I estimate gas for complex transactions?
Use web3.eth.estimateGas() with transaction parameters to get accurate gas estimates.
Q2: What’s the difference between EIP-1559 and traditional transactions?
EIP-1559 introduces a base fee burned by the network, while traditional transactions use manually set gas prices.
Q3: How can I reduce my bundle size when using Web3.js?
Directly import specific packages (e.g., web3-eth) instead of the entire library.
Q4: Why use Ganache for development?
Ganache provides a local blockchain with unlocked accounts for testing without real ETH costs.
Q5: How do I handle private keys securely?
Use environment variables and never hardcode keys in your source files.