testnet
GitHub

Precompile Custom Errors

mechanism developer

A mechanism for precompiles to return structured, Solidity-compatible error data.

Precompile Custom Errors are a feature that translates native Go errors from a Cosmos SDK module into standard Solidity custom errors. This provides a superior developer experience compared to opaque, generic reverts. When a precompile call fails, dApps can decode the revert data to understand exactly why it failed and access relevant context, such as which address was unauthorized.

How It Works

The process is implemented in precompiles/okrw/errors.go. When the precompile's Go logic catches an error from the underlying Cosmos module (e.g., sdkerrors.ErrUnauthorized), a mapping function like mintErrorToRevert is called. This function identifies the specific error type and uses a helper (e.g., packUnauthorizedMinterError) to construct the revert data. The data is composed of the 4-byte function selector of the Solidity error (e.g., bytes4(keccak256("UnauthorizedMinter(address,address)"))) followed by the ABI-encoded error arguments. This byte array is then returned to the EVM, causing the transaction to revert with data that can be parsed by clients like Ethers.js or by other smart contracts using try/catch.

Benefits

1. Clarity: Provides specific, machine-readable reasons for failure.
2. Gas Efficiency: Reverting with custom errors is more gas-efficient than reverting with a descriptive string.
3. Composability: Other smart contracts can catch these specific errors and implement conditional logic based on the failure type.
4. Improved UX: Frontends can parse the error and display a user-friendly message, such as "You are not the authorized minter. The minter is 0x..."

Example: `UnauthorizedMinter`

In the OKRW precompile, if a non-minter address calls mint, the x/okrw module returns sdkerrors.ErrUnauthorized. The mintErrorToRevert function catches this, finds the currently authorized minter's address from the module parameters, and packs both the caller's address and the authorized minter's address into the UnauthorizedMinter(address,address) error format. The EVM then reverts with this structured data.
Source: maroo
ESC
Type to search