1

ChainExtension will return Result<RetVal, DispatchError>. (As you can see in the bottom pircutre)

  • When ChainExtension implementation returns Ok(RetVal::Converging(1)) and handle that status code 1 on ink! contract side like below, then Extrinsic itself will be shown as success event even if dispatch failed.

impl ink_env::chain_extension::FromStatusCode for ContractErrorCode {
    fn from_status_code(status_code: u32) -> Result<(), Self> {
        match status_code {
            0 => Ok(()),
            1 => Err(Self::Failed),
            _ => panic!("encountered unknown status code"),
        }
    }
}

(What I did for experiments was ignore Error(DispatchError) and return Ok(RetVal::Converging(1)) instead so that ink! can handle status code 1.) enter image description here

  • When it returns DispatchError (Extrinsic itself fail), ink! smart contract cannot handle it and return its failure reason with #[message] functions' returned value. for e.g. Result<(), ContractError> (here ContractError reflects DispatchError's reason and perhaps we'd like to show it on UI).

The question is What is the appropriate way for ink! smart contract messages to handle DispatchError (from ChainExtension) and return it as its returned value?


enter image description here

1 Answer 1

1

The question is What is the appropriate way for ink! smart contract messages to handle DispatchError (from ChainExtension) and return it as its returned value?

How I understand your question is that you want for the frontend to have the information that an error occurred while calling a contract. And specifically that it was a DispatchError in a chain extension.

The problem that you face when returning RetVal::Diverging from a chain extension's fn call is that Substrate will emit an event ExtrinsicFailed(pallet_contracts::ContractReverted) without the information that it was a DispatchError.

What you can do is to throw the DispatchError immediately in your fn call. This will then result in an ExtrinsicFailed event being emitted on-chain, containing the info that it was a dispatch error.

So in the code from your screenshot it would be:

let result =
  RmrkExtension::execute_func::<E>(func_id_matcher, env)?;
2
  • 1
    Actually, I didn't use ` ::ink_env::chain_extension::ChainExtensionMethod` and its method handle_error_code at that time when I asked question. Then ContractReverted event itself wasn't emitted without using it. After using handle_error_code method properly, substrate emits ContractReverted event when returned RetVal from chain extension mapped to Err on contract side (by from_status_code impl). So, now, we can do both getting ExtrinsicFailed ContractReverted event + contract receiving dispatch error reason from status code (RetVal)
    – Shunsuke
    Sep 8, 2022 at 5:11
  • 1
    Thank you so much!
    – Shunsuke
    Sep 8, 2022 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.