Ethjs Ethereum Utility Library

0.4.0 · abandoned · verified Sun Apr 19

Ethjs is a lightweight, highly optimized JavaScript utility library designed for interacting with the Ethereum blockchain. Released in its current stable version 0.4.0 in 2018, it aimed to provide a more modular and async-only alternative to the then-dominant `web3.js` library, featuring `BN.js` for large number arithmetic and a focus on smaller bundle sizes. However, the project has been unmaintained since its last publish nearly eight years ago. Developers should be aware that `ethjs` is effectively abandoned; its GitHub repository explicitly recommends using `ethers.js` instead. Modern Ethereum development typically leverages actively maintained libraries like `ethers.js` or the `@ethereumjs` monorepo, which offer contemporary features such as ESM support, TypeScript definitions, and native JavaScript `BigInt` for handling large numbers, providing a more robust and future-proof development experience.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates initializing Ethjs with an HTTP provider, fetching blockchain data using a callback, converting units with a static utility, and interacting with a basic ERC-20 contract using a promise-based API.

const Eth = require('ethjs');
const eth = new Eth(new Eth.HttpProvider('https://ropsten.infura.io'));

// Fetch a block by number using a callback (common in older Node.js libs)
eth.getBlockByNumber(45300, true, (err, block) => {
  if (err) {
    console.error('Error fetching block:', err);
    return;
  }
  console.log('Block 45300 data:', block.number.toString(), block.hash);
});

// Convert ether to wei using Eth.toWei utility
const etherValue = Eth.toWei(72, 'ether');
console.log('72 Ether in Wei (BN.js object):', etherValue.toString());

// Interact with a contract using a promise-based API
const tokenABI = [{
  "constant": true,
  "inputs": [],
  "name": "totalSupply",
  "outputs":[{"name": "","type": "uint256"}],
  "payable": false,
  "type": "function"
}];

// This contract address is an example and may not be valid on Ropsten today
const tokenAddress = '0x6e0E0e02377Bc1d90E8a7c21f12BA385C2C35f78';
const token = eth.contract(tokenABI).at(tokenAddress);

token.totalSupply()
  .then((totalSupply) => {
    console.log(`Total supply for token at ${tokenAddress}: ${totalSupply.toString()}`);
  })
  .catch(error => {
    console.error(`Error fetching total supply for token at ${tokenAddress}:`, error);
  });

view raw JSON →