Implementing Custom Errors in Precompiles
How to create a Maroo precompile that returns structured Solidity errors.
1
Embed the Common Precompile
Ensure your precompile struct embeds
maroo/precompiles/common.Precompile instead of the standard cosmos-sdk precompile struct. This activates the custom error handling logic.import "github.com/maroo-network/maroo/precompiles/common"
type MyPrecompile struct {
common.Precompile
} 2
Return RevertWithData
When an error occurs, ABI-encode your error arguments and wrap them in
RevertWithData.func (p *MyPrecompile) SomeMethod(...) ([]byte, error) {
// ... logic ...
if unauthorized {
// Pack the Solidity error: error Unauthorized(address user)
packed, _ := p.ABI.Errors["Unauthorized"].Inputs.Pack(userAddr)
selector := p.ABI.Errors["Unauthorized"].ID
data := append(selector[:], packed...)
return nil, &common.RevertWithData{Data: data}
}
return result, nil
}