@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.
npm install @ethersproject/constantsimport {
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");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"Constants for commonly used Ethereum addresses.
/** The zero address (null address) for Ethereum */
const AddressZero: "0x0000000000000000000000000000000000000000";Constants for commonly used Ethereum hashes.
/** The zero hash (null hash) for Ethereum */
const HashZero: "0x0000000000000000000000000000000000000000000000000000000000000000";Constants for Ethereum-related string values.
/** The Unicode symbol for Ether (Ξ) */
const EtherSymbol: "\u039E";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;/** 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;
}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"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");
}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"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;
}