Skip to content
This repository was archived by the owner on Aug 27, 2024. It is now read-only.

Commit 6fe8f92

Browse files
Update metadata.md (#691)
* Update metadata.md update to v2.0.0 * updated json value for Substrate v2 Co-authored-by: Jimmy Chu <[email protected]>
1 parent 1466bcb commit 6fe8f92

File tree

1 file changed

+133
-134
lines changed

1 file changed

+133
-134
lines changed

docs/knowledgebase/runtime/metadata.md

Lines changed: 133 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Substrate node, as well as language-agnostic HTTP and WebSocket APIs.
2727

2828
The easiest way to get the metadata is by querying the automatically-generated JSON-RPC function
2929
`state_getMetadata`. This will return a vector of SCALE-encoded bytes. You can decode this using the
30-
[`frame-metadata`](https://substrate.dev/rustdocs/v2.0.0-rc6/frame_metadata/index.html) and
31-
[`parity-scale-codec`](https://substrate.dev/rustdocs/v2.0.0-rc6/parity_scale_codec/index.html) libraries.
30+
[`frame-metadata`](https://substrate.dev/rustdocs/v2.0.0/frame_metadata/index.html) and
31+
[`parity-scale-codec`](https://substrate.dev/rustdocs/v2.0.0/parity_scale_codec/index.html) libraries.
3232

3333
Some helpful libraries like [`substrate-subxt`](https://github.com/paritytech/substrate-subxt) fetch
3434
the metadata and decode them for you. Once decoded, the structure may be serialized into JSON with
@@ -54,9 +54,9 @@ console.log("Metadata: " + metadata.raw);
5454

5555
### HTTP & WebSocket APIs
5656

57-
Substrate nodes expose [a JSON-RPC API](https://substrate.dev/rustdocs/v2.0.0-rc6/sc_rpc/index.html) that you can
57+
Substrate nodes expose [a JSON-RPC API](https://substrate.dev/rustdocs/v2.0.0/sc_rpc/index.html) that you can
5858
access by way of **HTTP** or **WebSocket** requests. The message to
59-
[request metadata](https://substrate.dev/rustdocs/v2.0.0-rc6/sc_rpc/state/struct.StateClient.html#method.metadata)
59+
[request metadata](https://substrate.dev/rustdocs/v2.0.0/sc_rpc/state/struct.StateClient.html#method.metadata)
6060
from a node looks like this:
6161

6262
```json
@@ -110,52 +110,51 @@ The hex blob that is returned by the JSON-RPCs `state_getMetadata` method starts
110110
magic number, `0x6d657461`, which represents "meta" in plain text. The next piece of data (`0x0b` in
111111
the example above) represents the metadata version; decoding the hexadecimal value `0x0b` yields the
112112
decimal value 11, which is
113-
[the version of the Substrate metadata format](https://substrate.dev/rustdocs/v2.0.0-rc6/frame_metadata/enum.RuntimeMetadata.html)
113+
[the version of the Substrate metadata format](https://substrate.dev/rustdocs/v2.0.0/frame_metadata/enum.RuntimeMetadata.html)
114114
that the result encodes. After the metadata version, the next piece of information encoded in the
115115
result field is the number of pallets that inform the blockchain's runtime; in the example above,
116116
the hexadecimal value `0x7c` represents the decimal number 31, which is SCALE-encoded by taking its
117117
binary representation (`11111` or `0x1F` in hex), shifting it two bits to the left (`1111100`) and
118118
encoding that as hex.
119119

120120
The remaining blob encodes
121-
[the metadata of each pallet](https://substrate.dev/rustdocs/v2.0.0-rc6/frame_metadata/struct.ModuleMetadata.html),
121+
[the metadata of each pallet](https://substrate.dev/rustdocs/v2.0.0/frame_metadata/struct.ModuleMetadata.html),
122122
which will be reviewed below as well as some
123-
[extrinsic metadata](https://substrate.dev/rustdocs/v2.0.0-rc6/frame_metadata/struct.ExtrinsicMetadata.html), which
123+
[extrinsic metadata](https://substrate.dev/rustdocs/v2.0.0/frame_metadata/struct.ExtrinsicMetadata.html), which
124124
is mostly out of the scope of this document.
125125

126126
## Decoded Metadata Format
127127

128128
Here is a condensed version of decoded metadata:
129129

130130
```json
131-
[
132-
1635018093,
133-
{
131+
{
132+
"magicNumber": 1635018093,
133+
"metadata": {
134134
"V11": {
135-
"modules": [
136-
{
137-
...
138-
},
139-
{
140-
...
141-
}
142-
],
143-
"extrinsic" {
144-
"version": 4,
145-
"signed_extensions": [
146-
"RestrictFunctionality",
147-
"CheckVersion",
148-
"CheckGenesis",
149-
"CheckEra",
150-
"CheckNonce",
151-
"CheckWeight",
152-
"ChargeTransactionPayment",
153-
"LimitParathreadCommits"
154-
]
155-
}
156-
}
135+
"modules": [
136+
{
137+
// ...
138+
},
139+
{
140+
// ...
141+
}
142+
],
143+
"extrinsic": {
144+
"version": 4,
145+
"signedExtensions": [
146+
"CheckSpecVersion",
147+
"CheckTxVersion",
148+
"CheckGenesis",
149+
"CheckMortality",
150+
"CheckNonce",
151+
"CheckWeight",
152+
"ChargeTransactionPayment"
153+
]
154+
}
155+
}
157156
}
158-
]
157+
}
159158
```
160159

161160
As described above, the integer `1635018093` is a "magic number" that represents "meta" in plain
@@ -170,22 +169,23 @@ Here is a condensed example of a single element in the `modules` array:
170169

171170
```json
172171
{
173-
"name": "System",
174-
"storage": {
175-
..
176-
},
177-
"calls": [
178-
..
179-
],
180-
"event": [
181-
..
182-
],
183-
"constants": [
184-
..
185-
],
186-
"errors": [
187-
..
188-
]
172+
"name": "System",
173+
"storage": {
174+
// ..
175+
},
176+
"calls": [
177+
// ..
178+
],
179+
"events": [
180+
// ..
181+
],
182+
"constants": [
183+
// ..
184+
],
185+
"errors": [
186+
// ..
187+
],
188+
"index": 0
189189
}
190190
```
191191

@@ -202,36 +202,42 @@ about the module's storage:
202202

203203
```json
204204
{
205-
"name": "System",
206-
"storage": {
207-
"prefix": "System",
208-
"entries": [
209-
{
210-
"name": "Account",
211-
"modifier": "Default",
212-
"ty": {
213-
"Map": {
214-
"hasher": "Blake2_128Concat",
215-
"key": "T::AccountId",
216-
"value": "AccountInfo<T::Index, T::AccountData>",
217-
"unused": false
218-
}
219-
},
220-
"default": [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
221-
"documentation": [
222-
" The full account information for a particular account ID."
223-
]
224-
},
225-
{
205+
"name": "System",
206+
"storage": {
207+
"prefix": "System",
208+
"items": [
209+
{
210+
"name": "Account",
211+
"modifier": "Default",
212+
"type": {
213+
"Map": {
214+
"hasher": "Blake2_128Concat",
215+
"key": "AccountId",
216+
"value": "AccountInfo",
217+
"linked": false
218+
}
219+
},
220+
"fallback": "0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
221+
"documentation": [
222+
" The full account information for a particular account ID."
223+
]
224+
},
225+
{
226226
"name": "ExtrinsicCount",
227-
..
228-
},
229-
{
230-
"name": "AlLExtrinsicsWeight",
231-
..
232-
}
233-
]
234-
},
227+
// ..
228+
},
229+
{
230+
"name": "AllExtrinsicsLen",
231+
// ..
232+
}
233+
]
234+
},
235+
"calls": [/*...*/],
236+
"events": [/*...*/],
237+
"constants": [/*...*/],
238+
"errors": [/*...*/],
239+
"index": 0
240+
}
235241
```
236242

237243
Every storage item that is defined in a pallet will have a corresponding metadata entry. For
@@ -248,7 +254,7 @@ decl_storage! {
248254
```
249255

250256
Storage metadata provides blockchain clients with the information that is required to query
251-
[the JSON-RPC's storage function](https://substrate.dev/rustdocs/v2.0.0-rc6/sc_rpc/state/struct.StateClient.html#method.storage)
257+
[the JSON-RPC's storage function](https://substrate.dev/rustdocs/v2.0.0/sc_rpc/state/struct.StateClient.html#method.storage)
252258
to get information for a specific storage item.
253259

254260
##### Calls
@@ -293,11 +299,11 @@ This materializes in the metadata as follows:
293299
"calls": [
294300
{
295301
"name": "set",
296-
"arguments": [
297-
{
298-
"name": "now",
299-
"ty": "Compact<T::Moment>"
300-
}
302+
"args": [
303+
{
304+
"name": "now",
305+
"ty": "Compact<T::Moment>"
306+
}
301307
],
302308
"documentation": [
303309
" Set the current time.",
@@ -318,7 +324,7 @@ This materializes in the metadata as follows:
318324

319325
This metadata snippet is generated from this declaration in `frame-system`:
320326

321-
```Rust
327+
```rust
322328
decl_event!(
323329
/// Event for the System module
324330
pub enum Event<T> where AccountId = <T as Trait>::AccountId {
@@ -335,25 +341,25 @@ Substrate's metadata would describe these events as follows:
335341

336342
```json
337343
"event": [
338-
{
339-
"name": "ExtrinsicSuccess",
340-
"arguments": [
341-
"DispatchInfo"
342-
],
343-
"documentation": [
344-
" An extrinsic completed successfully."
345-
]
346-
},
347-
{
348-
"name": "ExtrinsicFailed",
349-
"arguments": [
350-
"DispatchError",
351-
"DispatchInfo"
352-
],
353-
"documentation": [
354-
" An extrinsic failed."
355-
]
356-
},
344+
{
345+
"name": "ExtrinsicSuccess",
346+
"arguments": [
347+
"DispatchInfo"
348+
],
349+
"documentation": [
350+
" An extrinsic completed successfully."
351+
]
352+
},
353+
{
354+
"name": "ExtrinsicFailed",
355+
"arguments": [
356+
"DispatchError",
357+
"DispatchInfo"
358+
],
359+
"documentation": [
360+
" An extrinsic failed."
361+
]
362+
},
357363
],
358364
```
359365

@@ -364,7 +370,7 @@ The metadata will include any module constants. From `pallet-babe`:
364370
```rust
365371
decl_module! {
366372
/// The BABE Pallet
367-
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
373+
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
368374
/// The number of **slots** that an epoch takes. We couple sessions to
369375
/// epochs, i.e. we start a new session once the new epoch begins.
370376
const EpochDuration: u64 = T::EpochDuration::get();
@@ -378,25 +384,17 @@ The metadata for this constant looks like this:
378384

379385
```json
380386
"constants": [
381-
{
382-
"name": "EpochDuration",
383-
"ty": "u64",
384-
"value": [
385-
88,
386-
2,
387-
0,
388-
0,
389-
0,
390-
0,
391-
0,
392-
0
393-
],
394-
"documentation": [
395-
" The number of **slots** that an epoch takes. We couple sessions to",
396-
" epochs, i.e. we start a new session once the new epoch begins."
397-
]
398-
},
399-
],
387+
{
388+
"name": "EpochDuration",
389+
"type": "u64",
390+
"value": "0x6009000000000000",
391+
"documentation": [
392+
" The number of **slots** that an epoch takes. We couple sessions to",
393+
" epochs, i.e. we start a new session once the new epoch begins."
394+
]
395+
},
396+
// ...
397+
]
400398
```
401399

402400
The metadata also includes constants defined in the runtime's `lib.rs`. For example, from Kusama:
@@ -414,7 +412,7 @@ Where `EPOCH_DURATION_IN_BLOCKS` is a constant defined in `runtime/src/constants
414412
Metadata will pull all the possible runtime errors from `decl_error!`. For example, from
415413
`frame-system`:
416414

417-
```Rust
415+
```rust
418416
decl_error! {
419417
/// Error for the system module
420418
pub enum Error for Module<T: Trait> {
@@ -430,19 +428,20 @@ This will expose the following metadata:
430428

431429
```json
432430
"errors": [
433-
{
434-
"name": "InvalidSpecName",
435-
"documentation": [
436-
" The name of specification does not match between the current runtime",
437-
" and the new runtime."
438-
]
439-
},
431+
{
432+
"name": "InvalidSpecName",
433+
"documentation": [
434+
" The name of specification does not match between the current runtime",
435+
" and the new runtime."
436+
]
437+
},
438+
// ...
440439
]
441440
```
442441

443442
These are errors that could occur during the submission or execution of an extrinsic. In this case,
444443
the FRAME System pallet is declaring that it may raise the
445-
[the `InvalidSpecName` error](https://substrate.dev/rustdocs/v2.0.0-rc6/frame_system/enum.Error.html#variant.InvalidSpecName).
444+
[the `InvalidSpecName` error](https://substrate.dev/rustdocs/v2.0.0/frame_system/enum.Error.html#variant.InvalidSpecName).
446445

447446
## Next Steps
448447

@@ -460,4 +459,4 @@ the FRAME System pallet is declaring that it may raise the
460459

461460
### References
462461

463-
- [Metadata](https://substrate.dev/rustdocs/v2.0.0-rc6/frame_metadata/index.html)
462+
- [Metadata](https://substrate.dev/rustdocs/v2.0.0/frame_metadata/index.html)

0 commit comments

Comments
 (0)