From 1f8f6e54bd8f5de44f59182791f5c861058bf8e4 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 14 Apr 2020 10:36:15 +0200 Subject: [PATCH] Add unsigned sending to cookbook (#2162) * Add unsigned sending to cookbook * Block author extraction example --- docs/cookbook/blocks.md | 20 ++++++++++++++++++++ docs/cookbook/tx.md | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/docs/cookbook/blocks.md b/docs/cookbook/blocks.md index 645dccd2c4..2a34177d12 100644 --- a/docs/cookbook/blocks.md +++ b/docs/cookbook/blocks.md @@ -22,3 +22,23 @@ signedBlock.block.extrinsics.forEach((ex, index) => { console.log(index, ex.hash.toHex()); }); ``` + +## How do I extract the block author? + +Th block author is encoded inside the consensus logs for the block. To extract, you need to decode the log (which the API does do) and then map the index of the validator to the list of session validators. This extraction is however available on the api derive for new head subscriptions, which returns an extended header with the author populated (assuming that the digest logs are known). + +```js +// subscribe to all new headers (with extended info) +api.derive.chain.subscribeNewHeads((header) => { + console.log(`#${header.number}: ${header.author}`); +}); +``` + +For a single header only, the derives also contain a `getHeader`, which once again returns a header extended with the author - + +```js +// retrieve the last header (hash optional) +const header = await api.derive.chain.getHeader(); + +console.log(`#${header.number}: ${header.author}`); +``` diff --git a/docs/cookbook/tx.md b/docs/cookbook/tx.md index dff66a4e12..ebe19f8a04 100644 --- a/docs/cookbook/tx.md +++ b/docs/cookbook/tx.md @@ -54,3 +54,21 @@ api.tx.balances } }); ``` + +## 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). + +```js +// construct the transaction, exactly as per normal +const utx = api.tx.claims.claim(beneficiary, ethSignature); + +// send it without calling sign, pass callback with status/events +tx.send(({ status }) => { + if (status.isInBlock) { + console.log(`included in ${status.asInBlock}`); + } +}); +``` + +The signing is indicated by the first byte in the transaction, so in this case we have called `.send` on it (no `.sign` or `.signAndSend`), so it will be sent using the unsigned state, without signature attached.