CLI Tool Explore Command: Runtime APIs, Events, Colorized outputs, scale-typegen integration (#1290)

* restructure cli commands

* config: Add `SkipCheckIfFeeless` signed extension (#1264)

* config: Add `SkipCheckIfFeeless` signed extension

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* config: Add extra extension to the default params

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* examples: Adjust signed extension example

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* config: Extend SkipCheckIfFeeless with inner signed extension

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* config: Configure SkipCheck with inner signed extension params

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* config: Implement Deafult for SkipCheckIfFeelessParams with Option

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* examples: Fix example with proper extension

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* config: Extend <T as Config>::AssetId with EncodeAsType and Clone

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* config: Add SkipCheck with AssetTx

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* config: Encode as type from metadata the inner signed extensions

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Adjust examples

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* blocks: Use `SkipCheckIfFeeless` for decoding the tip of extensions

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* config: Decode `SkipCheckIfFeeless` with `Self`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests: Adjust testing

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* config: Descriptive errors for building `SkipCheckIfFeeless`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* config: Add docs for extra error types

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Add extra derives to signed extensions

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* config: Use `Default::default` to simplify type init

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Replace removed lint (#1270)

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Add support for multi-chain usecase (#1238)

* lightclient: Make `smoldot::chainID` part of the RPC requests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Make `BackgroundTask` generic over `PlatformRef` and chain

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Construct from raw smoldot and target different chains

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Update cargo lock for wasm tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Reuse `new_from_client` method and removed unused imports

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Reexport smoldot client and RPC objects used in pub
interface

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Adjust `new_from_client` interface

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Extend background to poll over multiple RPC objects

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Build light client from raw and target different chains

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* artifacts: Add demo chain specs

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* artifacts: Move artifacts to dedicated folder

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Use SelectAll to drive all streams

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Fetch initial data from the target chain

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Reexport other smoldot objects

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Target chain with potentially different config

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt/rpc: Log chainID for debugging

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt/examples: Add smoldot client with parachain example

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Propagate chain ID together with rpc responses object

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Multiplex responses by request ID and chain ID

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Add raw light client builder

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Add cargo feature flag for parachains example

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Derive default for internal structure

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Guard reexports by std feature flag

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update subxt/src/client/light_client/mod.rs

Co-authored-by: James Wilson <james@jsdw.me>

* lightclient: Update the builder pattern and chain targetting

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Fix documentation

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Provide more insightful docs wrt native/wasm panics

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* examples: Adjust comment location

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Refactor UniqueChainId into the background task

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update lightclient/src/background.rs

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Update subxt/src/client/light_client/builder.rs

Co-authored-by: James Wilson <james@jsdw.me>

* lightclient: Update docs wrt panics

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Update docs wrt to smoldot instance -> client

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Use IntoIter instead of Iterator

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Adjsut docs wrt [`Self::new_from_client`]

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Remove RawRpc from LightClient in favor of chainID

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Reexport everything under smoldot module

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* artifacts: Use stateRootHash instead of genesis.raw

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: James Wilson <james@jsdw.me>
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Bump futures from 0.3.28 to 0.3.29 (#1272)

Bumps [futures](https://github.com/rust-lang/futures-rs) from 0.3.28 to 0.3.29.
- [Release notes](https://github.com/rust-lang/futures-rs/releases)
- [Changelog](https://github.com/rust-lang/futures-rs/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rust-lang/futures-rs/compare/0.3.28...0.3.29)

---
updated-dependencies:
- dependency-name: futures
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump zeroize from 1.6.0 to 1.7.0 (#1274)

Bumps [zeroize](https://github.com/RustCrypto/utils) from 1.6.0 to 1.7.0.
- [Commits](https://github.com/RustCrypto/utils/commits)

---
updated-dependencies:
- dependency-name: zeroize
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump tracing-subscriber from 0.3.17 to 0.3.18 (#1275)

Bumps [tracing-subscriber](https://github.com/tokio-rs/tracing) from 0.3.17 to 0.3.18.
- [Release notes](https://github.com/tokio-rs/tracing/releases)
- [Commits](https://github.com/tokio-rs/tracing/compare/tracing-subscriber-0.3.17...tracing-subscriber-0.3.18)

---
updated-dependencies:
- dependency-name: tracing-subscriber
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump tracing-subscriber from 0.3.17 to 0.3.18 (#1275)

Bumps [tracing-subscriber](https://github.com/tokio-rs/tracing) from 0.3.17 to 0.3.18.
- [Release notes](https://github.com/tokio-rs/tracing/releases)
- [Commits](https://github.com/tokio-rs/tracing/compare/tracing-subscriber-0.3.17...tracing-subscriber-0.3.18)

---
updated-dependencies:
- dependency-name: tracing-subscriber
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump getrandom from 0.2.10 to 0.2.11 (#1273)

Bumps [getrandom](https://github.com/rust-random/getrandom) from 0.2.10 to 0.2.11.
- [Changelog](https://github.com/rust-random/getrandom/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rust-random/getrandom/compare/v0.2.10...v0.2.11)

---
updated-dependencies:
- dependency-name: getrandom
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* impl RpcClientT for Arc<T> and Box<T> (#1277)

* impl RpcClientT for Arc<WsClient>

* fix grumbles: impl for Box<T> and Arc<T>

* grumbles: move RpcClientT impls

* first iteration of using scale_typegen

* introduce indoc for formatting

* calls, constants and home are cleaner now

* added event subcommand

* show runtime apis working

* add better code formatting

* fix style

* adjust tests, use owo_colorize to not add extra dependency

* fmt

* adjust docs

* move scale-typegen-description dependency to workspace

* improve `substrate-compat` (#1265)

* improve `substrate-compat`

* From => Into

---------

Co-authored-by: James Wilson <james@jsdw.me>

* Bump proc-macro2 from 1.0.69 to 1.0.70 (#1292)

Bumps [proc-macro2](https://github.com/dtolnay/proc-macro2) from 1.0.69 to 1.0.70.
- [Release notes](https://github.com/dtolnay/proc-macro2/releases)
- [Commits](https://github.com/dtolnay/proc-macro2/compare/1.0.69...1.0.70)

---
updated-dependencies:
- dependency-name: proc-macro2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump serde from 1.0.192 to 1.0.193 (#1291)

Bumps [serde](https://github.com/serde-rs/serde) from 1.0.192 to 1.0.193.
- [Release notes](https://github.com/serde-rs/serde/releases)
- [Commits](https://github.com/serde-rs/serde/compare/v1.0.192...v1.0.193)

---
updated-dependencies:
- dependency-name: serde
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* lightclient: Fix wasm socket closure called after being dropped (#1289)

* lightclient: Close wasm socket while dropping from connecting state

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Construct one time only closures

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* testing: Enable console logs for lightclient WASM testing

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Separate wakes and check connectivity on poll_read

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Close the socket depending on internal state

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Revert "lightclient: Separate wakes and check connectivity on poll_read"

This reverts commit 866094001d4c0b119a80ed681a74b323f74eae1b.

* lightclient: Return pending if socket is opening from poll_read

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Close the socket on `poll_close`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Reset closures on Drop to avoid recursive invokation

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Close the socket if not already closing

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* workflows: Install rustup component for building substrate (#1295)

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Command to fetch chainSpec and optimise its size (#1278)

* cli: Add chainSpec command

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli/chainSpec: Move to dedicated module

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Compute the state root hash

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Remove code substitutes

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* artifacts: Update polkadot.json

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* scripts: Generate the chain spec

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Remove testing artifacts

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Fix clippy

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Apply rustfmt

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Introduce feature flag for smoldot dependency

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* cli: Rename chain-spec to chain-spec-pruning

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* scripts: Update chain-spec command

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* update to new scale-typegen interfaces

* use released version of scale-typegen

* Merge branch 'master' into tadeohepperle/cli-support-runtime-apis

* remove unused debug file

* resolve merge errors

* adjustments

* constants file adjustment

* method renaming

* fix issue with encoding runtime api params

* Add logging to submit_transaction and unstable driver, and ensure unpin evs complete

* panic if None returned from subscription too, also with stats

* change panic to Err just to be on the safe side

* clippy

* make long tests run only after clippy + fmt pass

* megre in light client test change pr

* chore(subxt/src): typo fix (#1370)

* rpcmethods

* followstr

* mod and else

* Weekly Cronjob fetching artifacts and generating polkadot.rs file. (#1352)

* github CI action cronjob

* add commit message

* fix the CI yml files

* binary crate for CI script with substrate-runner

* update the CI script

* correct the artifacts script

* remove bash script

* lightclient(fix): Ensure lightclient chainSpec is at least one block old (#1372)

* testing(fix): Ensure lightclient chainSpec is at least one block old

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Revert "testing(fix): Ensure lightclient chainSpec is at least one block old"

This reverts commit 0eafcb2ca59d1f1cd2cef86b770f5a0401cce59f.

* lightclient(fix): Ensure lightclient chainSpec is at least one block old

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Link smoldot issue

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Use tokio under lightclient feature flag

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Do not sleep on errors to fetch the chainSpec

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* artifacts: Remove test file

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* lightclient: Subscribe to two finalized blocks

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Revert cargo toml

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* ci: Reduce the light client timeout to 15 minutes (#1373)

* ci: Reduce the light client timpeut to 15 seconds

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* ci: Use ubuntu-latest for light-client tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* actually only wait for machete+fmt, clippy can be much slower

* update CI file from Alex PR

* resolve clippy err

* Try a few RPC nodes in case one of them is not working

* fix submit_transaction debug logging of message

* Improve Signed Extension and Block Decoding Examples/Book (#1357)

* asset hub example and book adjustment

* formatting

* recursive derives

* polkadot monitor example and book adjustments

* formatting

* adjust docs and examples, add dynamic example

* james suggestions

* fmt

* chore(subxt/src): typo fix (#1370)

* rpcmethods

* followstr

* mod and else

* Weekly Cronjob fetching artifacts and generating polkadot.rs file. (#1352)

* github CI action cronjob

* add commit message

* fix the CI yml files

* binary crate for CI script with substrate-runner

* update the CI script

* correct the artifacts script

* remove bash script

---------

Co-authored-by: James Wilson <james@jsdw.me>
Co-authored-by: Pan chao <152830401+Pan-chao@users.noreply.github.com>

* fix formatting of returned sections

* make storage use execute flag as well

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
Co-authored-by: James Wilson <james@jsdw.me>
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: yjh <yjh465402634@gmail.com>
Co-authored-by: Pan chao <152830401+Pan-chao@users.noreply.github.com>
This commit is contained in:
Tadeo Hepperle
2024-01-19 17:57:16 +01:00
committed by GitHub
parent 0f48d54209
commit 05ced83f74
15 changed files with 1345 additions and 1135 deletions
-94
View File
@@ -1,94 +0,0 @@
use clap::Args;
use color_eyre::eyre::eyre;
use std::fmt::Write;
use subxt::metadata::{types::PalletMetadata, Metadata};
use crate::utils::type_description::print_type_description;
use crate::utils::{print_first_paragraph_with_indent, with_indent};
#[derive(Debug, Clone, Args)]
pub struct ConstantsSubcommand {
constant: Option<String>,
}
pub fn explore_constants(
command: ConstantsSubcommand,
metadata: &Metadata,
pallet_metadata: PalletMetadata,
output: &mut impl std::io::Write,
) -> color_eyre::Result<()> {
let pallet_name = pallet_metadata.name();
let Some(constant_name) = command.constant else {
let available_constants = print_available_constants(pallet_metadata, pallet_name);
writeln!(output, "Usage:")?;
writeln!(
output,
" subxt explore {pallet_name} constants <CONSTANT>"
)?;
writeln!(
output,
" explore a specific call within this pallet\n\n{available_constants}"
)?;
return Ok(());
};
// if specified constant is wrong, show user the constants to choose from (but this time as an error):
let Some(constant) = pallet_metadata
.constants()
.find(|constant| constant.name().to_lowercase() == constant_name.to_lowercase())
else {
let available_constants = print_available_constants(pallet_metadata, pallet_name);
let mut description = "Usage:".to_string();
writeln!(
description,
" subxt explore {pallet_name} constants <CONSTANT>"
)?;
writeln!(
description,
" explore a specific call within this pallet\n\n{available_constants}"
)?;
let err = eyre!(
"constant \"{constant_name}\" not found in \"{pallet_name}\" pallet!\n\n{description}"
);
return Err(err);
};
// docs
let doc_string = print_first_paragraph_with_indent(constant.docs(), 4);
if !doc_string.is_empty() {
writeln!(output, "Description:\n{doc_string}")?;
}
// shape
let mut type_description = print_type_description(&constant.ty(), metadata.types())?;
type_description = with_indent(type_description, 4);
writeln!(
output,
"\nThe constant has the following shape:\n{type_description}"
)?;
// value
let scale_val =
scale_value::scale::decode_as_type(&mut constant.value(), constant.ty(), metadata.types())?;
writeln!(
output,
"\nThe value of the constant is:\n {}",
scale_value::stringify::to_string(&scale_val)
)?;
Ok(())
}
fn print_available_constants(pallet_metadata: PalletMetadata, pallet_name: &str) -> String {
if pallet_metadata.constants().len() == 0 {
return format!("No <CONSTANT>'s available in the \"{pallet_name}\" pallet.");
}
let mut output = format!("Available <CONSTANT>'s in the \"{pallet_name}\" pallet:");
let mut strings: Vec<_> = pallet_metadata.constants().map(|c| c.name()).collect();
strings.sort();
for constant in strings {
output.push_str("\n ");
output.push_str(constant);
}
output
}
+332 -122
View File
@@ -1,159 +1,240 @@
use crate::utils::{print_first_paragraph_with_indent, validate_url_security, FileOrUrl};
use clap::{Parser as ClapParser, Subcommand};
use crate::utils::validate_url_security;
use crate::utils::FileOrUrl;
use clap::{command, Parser, Subcommand};
use codec::Decode;
use color_eyre::eyre::eyre;
use color_eyre::owo_colors::OwoColorize;
use indoc::writedoc;
use std::fmt::Write;
use std::write;
use codec::Decode;
use color_eyre::eyre::eyre;
use crate::commands::explore::calls::{explore_calls, CallsSubcommand};
use crate::commands::explore::constants::{explore_constants, ConstantsSubcommand};
use crate::commands::explore::storage::{explore_storage, StorageSubcommand};
use subxt::Metadata;
mod calls;
mod constants;
mod storage;
use self::pallets::PalletSubcommand;
mod pallets;
mod runtime_apis;
/// Explore pallets, calls, call parameters, storage entries and constants. Also allows for creating (unsigned) extrinsics.
///
/// # Example
///
/// ## Pallets
///
/// Show the pallets that are available:
/// Show the pallets and runtime apis that are available:
/// ```text
/// subxt explore --file=polkadot_metadata.scale
/// ```
///
/// ## Calls
/// ## Pallets
///
/// each pallet has `calls`, `constants`, `storage` and `events` that can be explored.
///
/// ### Calls
///
/// Show the calls in a pallet:
///
/// ```text
/// subxt explore Balances calls
/// subxt explore pallet Balances calls
/// ```
///
/// Show the call parameters a call expects:
///
/// ```text
/// subxt explore Balances calls transfer
/// subxt explore pallet Balances calls transfer
/// ```
///
/// Create an unsigned extrinsic from a scale value, validate it and output its hex representation
///
/// ```text
/// subxt explore Grandpa calls note_stalled { "delay": 5, "best_finalized_block_number": 5 }
/// subxt explore pallet Grandpa calls note_stalled { "delay": 5, "best_finalized_block_number": 5 }
/// # Encoded call data:
/// # 0x2c0411020500000005000000
/// subxt explore Balances calls transfer "{ \"dest\": v\"Raw\"((255, 255, 255)), \"value\": 0 }"
/// subxt explore pallet Balances calls transfer "{ \"dest\": v\"Raw\"((255, 255, 255)), \"value\": 0 }"
/// # Encoded call data:
/// # 0x24040607020cffffff00
/// ```
/// ## Constants
///
/// ### Constants
///
/// Show the constants in a pallet:
///
/// ```text
/// subxt explore Balances constants
/// subxt explore pallet Balances constants
/// ```
/// ## Storage
///
/// ### Storage
///
/// Show the storage entries in a pallet
///
/// ```text
/// subxt explore Alliance storage
/// ```
/// Show the types and value of a specific storage entry
/// ```text
/// subxt explore Alliance storage Announcements [KEY_SCALE_VALUE]
/// subxt explore pallet Alliance storage
/// ```
///
#[derive(Debug, ClapParser)]
/// Show the types and value of a specific storage entry
///
/// ```text
/// subxt explore pallet Alliance storage Announcements [KEY_SCALE_VALUE]
/// ```
///
/// ### Events
///
/// ```text
/// subxt explore pallet Balances events
/// ```
///
/// Show the type of a specific event
///
/// ```text
/// subxt explore pallet Balances events frozen
/// ```
///
/// ## Runtime APIs
/// Show the input and output types of a runtime api method.
/// In this example "core" is the name of the runtime api and "version" is a method on it:
///
/// ```text
/// subxt explore api core version
/// ```
///
/// Execute a runtime API call with the `--execute` (`-e`) flag, to see the return value.
/// For example here we get the "version", via the "core" runtime API from the connected node:
///
/// ```text
/// subxt explore api core version --execute
/// ```
///
#[derive(Debug, Parser)]
pub struct Opts {
#[command(flatten)]
file_or_url: FileOrUrl,
pallet: Option<String>,
#[command(subcommand)]
pallet_subcommand: Option<PalletSubcommand>,
subcommand: Option<PalletOrRuntimeApi>,
/// Allow insecure URLs e.g. URLs starting with ws:// or http:// without SSL encryption
#[clap(long, short)]
allow_insecure: bool,
}
#[derive(Debug, Clone, Subcommand)]
pub enum PalletSubcommand {
Calls(CallsSubcommand),
Constants(ConstantsSubcommand),
Storage(StorageSubcommand),
#[derive(Debug, Subcommand)]
pub enum PalletOrRuntimeApi {
Pallet(PalletOpts),
Api(RuntimeApiOpts),
}
#[derive(Debug, Parser)]
pub struct PalletOpts {
pub name: Option<String>,
#[command(subcommand)]
pub subcommand: Option<PalletSubcommand>,
}
#[derive(Debug, Parser)]
pub struct RuntimeApiOpts {
pub name: Option<String>,
#[clap(required = false)]
pub method: Option<String>,
#[clap(long, short, action)]
pub execute: bool,
#[clap(required = false)]
trailing_args: Vec<String>,
}
pub async fn run(opts: Opts, output: &mut impl std::io::Write) -> color_eyre::Result<()> {
validate_url_security(opts.file_or_url.url.as_ref(), opts.allow_insecure)?;
// get the metadata
let bytes = opts.file_or_url.fetch().await?;
let file_or_url = opts.file_or_url;
let bytes = file_or_url.fetch().await?;
let metadata = Metadata::decode(&mut &bytes[..])?;
// if no pallet specified, show user the pallets to choose from:
let Some(pallet_name) = opts.pallet else {
let available_pallets = print_available_pallets(&metadata);
writeln!(output, "Usage:",)?;
writeln!(output, " subxt explore <PALLET>",)?;
writeln!(output, " explore a specific pallet",)?;
writeln!(output, "\n{available_pallets}",)?;
let pallet_placeholder = "<PALLET>".blue();
let runtime_api_placeholder = "<RUNTIME_API>".blue();
// if no pallet/runtime_api specified, show user the pallets/runtime_apis to choose from:
let Some(pallet_or_runtime_api) = opts.subcommand else {
let pallets = pallets_as_string(&metadata);
let runtime_apis = runtime_apis_as_string(&metadata);
writedoc! {output, "
Usage:
subxt explore pallet {pallet_placeholder}
explore a specific pallet
subxt explore api {runtime_api_placeholder}
explore a specific runtime api
{pallets}
{runtime_apis}
"}?;
return Ok(());
};
// if specified pallet is wrong, show user the pallets to choose from (but this time as an error):
let Some(pallet_metadata) = metadata
.pallets()
.find(|pallet| pallet.name().to_lowercase() == pallet_name.to_lowercase())
else {
return Err(eyre!(
"pallet \"{}\" not found in metadata!\n{}",
pallet_name,
print_available_pallets(&metadata)
));
};
match pallet_or_runtime_api {
PalletOrRuntimeApi::Pallet(opts) => {
let Some(name) = opts.name else {
let pallets = pallets_as_string(&metadata);
writedoc! {output, "
Usage:
subxt explore pallet {pallet_placeholder}
explore a specific pallet
{pallets}
"}?;
return Ok(());
};
// if correct pallet was specified but no subcommand, instruct the user how to proceed:
let Some(pallet_subcomand) = opts.pallet_subcommand else {
let docs_string = print_first_paragraph_with_indent(pallet_metadata.docs(), 4);
if !docs_string.is_empty() {
writeln!(output, "Description:\n{docs_string}")?;
if let Some(pallet) = metadata
.pallets()
.find(|e| e.name().eq_ignore_ascii_case(&name))
{
pallets::run(opts.subcommand, pallet, &metadata, file_or_url, output).await
} else {
Err(eyre!(
"pallet \"{name}\" not found in metadata!\n{}",
pallets_as_string(&metadata),
))
}
}
writeln!(output, "Usage:")?;
writeln!(output, " subxt explore {pallet_name} calls")?;
writeln!(
output,
" explore the calls that can be made into this pallet"
)?;
writeln!(output, " subxt explore {pallet_name} constants")?;
writeln!(output, " explore the constants held in this pallet")?;
writeln!(output, " subxt explore {pallet_name} storage")?;
writeln!(
output,
" explore the storage values held in this pallet"
)?;
return Ok(());
};
PalletOrRuntimeApi::Api(opts) => {
let Some(name) = opts.name else {
let runtime_apis = runtime_apis_as_string(&metadata);
writedoc! {output, "
Usage:
subxt explore api {runtime_api_placeholder}
explore a specific runtime api
match pallet_subcomand {
PalletSubcommand::Calls(command) => {
explore_calls(command, &metadata, pallet_metadata, output)
}
PalletSubcommand::Constants(command) => {
explore_constants(command, &metadata, pallet_metadata, output)
}
PalletSubcommand::Storage(command) => {
// if the metadata is in some url, we use that same url to make storage calls against.
let node_url = opts.file_or_url.url.map(|url| url.to_string());
explore_storage(command, &metadata, pallet_metadata, node_url, output).await
{runtime_apis}
"}?;
return Ok(());
};
if let Some(runtime_api) = metadata
.runtime_api_traits()
.find(|e| e.name().eq_ignore_ascii_case(&name))
{
runtime_apis::run(
opts.method,
opts.execute,
opts.trailing_args,
runtime_api,
&metadata,
file_or_url,
output,
)
.await
} else {
Err(eyre!(
"runtime api \"{name}\" not found in metadata!\n{}",
runtime_apis_as_string(&metadata),
))
}
}
}
}
fn print_available_pallets(metadata: &Metadata) -> String {
fn pallets_as_string(metadata: &Metadata) -> String {
let pallet_placeholder = "<PALLET>".blue();
if metadata.pallets().len() == 0 {
"There are no <PALLET> values available.".to_string()
format!("There are no {pallet_placeholder}'s available.")
} else {
let mut output = "Available <PALLET> values are:".to_string();
let mut output = format!("Available {pallet_placeholder}'s are:");
let mut strings: Vec<_> = metadata.pallets().map(|p| p.name()).collect();
strings.sort();
for pallet in strings {
@@ -163,8 +244,27 @@ fn print_available_pallets(metadata: &Metadata) -> String {
}
}
pub fn runtime_apis_as_string(metadata: &Metadata) -> String {
let runtime_api_placeholder = "<RUNTIME_API>".blue();
if metadata.runtime_api_traits().len() == 0 {
format!("There are no {runtime_api_placeholder}'s available.")
} else {
let mut output = format!("Available {runtime_api_placeholder}'s are:");
let mut strings: Vec<_> = metadata.runtime_api_traits().map(|p| p.name()).collect();
strings.sort();
for api in strings {
write!(output, "\n {}", api).unwrap();
}
output
}
}
#[cfg(test)]
pub mod tests {
use indoc::formatdoc;
use pretty_assertions::assert_eq;
use super::Opts;
async fn run(cli_command: &str) -> color_eyre::Result<String> {
@@ -173,9 +273,25 @@ pub mod tests {
args.append(&mut split);
let opts: Opts = clap::Parser::try_parse_from(args)?;
let mut output: Vec<u8> = Vec::new();
super::run(opts, &mut output)
let r = super::run(opts, &mut output)
.await
.map(|_| String::from_utf8(output).unwrap())
.map(|_| String::from_utf8(output).unwrap())?;
Ok(r)
}
trait StripAnsi: ToString {
fn strip_ansi(&self) -> String {
let bytes = strip_ansi_escapes::strip(self.to_string().as_bytes());
String::from_utf8(bytes).unwrap()
}
}
impl<T: ToString> StripAnsi for T {}
macro_rules! assert_eq_start {
($a:expr, $b:expr) => {
assert_eq!(&$a[0..$b.len()], &$b[..]);
};
}
async fn run_against_file(cli_command: &str) -> color_eyre::Result<String> {
@@ -187,44 +303,138 @@ pub mod tests {
#[tokio::test]
async fn test_commands() {
// show pallets:
let output = run_against_file("").await;
assert_eq!(output.unwrap(), "Usage:\n subxt explore <PALLET>\n explore a specific pallet\n\nAvailable <PALLET> values are:\n Balances\n Multisig\n ParaInherent\n System\n Timestamp\n");
// shows pallets and runtime apis:
let output = run_against_file("").await.unwrap().strip_ansi();
let expected_output = formatdoc! {
"Usage:
subxt explore pallet <PALLET>
explore a specific pallet
subxt explore api <RUNTIME_API>
explore a specific runtime api
Available <PALLET>'s are:
Balances
Multisig
ParaInherent
System
Timestamp
Available <RUNTIME_API>'s are:
AccountNonceApi
AuthorityDiscoveryApi
BabeApi
BeefyApi
BeefyMmrApi
BlockBuilder
Core
GenesisBuilder
GrandpaApi
Metadata
MmrApi
OffchainWorkerApi
ParachainHost
SessionKeys
TaggedTransactionQueue
TransactionPaymentApi
"};
assert_eq!(output, expected_output);
// if incorrect pallet, error:
let output = run_against_file("abc123").await;
assert!(output.is_err());
// if correct pallet, show options (calls, constants, storage)
let output = run_against_file("Balances").await;
assert_eq!(output.unwrap(), "Usage:\n subxt explore Balances calls\n explore the calls that can be made into this pallet\n subxt explore Balances constants\n explore the constants held in this pallet\n subxt explore Balances storage\n explore the storage values held in this pallet\n");
let output = run_against_file("pallet Balances")
.await
.unwrap()
.strip_ansi();
let expected_output = formatdoc! {"
Usage:
subxt explore pallet Balances calls
explore the calls that can be made into a pallet
subxt explore pallet Balances constants
explore the constants of a pallet
subxt explore pallet Balances storage
explore the storage values of a pallet
subxt explore pallet Balances events
explore the events of a pallet
"};
assert_eq!(output, expected_output);
// check that exploring calls, storage entries and constants is possible:
let output = run_against_file("Balances calls").await;
assert!(output.unwrap().starts_with("Usage:\n subxt explore Balances calls <CALL>\n explore a specific call within this pallet\n\nAvailable <CALL>'s in the \"Balances\" pallet:\n"));
let output = run_against_file("Balances storage").await;
assert!(output.unwrap().starts_with("Usage:\n subxt explore Balances storage <STORAGE_ENTRY>\n view details for a specific storage entry\n\nAvailable <STORAGE_ENTRY>'s in the \"Balances\" pallet:\n"));
let output = run_against_file("Balances constants").await;
assert!(output.unwrap().starts_with("Usage:\n subxt explore Balances constants <CONSTANT>\n explore a specific call within this pallet\n\nAvailable <CONSTANT>'s in the \"Balances\" pallet:\n"));
let output = run_against_file("pallet Balances calls")
.await
.unwrap()
.strip_ansi();
let start = formatdoc! {"
Usage:
subxt explore pallet Balances calls <CALL>
explore a specific call of this pallet
Available <CALL>'s in the \"Balances\" pallet:"};
assert_eq_start!(output, start);
let output = run_against_file("pallet Balances storage")
.await
.unwrap()
.strip_ansi();
let start = formatdoc! {"
Usage:
subxt explore pallet Balances storage <STORAGE_ENTRY>
explore a specific storage entry of this pallet
Available <STORAGE_ENTRY>'s in the \"Balances\" pallet:
"};
assert_eq_start!(output, start);
let output = run_against_file("pallet Balances constants")
.await
.unwrap()
.strip_ansi();
let start = formatdoc! {"
Usage:
subxt explore pallet Balances constants <CONSTANT>
explore a specific constant of this pallet
Available <CONSTANT>'s in the \"Balances\" pallet:
"};
assert_eq_start!(output, start);
let output = run_against_file("pallet Balances events")
.await
.unwrap()
.strip_ansi();
let start = formatdoc! {"
Usage:
subxt explore pallet Balances events <EVENT>
explore a specific event of this pallet
Available <EVENT>'s in the \"Balances\" pallet:
"};
assert_eq_start!(output, start);
// check that invalid subcommands don't work:
let output = run_against_file("Balances abc123").await;
let output = run_against_file("pallet Balances abc123").await;
assert!(output.is_err());
// check that we can explore a certain call:
let output = run_against_file("Balances calls transfer_allow_death").await;
assert!(output.unwrap().starts_with("Usage:\n subxt explore Balances calls transfer_allow_death <SCALE_VALUE>\n construct the call by providing a valid argument\n\nThe call expect expects a <SCALE_VALUE> with this shape:\n {\n dest: enum MultiAddress"));
// check that unsigned extrinsic can be constructed:
let output = run_against_file(
"Balances calls transfer_allow_death {\"dest\":v\"Raw\"((255,255, 255)),\"value\":0}",
)
.await;
assert_eq!(
output.unwrap(),
"Encoded call data:\n 0x24040400020cffffff00\n"
);
// check that we can explore a certain constant:
let output = run_against_file("Balances constants ExistentialDeposit").await;
assert_eq!(output.unwrap(), "Description:\n The minimum amount required to keep an account open. MUST BE GREATER THAN ZERO!\n\nThe constant has the following shape:\n u128\n\nThe value of the constant is:\n 33333333\n");
// check that we can explore a certain storage entry:
let output = run_against_file("System storage Account").await;
assert!(output.unwrap().starts_with("Usage:\n subxt explore System storage Account <KEY_VALUE>\n\nDescription:\n The full account information for a particular account ID."));
// in the future we could also integrate with substrate-testrunner to spawn up a node and send an actual storage query to it: e.g. `subxt explore System storage Digest`
let output = run_against_file("pallet Balances calls transfer_keep_alive")
.await
.unwrap()
.strip_ansi();
// Note: at some point we want to switch to new metadata in the artifacts folder which has e.g. transfer_keep_alive instead of transfer.
let start = formatdoc! {"
Usage:
subxt explore pallet Balances calls transfer_keep_alive <SCALE_VALUE>
construct the call by providing a valid argument
"};
assert_eq_start!(output, start);
// check that we can see methods of a runtime api:
let output = run_against_file("api metadata").await.unwrap().strip_ansi();
let start = formatdoc! {"
Description:
The `Metadata` api trait that returns metadata for the runtime.
Usage:
subxt explore api Metadata <METHOD>
explore a specific runtime api method
Available <METHOD>'s available for the \"Metadata\" runtime api:
"};
assert_eq_start!(output, start);
}
#[tokio::test]
@@ -1,5 +1,7 @@
use clap::Args;
use color_eyre::eyre::eyre;
use color_eyre::owo_colors::OwoColorize;
use indoc::{formatdoc, writedoc};
use scale_info::form::PortableForm;
use scale_info::{PortableRegistry, Type, TypeDef, TypeDefVariant};
use scale_value::{Composite, ValueDef};
@@ -13,9 +15,10 @@ use subxt::{
OfflineClient,
};
use crate::utils::type_description::print_type_description;
use crate::utils::type_example::print_type_examples;
use crate::utils::with_indent;
use crate::utils::{
fields_composite_example, fields_description, parse_string_into_scale_value, Indent,
SyntaxHighlight,
};
#[derive(Debug, Clone, Args)]
pub struct CallsSubcommand {
@@ -26,8 +29,8 @@ pub struct CallsSubcommand {
pub fn explore_calls(
command: CallsSubcommand,
metadata: &Metadata,
pallet_metadata: PalletMetadata,
metadata: &Metadata,
output: &mut impl std::io::Write,
) -> color_eyre::Result<()> {
let pallet_name = pallet_metadata.name();
@@ -36,10 +39,20 @@ pub fn explore_calls(
let (calls_enum_type_def, _calls_enum_type) =
get_calls_enum_type(pallet_metadata, metadata.types())?;
let usage = || {
let calls = calls_to_string(calls_enum_type_def, pallet_name);
formatdoc! {"
Usage:
subxt explore pallet {pallet_name} calls <CALL>
explore a specific call of this pallet
{calls}
"}
};
// if no call specified, show user the calls to choose from:
let Some(call_name) = command.call else {
let available_calls = print_available_calls(calls_enum_type_def, pallet_name);
writeln!(output, "Usage:\n subxt explore {pallet_name} calls <CALL>\n explore a specific call within this pallet\n\n{available_calls}")?;
writeln!(output, "{}", usage())?;
return Ok(());
};
@@ -47,12 +60,11 @@ pub fn explore_calls(
let Some(call) = calls_enum_type_def
.variants
.iter()
.find(|variant| variant.name.to_lowercase() == call_name.to_lowercase())
.find(|variant| variant.name.eq_ignore_ascii_case(&call_name))
else {
let available_calls = print_available_calls(calls_enum_type_def, pallet_name);
let description = format!("Usage:\n subxt explore {pallet_name} calls <CALL>\n explore a specific call within this pallet\n\n{available_calls}", );
return Err(eyre!(
"\"{call_name}\" call not found in \"{pallet_name}\" pallet!\n\n{description}"
"\"{call_name}\" call not found in \"{pallet_name}\" pallet!\n\n{}",
usage()
));
};
@@ -61,38 +73,49 @@ pub fn explore_calls(
// if no trailing arguments specified show user the expected type of arguments with examples:
if trailing_args.is_empty() {
let mut type_description = print_type_description(&call.fields, metadata.types())?;
type_description = with_indent(type_description, 4);
let mut type_examples = print_type_examples(&call.fields, metadata.types(), "SCALE_VALUE")?;
type_examples = with_indent(type_examples, 4);
writeln!(output, "Usage:")?;
writeln!(
output,
" subxt explore {pallet_name} calls {call_name} <SCALE_VALUE>"
)?;
writeln!(
output,
" construct the call by providing a valid argument\n"
)?;
writeln!(
output,
"The call expect expects a <SCALE_VALUE> with this shape:\n{type_description}\n\n{}\n\nYou may need to surround the value in single quotes when providing it as an argument."
, &type_examples[4..])?;
let fields: Vec<(Option<&str>, u32)> = call
.fields
.iter()
.map(|f| (f.name.as_deref(), f.ty.id))
.collect();
let type_description = fields_description(&fields, &call.name, metadata.types()).indent(4);
let fields_example =
fields_composite_example(call.fields.iter().map(|e| e.ty.id), metadata.types())
.indent(4)
.highlight();
let scale_value_placeholder = "<SCALE_VALUE>".blue();
writedoc! {output, "
Usage:
subxt explore pallet {pallet_name} calls {call_name} {scale_value_placeholder}
construct the call by providing a valid argument
The call expects a {scale_value_placeholder} with this shape:
{type_description}
For example you could provide this {scale_value_placeholder}:
{fields_example}
"}?;
return Ok(());
}
// parse scale_value from trailing arguments and try to create an unsigned extrinsic with it:
let value = scale_value::stringify::from_str(&trailing_args).0.map_err(|err| eyre!("scale_value::stringify::from_str led to a ParseError.\n\ntried parsing: \"{}\"\n\n{}", trailing_args, err))?;
let value = parse_string_into_scale_value(&trailing_args)?;
let value_as_composite = value_into_composite(value);
let offline_client = mocked_offline_client(metadata.clone());
let payload = tx::dynamic(pallet_name, call_name, value_as_composite);
let unsigned_extrinsic = offline_client.tx().create_unsigned(&payload)?;
let hex_bytes = format!("0x{}", hex::encode(unsigned_extrinsic.encoded()));
writeln!(output, "Encoded call data:\n {hex_bytes}")?;
writedoc! {output, "
Encoded call data:
{hex_bytes}
"}?;
Ok(())
}
fn print_available_calls(pallet_calls: &TypeDefVariant<PortableForm>, pallet_name: &str) -> String {
fn calls_to_string(pallet_calls: &TypeDefVariant<PortableForm>, pallet_name: &str) -> String {
if pallet_calls.variants.is_empty() {
return format!("No <CALL>'s available in the \"{pallet_name}\" pallet.");
}
@@ -0,0 +1,96 @@
use clap::Args;
use color_eyre::eyre::eyre;
use indoc::{formatdoc, writedoc};
use scale_typegen_description::type_description;
use subxt::metadata::{types::PalletMetadata, Metadata};
use crate::utils::{first_paragraph_of_docs, format_scale_value, Indent, SyntaxHighlight};
#[derive(Debug, Clone, Args)]
pub struct ConstantsSubcommand {
constant: Option<String>,
}
pub fn explore_constants(
command: ConstantsSubcommand,
pallet_metadata: PalletMetadata,
metadata: &Metadata,
output: &mut impl std::io::Write,
) -> color_eyre::Result<()> {
let pallet_name = pallet_metadata.name();
let usage = || {
let constants = constants_to_string(pallet_metadata, pallet_name);
formatdoc! {"
Usage:
subxt explore pallet {pallet_name} constants <CONSTANT>
explore a specific constant of this pallet
{constants}
"}
};
let Some(constant_name) = command.constant else {
writeln!(output, "{}", usage())?;
return Ok(());
};
// if specified constant is wrong, show user the constants to choose from (but this time as an error):
let Some(constant) = pallet_metadata
.constants()
.find(|constant| constant.name().eq_ignore_ascii_case(&constant_name))
else {
let err = eyre!(
"constant \"{constant_name}\" not found in \"{pallet_name}\" pallet!\n\n{}",
usage()
);
return Err(err);
};
// docs
let doc_string = first_paragraph_of_docs(constant.docs()).indent(4);
if !doc_string.is_empty() {
writedoc! {output, "
Description:
{doc_string}
"}?;
}
// shape
let type_description = type_description(constant.ty(), metadata.types(), true)
.expect("No Type Description")
.indent(4)
.highlight();
// value
let value =
scale_value::scale::decode_as_type(&mut constant.value(), constant.ty(), metadata.types())?;
let value = format_scale_value(&value).indent(4);
writedoc!(
output,
"
The constant has the following shape:
{type_description}
The value of the constant is:
{value}
"
)?;
Ok(())
}
fn constants_to_string(pallet_metadata: PalletMetadata, pallet_name: &str) -> String {
if pallet_metadata.constants().len() == 0 {
return format!("No <CONSTANT>'s available in the \"{pallet_name}\" pallet.");
}
let mut output = format!("Available <CONSTANT>'s in the \"{pallet_name}\" pallet:");
let mut strings: Vec<_> = pallet_metadata.constants().map(|c| c.name()).collect();
strings.sort();
for constant in strings {
output.push_str("\n ");
output.push_str(constant);
}
output
}
@@ -0,0 +1,88 @@
use clap::Args;
use color_eyre::eyre::eyre;
use indoc::{formatdoc, writedoc};
use scale_info::{form::PortableForm, Variant};
use subxt::metadata::{types::PalletMetadata, Metadata};
use crate::utils::{fields_description, first_paragraph_of_docs, Indent};
#[derive(Debug, Clone, Args)]
pub struct EventsSubcommand {
event: Option<String>,
}
pub fn explore_events(
command: EventsSubcommand,
pallet_metadata: PalletMetadata,
metadata: &Metadata,
output: &mut impl std::io::Write,
) -> color_eyre::Result<()> {
let pallet_name = pallet_metadata.name();
let event_variants = pallet_metadata.event_variants().unwrap_or(&[]);
let usage = || {
let events = events_to_string(event_variants, pallet_name);
formatdoc! {"
Usage:
subxt explore pallet {pallet_name} events <EVENT>
explore a specific event of this pallet
{events}
"}
};
let Some(event_name) = command.event else {
writeln!(output, "{}", usage())?;
return Ok(());
};
// if specified event is wrong, show user the events to choose from (but this time as an error):
let Some(event) = event_variants
.iter()
.find(|event| event.name.eq_ignore_ascii_case(&event_name))
else {
let err = eyre!(
"event \"{event_name}\" not found in \"{pallet_name}\" pallet!\n\n{}",
usage()
);
return Err(err);
};
let doc_string = first_paragraph_of_docs(&event.docs).indent(4);
if !doc_string.is_empty() {
writedoc! {output, "
Description:
{doc_string}
"}?;
}
let fields: Vec<(Option<&str>, u32)> = event
.fields
.iter()
.map(|f| (f.name.as_deref(), f.ty.id))
.collect();
let type_description = fields_description(&fields, &event.name, metadata.types()).indent(4);
writedoc!(
output,
"
The event has the following shape:
{type_description}
"
)?;
Ok(())
}
fn events_to_string(event_variants: &[Variant<PortableForm>], pallet_name: &str) -> String {
if event_variants.is_empty() {
return format!("No <EVENT>'s available in the \"{pallet_name}\" pallet.");
}
let mut output = format!("Available <EVENT>'s in the \"{pallet_name}\" pallet:");
let mut strings: Vec<_> = event_variants.iter().map(|c| &c.name).collect();
strings.sort();
for event in strings {
output.push_str("\n ");
output.push_str(event);
}
output
}
+80
View File
@@ -0,0 +1,80 @@
use clap::Subcommand;
use indoc::writedoc;
use subxt::Metadata;
use subxt_metadata::PalletMetadata;
use crate::utils::{first_paragraph_of_docs, FileOrUrl, Indent};
use self::{
calls::CallsSubcommand,
constants::ConstantsSubcommand,
events::{explore_events, EventsSubcommand},
storage::StorageSubcommand,
};
use calls::explore_calls;
use constants::explore_constants;
use storage::explore_storage;
mod calls;
mod constants;
mod events;
mod storage;
#[derive(Debug, Clone, Subcommand)]
pub enum PalletSubcommand {
Calls(CallsSubcommand),
Constants(ConstantsSubcommand),
Storage(StorageSubcommand),
Events(EventsSubcommand),
}
pub async fn run<'a>(
subcommand: Option<PalletSubcommand>,
pallet_metadata: PalletMetadata<'a>,
metadata: &'a Metadata,
file_or_url: FileOrUrl,
output: &mut impl std::io::Write,
) -> color_eyre::Result<()> {
let pallet_name = pallet_metadata.name();
let Some(subcommand) = subcommand else {
let docs_string = first_paragraph_of_docs(pallet_metadata.docs()).indent(4);
if !docs_string.is_empty() {
writedoc! {output, "
Description:
{docs_string}
"}?;
}
writedoc! {output, "
Usage:
subxt explore pallet {pallet_name} calls
explore the calls that can be made into a pallet
subxt explore pallet {pallet_name} constants
explore the constants of a pallet
subxt explore pallet {pallet_name} storage
explore the storage values of a pallet
subxt explore pallet {pallet_name} events
explore the events of a pallet
"}?;
return Ok(());
};
match subcommand {
PalletSubcommand::Calls(command) => {
explore_calls(command, pallet_metadata, metadata, output)
}
PalletSubcommand::Constants(command) => {
explore_constants(command, pallet_metadata, metadata, output)
}
PalletSubcommand::Storage(command) => {
// if the metadata came from some url, we use that same url to make storage calls against.
explore_storage(command, pallet_metadata, metadata, file_or_url, output).await
}
PalletSubcommand::Events(command) => {
explore_events(command, pallet_metadata, metadata, output)
}
}
}
+222
View File
@@ -0,0 +1,222 @@
use clap::Args;
use color_eyre::{
eyre::{bail, eyre},
owo_colors::OwoColorize,
};
use indoc::{formatdoc, writedoc};
use scale_typegen_description::type_description;
use scale_value::Value;
use std::fmt::Write;
use std::write;
use subxt::{
ext::scale_encode::EncodeAsType,
metadata::{
types::{PalletMetadata, StorageEntryType, StorageMetadata},
Metadata,
},
};
use crate::utils::{
create_client, first_paragraph_of_docs, parse_string_into_scale_value, type_example, FileOrUrl,
Indent, SyntaxHighlight,
};
#[derive(Debug, Clone, Args)]
pub struct StorageSubcommand {
storage_entry: Option<String>,
#[clap(long, short, action)]
execute: bool,
#[clap(required = false)]
trailing_args: Vec<String>,
}
pub async fn explore_storage(
command: StorageSubcommand,
pallet_metadata: PalletMetadata<'_>,
metadata: &Metadata,
file_or_url: FileOrUrl,
output: &mut impl std::io::Write,
) -> color_eyre::Result<()> {
let pallet_name = pallet_metadata.name();
let trailing_args = command.trailing_args.join(" ");
let trailing_args = trailing_args.trim();
let Some(storage_metadata) = pallet_metadata.storage() else {
writeln!(
output,
"The \"{pallet_name}\" pallet has no storage entries."
)?;
return Ok(());
};
let storage_entry_placeholder = "<STORAGE_ENTRY>".blue();
let usage = || {
let storage_entries = storage_entries_string(storage_metadata, pallet_name);
formatdoc! {"
Usage:
subxt explore pallet {pallet_name} storage {storage_entry_placeholder}
explore a specific storage entry of this pallet
{storage_entries}
"}
};
// if no storage entry specified, show user the calls to choose from:
let Some(entry_name) = command.storage_entry else {
writeln!(output, "{}", usage())?;
return Ok(());
};
// if specified call storage entry wrong, show user the storage entries to choose from (but this time as an error):
let Some(storage) = storage_metadata
.entries()
.iter()
.find(|entry| entry.name().eq_ignore_ascii_case(&entry_name))
else {
bail!(
"Storage entry \"{entry_name}\" not found in \"{pallet_name}\" pallet!\n\n{}",
usage()
);
};
let (return_ty_id, key_ty_id) = match storage.entry_type() {
StorageEntryType::Plain(value) => (*value, None),
StorageEntryType::Map {
value_ty, key_ty, ..
} => (*value_ty, Some(*key_ty)),
};
let key_value_placeholder = "<KEY_VALUE>".blue();
let docs_string = first_paragraph_of_docs(storage.docs()).indent(4);
if !docs_string.is_empty() {
writedoc! {output, "
Description:
{docs_string}
"}?;
}
// only inform user about usage if `execute` flag not provided
if !command.execute {
writedoc! {output, "
Usage:
subxt explore pallet {pallet_name} storage {entry_name} --execute {key_value_placeholder}
retrieve a value from storage
"}?;
}
let return_ty_description = type_description(return_ty_id, metadata.types(), true)
.expect("No type Description")
.indent(4)
.highlight();
writedoc! {output, "
The storage entry has the following shape:
{return_ty_description}
"}?;
// inform user about shape of the key if it can be provided:
if let Some(key_ty_id) = key_ty_id {
let key_ty_description = type_description(key_ty_id, metadata.types(), true)
.expect("No type Description")
.indent(4)
.highlight();
let key_ty_example = type_example(key_ty_id, metadata.types())
.indent(4)
.highlight();
writedoc! {output, "
The {key_value_placeholder} has the following shape:
{key_ty_description}
For example you could provide this {key_value_placeholder}:
{key_ty_example}
"}?;
} else {
writedoc! {output,"
Can be accessed without providing a {key_value_placeholder}.
"}?;
}
// if `--execute`/`-e` flag is set, try to execute the storage entry request
if !command.execute {
return Ok(());
}
let storage_entry_keys: Vec<Value> = match (trailing_args.is_empty(), key_ty_id) {
(false, None) => {
let warning = format!("Warning: You submitted a key, but no key is needed: \"{trailing_args}\". To access the storage value, please do not provide any key.");
writeln!(output, "{}", warning.yellow())?;
return Ok(());
}
(true, Some(_)) => {
// just return. The user was instructed above how to provide a value if they want to.
return Ok(());
}
(true, None) => vec![],
(false, Some(type_id)) => {
let value = parse_string_into_scale_value(trailing_args)?;
let value_str = value.indent(4);
writedoc! {output, "
You submitted the following {key_value_placeholder}:
{value_str}
"}?;
let key_bytes = value.encode_as_type(type_id, metadata.types())?;
let bytes_composite = Value::from_bytes(key_bytes);
vec![bytes_composite]
}
};
// construct the client:
let client = create_client(&file_or_url).await?;
let storage_query = subxt::dynamic::storage(pallet_name, storage.name(), storage_entry_keys);
let decoded_value_thunk_or_none = client
.storage()
.at_latest()
.await?
.fetch(&storage_query)
.await?;
let decoded_value_thunk =
decoded_value_thunk_or_none.ok_or(eyre!("Value not found in storage."))?;
let value = decoded_value_thunk.to_value()?.to_string().highlight();
writedoc! {output, "
The value of the storage entry is:
{value}
"}?;
Ok(())
}
fn storage_entries_string(storage_metadata: &StorageMetadata, pallet_name: &str) -> String {
let storage_entry_placeholder = "<STORAGE_ENTRY>".blue();
if storage_metadata.entries().is_empty() {
format!("No {storage_entry_placeholder}'s available in the \"{pallet_name}\" pallet.")
} else {
let mut output = format!(
"Available {storage_entry_placeholder}'s in the \"{}\" pallet:",
pallet_name
);
let mut strings: Vec<_> = storage_metadata
.entries()
.iter()
.map(|s| s.name())
.collect();
strings.sort();
for entry in strings {
write!(output, "\n {}", entry).unwrap();
}
output
}
}
@@ -0,0 +1,203 @@
use crate::utils::{
create_client, fields_composite_example, fields_description, first_paragraph_of_docs,
parse_string_into_scale_value, FileOrUrl, Indent, SyntaxHighlight,
};
use color_eyre::{
eyre::{bail, eyre},
owo_colors::OwoColorize,
};
use indoc::{formatdoc, writedoc};
use scale_typegen_description::type_description;
use scale_value::Value;
use subxt::{
ext::{scale_decode::DecodeAsType, scale_encode::EncodeAsType},
Metadata,
};
use subxt_metadata::RuntimeApiMetadata;
/// Runs for a specified runtime API trait.
/// Cases to consider:
/// ```norun
/// method is:
/// None => Show pallet docs + available methods
/// Some (invalid) => Show Error + available methods
/// Some (valid) => Show method docs + output type description
/// exectute is:
/// false => Show input type description + Example Value
/// true => validate (trailing args + build node connection)
/// validation is:
/// Err => Show Error
/// Ok => Make a runtime api call witht the provided args.
/// response is:
/// Err => Show Error
/// Ok => Show the result
/// ```
pub async fn run<'a>(
method: Option<String>,
execute: bool,
trailing_args: Vec<String>,
runtime_api_metadata: RuntimeApiMetadata<'a>,
metadata: &'a Metadata,
file_or_url: FileOrUrl,
output: &mut impl std::io::Write,
) -> color_eyre::Result<()> {
let api_name = runtime_api_metadata.name();
let usage = || {
let methods = methods_to_string(&runtime_api_metadata);
formatdoc! {"
Usage:
subxt explore api {api_name} <METHOD>
explore a specific runtime api method
{methods}
"}
};
// If method is None: Show pallet docs + available methods
let Some(method_name) = method else {
let doc_string = first_paragraph_of_docs(runtime_api_metadata.docs()).indent(4);
if !doc_string.is_empty() {
writedoc! {output, "
Description:
{doc_string}
"}?;
}
writeln!(output, "{}", usage())?;
return Ok(());
};
// If method is invalid: Show Error + available methods
let Some(method) = runtime_api_metadata
.methods()
.find(|e| e.name().eq_ignore_ascii_case(&method_name))
else {
return Err(eyre!(
"\"{method_name}\" method not found for \"{method_name}\" runtime api!\n\n{}",
usage()
));
};
// redeclare to not use the wrong capitalization of the input from here on:
let method_name = method.name();
// Method is valid. Show method docs + output type description
let doc_string = first_paragraph_of_docs(method.docs()).indent(4);
if !doc_string.is_empty() {
writedoc! {output, "
Description:
{doc_string}
"}?;
}
let input_value_placeholder = "<INPUT_VALUE>".blue();
// Output type description
let input_values = || {
if method.inputs().len() == 0 {
return format!("The method does not require an {input_value_placeholder}");
}
let fields: Vec<(Option<&str>, u32)> = method
.inputs()
.map(|f| (Some(f.name.as_str()), f.ty))
.collect();
let fields_description =
fields_description(&fields, method.name(), metadata.types()).indent(4);
let fields_example =
fields_composite_example(method.inputs().map(|e| e.ty), metadata.types())
.indent(4)
.highlight();
formatdoc! {"
The method expects an {input_value_placeholder} with this shape:
{fields_description}
For example you could provide this {input_value_placeholder}:
{fields_example}"}
};
let execute_usage = || {
let output = type_description(method.output_ty(), metadata.types(), true)
.expect("No Type Description")
.indent(4)
.highlight();
let input = input_values();
formatdoc! {"
Usage:
subxt explore api {api_name} {method_name} --execute {input_value_placeholder}
make a runtime api request
The Output of this method has the following shape:
{output}
{input}"}
};
writeln!(output, "{}", execute_usage())?;
if !execute {
return Ok(());
}
if trailing_args.len() != method.inputs().len() {
bail!("The number of trailing arguments you provided after the `execute` flag does not match the expected number of inputs!\n{}", execute_usage());
}
// encode each provided input as bytes of the correct type:
let args_data: Vec<Value> = method
.inputs()
.zip(trailing_args.iter())
.map(|(ty, arg)| {
let value = parse_string_into_scale_value(arg)?;
let value_str = value.indent(4);
// convert to bytes:
writedoc! {output, "
You submitted the following {input_value_placeholder}:
{value_str}
"}?;
// encode, then decode. This ensures that the scale value is of the correct shape for the param:
let bytes = value.encode_as_type(ty.ty, metadata.types())?;
let value = Value::decode_as_type(&mut &bytes[..], ty.ty, metadata.types())?
.map_context(|_| ());
Ok(value)
})
.collect::<color_eyre::Result<Vec<Value>>>()?;
let method_call = subxt::dynamic::runtime_api_call(api_name, method.name(), args_data);
let client = create_client(&file_or_url).await?;
let output_value = client
.runtime_api()
.at_latest()
.await?
.call(method_call)
.await?;
let output_value = output_value.to_value()?.to_string().highlight();
writedoc! {output, "
Returned value:
{output_value}
"}?;
Ok(())
}
fn methods_to_string(runtime_api_metadata: &RuntimeApiMetadata<'_>) -> String {
let api_name = runtime_api_metadata.name();
if runtime_api_metadata.methods().len() == 0 {
return format!("No <METHOD>'s available for the \"{api_name}\" runtime api.");
}
let mut output = format!("Available <METHOD>'s available for the \"{api_name}\" runtime api:");
let mut strings: Vec<_> = runtime_api_metadata.methods().map(|e| e.name()).collect();
strings.sort();
for variant in strings {
output.push_str("\n ");
output.push_str(variant);
}
output
}
-192
View File
@@ -1,192 +0,0 @@
use clap::Args;
use color_eyre::eyre::eyre;
use std::fmt::Write;
use std::write;
use subxt::OnlineClient;
use subxt::{
config::SubstrateConfig,
metadata::{
types::{PalletMetadata, StorageEntryType, StorageMetadata},
Metadata,
},
};
use crate::utils::type_description::print_type_description;
use crate::utils::type_example::print_type_examples;
use crate::utils::{print_first_paragraph_with_indent, with_indent};
#[derive(Debug, Clone, Args)]
pub struct StorageSubcommand {
storage_entry: Option<String>,
#[clap(required = false)]
trailing_args: Vec<String>,
}
pub async fn explore_storage(
command: StorageSubcommand,
metadata: &Metadata,
pallet_metadata: PalletMetadata<'_>,
custom_online_client_url: Option<String>,
output: &mut impl std::io::Write,
) -> color_eyre::Result<()> {
let pallet_name = pallet_metadata.name();
let trailing_args = command.trailing_args.join(" ");
let trailing_args = trailing_args.trim();
let Some(storage_metadata) = pallet_metadata.storage() else {
writeln!(
output,
"The \"{pallet_name}\" pallet has no storage entries."
)?;
return Ok(());
};
// if no storage entry specified, show user the calls to choose from:
let Some(entry_name) = command.storage_entry else {
let storage_entries = print_available_storage_entries(storage_metadata, pallet_name);
writeln!(output, "Usage:\n subxt explore {pallet_name} storage <STORAGE_ENTRY>\n view details for a specific storage entry\n\n{storage_entries}")?;
return Ok(());
};
// if specified call storage entry wrong, show user the storage entries to choose from (but this time as an error):
let Some(storage) = storage_metadata
.entries()
.iter()
.find(|entry| entry.name().to_lowercase() == entry_name.to_lowercase())
else {
let storage_entries = print_available_storage_entries(storage_metadata, pallet_name);
let description = format!("Usage:\n subxt explore {pallet_name} storage <STORAGE_ENTRY>\n view details for a specific storage entry\n\n{storage_entries}");
return Err(eyre!("Storage entry \"{entry_name}\" not found in \"{pallet_name}\" pallet!\n\n{description}"));
};
let (return_ty_id, key_ty_id) = match storage.entry_type() {
StorageEntryType::Plain(value) => (*value, None),
StorageEntryType::Map {
value_ty, key_ty, ..
} => (*value_ty, Some(*key_ty)),
};
// only inform user about usage if a key can be provided:
if key_ty_id.is_some() && trailing_args.is_empty() {
writeln!(output, "Usage:")?;
writeln!(
output,
" subxt explore {pallet_name} storage {entry_name} <KEY_VALUE>\n"
)?;
}
let docs_string = print_first_paragraph_with_indent(storage.docs(), 4);
if !docs_string.is_empty() {
writeln!(output, "Description:\n{docs_string}")?;
}
// inform user about shape of key if it can be provided:
if let Some(key_ty_id) = key_ty_id {
let mut key_ty_description = print_type_description(&key_ty_id, metadata.types())?;
key_ty_description = with_indent(key_ty_description, 4);
let mut key_ty_examples = print_type_examples(&key_ty_id, metadata.types(), "<KEY_VALUE>")?;
key_ty_examples = with_indent(key_ty_examples, 4);
writeln!(
output,
"\nThe <KEY_VALUE> has the following shape:\n {key_ty_description}\n"
)?;
writeln!(output, "{}", &key_ty_examples[4..])?;
} else {
writeln!(
output,
"The constant can be accessed without providing a key."
)?;
}
let mut return_ty_description = print_type_description(&return_ty_id, metadata.types())?;
return_ty_description = if return_ty_description.contains('\n') {
format!("\n{}", with_indent(return_ty_description, 4))
} else {
return_ty_description
};
writeln!(
output,
"\nThe storage entry has the following shape: {}",
return_ty_description
)?;
// construct the vector of scale_values that should be used as a key to the storage (often empty)
let key_scale_values = if let Some(key_ty_id) = key_ty_id.filter(|_| !trailing_args.is_empty())
{
let key_scale_value = scale_value::stringify::from_str(trailing_args).0.map_err(|err| eyre!("scale_value::stringify::from_str led to a ParseError.\n\ntried parsing: \"{}\"\n\n{}", trailing_args, err))?;
writeln!(
output,
"\n\nYou submitted the following value as a key:\n{}",
with_indent(scale_value::stringify::to_string(&key_scale_value), 4)
)?;
let mut key_bytes: Vec<u8> = Vec::new();
scale_value::scale::encode_as_type(
&key_scale_value,
key_ty_id,
metadata.types(),
&mut key_bytes,
)?;
let bytes_composite = scale_value::Value::from_bytes(&key_bytes);
vec![bytes_composite]
} else {
Vec::new()
};
if key_ty_id.is_none() && !trailing_args.is_empty() {
writeln!(output, "\n\nWarning: You submitted the following value as a key, but it will be ignored, because the storage entry does not require a key: \"{}\"", trailing_args)?;
}
// construct and submit the storage entry request if either no key is needed or som key was provided as a scale value
if key_ty_id.is_none() || !key_scale_values.is_empty() {
let online_client = match custom_online_client_url {
None => OnlineClient::<SubstrateConfig>::new().await?,
Some(url) => OnlineClient::<SubstrateConfig>::from_url(url).await?,
};
let storage_query = subxt::dynamic::storage(pallet_name, entry_name, key_scale_values);
let decoded_value_thunk_or_none = online_client
.storage()
.at_latest()
.await?
.fetch(&storage_query)
.await?;
let decoded_value_thunk =
decoded_value_thunk_or_none.ok_or(eyre!("Value not found in storage."))?;
let value = decoded_value_thunk.to_value()?;
let mut value_string = scale_value::stringify::to_string(&value);
value_string = with_indent(value_string, 4);
writeln!(
output,
"\nThe value of the storage entry is:\n{value_string}"
)?;
}
Ok(())
}
fn print_available_storage_entries(
storage_metadata: &StorageMetadata,
pallet_name: &str,
) -> String {
if storage_metadata.entries().is_empty() {
format!("No <STORAGE_ENTRY>'s available in the \"{pallet_name}\" pallet.")
} else {
let mut output = format!(
"Available <STORAGE_ENTRY>'s in the \"{}\" pallet:",
pallet_name
);
let mut strings: Vec<_> = storage_metadata
.entries()
.iter()
.map(|s| s.name())
.collect();
strings.sort();
for entry in strings {
write!(output, "\n {}", entry).unwrap();
}
output
}
}