UNPKG

0xtrails

Version:

SDK for Trails

89 lines (78 loc) 2.83 kB
export async function requestWithTimeout<T>( client: { request: (...args: any[]) => Promise<T> }, args: Parameters<typeof client.request>, timeoutMs: number, ): Promise<T> { return Promise.race([ client.request(...args), new Promise<never>((_, reject) => setTimeout(() => reject(new Error("Request timed out")), timeoutMs), ), ]) } export function bigintToString(n: bigint): string { return n.toString() } /** * Formats completion time in seconds to human-readable format * @param seconds - Time in seconds * @returns Formatted time string (e.g., "10s", "1m30s", "2h15m") */ export function formatElapsed(seconds: number): string { if (seconds < 60) { return `${seconds}s` } else if (seconds < 3600) { const minutes = Math.floor(seconds / 60) const remainingSeconds = seconds % 60 return remainingSeconds > 0 ? `${minutes}m${remainingSeconds}s` : `${minutes}m` } else { const hours = Math.floor(seconds / 3600) const remainingMinutes = Math.floor((seconds % 3600) / 60) const remainingSeconds = seconds % 60 if (remainingMinutes > 0 || remainingSeconds > 0) { return `${hours}h${remainingMinutes}m${remainingSeconds > 0 ? `${remainingSeconds}s` : ""}` } else { return `${hours}h` } } } /** * Calculates and formats time ago from a timestamp * @param timestamp - Timestamp in milliseconds * @returns Formatted time ago string (e.g., "Sent just now", "Sent 5 minutes ago") */ export function getTimeAgo(timestamp?: number): string { if (!timestamp) return "Sent just now" const now = Date.now() const diffInSeconds = Math.floor((now - timestamp) / 1000) if (diffInSeconds < 1) return "Sent just now" if (diffInSeconds === 1) return "Sent 1 second ago" if (diffInSeconds < 60) return `Sent ${diffInSeconds} seconds ago` const diffInMinutes = Math.floor(diffInSeconds / 60) if (diffInMinutes === 1) return "Sent 1 minute ago" if (diffInMinutes < 60) return `Sent ${diffInMinutes} minutes ago` const diffInHours = Math.floor(diffInMinutes / 60) if (diffInHours === 1) return "Sent 1 hour ago" if (diffInHours < 24) return `Sent ${diffInHours} hours ago` const diffInDays = Math.floor(diffInHours / 24) if (diffInDays === 1) return "Sent 1 day ago" return `Sent ${diffInDays} days ago` } export function bigintReplacer(_key: string, value: any) { return typeof value === "bigint" ? value.toString() : value } /** * Truncates an Ethereum address to show first 6 and last 4 characters * @param address - The full Ethereum address * @returns Truncated address string (e.g., "0x1234...abcd") */ export function truncateAddress( address?: string | null, start = 6, end = 4, ): string { if (!address) return "" return `${address.slice(0, start)}${address.slice(-end)}` }