Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4dbbd16
Update cargo.lock to use scale-info v2.4.0
lexnv Mar 23, 2023
99934de
metadata: Retain only a subset of the metadata
lexnv Mar 23, 2023
3f0f55f
codegen: Generate top level Event
lexnv Mar 24, 2023
704c512
metadata: Only retain DispatchError
lexnv Mar 24, 2023
d49c801
metadata: Export just the retain method
lexnv Mar 24, 2023
5ebab47
cli: Retain pallets
lexnv Mar 24, 2023
47e9163
metadata: Do not include extrinsic metadata
lexnv Mar 24, 2023
19eec1f
retain: Fix clippy
lexnv Mar 24, 2023
725a2e5
test-runtime: Generate per pallet metadata and rs file
lexnv Mar 27, 2023
8515b1e
ui-tests: Check per metadata generated files
lexnv Mar 27, 2023
a8d368b
Revert "test-runtime: Generate per pallet metadata and rs file"
lexnv Mar 27, 2023
64c316b
Merge remote-tracking branch 'origin/master' into lexnv/stirp_metadata
lexnv Mar 27, 2023
36bfe9e
ui-tests: Adjust path to metadata file
lexnv Mar 27, 2023
e90920c
ui-tests: Change drop order to keep `PalletMetadata` around
lexnv Mar 27, 2023
396cc7c
Update metadata/src/retain.rs
lexnv Mar 28, 2023
cc1809f
Address feedback
lexnv Mar 28, 2023
5a3c81d
retain: Keep extrinsic type
lexnv Mar 27, 2023
4a71546
cli: Introduce `MetadataSource`
lexnv Mar 28, 2023
a663236
cli: Use `MetadataSource` helper
lexnv Mar 28, 2023
d8b9824
cli: Use `FileOrUrl` flatten command argument
lexnv Mar 29, 2023
01ff1b5
retain: Do not include generic type params in retained metadata
lexnv Mar 29, 2023
e28bb8a
Adjust subxt to scale-info v2.5.0
lexnv Mar 29, 2023
2dee176
Update scaleinfo to v2.5.0
lexnv Mar 29, 2023
13cbbd6
Remove deprecated fn
lexnv Mar 29, 2023
115fbfb
testing: Fix clippy
lexnv Mar 29, 2023
6284f4a
benches: Use inner fields of scale info
lexnv Mar 29, 2023
314cb7b
Merge branch 'master' into lexnv/stirp_metadata
jsdw Apr 3, 2023
c62309f
Merge branch 'master' into lexnv/stirp_metadata
jsdw Apr 4, 2023
72e828d
address nits, and strip RuntimeCall type instead of trying to filter …
jsdw Apr 4, 2023
17d1976
fix UI test
jsdw Apr 4, 2023
e6e3016
move utils out of commands folder and fix clippy etc
jsdw Apr 4, 2023
98e3684
address nits
jsdw Apr 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions cli/src/commands/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
// see LICENSE for license details.

use clap::Parser as ClapParser;
use jsonrpsee::client_transport::ws::Uri;
use std::path::PathBuf;
use subxt_codegen::{DerivesRegistry, TypeSubstitutes};

use super::utils::MetadataSource;
use super::utils::FileOrUrl;

/// Generate runtime API client code from metadata.
///
Expand All @@ -16,12 +14,8 @@ use super::utils::MetadataSource;
/// `subxt codegen | rustfmt --edition=2018 --emit=stdout`
#[derive(Debug, ClapParser)]
pub struct Opts {
/// The url of the substrate node to query for metadata for codegen.
#[clap(name = "url", long, value_parser)]
url: Option<Uri>,
/// The path to the encoded metadata file.
#[clap(short, long, value_parser)]
file: Option<PathBuf>,
#[command(flatten)]
file_or_url: FileOrUrl,
/// Additional derives
#[clap(long = "derive")]
derives: Vec<String>,
Expand Down Expand Up @@ -53,8 +47,7 @@ fn derive_for_type_parser(src: &str) -> Result<(String, String), String> {
}

pub async fn run(opts: Opts) -> color_eyre::Result<()> {
let source = MetadataSource::new(opts.url, opts.file)?;
let bytes = source.fetch().await?;
let bytes = opts.file_or_url.fetch().await?;

codegen(
&bytes,
Expand Down
19 changes: 5 additions & 14 deletions cli/src/commands/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,17 @@
use clap::Parser as ClapParser;
use color_eyre::eyre;
use frame_metadata::{RuntimeMetadata, RuntimeMetadataPrefixed};
use jsonrpsee::client_transport::ws::Uri;
use scale::{Decode, Encode};
use std::{
io::{self, Write},
path::PathBuf,
};
use std::io::{self, Write};
use subxt_metadata::retain_metadata_pallets;

use super::utils::MetadataSource;
use super::utils::FileOrUrl;

/// Download metadata from a substrate node, for use with `subxt` codegen.
#[derive(Debug, ClapParser)]
pub struct Opts {
/// The url of the substrate node to query for metadata for codegen.
#[clap(name = "url", long, value_parser)]
url: Option<Uri>,
/// The path to the encoded metadata file.
#[clap(short, long, value_parser)]
file: Option<PathBuf>,
#[command(flatten)]
file_or_url: FileOrUrl,
/// The format of the metadata to display: `json`, `hex` or `bytes`.
#[clap(long, short, default_value = "bytes")]
format: String,
Expand All @@ -34,8 +26,7 @@ pub struct Opts {
}

pub async fn run(opts: Opts) -> color_eyre::Result<()> {
let source = MetadataSource::new(opts.url, opts.file)?;
let bytes = source.fetch().await?;
let bytes = opts.file_or_url.fetch().await?;
let mut metadata = <RuntimeMetadataPrefixed as Decode>::decode(&mut &bytes[..])?;

if let Some(pallets) = opts.pallets {
Expand Down
49 changes: 22 additions & 27 deletions cli/src/commands/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,42 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.

use clap::Args;
use std::{fs, io::Read, path::PathBuf};

use color_eyre::eyre;
use subxt_codegen::utils::Uri;

// The source of the metadata.
pub enum MetadataSource {
// Metadata is loaded from a file.
FilePath(PathBuf),
// Metadata is downloaded from a runtime node.
Uri(Uri),
/// The source of the metadata.
#[derive(Debug, Args)]
pub struct FileOrUrl {
/// The url of the substrate node to query for metadata for codegen.
#[clap(name = "url", long, value_parser)]
url: Option<Uri>,
/// The path to the encoded metadata file.
#[clap(short, long, value_parser)]
file: Option<PathBuf>,
}

impl MetadataSource {
/// Constructs a new [`MetadataSource`].
pub fn new(url: Option<Uri>, file: Option<PathBuf>) -> color_eyre::Result<Self> {
if let Some(file) = file {
if url.is_some() {
impl FileOrUrl {
/// Fetch the metadata bytes.
pub async fn fetch(&self) -> color_eyre::Result<Vec<u8>> {
if let Some(path) = &self.file {
if self.url.is_some() {
eyre::bail!("specify one of `--url` or `--file` but not both")
};

return Ok(Self::FilePath(file));
let mut file = fs::File::open(path)?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)?;
return Ok(bytes);
}
let url = url.unwrap_or_else(|| {

let url = self.url.clone().unwrap_or_else(|| {
"http://localhost:9933"
.parse::<Uri>()
.expect("default url is valid")
});
Ok(Self::Uri(url))
}

/// Fetch the metadata bytes.
pub async fn fetch(&self) -> color_eyre::Result<Vec<u8>> {
match &self {
Self::FilePath(path) => {
let mut file = fs::File::open(path)?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)?;
Ok(bytes)
}
Self::Uri(url) => Ok(subxt_codegen::utils::fetch_metadata_bytes(url).await?),
}
Ok(subxt_codegen::utils::fetch_metadata_bytes(&url).await?)
}
}