How to create pallet errors that show up on the Polkadot JS apps interface?
So I've been at my wits' end for quite some time regarding #[pallet::error]
showing up on Polkadot JS apps interface. So as I've come to understand following substrate kitties tutorial that we require to return a DispatchResult
out of our dispatchable calls and after doing so, I have a case where I am not seeing any errors on my apps interface, although the operation does fail successfully as I don't see any mistakes on the balancesToAccount hashmap.
This code compiles fine.
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
/// Allow only Root to mint new tokens & transfer it to some benefactor account
/// Set a hard uppper limit on the total number of tokens in supply
pub fn mint(
origin: OriginFor<T>,
#[pallet::compact] amount: <T as Config>::Balance,
benefactor: T::AccountId,
) -> DispatchResult {
// Check if sudo call
ensure_root(origin.clone())?;
// Check if existing amount doesn't overflow MaxTokenSupply
Error |----> Self::does_adding_overflow_maxtokensupply(amount)?;
// Check if Benefactor already has funds
let previous_balance = <BalanceToAccount<T>>::try_get(&benefactor).unwrap_or_default();
let final_balance = previous_balance.saturating_add(amount);
<BalanceToAccount<T>>::insert(&benefactor, final_balance);
Self::deposit_event(Event::MintedNewSupply(amount));
Ok(().into())
}
}
As you can see I have a helper function that will check if minting amount
causes a overflow to the total token supply. And that function is defined here:
impl<T: Config> Pallet<T> {
fn does_adding_overflow_maxtokensupply(amount: T::Balance) -> Result<(), Error<T>> {
let total_already_minted: T::Balance = BalanceToAccount::<T>::iter_values().sum();
let new_supply =
total_already_minted.checked_add(&amount).ok_or(Error::<T>::MintTypeOverflow)?;
if new_supply <= T::MaxTokenSupply::get() {
Ok(())
} else {
Err(Error::<T>::MintCausingTotalSupplyOverflow)
}
}
}
I was expected ?
to automagically cast the Error into something DispatchResult
can work with, but I'm not sure if that's happening on this line:
Self::does_adding_overflow_maxtokensupply(amount)?;
Here's what Polkadot JS apps shows when I give it a number greater than max supply : Next I check whether that balance went through (It shouldn't), and indeed it didn't go through.
What I would like the apps interface is to show an error rejecting such requests as it does in case of BadOrigin
. How can I achieve this?
Edit #1: I should mention that this function is only callable by sudo
, if that makes a difference. I can see extrinsic Errors on other functions, except this one.
Solution 1:
I suspect that this is similar to util.batch - I suspect the sudo is successful whether the internal extrinsic(s) are successful or not and that the UI I suspect is not showing the aggregation of whether all the extrinsics within the wrapper are successful. It is an unsatisfying answer though. I would check to see if this has been raised as an issue in polkadot-js and if not raise it.