Convert a human-readable decimal string to the smallest unit (wei, satoshi, etc.).
import { parseUnits } from '@avail-project/nexus-core';// Parse ETH to weiconst wei = parseUnits('1.5', 18);// 1500000000000000000n// Parse USDC to base unitsconst usdcAmount = parseUnits('100.50', 6);// 100500000n
import { parseUnits } from '@avail-project/nexus-core';const amount = parseUnits('1.5', 18);// 1500000000000000000n (1.5 ETH in wei)const small = parseUnits('0.001', 18);// 1000000000000000n (0.001 ETH in wei)
Convert from the smallest unit to a human-readable decimal string.
import { formatUnits } from '@avail-project/nexus-core';// Convert wei to ETHconst eth = formatUnits(1500000000000000000n, 18);// "1.5"// Convert USDC base units to decimalconst usdc = formatUnits(100500000n, 6);// "100.5"
Convert a human-readable token amount to bigint using the SDK’s chain and token metadata. This is a convenience method that automatically looks up the correct decimals for a token on a specific chain.
import { NexusSDK } from '@avail-project/nexus-core';const sdk = new NexusSDK({ network: 'mainnet' });// Convert USDC on Ethereum (6 decimals)const ethUSDC = sdk.convertTokenReadableAmountToBigInt('100.50', 'USDC', 1);// 100500000n// Convert USDC on BNB Chain (18 decimals)const bnbUSDC = sdk.convertTokenReadableAmountToBigInt('100.50', 'USDC', 56);// 100500000000000000000n// Use in bridge operationconst amount = sdk.convertTokenReadableAmountToBigInt('50', 'USDC', 137);await sdk.bridge({ token: 'USDC', amount, // Automatically uses correct decimals for Polygon USDC toChainId: 137,});
This method requires the SDK to be initialized with chain metadata. Use the standalone parseUnits() function if you need to convert amounts before SDK initialization.
// SDK method - decimals looked up automaticallyconst amount = sdk.convertTokenReadableAmountToBigInt('100', 'USDC', 1);// Automatically uses 6 decimals for USDC on Ethereum
import { parseUnits } from '@avail-project/nexus-core';// Manual method - must specify decimalsconst amount = parseUnits('100', 6);// Must know USDC has 6 decimals on Ethereum
Use sdk.convertTokenReadableAmountToBigInt() when working with supported tokens to avoid decimal mismatch errors across different chains. For example, USDC has 6 decimals on most chains but 18 decimals on BNB Chain.