Arbitrum Web3.js Integration Guide
Web3.js is the original and most widely-used JavaScript library for interacting with the Arbitrum blockchain. This guide covers everything from basic setup to advanced patterns using BlockEden.xyz's Arbitrum infrastructure.
Overview
Web3.js provides a complete JavaScript API for Arbitrum, including:
- Account Management: Create, import, and manage Arbitrum 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/arbitrum/${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);
Account Management
Creating Accounts
// Create a new account
const account = web3.eth.accounts.create();
console.log('Address:', account.address);
console.log('Private Key:', account.privateKey);
// Create multiple accounts
const accounts = web3.eth.accounts.create(5); // Creates 5 accounts
console.log('Generated accounts:', accounts);
// Create from entropy
const entropy = web3.utils.randomHex(32);
const accountFromEntropy = web3.eth.accounts.create(entropy);
Account Recovery
// Recover account from private key
const privateKey = '0x1234567890abcdef...';
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
// Recover from mnemonic (requires additional library)
import { generateMnemonic, mnemonicToSeedSync } from 'bip39';
import { hdkey } from 'ethereumjs-wallet';
function recoverFromMnemonic(mnemonic, index = 0) {
const seed = mnemonicToSeedSync(mnemonic);
const hdwallet = hdkey.fromMasterSeed(seed);
const path = `m/44'/60'/0'/0/${index}`;
const wallet = hdwallet.derivePath(path).getWallet();
return web3.eth.accounts.privateKeyToAccount('0x' + wallet.getPrivateKey().toString('hex'));
}
// Usage
const mnemonic = 'your twelve word mnemonic phrase here...';
const account = recoverFromMnemonic(mnemonic, 0);
Wallet Management
class ArbitrumWallet {
constructor(web3Instance) {
this.web3 = web3Instance;
this.accounts = new Map();
}
// Add account to wallet
addAccount(privateKey, password) {
const account = this.web3.eth.accounts.privateKeyToAccount(privateKey);
const encrypted = this.web3.eth.accounts.encrypt(privateKey, password);
this.accounts.set(account.address.toLowerCase(), {
account,
encrypted
});
return account.address;
}
// Get account by address
getAccount(address) {
return this.accounts.get(address.toLowerCase())?.account;
}
// Sign transaction
async signTransaction(txObject, fromAddress, password) {
const accountData = this.accounts.get(fromAddress.toLowerCase());
if (!accountData) {
throw new Error('Account not found in wallet');
}
const decrypted = this.web3.eth.accounts.decrypt(accountData.encrypted, password);
return await this.web3.eth.accounts.signTransaction(txObject, decrypted.privateKey);
}
// Get all addresses
getAddresses() {
return Array.from(this.accounts.keys());
}
// Remove account
removeAccount(address) {
return this.accounts.delete(address.toLowerCase());
}
}
// Usage
const wallet = new ArbitrumWallet(web3);
const address = wallet.addAccount('0x1234...', 'password123');
console.log('Added account:', address);