Linea Web3.js Integration Guide
Web3.js is the original and most widely-used JavaScript library for interacting with the Linea blockchain. This guide covers everything from basic setup to advanced patterns using BlockEden.xyz's Linea infrastructure.
Overview
Web3.js provides a complete JavaScript API for Linea, including:
- Account Management: Create, import, and manage Linea accounts
- Smart Contract Interaction: Deploy and call smart contracts
- Transaction Handling: Send, sign, and monitor transactions
- Event Monitoring: Subscribe to blockchain events
- Utility Functions: Format conversions, hashing, and validation
Installation & Setup
Installation
# Using npm
npm install web3
# Using yarn
yarn add web3
# Using pnpm
pnpm add web3
Basic Setup
import { Web3 } from 'web3';
// Initialize with BlockEden.xyz endpoint
const web3 = new Web3('https://api.blockeden.xyz/linea/${accessKey}');
// Verify connection
async function testConnection() {
try {
const blockNumber = await web3.eth.getBlockNumber();
console.log('Current block number:', blockNumber);
console.log('Web3.js version:', web3.version);
} catch (error) {
console.error('Connection failed:', error);
}
}
testConnection();
Environment Configuration
// config/web3.js
import { Web3 } from 'web3';
const networks = {
mainnet: 'https://ethereum-mainnet.blockeden.xyz',
sepolia: 'https://ethereum-sepolia.blockeden.xyz',
polygon: 'https://polygon-mainnet.blockeden.xyz',
arbitrum: 'https://arbitrum-mainnet.blockeden.xyz'
};
export function createWeb3Instance(network = 'mainnet', apiKey) {
const rpcUrl = `${networks[network]}/${apiKey}`;
return new Web3(rpcUrl);
}
// Usage
const web3 = createWeb3Instance('mainnet', process.env.BLOCKEDEN_API_KEY);