The web3-eth package enables seamless interaction with the Ethereum blockchain and smart contracts. Below is a comprehensive guide to its functionalities, optimized for clarity and search intent.
Introduction to web3-eth
The web3-eth package is a core component of Web3.js, designed to facilitate Ethereum blockchain interactions. It supports:
- Querying blockchain data
- Sending transactions
- Deploying and interacting with smart contracts
Basic Setup
// Importing the Eth package
var Eth = require('web3-eth');
var eth = new Eth(Eth.givenProvider || 'ws://some.local-or-remote.node:8546');
// Using the umbrella Web3 package
var Web3 = require('web3');
var web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546');
// Access via web3.ethKey Features
1. Checksum Addresses
All Ethereum addresses returned by web3-eth are checksummed (mixed uppercase/lowercase) to ensure validity. Invalid checksums trigger errors.
Example
web3.eth.getAccounts(console.log);
> ["0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", "0x85F43D8a49eeB85d32Cf465507DD71d507100C1d"]2. Provider Management
Configure providers for modules like web3.eth, web3.shh, and web3.bzz.
Methods
setProvider(myProvider): Updates the provider for a module.providers: Lists available providers (HTTP, Websocket, IPC).
Example: Local Geth Node
var web3 = new Web3('http://localhost:8545');
web3.setProvider(new Web3.providers.WebsocketProvider('ws://localhost:8546'));3. Transaction Handling
Methods
sendTransaction(): Broadcasts transactions.signTransaction(): Signs transactions offline.getTransactionReceipt(): Retrieves transaction details.
Example
web3.eth.sendTransaction({
from: '0x...',
to: '0x...',
value: '1000000000000000000'
}).then(console.log);4. Blockchain Queries
Methods
getBlock(): Fetches block data.getBalance(): Checks an address balance.getCode(): Retrieves contract bytecode.
Example
web3.eth.getBalance("0x...").then(console.log);
> "1000000000000000000"FAQs
1. How do I handle checksum addresses?
Checksum addresses are auto-validated. Use .toLowerCase() to bypass checksums if needed.
2. What providers are supported?
- HTTP
- Websocket
- IPC (Node.js only)
๐ Explore Ethereum development tools
3. How do I estimate gas costs?
Use estimateGas() with a transaction object:
web3.eth.estimateGas({to: "0x...", data: "0x..."}).then(console.log);Conclusion
web3-eth simplifies Ethereum interactions with robust methods for queries, transactions, and contract management. For advanced use, refer to the official Web3.js docs.
๐ Start building on Ethereum today
### **Keywords Identified**
- `web3-eth`
- Ethereum interactions
- Web3.js documentation
- Smart contract transactions
- Blockchain queries
- Checksum addresses