Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward dispatch errors #89

Open
forgetso opened this issue Jun 5, 2023 · 0 comments
Open

Forward dispatch errors #89

forgetso opened this issue Jun 5, 2023 · 0 comments
Assignees

Comments

@forgetso
Copy link

forgetso commented Jun 5, 2023

Hi,

Dispatch errors are not properly forwarded from typechain-polkadot. The query will pass if the transaction is valid but then the dispatch error is not properly unpacked, resulting in the unhelpful

{"message":"Module"}

Checking polkadot apps shows

system.ExtrinsicFailed
An extrinsic failed. 
dispatchError: SpRuntimeDispatchError
{
  Module: {
    index: 8
    error: 0x06000000
  }
}
type
contracts.ContractNotFound

It seems like there is no way to access the dispatch error from typechain polkadot. I can assign the transaction result to a const but it is never populated as the error is thrown within typechain-polkadot

const txResult = await tasks.contract.tx.myFunction(...myArgs) // txResult is never populated

Instead I have to catch the error from the transaction

    try {
        const txResult = await tasks.contract.tx.myFunction(...myArgs) // txResult is never populated
    } catch (err) {
        logger.error(JSON.stringify(err)) // the module error appears here
    }

Logger output only gives

{"from":"5EjTA28bKSbFPPyMbUjNtArxyqjwq38r1BapVmLZShaqEedV","txHash":"0xa9ad2d8b949c22c1806fa297162305ca5b0735bc4a5c09b6829980214b5af788","blockHash":"0x28e974ee048f13268cb5ad16cd9d478f7e051b0d685e7b270a0a85dd5806f22c","events":[],"error":{"message":"Module"}}

Therefore, you either need to

  1. Decode the dispatch error before throwing the error so that the relevant error message is thrown when the user uses try-catch
    or
  2. Not throw an error and pass through the raw error information to the user. The user would then have to decode the error with the registry.
// Convert a dispatch error to a readable message
export function getDispatchError(dispatchError: DispatchError): string {
    if (dispatchError.isModule) {
        try {
            const mod = dispatchError.asModule
            const error = dispatchError.registry.findMetaError(mod)
            ...
}

Personally, I prefer option 1 as option 2 is what polkadot-js does and it leaves a lot of complexity to the smart contract developer.

Maybe there is an option 3. which is

  1. Decode the error and not throw, simply passing passing back the decoded error in result.error, again allowing the user to decide when to throw.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants