3

I am doing sudo call using this example Making sudo call using subxt:

let found_event = api
        .tx()
        .sign_and_submit_then_watch_default(&tx, &bob)
        .await?
        .wait_for_finalized_success()
        .await?
        .has::<rococo::sudo::events::Sudid>()?;

I want to throw an error if the extrinsic fails, like in this example someone without sudo privileges (Bob) tries to do the call.

How is the best way using subxt to submit an extrinsic to catch and parse the error?

1 Answer 1

3

A DispatchError is returned if the extrinsic failed; this ends up being returned from the above as a subxt::Error::Runtime(dispatch_error) after wait_for_finalized_success, so you can match on it there and inspect the error to see what went wrong, eg like this pseudocode:

fn submit_foo() -> Result<bool, subxt::Error> {
    api
        .tx()
        .sign_and_submit_then_watch_default(&tx, &bob)
        .await?
        .wait_for_finalized_success()
        .await?
        .has::<rococo::sudo::events::Sudid>()?
}

if let Err(subxt::Error::Runtime(dispatch_err)) = submit_foo() {
    eprintln!("{dispatch_err}"); 
    // ...or match on it to check for specific variants,
    // DispatchError::Module(module_err) for instance lets
    // you access and work with pallet specific errors.
}

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.