From 20d669a0a6fd7d64d83bf35d805627cb169ad8e1 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 15 Oct 2020 18:36:00 +0200 Subject: [PATCH 1/2] Simplified dispatchError lookup on 2.3 --- docs/api/cookbook/tx.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/api/cookbook/tx.md b/docs/api/cookbook/tx.md index f92f7f131b..b7aa28c6b8 100644 --- a/docs/api/cookbook/tx.md +++ b/docs/api/cookbook/tx.md @@ -57,6 +57,29 @@ api.tx.balances }); ``` +As of the `@polkadot/api` 2.3.1 additional result fields are exposed. Firstly there is `dispatchInfo: DispatchInfo` which occurs in both `ExtrinsicSuccess` & `ExtrinsicFailed` events. Additionally, on failures the `dispatchError: DispatchError` is exposed. With this in mind, the above can be simplified to be - + +```js +api.tx.balances + .transfer(recipient, 123) + .signAndSend(sender, ({ status, events, dispatchError }) => { + // status would still be set, but in the case of error we can shortcut + // to just check it (so an error would indicate InBlock or Finalized) + if (dispatchError) { + if (dispatchError.isModule) { + // for module errors, we have the section indexed, lookup + const decoded = api.registry.findMetaError(error.asModule); + const { documentation, method, section } = decoded; + + console.log(`${section}.${method}: ${documentation.join(' ')}`); + } else { + // Other, CannotLookup, BadOrigin, no extra info + console.log(error.toString()); + } + } + }); +``` + ## How do I send an unsigned extrinsic? For most runtime modules, transactions need to be signed and validation for this happens node-side. There are however modules that accepts unsigned extrinsics, an example would be the Polkadot/Kusama token claims (which is here used as an example). From d7d08a2e6d8a99bc710dc11246f6db55b9ff844e Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 15 Oct 2020 18:36:49 +0200 Subject: [PATCH 2/2] Update docs/api/cookbook/tx.md --- docs/api/cookbook/tx.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/cookbook/tx.md b/docs/api/cookbook/tx.md index b7aa28c6b8..3b2f74db9e 100644 --- a/docs/api/cookbook/tx.md +++ b/docs/api/cookbook/tx.md @@ -68,7 +68,7 @@ api.tx.balances if (dispatchError) { if (dispatchError.isModule) { // for module errors, we have the section indexed, lookup - const decoded = api.registry.findMetaError(error.asModule); + const decoded = api.registry.findMetaError(dispatchError.asModule); const { documentation, method, section } = decoded; console.log(`${section}.${method}: ${documentation.join(' ')}`);