A complete and compact Ethereum library for dapps, wallets and any other tools.
npx @tessl/cli install tessl/npm-ethers@6.15.0Ethers is a complete and compact Ethereum library for building decentralized applications (dApps), wallets, and blockchain tools. It provides comprehensive functionality for wallet management, smart contract interaction, blockchain connectivity, and cryptographic operations with full TypeScript support.
npm install ethersimport { ethers } from "ethers";For direct imports:
import {
Contract,
Wallet,
JsonRpcProvider,
parseEther,
formatEther
} from "ethers";CommonJS:
const { ethers } = require("ethers");
const { Contract, Wallet, JsonRpcProvider } = require("ethers");import { ethers } from "ethers";
// Connect to Ethereum network
const provider = new ethers.JsonRpcProvider("https://mainnet.infura.io/v3/YOUR-PROJECT-ID");
// Create a wallet
const wallet = new ethers.Wallet("YOUR-PRIVATE-KEY", provider);
// Interact with a contract
const contractAddress = "0x...";
const abi = [...]; // Contract ABI
const contract = new ethers.Contract(contractAddress, abi, wallet);
// Call a contract method
const result = await contract.balanceOf(wallet.address);
console.log(ethers.formatEther(result));
// Send a transaction
const tx = await contract.transfer("0x...", ethers.parseEther("1.0"));
await tx.wait();Ethers is built around several key components:
Connect to Ethereum networks through various provider types including JSON-RPC, WebSocket, and browser-injected providers. Supports mainnet, testnets, and custom networks with automatic network detection.
class JsonRpcProvider extends AbstractProvider {
constructor(url?: string, network?: Networkish, options?: JsonRpcApiProviderOptions);
}
class WebSocketProvider extends SocketProvider {
constructor(url: string | WebSocketLike, network?: Networkish);
}
function getDefaultProvider(network?: Networkish, options?: any): Provider;Providers and Network Connection
Create and manage Ethereum accounts with support for private keys, mnemonic phrases, HD wallets, JSON keystores, and hardware wallet integration.
class Wallet extends BaseWallet {
constructor(key: string | SigningKey, provider?: Provider);
static createRandom(provider?: Provider): Wallet;
static fromPhrase(phrase: string, provider?: Provider): Wallet;
}
class HDNodeWallet extends BaseWallet {
static fromPhrase(phrase: string, password?: string, path?: string, provider?: Provider): HDNodeWallet;
derivePath(path: string): HDNodeWallet;
}Interact with deployed smart contracts through type-safe interfaces with automatic ABI encoding/decoding, event filtering, and transaction management.
class Contract extends BaseContract {
constructor(target: string, abi: Interface | InterfaceAbi, runner?: ContractRunner);
connect(runner: ContractRunner): Contract;
attach(target: string): Contract;
}
class ContractFactory {
constructor(abi: Interface | InterfaceAbi, bytecode: BytesLike, runner?: ContractRunner);
deploy(...args: Array<any>): Promise<BaseContract>;
}Complete Application Binary Interface utilities for encoding function calls, decoding transaction data, and parsing event logs with full type safety.
class AbiCoder {
encode(types: ReadonlyArray<string | ParamType>, values: ReadonlyArray<any>): string;
decode(types: ReadonlyArray<string | ParamType>, data: BytesLike): Result;
}
class Interface {
constructor(fragments: InterfaceAbi);
encodeFunctionData(fragment: string | FunctionFragment, values?: ReadonlyArray<any>): string;
decodeFunctionResult(fragment: string | FunctionFragment, data: BytesLike): Result;
}Low-level cryptographic functions for key generation, signing, hashing, and verification with support for various algorithms including keccak256, ECDSA, and BIP-39 mnemonics.
function keccak256(data: BytesLike): string;
function sha256(data: BytesLike): string;
function randomBytes(length: number): Uint8Array;
class SigningKey {
constructor(privateKey: BytesLike);
readonly address: string;
sign(digest: BytesLike): Signature;
}Essential utilities for data manipulation, unit conversion, address validation, ENS name resolution, and error handling.
function parseEther(ether: string): bigint;
function formatEther(wei: BigNumberish): string;
function parseUnits(value: string, unit?: string | number): bigint;
function formatUnits(value: BigNumberish, unit?: string | number): string;
function isAddress(value: any): value is string;
function getAddress(address: string): string;Core types used throughout the ethers library:
type Provider = {
getNetwork(): Promise<Network>;
getBlockNumber(): Promise<number>;
getBalance(address: string, blockTag?: BlockTag): Promise<bigint>;
call(transaction: TransactionRequest): Promise<string>;
sendTransaction(signedTransaction: string): Promise<TransactionResponse>;
// ... additional provider methods
};
type Signer = {
provider?: Provider;
getAddress(): Promise<string>;
signMessage(message: string | Uint8Array): Promise<string>;
signTransaction(transaction: TransactionRequest): Promise<string>;
sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse>;
};
interface TransactionRequest {
to?: string;
from?: string;
value?: BigNumberish;
data?: string;
gasLimit?: BigNumberish;
gasPrice?: BigNumberish;
maxFeePerGas?: BigNumberish;
maxPriorityFeePerGas?: BigNumberish;
nonce?: number;
type?: number;
chainId?: number;
}
interface TransactionResponse {
hash: string;
to?: string;
from: string;
nonce: number;
gasLimit: bigint;
gasPrice?: bigint;
maxFeePerGas?: bigint;
maxPriorityFeePerGas?: bigint;
data: string;
value: bigint;
chainId: bigint;
signature: Signature;
accessList?: AccessList;
wait(confirmations?: number): Promise<TransactionReceipt>;
}
type BytesLike = string | Uint8Array;
type BigNumberish = string | number | bigint;
type Addressable = { getAddress(): Promise<string> };
type AddressLike = string | Promise<string> | Addressable;