Convert Figma logo to code with AI

ethers-io logoethers.js

Complete Ethereum library and wallet implementation in JavaScript.

7,869
1,819
7,869
518

Top Related Projects

19,151

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.

14,020

:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.

:globe_with_meridians: :electric_plug: The MetaMask browser extension enables browsing Ethereum blockchain enabled websites

OpenZeppelin Contracts is a library for secure smart contract development.

Quick Overview

Ethers.js is a complete and compact library for interacting with the Ethereum blockchain and its ecosystem. It provides a set of tools for developers to work with Ethereum, smart contracts, and decentralized applications (dApps) in JavaScript and TypeScript environments.

Pros

  • Lightweight and modular design, allowing for efficient tree-shaking
  • Extensive documentation and active community support
  • Supports both browser and Node.js environments
  • Type-safe with built-in TypeScript support

Cons

  • Learning curve for developers new to blockchain development
  • Some features may require additional dependencies
  • Performance can be affected when dealing with large-scale operations
  • Limited support for older Ethereum node versions

Code Examples

  1. Connecting to an Ethereum network:
const { ethers } = require("ethers");

// Connect to the Ethereum mainnet
const provider = new ethers.providers.JsonRpcProvider("https://mainnet.infura.io/v3/YOUR-PROJECT-ID");
  1. Sending a transaction:
async function sendTransaction(to, value) {
  const signer = provider.getSigner();
  const tx = await signer.sendTransaction({
    to: to,
    value: ethers.utils.parseEther(value)
  });
  await tx.wait();
  console.log("Transaction sent:", tx.hash);
}
  1. Interacting with a smart contract:
const contractABI = [/* ABI array */];
const contractAddress = "0x...";

const contract = new ethers.Contract(contractAddress, contractABI, provider);

async function callContractMethod() {
  const result = await contract.someMethod();
  console.log("Method result:", result);
}

Getting Started

To start using Ethers.js in your project:

  1. Install the package:

    npm install ethers
    
  2. Import and use in your JavaScript/TypeScript file:

    const { ethers } = require("ethers");
    // or
    import { ethers } from "ethers";
    
    // Connect to a provider
    const provider = new ethers.providers.JsonRpcProvider("https://mainnet.infura.io/v3/YOUR-PROJECT-ID");
    
    // Now you can use the provider to interact with the Ethereum network
    

For more detailed information and advanced usage, refer to the official Ethers.js documentation.

Competitor Comparisons

19,151

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.

Pros of web3.js

  • Longer history and wider adoption in the Ethereum ecosystem
  • More comprehensive documentation and community resources
  • Supports a broader range of Ethereum-related functionalities

Cons of web3.js

  • Larger bundle size, which can impact application performance
  • Less intuitive API design, requiring more verbose code
  • Slower development cycle and less frequent updates

Code Comparison

web3.js:

const Web3 = require('web3');
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR-PROJECT-ID');
const balance = await web3.eth.getBalance('0x1234...');

ethers.js:

const ethers = require('ethers');
const provider = new ethers.providers.JsonRpcProvider('https://mainnet.infura.io/v3/YOUR-PROJECT-ID');
const balance = await provider.getBalance('0x1234...');

Both libraries serve similar purposes, but ethers.js generally offers a more modern and streamlined approach to interacting with Ethereum networks. While web3.js has been the go-to library for many developers due to its long-standing presence, ethers.js has gained popularity for its smaller size, better TypeScript support, and more intuitive API design. The choice between the two often depends on specific project requirements and developer preferences.

14,020

:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.

Pros of Truffle

  • Comprehensive development environment with built-in testing framework
  • Automated contract deployments and migrations
  • Integrated console for direct contract interaction

Cons of Truffle

  • Steeper learning curve for beginners
  • Less flexible for custom configurations compared to Ethers.js
  • Heavier dependency footprint

Code Comparison

Truffle contract deployment:

const MyContract = artifacts.require("MyContract");

module.exports = function(deployer) {
  deployer.deploy(MyContract);
};

Ethers.js contract deployment:

const factory = new ethers.ContractFactory(abi, bytecode, signer);
const contract = await factory.deploy();
await contract.deployed();

Truffle focuses on providing a full-featured development environment for Ethereum smart contracts, including testing, deployment, and interaction tools. It offers a more structured approach to contract development and deployment.

Ethers.js, on the other hand, is a lightweight and flexible library for interacting with the Ethereum blockchain. It provides low-level access to Ethereum functionality and is often used in conjunction with other tools for a complete development setup.

The choice between Truffle and Ethers.js depends on the project requirements, developer preferences, and the desired level of control over the development process.

:globe_with_meridians: :electric_plug: The MetaMask browser extension enables browsing Ethereum blockchain enabled websites

Pros of metamask-extension

  • User-friendly browser extension with a graphical interface for managing Ethereum accounts and transactions
  • Widely adopted and integrated with numerous dApps, providing a seamless user experience
  • Includes features like token management, network switching, and transaction history

Cons of metamask-extension

  • Larger codebase and more complex architecture due to its full-featured nature
  • Potential security risks associated with browser extensions and key management
  • Less flexible for developers looking to integrate Ethereum functionality into their own applications

Code Comparison

metamask-extension (React component for displaying account balance):

const AccountBalance = ({ balance }) => (
  <div className="account-balance">
    <span>{formatBalance(balance)}</span>
    <span>ETH</span>
  </div>
);

ethers.js (JavaScript code for retrieving account balance):

const provider = new ethers.providers.JsonRpcProvider();
const balance = await provider.getBalance(address);
console.log(ethers.utils.formatEther(balance));

While metamask-extension focuses on providing a complete user interface, ethers.js offers a more lightweight and flexible approach for developers to interact with Ethereum networks programmatically.

OpenZeppelin Contracts is a library for secure smart contract development.

Pros of openzeppelin-contracts

  • Provides a comprehensive library of secure, audited smart contract implementations
  • Offers standardized and battle-tested contracts for common use cases (e.g., ERC20, ERC721)
  • Includes built-in security features and best practices for Solidity development

Cons of openzeppelin-contracts

  • Focused solely on smart contract development, lacking client-side interaction capabilities
  • May require additional tools or libraries for full dApp development
  • Learning curve for understanding and customizing complex contract structures

Code Comparison

openzeppelin-contracts (ERC20 token implementation):

contract MyToken is ERC20 {
    constructor(uint256 initialSupply) ERC20("MyToken", "MTK") {
        _mint(msg.sender, initialSupply);
    }
}

ethers.js (Interacting with an ERC20 token):

const tokenContract = new ethers.Contract(tokenAddress, tokenABI, signer);
const balance = await tokenContract.balanceOf(walletAddress);
await tokenContract.transfer(recipientAddress, amount);

While openzeppelin-contracts focuses on secure smart contract implementations, ethers.js provides a powerful JavaScript library for interacting with Ethereum and smart contracts. They serve different purposes in the Ethereum development ecosystem and are often used together in dApp projects.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

The Ethers Project

npm (tag) CI Tests npm bundle size (version) npm (downloads) GitPOAP Badge Twitter Follow


A complete, compact and simple library for Ethereum and ilk, written in TypeScript.

Features

  • Keep your private keys in your client, safe and sound
  • Import and export JSON wallets (Geth, Parity and crowdsale)
  • Import and export BIP 39 mnemonic phrases (12 word backup phrases) and HD Wallets (English as well as Czech, French, Italian, Japanese, Korean, Simplified Chinese, Spanish, Traditional Chinese)
  • Meta-classes create JavaScript objects from any contract ABI, including ABIv2 and Human-Readable ABI
  • Connect to Ethereum nodes over JSON-RPC, INFURA, Etherscan, Alchemy, Ankr or MetaMask
  • ENS names are first-class citizens; they can be used anywhere an Ethereum addresses can be used
  • Small (~144kb compressed; 460kb uncompressed)
  • Tree-shaking focused; include only what you need during bundling
  • Complete functionality for all your Ethereum desires
  • Extensive documentation
  • Large collection of test cases which are maintained and added to
  • Fully written in TypeScript, with strict types for security and safety
  • MIT License (including ALL dependencies); completely open source to do with as you please

Keep Updated

For advisories and important notices, follow @ethersproject on Twitter (low-traffic, non-marketing, important information only) as well as watch this GitHub project.

For more general news, discussions, and feedback, follow or DM me, @ricmoo on Twitter or on the Ethers Discord.

For the latest changes, see the CHANGELOG.

Summaries

Installing

NodeJS

/home/ricmoo/some_project> npm install ethers

Browser (ESM)

The bundled library is available in the ./dist/ folder in this repo.

<script type="module">
    import { ethers } from "./dist/ethers.min.js";
</script>

Documentation

Browse the documentation online:

Providers

Ethers works closely with an ever-growing list of third-party providers to ensure getting started is quick and easy, by providing default keys to each service.

These built-in keys mean you can use ethers.getDefaultProvider() and start developing right away.

However, the API keys provided to ethers are also shared and are intentionally throttled to encourage developers to eventually get their own keys, which unlock many other features, such as faster responses, more capacity, analytics and other features like archival data.

When you are ready to sign up and start using for your own keys, please check out the Provider API Keys in the documentation.

A special thanks to these services for providing community resources:

Extension Packages

The ethers package only includes the most common and most core functionality to interact with Ethereum. There are many other packages designed to further enhance the functionality and experience.

  • MulticallProvider - A Provider which bundles multiple call requests into a single call to reduce latency and backend request capacity
  • MulticoinPlugin - A Provider plugin to expand the support of ENS coin types
  • GanaceProvider - A Provider for in-memory node instances, for fast debugging, testing and simulating blockchain operations
  • Optimism Utilities - A collection of Optimism utilities
  • LedgerSigner - A Signer to interact directly with Ledger Hardware Wallets

License

MIT License (including all dependencies).

NPM DownloadsLast 30 Days