1

Is there a way to preflight/preview a transaction before the actual signAndSend?

I would like to know if a transaction would fail before letting user submit the transaction and prevent user losing gas.

I guess polkadot.js has this kind of function, but nothing find in the doc.

Thanks.

1
  • Have you tried to make query call for the extrinsic? I usually do that if I expect tx to fail.
    – Maario
    Sep 14, 2022 at 12:15

1 Answer 1

4

You can use the DryRun RPC method to check the outcome of the transaction.

Here is an example:

export async function dryRun(api: ApiPromise, signer: KeyringPair, tx: SubmittableExtrinsic<"promise", ISubmittableResult>): Promise<[boolean, ApplyExtrinsicResult]> {
    const signed = await tx.signAsync(signer);
    const dryRun = await api.rpc.system.dryRun(signed.toHex());
    return [dryRun.isOk && dryRun.asOk.isOk, dryRun]
}

Note that this RPC is marked as unsafe, most public nodes will not expose this and you might need to run your own local node in order to be able to call this.

Your Answer

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

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