This release introduces two quality of life improvements around error handling to the Stylus SDK.
Infallible Methods
External methods may now be infallible. That is, you don't have to write Result
and Ok
unless the code returns an error.
#[external]
impl Counter {
pub fn number(&self) -> U256 {
self.number.get()
}
}
[derive(SolidityError)]
The derive(SolidityError)
macro simplifies the error declaration process.
sol! {
error InsufficientBalance(address from, uint256 have, uint256 want);
error InsufficientAllowance(address owner, address spender, uint256 have, uint256 want);
}
#[derive(SolidityError)]
pub enum Erc20Error {
InsufficientBalance(InsufficientBalance),
InsufficientAllowance(InsufficientAllowance),
}
#[external]
impl Contract {
pub fn fallible_method() -> Result<(), Erc20Error> {
// code that might revert
}
}
The above is abi-compatible with Solidity and will auto-generate interface types.
cargo stylus export-abi
interface IContract {
function fallibleMethod() external;
error InsufficientBalance(address, uint256, uint256);
error InsufficientAllowance(address, address, uint256, uint256);
}