Overview
TUNA provides formatting utilities for displaying data in a user-friendly way. These utilities handle currency formatting, image URL proxying, and other display-related transformations.Currency Formatting
formatSui
Format MIST amounts into human-readable SUI values with appropriate precision.Amount in MIST (1 SUI = 1,000,000,000 MIST)
string - Formatted string with “SUI” suffix
Formatting Logic
The function applies different precision levels based on the amount:Displays as
"< 0.001 SUI" for very small amountsDisplays with 3 decimal places for amounts less than 1 SUI
Displays with 2 decimal places for amounts of 1 SUI or more
Implementation
Usage Examples
Image Utilities
getProxiedImageUrl
Generate a proxied image URL to bypass CORS and COEP restrictions using wsrv.nl.Original image URL to proxy
Optional width to resize the image to (optimization)
string - Proxied URL with CORS headers
Smart Proxying Logic
The function intelligently determines when to proxy:URLs starting with
data: are returned as-isURLs starting with
/ or blob: are returned unchangedURLs containing
wsrv.nl are not double-proxiedRemote URLs are proxied through wsrv.nl with optimization
Implementation Details
Why Use Image Proxying?
CORS Headers: The wsrv.nl proxy service adds proper CORS headers, allowing images to be used in canvas operations and with strict security policies.
Format Conversion: Images are automatically converted to WebP format for better performance and smaller file sizes.
Resize Optimization: Specifying a width parameter resizes images server-side, reducing bandwidth and improving load times.
Usage Examples
Best Practices
Currency Display
Consistency: Always use
formatSui() for displaying amounts to users. Don’t format MIST values manually to ensure consistent precision across the application.Input vs Display: Store and process amounts in MIST (integer), but display them using
formatSui() for better UX.Image Proxying
Width Optimization: Choose appropriate width values based on the display context. Smaller widths for thumbnails and avatars, larger widths for hero images.
Walrus Integration: Always proxy Walrus URLs for external images to ensure CORS compatibility and optimize delivery.
Source Reference
Formatting utilities are implemented across multiple files: Currency formatting:Related Functions
See also:- suiToMist - Convert SUI to MIST
- mistToSui - Convert MIST to SUI
- getWalrusUrl - Get Walrus blob URLs
