From 1b12c0094b32ea67ca70cac3c8d3df831b0c40c9 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 18 Oct 2018 16:32:50 +0200 Subject: [PATCH] Generate Event metadata docs (#316) * Generate Event metadata docs * Update MetadataMd.ts --- docs/METHODS_EVENTS.md | 163 +++++++++++++++++++++++ docs/SUMMARY.md | 6 +- packages/types/src/scripts/MetadataMd.ts | 122 ++++++++++------- 3 files changed, 243 insertions(+), 48 deletions(-) create mode 100644 docs/METHODS_EVENTS.md diff --git a/docs/METHODS_EVENTS.md b/docs/METHODS_EVENTS.md new file mode 100644 index 0000000000..09fb569678 --- /dev/null +++ b/docs/METHODS_EVENTS.md @@ -0,0 +1,163 @@ +## Events + +Events are emitted for cerain operations on the runtime. The following sections describe the events that are part of the default Substrate runtime. +- **[balances](#balances)** + +- **[contract](#contract)** + +- **[council](#council)** + +- **[councilMotions](#councilMotions)** + +- **[councilVoting](#councilVoting)** + +- **[democracy](#democracy)** + +- **[session](#session)** + +- **[staking](#staking)** + +- **[system](#system)** + +- **[treasury](#treasury)** + + +___ +Back To Top + +### balances + +▸ **NewAccount**(`AccountId`, `AccountIndex`, `NewAccountOutcome`) +- **summary**: A new account was created. + +▸ **ReapedAccount**(`AccountId`) +- **summary**: An account was reaped. + +▸ **Transfer**(`AccountId`, `AccountId`, `Balance`, `Balance`) +- **summary**: Transfer succeeded (from, to, value, fees). + +___ +Back To Top + +### contract + +▸ **Transfer**(`AccountId`, `AccountId`, `Balance`) +- **summary**: Transfer happened `from` -> `to` with given `value` as part of a `message-call` or `create`. + +___ +Back To Top + +### council + +▸ **BadReaperSlashed**(`AccountId`) +- **summary**: slashed reaper + +▸ **TallyFinalised**(`Vec`, `Vec`) +- **summary**: A tally (for approval votes of council seat(s)) has ended (with one or more new members). + +▸ **TallyStarted**(`u32`) +- **summary**: A tally (for approval votes of council seat(s)) has started. + +▸ **VoterReaped**(`AccountId`, `AccountId`) +- **summary**: reaped voter, reaper + +___ +Back To Top + +### councilMotions + +▸ **Approved**(`Hash`) +- **summary**: A motion was approved by the required threshold. + +▸ **Disapproved**(`Hash`) +- **summary**: A motion was not approved by the required threshold. + +▸ **Executed**(`Hash`, `bool`) +- **summary**: A motion was executed; `bool` is true if returned without error. + +▸ **Proposed**(`AccountId`, `ProposalIndex`, `Hash`, `u32`) +- **summary**: A motion (given hash) has been proposed (by given account) with a threshold (given u32). + +▸ **Voted**(`AccountId`, `Hash`, `bool`, `u32`, `u32`) +- **summary**: A motion (given hash) has been voted on by given account, leaving a tally (yes votes and no votes given as u32s respectively). + +___ +Back To Top + +### councilVoting + +▸ **TallyCancelation**(`Hash`, `u32`, `u32`, `u32`) +- **summary**: A voting tally has happened for a referendum cancellation vote. Last three are yes, no, abstain counts. + +▸ **TallyReferendum**(`Hash`, `u32`, `u32`, `u32`) +- **summary**: A voting tally has happened for a referendum vote. Last three are yes, no, abstain counts. + +___ +Back To Top + +### democracy + +▸ **Cancelled**(`ReferendumIndex`) + +▸ **Executed**(`ReferendumIndex`, `bool`) + +▸ **NotPassed**(`ReferendumIndex`) + +▸ **Passed**(`ReferendumIndex`) + +▸ **Started**(`ReferendumIndex`, `VoteThreshold`) + +▸ **Tabled**(`PropIndex`, `Balance`, `Vec`) + +___ +Back To Top + +### session + +▸ **NewSession**(`BlockNumber`) +- **summary**: New session has happened. Note that the argument is the session index, not the block number as the type might suggest. + +___ +Back To Top + +### staking + +▸ **OfflineSlash**(`AccountId`, `Balance`) +- **summary**: One validator (and their nominators) has been slashed by the given amount. + +▸ **OfflineWarning**(`AccountId`, `u32`) +- **summary**: One validator (and their nominators) has been given a offline-warning (they're still within their grace). The accrued number of slashes is recorded, too. + +▸ **Reward**(`Balance`) +- **summary**: All validators have been rewarded by the given balance. + +___ +Back To Top + +### system + +▸ **ExtrinsicFailed**() +- **summary**: An extrinsic failed. + +▸ **ExtrinsicSuccess**() +- **summary**: An extrinsic completed successfully. + +___ +Back To Top + +### treasury + +▸ **Awarded**(`ProposalIndex`, `Balance`, `AccountId`) +- **summary**: Some funds have been allocated. + +▸ **Burnt**(`Balance`) +- **summary**: Some of our funds have been burnt. + +▸ **Proposed**(`ProposalIndex`) +- **summary**: New proposal. + +▸ **Rollover**(`Balance`) +- **summary**: Spending has finished; this is the amount that rolls over until next spend. + +▸ **Spending**(`Balance`) +- **summary**: We have ended a spend period and will now allocate funds. diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index d90af1bf54..83b35352d4 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -7,10 +7,12 @@ - [HttpProvider](rpc-provider/classes/_http_index_.httpprovider.md) - [WsProvider](rpc-provider/classes/_ws_index_.wsprovider.md) -## Node Interfaces +## Interfaces + - [RPC](METHODS_RPC.md) -- [Storage chain state (runtime)](METHODS_STORAGE.md) +- [Chain state (runtime)](METHODS_STORAGE.md) - [Extrinsics (runtime)](METHODS_EXTRINSICS.md) +- [Events (runtime)](METHODS_EVENTS.md) ## Examples diff --git a/packages/types/src/scripts/MetadataMd.ts b/packages/types/src/scripts/MetadataMd.ts index 8f08e15b13..0fe51f6475 100644 --- a/packages/types/src/scripts/MetadataMd.ts +++ b/packages/types/src/scripts/MetadataMd.ts @@ -13,34 +13,31 @@ import Method from '../Method'; const ANCHOR_TOP = ``; const LINK_BACK_TO_TOP = `Back To Top`; -// some intro text goes in here -const DESC_RPC = '\n\n_The following sections contain RPC methods that are Remote Calls available by default and allow you to interact with the actual node, query, and submit. The RPCs are provided by Substrate itself. The RPCs are never exposed by the runtime._'; const DESC_EXTRINSICS = '\n\n_The following sections contain Extrinsics methods are part of the default Substrate runtime. Since an Extrinsic is a holder of an object that is just an array of bytes to be included, it does not have a return._\n'; +const DESC_EVENTS = '\n\nEvents are emitted for certain operations on the runtime. The following sections describe the events that are part of the default Substrate runtime.\n'; +const DESC_RPC = '\n\n_The following sections contain RPC methods that are Remote Calls available by default and allow you to interact with the actual node, query, and submit. The RPCs are provided by Substrate itself. The RPCs are never exposed by the runtime._'; const DESC_STORAGE = '\n\n_The following sections contain Storage methods are part of the default Substrate runtime._\n'; function sectionLink (sectionName: string) { return `- **[${stringCamelCase(sectionName)}](#${stringCamelCase(sectionName)})**\n\n`; } -function generateSectionLinks (sectionName: string, metadata?: any) { +function generateSectionLinks (sectionName: string, metadata: Metadata) { switch (sectionName) { - case 'rpc': { - return Object.keys(interfaces) - .sort().map((sectionName) => sectionLink(sectionName)).join(''); - } - case 'extrinsics': - case 'storage': { - return metadata.modules.raw - .sort().map((runtimeModuleMetadata: any) => { - const sectionName = runtimeModuleMetadata.raw.prefix.raw; + case 'events': + return metadata.events.map((i) => i).sort().map((data: any) => + sectionLink(data.name.toString()) + ).join(''); - return sectionLink(sectionName); - }).join(''); - } - default: { + case 'extrinsics': + case 'storage': + return metadata.modules.map((i) => i).sort().map((runtimeModuleMetadata: any) => + sectionLink(runtimeModuleMetadata.prefix.toString()) + ).join(''); + + default: console.error('Unknown section name provided to generate anchors'); break; - } } } @@ -51,7 +48,9 @@ function generateSectionHeader (md: string, sectionName: string) { function addRpc () { const renderHeading = `## ${ANCHOR_TOP}JSON-RPC${DESC_RPC}\n`; const orderedSections = Object.keys(interfaces).sort(); - const renderAnchors = generateSectionLinks('rpc'); + const renderAnchors = Object.keys(interfaces).sort().map((sectionName) => + sectionLink(sectionName) + ).join(''); return orderedSections.reduce((md, sectionName) => { const section = interfaces[sectionName]; @@ -79,16 +78,43 @@ function addRpc () { function sortExtrinsicsSectionMethods () { return function (a: any, b: any) { // ignore upper and lowercase - const nameA = a.raw.name.raw.toUpperCase(); - const nameB = b.raw.name.raw.toUpperCase(); + const nameA = a.name.toString().toUpperCase(); + const nameB = b.name.toString().toUpperCase(); return nameA.localeCompare(nameB); }; } -function addExtrinsics (metadata: any) { +function addEvents (metadata: Metadata) { + const renderHeading = `## ${ANCHOR_TOP}Events${DESC_EVENTS}`; + const orderedSections = metadata.events.map((i) => i).sort(); + const renderAnchors = generateSectionLinks('events', metadata); + + return orderedSections.reduce((md: string, meta: any) => { + if (!meta.events || !meta.events.length) { + return md; + } + + const sectionName = stringCamelCase(meta.name.toString()); + const renderSection = generateSectionHeader(md, sectionName); + + const orderedMethods = meta.events.map((i: any) => i).sort(sortExtrinsicsSectionMethods()); + + return orderedMethods.reduce((md: string, func: any) => { + const methodName = func.name.toString(); + const args = func.arguments.map((type: any) => '`' + type + '`').join(', '); + const doc = func.documentation.reduce((md: string, doc: string) => `${md} ${doc}`, ''); + const renderSignature = `${md}\n▸ **${methodName}**(${args})`; + const renderSummary = `${doc ? `\n- **summary**: ${doc}\n` : '\n'}`; + + return renderSignature + renderSummary; + }, renderSection); + }, renderHeading + renderAnchors); +} + +function addExtrinsics (metadata: Metadata) { const renderHeading = `## ${ANCHOR_TOP}Extrinsics${DESC_EXTRINSICS}`; - const orderedSections = metadata.modules.raw.sort(); + const orderedSections = metadata.modules.map((i) => i).sort(); const renderAnchors = generateSectionLinks('extrinsics', metadata); return orderedSections.reduce((md: string, meta: any) => { @@ -98,9 +124,7 @@ function addExtrinsics (metadata: any) { const sectionName = stringCamelCase(meta.prefix.toString()); const renderSection = generateSectionHeader(md, sectionName); - - // Sort methods by name. Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort - const orderedMethods = meta.module.call.functions.raw.sort(sortExtrinsicsSectionMethods()); + const orderedMethods = meta.module.call.functions.map((i: any) => i).sort(sortExtrinsicsSectionMethods()); return orderedMethods.reduce((md: string, func: any) => { const methodName = stringCamelCase(func.name.toString()); @@ -114,9 +138,9 @@ function addExtrinsics (metadata: any) { }, renderHeading + renderAnchors); } -function addStorage (metadata: any) { +function addStorage (metadata: Metadata) { const renderHeading = `## ${ANCHOR_TOP}Storage${DESC_STORAGE}`; - const orderedSections = metadata.modules.raw.sort(); + const orderedSections = metadata.modules.map((i: any) => i).sort(); const renderAnchors = generateSectionLinks('storage', metadata); return orderedSections.reduce((md: string, moduleMetadata: any) => { @@ -126,7 +150,7 @@ function addStorage (metadata: any) { const sectionName = stringLowerFirst(moduleMetadata.storage.prefix.toString()); const renderSection = generateSectionHeader(md, sectionName); - const orderedMethods = moduleMetadata.storage.functions.raw.sort(); + const orderedMethods = moduleMetadata.storage.functions.map((i: any) => i).sort(); return orderedMethods.reduce((md: string, func: any) => { const methodName = stringLowerFirst(func.name.toString()); @@ -140,16 +164,6 @@ function addStorage (metadata: any) { }, renderHeading + renderAnchors); } -function metadataStorageMethodsAsText () { - const metadata = new Metadata(rpcdata); - return addStorage(metadata); -} - -function metadataExtrinsicsMethodsAsText () { - const metadata = new Metadata(rpcdata); - return addExtrinsics(metadata); -} - function writeToRpcMd () { const options = { flags: 'w', encoding: 'utf8' }; const writeStream = fs.createWriteStream('docs/METHODS_RPC.md', options); @@ -163,7 +177,7 @@ function writeToRpcMd () { writeStream.end(); } -function writeToStorageMd () { +function writeToStorageMd (metadata: Metadata) { const optionsRead = { flags: 'r', encoding: 'utf8' }; fs.readFile('packages/types/src/scripts/METHODS_STORAGE_SUBSTRATE.md', optionsRead, function read (err: Error, data: string) { if (err) { @@ -174,31 +188,47 @@ function writeToStorageMd () { const options = { flags: 'w', encoding: 'utf8' }; const writeStream = fs.createWriteStream('docs/METHODS_STORAGE.md', options); - writeStream.write(metadataStorageMethodsAsText()); + writeStream.write(addStorage(metadata)); writeStream.write(data); // finish emitting event when all data flushed from stream writeStream.on('finish', () => { - console.log('wrote all storage method metadata to Gitbook Markdown file'); + console.log('wrote all storage metadata to Gitbook Markdown file'); }); writeStream.end(); }); } -function writeToExtrinsicsMd () { +function writeToExtrinsicsMd (metadata: Metadata) { const options = { flags: 'w', encoding: 'utf8' }; const writeStream = fs.createWriteStream('docs/METHODS_EXTRINSICS.md', options); - writeStream.write(metadataExtrinsicsMethodsAsText()); + writeStream.write(addExtrinsics(metadata)); writeStream.on('finish', () => { - console.log('wrote all extrinsics method metadata to Gitbook Markdown file'); + console.log('wrote all extrinsics metadata to Gitbook Markdown file'); }); writeStream.end(); } +function writeToEventsMd (metadata: Metadata) { + const options = { flags: 'w', encoding: 'utf8' }; + const writeStream = fs.createWriteStream('docs/METHODS_EVENTS.md', options); + + writeStream.write(addEvents(metadata)); + + writeStream.on('finish', () => { + console.log('wrote all event metadata to Gitbook Markdown file'); + }); + + writeStream.end(); +} + +const metadata = new Metadata(rpcdata); + writeToRpcMd(); -writeToStorageMd(); -writeToExtrinsicsMd(); +writeToStorageMd(metadata); +writeToExtrinsicsMd(metadata); +writeToEventsMd(metadata);