From 180738306b045bd665765679bbb9d4297edcf632 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sat, 29 Feb 2020 09:38:11 +0100 Subject: [PATCH] Remove deprecated extrinsic extend docs (confusing) (#1976) * Remove extrinsic extend * Example for Address overrides --- docs/.vuepress/config.js | 1 - ...sic.extend.md => .OLD.extrinsic.extend.md} | 5 +-- docs/start/types.create.md | 5 ++- docs/start/types.extend.md | 42 ++++++++++++++++++- 4 files changed, 46 insertions(+), 7 deletions(-) rename docs/start/{extrinsic.extend.md => .OLD.extrinsic.extend.md} (95%) diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js index 278a714782..0a56612eea 100644 --- a/docs/.vuepress/config.js +++ b/docs/.vuepress/config.js @@ -44,7 +44,6 @@ module.exports = { ['start/types.basics.md', 'Type basics'], ['start/types.extend.md', 'Extending types'], ['start/types.create.md', 'Creating types'], - ['start/extrinsic.extend.md', 'Extending extrinsics'], ['start/typescript.md', 'TypeScript interfaces'], ['start/typescript.user.md', 'TypeScript user generated'], ['start/FAQ.md', 'FAQ'] diff --git a/docs/start/extrinsic.extend.md b/docs/start/.OLD.extrinsic.extend.md similarity index 95% rename from docs/start/extrinsic.extend.md rename to docs/start/.OLD.extrinsic.extend.md index cbb6026fe0..c4f09d911b 100644 --- a/docs/start/extrinsic.extend.md +++ b/docs/start/.OLD.extrinsic.extend.md @@ -1,3 +1,5 @@ +**NOTE** Removed - in metadata v11 we get the extensions from the chain, we need to be able to specify these. These docs are now outdated. + # Extending extrinsics On some chains, the need may arise to customize the extrinsic format. In this section we will explain what extrinsics and their payloads contain, explain how signed extensions work and provide a walk through of an advanced use-case where extrinsics are customized for a specific Substrate chain. @@ -153,6 +155,3 @@ In the above example, should these updates only be made on the node side, withou (These extensions are not exposed via metadata at all, and would be quite difficult to do as well - since each of these have specific logic as well as data types assigned.) -## Using with TypeScript - -The API is built with TypeScript (as are all projects in the [polkadot-js organization](https://github.com/polkadot-js/)) and as such allows developers using TS to have access to all the type interfaces defined on the chain, as well as having access to typings on interacting with the `api.*` namespaces. In the next section we will provide an overview of [what is available in terms of types and TypeScript](typescript.md). diff --git a/docs/start/types.create.md b/docs/start/types.create.md index a4f626a394..acbf4ac01a 100644 --- a/docs/start/types.create.md +++ b/docs/start/types.create.md @@ -49,6 +49,7 @@ api.registry.createType('Balance', 123n); createType(api.registry, 'Balance', '123'); ``` -## Extending extrinsics +## Using with TypeScript + +The API is built with TypeScript (as are all projects in the [polkadot-js organization](https://github.com/polkadot-js/)) and as such allows developers using TS to have access to all the type interfaces defined on the chain, as well as having access to typings on interacting with the `api.*` namespaces. In the next section we will provide an overview of [what is available in terms of types and TypeScript](typescript.md). -On customized chains, it is possible to [extend the format of the extrinsics and extrinsic payload for the chain](extrinsic.extend.md), next up we will look at this advanced use-case. diff --git a/docs/start/types.extend.md b/docs/start/types.extend.md index 5873594eb7..be5eeec705 100644 --- a/docs/start/types.extend.md +++ b/docs/start/types.extend.md @@ -10,7 +10,7 @@ There is a the [recommendation](install.md#betas) to use a `@polkadot/api@beta` ## Extension -As a blockchain toolkit, Substrate makes it easy to add your own modules and types. In most non-trivial implementations, this would mean that developers are adding specific types for their implementation as well. The API will get to know the names of these types via the metadata, however it won't understand what they are, which means it cannot encode or decode them. +As a blockchain toolkit, Substrate makes it easy to add your own modules and types. In most non-trivial implementations, this would mean that developers are adding specific types for their implementation as well. The API will get to know the names of these types via the metadata, however it won't understand what they are, which means it cannot encode or decode them. Additionally, when a type is mismatched between the node and the API, the decoding can fail, yielding issues such as [Could not convert errors](FAQ.md#the-node-returns-a-could-not-convert-error-on-send) when submitting transactions. To close this gap, the API allows for the injection of types, i.e. you can explicitly define (or override) types for the node/chain you are connecting to. In the simplest example, assuming you have a chain where your `Balance` type is a `u64` (as opposed to the default `u128`), you need to let the API know - @@ -161,6 +161,46 @@ const api = await ApiPromise.create({ `Balance` would be defined as an `u128` at the end. Effectively based on the flow it is first registered as a `u32`, then overridden as a `u64` and finally overridden once more as a `u128` by the chain types. +## Impact on extrinsics + +When configuring your chain, be cognizant of the types you are using, and always ensure that any changes are replicated back to the API. In an earlier example we configures `Balance` as `u64` - the same changes needs to be applied on the API when there are mismatches to Substrate master, otherwise failures will occur. + +This also applies to any other chain-specific configured types, for instance you can customize `Lookup` and `Address` on your chain. A real example of this is the Substrate master node vs the Substrate master node-template - + +```rust +/// The lookup mechanism to get account ID from whatever is passed in dispatchers. +type Lookup = Indices; +... +/// The address format for describing accounts. +pub type Address = ::Source; +``` + +vs what is defined on the node-template - + +```rust +/// The lookup mechanism to get account ID from whatever is passed in dispatchers. +type Lookup = IdentityLookup; +... +/// The address format for describing accounts. +pub type Address = AccountId; +``` + +Here the template was customized from the node defaults and the API needs to know how to map these types otherwise transactions will fail. As such the correct types that needs to be added here would be - + +```js +const api = await ApiPromise.create({ + ..., + types: { + // mapping the actual specified address format + Address: 'AccountId', + // mapping the lookup + LookupSource: 'AccountId' + } +}); +``` + +Always look at customization and understand the impacts, replicating these changes between the node and the API. For the above the `Address` type is used in the construction of the `UncheckedExtrinsic` type, while the lookup type is applicable on transactions such as `balances.transfer(to: LookupSource, value: Balance)` + ## Type creation While the API always converts all the inputs into the underlying type required by the operation, in some cases you may want to [cret an instance of a type](types.create.md) yourself.