UNPKG

@wasserstoff/tribes-sdk

Version:

SDK for integrating with Tribes by Astrix platform on any EVM compatible chain

91 lines (90 loc) 3.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatTokenAmount = formatTokenAmount; exports.formatAddress = formatAddress; exports.formatDate = formatDate; exports.formatNumber = formatNumber; exports.formatPercentage = formatPercentage; const ethers_1 = require("ethers"); /** * Format a token amount to a human-readable string * @param amount Amount in wei * @param decimals Number of decimals * @param symbol Token symbol */ function formatTokenAmount(amount, decimals = 18, symbol) { const formattedAmount = ethers_1.ethers.formatUnits(amount, decimals); if (symbol) { return `${formattedAmount} ${symbol}`; } return formattedAmount; } /** * Format an address with truncation * @param address Address to format * @param prefixLength Number of characters to keep at the beginning * @param suffixLength Number of characters to keep at the end */ function formatAddress(address, prefixLength = 6, suffixLength = 4) { if (!address || !ethers_1.ethers.isAddress(address)) { return address; } const start = address.slice(0, prefixLength + 2); // +2 for '0x' const end = address.slice(-suffixLength); return `${start}...${end}`; } /** * Format a date to ISO string or custom format * @param timestamp Timestamp in seconds * @param format Format string (currently only supports 'iso') */ function formatDate(timestamp, format = 'iso') { // Convert to milliseconds if it's in seconds const ts = timestamp < 1000000000000 ? timestamp * 1000 : timestamp; const date = new Date(ts); if (format === 'relative') { const now = Date.now(); const diff = now - ts; // Calculate relative time const seconds = Math.floor(diff / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); const days = Math.floor(hours / 24); if (days > 0) { return `${days} day${days > 1 ? 's' : ''} ago`; } else if (hours > 0) { return `${hours} hour${hours > 1 ? 's' : ''} ago`; } else if (minutes > 0) { return `${minutes} minute${minutes > 1 ? 's' : ''} ago`; } else { return `${seconds} second${seconds !== 1 ? 's' : ''} ago`; } } return date.toISOString(); } /** * Format a number with thousand separators * @param value Number to format * @param decimals Number of decimal places */ function formatNumber(value, decimals = 0) { return new Intl.NumberFormat('en-US', { minimumFractionDigits: decimals, maximumFractionDigits: decimals }).format(value); } /** * Format a percentage * @param value Percentage value (0-100) * @param decimals Number of decimal places */ function formatPercentage(value, decimals = 2) { return new Intl.NumberFormat('en-US', { style: 'percent', minimumFractionDigits: decimals, maximumFractionDigits: decimals }).format(value / 100); }