CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ethersproject--constants

Common Ethereum constants used for ethers including addresses, hashes, symbols, and BigNumber mathematical constants.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

@ethersproject/constants

@ethersproject/constants provides essential Ethereum constants commonly used across blockchain applications and smart contract interactions. It exports fundamental constants including zero addresses, zero hashes, mathematical constants as BigNumbers, Ethereum-specific values for unit conversions, boundary values for safe arithmetic operations, and display symbols.

Package Information

  • Package Name: @ethersproject/constants
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @ethersproject/constants

Core Imports

import {
  AddressZero,
  HashZero,
  EtherSymbol,
  NegativeOne,
  Zero,
  One,
  Two,
  WeiPerEther,
  MaxUint256,
  MinInt256,
  MaxInt256
} from "@ethersproject/constants";

For CommonJS:

const {
  AddressZero,
  HashZero,
  EtherSymbol,
  NegativeOne,
  Zero,
  One,
  Two,
  WeiPerEther,
  MaxUint256,
  MinInt256,
  MaxInt256
} = require("@ethersproject/constants");

Basic Usage

import { AddressZero, WeiPerEther, Zero, One } from "@ethersproject/constants";

// Use zero address for null address checks
if (userAddress === AddressZero) {
  console.log("Invalid address");
}

// Convert ether to wei
const oneEtherInWei = WeiPerEther;
console.log(oneEtherInWei.toString()); // "1000000000000000000"

// Use BigNumber constants for arithmetic
const sum = Zero.add(One);
console.log(sum.toString()); // "1"

Capabilities

Address Constants

Constants for commonly used Ethereum addresses.

/** The zero address (null address) for Ethereum */
const AddressZero: "0x0000000000000000000000000000000000000000";

Hash Constants

Constants for commonly used Ethereum hashes.

/** The zero hash (null hash) for Ethereum */
const HashZero: "0x0000000000000000000000000000000000000000000000000000000000000000";

String Constants

Constants for Ethereum-related string values.

/** The Unicode symbol for Ether (Ξ) */
const EtherSymbol: "\u039E";

Mathematical Constants

BigNumber constants for common mathematical operations and Ethereum-specific values.

/** BigNumber representation of -1 */
const NegativeOne: BigNumber;

/** BigNumber representation of 0 */
const Zero: BigNumber;

/** BigNumber representation of 1 */
const One: BigNumber;

/** BigNumber representation of 2 */
const Two: BigNumber;

/** Number of wei in one ether (10^18) */
const WeiPerEther: BigNumber;

/** Maximum value for a 256-bit unsigned integer */
const MaxUint256: BigNumber;

/** Minimum value for a 256-bit signed integer */
const MinInt256: BigNumber;

/** Maximum value for a 256-bit signed integer */
const MaxInt256: BigNumber;

Types

/** BigNumber type from @ethersproject/bignumber package */
type BigNumberish = BigNumber | string | number | bigint;

declare class BigNumber {
  // Arithmetic operations
  add(other: BigNumberish): BigNumber;
  sub(other: BigNumberish): BigNumber;
  mul(other: BigNumberish): BigNumber;
  div(other: BigNumberish): BigNumber;
  mod(other: BigNumberish): BigNumber;
  pow(other: BigNumberish): BigNumber;
  abs(): BigNumber;
  
  // Comparison operations
  eq(other: BigNumberish): boolean;
  lt(other: BigNumberish): boolean;
  lte(other: BigNumberish): boolean;
  gt(other: BigNumberish): boolean;
  gte(other: BigNumberish): boolean;
  
  // State checking
  isZero(): boolean;
  isNegative(): boolean;
  
  // Conversion methods
  toString(): string;
  toNumber(): number;
  toHexString(): string;
  toBigInt(): bigint;
  
  // Static constructor
  static from(value: any): BigNumber;
  static isBigNumber(value: any): value is BigNumber;
}

Usage Examples

Unit Conversion

import { WeiPerEther } from "@ethersproject/constants";
import { BigNumber } from "@ethersproject/bignumber";

// Convert ether amount to wei
function etherToWei(etherAmount: number): BigNumber {
  return WeiPerEther.mul(etherAmount);
}

// Example: Convert 5 ether to wei
const fiveEtherInWei = etherToWei(5);
console.log(fiveEtherInWei.toString()); // "5000000000000000000"

Address Validation

import { AddressZero } from "@ethersproject/constants";

// Check if an address is the zero address
function isNullAddress(address: string): boolean {
  return address === AddressZero;
}

// Example usage
const userAddress = "0x0000000000000000000000000000000000000000";
if (isNullAddress(userAddress)) {
  console.log("This is a null address");
}

Safe Arithmetic Operations

import { Zero, One, MaxUint256 } from "@ethersproject/constants";
import { BigNumber } from "@ethersproject/bignumber";

// Safe addition that checks for overflow
function safeAdd(a: BigNumber, b: BigNumber): BigNumber {
  const result = a.add(b);
  if (result.gt(MaxUint256)) {
    throw new Error("Addition overflow");
  }
  return result;
}

// Example: Basic calculations
const sum = Zero.add(One);
const doubleSum = sum.add(One);
console.log(doubleSum.toString()); // "2"

Hash Comparisons

import { HashZero } from "@ethersproject/constants";

// Check if a transaction hash is the zero hash
function isNullHash(hash: string): boolean {
  return hash === HashZero;
}

// Example usage in transaction validation
function validateTransaction(txHash: string) {
  if (isNullHash(txHash)) {
    throw new Error("Invalid transaction hash");
  }
  return true;
}
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@ethersproject/constants@5.8.x
Publish Source
CLI
Badge
tessl/npm-ethersproject--constants badge