@chain1/crypto-utils
v1.0.1
Published
Cryptocurrency utility functions for formatting and conversion
Downloads
9
Maintainers
Readme
@chain1/crypto-utils
Essential cryptocurrency utility functions for Ethereum-compatible blockchains.
Installation
npm install @chain1/crypto-utilsFeatures
- Wei/Ether/Gwei conversion utilities
- Gas fee calculations
- Address validation
- BigNumber operations
- Zero-dependency ethers.js wrapper
Usage
Wei/Ether Conversion
import { formatEther, parseEther, formatGwei, parseGwei } from '@chain1/crypto-utils';
// Convert Wei to Ether
const ether = formatEther('1000000000000000000'); // "1.0"
// Convert Ether to Wei
const wei = parseEther('1.5'); // BigNumber
// Gwei conversions
const gwei = formatGwei('1000000000'); // "1.0"
const weiFromGwei = parseGwei('50'); // BigNumberGas Fee Calculations
import { calculateGasFee, calculateGasFeeInEther } from '@chain1/crypto-utils';
import { ethers } from 'ethers';
const gasPrice = ethers.BigNumber.from('50000000000'); // 50 Gwei
const gasLimit = ethers.BigNumber.from('21000');
// Calculate gas fee in Wei
const feeWei = calculateGasFee(gasPrice, gasLimit);
// Calculate gas fee in Ether
const feeEther = calculateGasFeeInEther(gasPrice, gasLimit); // "0.00105"Address Validation
import { isValidAddress } from '@chain1/crypto-utils';
const valid = isValidAddress('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb'); // true
const invalid = isValidAddress('invalid-address'); // falseFormat Units
import { formatUnits, parseUnits } from '@chain1/crypto-utils';
// Format custom decimal tokens
const usdt = formatUnits('1000000', 6); // "1.0" (USDT has 6 decimals)
// Parse custom decimal tokens
const amount = parseUnits('100', 6); // BigNumber for 100 USDTAPI Reference
Conversion Functions
formatEther(value: BigNumberish): string- Wei to EtherparseEther(value: string): BigNumber- Ether to WeiformatGwei(value: BigNumberish): string- Wei to GweiparseGwei(value: string): BigNumber- Gwei to WeiformatUnits(value: BigNumberish, decimals: number): string- Wei to custom decimalsparseUnits(value: string, decimals: number): BigNumber- Custom decimals to Wei
Gas Functions
calculateGasFee(gasPrice: BigNumber, gasLimit: BigNumber): BigNumber- Calculate fee in WeicalculateGasFeeInEther(gasPrice: BigNumber, gasLimit: BigNumber): string- Calculate fee in Ether
Validation Functions
isValidAddress(address: string): boolean- Validate Ethereum address
TypeScript Support
Full TypeScript support with type definitions included.
import type { BigNumber, BigNumberish } from 'ethers';License
MIT
Repository
https://github.com/stochain/packages
