Utilities Best Practices

Recommendations

Formatting and Parsing

Use Correct Decimals

Different tokens have different decimal configurations. Always fetch and use the token's actual decimal value rather than assuming a default.

Avoid Floating Point

Use bigint for all calculations to prevent floating-point precision errors. Format values to decimal strings only for display purposes.

Handle User Input

Validate and sanitize user-provided amounts before parsing. Check for empty strings, invalid characters, and negative values before conversion.

Round Appropriately

Display values with appropriate precision for the context. Full precision may be necessary for transaction details, while condensed formats suit dashboard displays.

Include Symbol

Always show the token or currency symbol alongside formatted values to provide clear context and prevent user confusion.

Revert Decoder

Always Check Reverts

Inspect the revert property on CallResult after simulations, and both revert and rawRevert on TransactionBase for confirmed transactions.

User-Friendly Messages

Convert technical error messages to user-friendly text. Map common revert reasons to clear explanations that guide users toward resolution.

Log Raw Data

Preserve raw revert data for debugging and support purposes. Technical details aid in diagnosing issues that user-friendly messages may obscure.

Handle Unknown Errors

Provide fallback handling for unrecognized error patterns. Display a generic message while logging the full error for investigation.

Centralize Handling

Use a dedicated service class for consistent error handling across the application. Centralizing logic ensures uniform user experience and simplifies maintenance.