110 lines
2.5 KiB
TypeScript
110 lines
2.5 KiB
TypeScript
/**
|
|
* Format cUSD amount for display
|
|
*/
|
|
export function formatCUSD(amount: number): string {
|
|
if (amount >= 1000) {
|
|
return `${(amount / 1000).toFixed(1)}K cUSD`
|
|
}
|
|
if (amount >= 1) {
|
|
return `${amount.toFixed(2)} cUSD`
|
|
}
|
|
return `${amount.toFixed(3)} cUSD`
|
|
}
|
|
|
|
/**
|
|
* Format time remaining
|
|
*/
|
|
export function formatTimeRemaining(endTime: number): string {
|
|
const now = Date.now()
|
|
const remaining = endTime - now
|
|
|
|
if (remaining <= 0) return 'Poll ended'
|
|
|
|
const hours = Math.floor(remaining / (1000 * 60 * 60))
|
|
const minutes = Math.floor((remaining % (1000 * 60 * 60)) / (1000 * 60))
|
|
const seconds = Math.floor((remaining % (1000 * 60)) / 1000)
|
|
|
|
if (hours > 0) {
|
|
return `${hours}h ${minutes}m left`
|
|
}
|
|
if (minutes > 0) {
|
|
return `${minutes}m ${seconds}s left`
|
|
}
|
|
return `${seconds}s left`
|
|
}
|
|
|
|
/**
|
|
* Get percentage of option votes
|
|
*/
|
|
export function getPercentage(optionVotes: number, totalVotes: number): number {
|
|
if (totalVotes === 0) return 0
|
|
return Math.round((optionVotes / totalVotes) * 100)
|
|
}
|
|
|
|
/**
|
|
* Format large numbers with K, M, B suffixes
|
|
*/
|
|
export function formatNumber(num: number): string {
|
|
if (num >= 1000000000) {
|
|
return `${(num / 1000000000).toFixed(1)}B`
|
|
}
|
|
if (num >= 1000000) {
|
|
return `${(num / 1000000).toFixed(1)}M`
|
|
}
|
|
if (num >= 1000) {
|
|
return `${(num / 1000).toFixed(1)}K`
|
|
}
|
|
return num.toString()
|
|
}
|
|
|
|
/**
|
|
* Format percentage with proper decimal places
|
|
*/
|
|
export function formatPercentage(value: number): string {
|
|
return `${(value * 100).toFixed(2)}%`
|
|
}
|
|
|
|
/**
|
|
* Format timestamp to readable date
|
|
*/
|
|
export function formatDate(timestamp: number): string {
|
|
return new Date(timestamp).toLocaleDateString('en-US', {
|
|
year: 'numeric',
|
|
month: 'short',
|
|
day: 'numeric',
|
|
hour: '2-digit',
|
|
minute: '2-digit'
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Format relative time (e.g., "2 hours ago")
|
|
*/
|
|
export function formatRelativeTime(timestamp: number): string {
|
|
const now = Date.now()
|
|
const diff = now - timestamp
|
|
|
|
const minutes = Math.floor(diff / (1000 * 60))
|
|
const hours = Math.floor(diff / (1000 * 60 * 60))
|
|
const days = Math.floor(diff / (1000 * 60 * 60 * 24))
|
|
|
|
if (days > 0) {
|
|
return `${days} day${days > 1 ? 's' : ''} ago`
|
|
}
|
|
if (hours > 0) {
|
|
return `${hours} hour${hours > 1 ? 's' : ''} ago`
|
|
}
|
|
if (minutes > 0) {
|
|
return `${minutes} minute${minutes > 1 ? 's' : ''} ago`
|
|
}
|
|
return 'Just now'
|
|
}
|
|
|
|
/**
|
|
* Shorten Ethereum address
|
|
*/
|
|
export function shortenAddress(address: string): string {
|
|
if (!address) return ''
|
|
return `${address.slice(0, 6)}...${address.slice(-4)}`
|
|
}
|