Skip to content

Commit

Permalink
feat: get para_id from event
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexD10S committed Feb 17, 2025
1 parent e92afde commit e8a05fa
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ subxt = "0.38.0"
ink_env = "5.0.0"
sp-core = "32.0.0"
sp-weights = "31.0.0"
scale = { package = "parity-scale-codec", version = "3.0.0", features = ["derive"] }
scale-info = { version = "2.11.4", default-features = false, features = ["derive"] }
scale-value = { version = "0.17.0", default-features = false, features = ["from-string", "parser-ss58"] }
contract-build = "5.0.2"
Expand Down
24 changes: 15 additions & 9 deletions crates/pop-cli/src/commands/up/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::{
use anyhow::{anyhow, Result};
use clap::Args;
use pop_parachains::{
construct_extrinsic, find_dispatchable_by_name, parse_chain_metadata, set_up_client, Action,
Payload,
construct_extrinsic, extract_para_id_from_event, find_dispatchable_by_name,
parse_chain_metadata, set_up_client, Action, Payload,
};

use std::path::PathBuf;
Expand Down Expand Up @@ -50,8 +50,8 @@ impl UpParachainCommand {
match reserve_para_id(&chain, cli).await {
Ok(id) => id,
Err(e) => {
cli.outro_cancel(&format!("Failed to reserve parachain ID: {}", e))?;
return Err(e);
cli.outro_cancel(&format!("{}", e))?;
return Ok(());
},
}
},
Expand All @@ -60,12 +60,12 @@ impl UpParachainCommand {
match (self.genesis_state.clone(), self.genesis_code.clone()) {
(Some(state), Some(code)) => (state, code),
_ => {
cli.info("Generating the chain spec for your parachain, some extra information is needed:")?;
cli.info("Generating the chain spec for your parachain.")?;
match generate_spec_files(para_id, self.path, cli).await {
Ok(files) => files,
Err(e) => {
cli.outro_cancel(&format!("Failed to generate spec files: {}", e))?;
return Err(e);
return Ok(());
},
}
},
Expand All @@ -74,7 +74,7 @@ impl UpParachainCommand {
if let Err(e) = register_parachain(&chain, para_id, genesis_state, genesis_code, cli).await
{
cli.outro_cancel(&format!("Failed to register parachain: {}", e))?;
return Err(e);
return Ok(());
}

cli.outro("Parachain deployment complete.")?;
Expand Down Expand Up @@ -108,8 +108,14 @@ impl UpParachainCommand {
/// Reserves a parachain ID by submitting an extrinsic.
async fn reserve_para_id(chain: &Chain, cli: &mut impl Cli) -> Result<u32> {
let call_data = prepare_reserve_para_id_extrinsic(chain)?;
let events = submit_extrinsic_with_wallet(&chain.client, &chain.url, call_data, cli).await?;
Ok(2000)
let events = submit_extrinsic_with_wallet(&chain.client.clone(), &chain.url, call_data, cli)
.await
.map_err(|e| anyhow::anyhow!("Parachain ID reservation failed: {}", e))?;
let para_id = extract_para_id_from_event(&events).await.map_err(|_| {
anyhow::anyhow!("Unable to parse the event. Specify the parachain ID manually with --id.")
})?;
cli.success(format!("Successfully reserved parachain ID: {}", para_id))?;
Ok(para_id)
}

/// Constructs an extrinsic for reserving a parachain ID.
Expand Down
1 change: 1 addition & 0 deletions crates/pop-parachains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ url.workspace = true

askama.workspace = true
indexmap.workspace = true
scale.workspace = true
scale-info.workspace = true
scale-value.workspace = true
sp-core.workspace = true
Expand Down
35 changes: 35 additions & 0 deletions crates/pop-parachains/src/call/metadata/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: GPL-3.0

use scale::{Decode, Encode};
use subxt::{
blocks::ExtrinsicEvents,
events::StaticEvent,
ext::{scale_decode::DecodeAsType, scale_encode::EncodeAsType},
SubstrateConfig,
};

use crate::Error;

#[derive(Debug, Encode, Decode, DecodeAsType, EncodeAsType)]
#[decode_as_type(crate_path = "subxt::ext::scale_decode")]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
pub struct Reserved {
pub para_id: u32,
}
impl StaticEvent for Reserved {
const PALLET: &'static str = "Registrar";
const EVENT: &'static str = "Reserved";
}

/// Extracts the `para_id` field from a `Reserved` event.
///
/// # Arguments
/// * `events` - The extrinsic events from a transaction.
pub async fn extract_para_id_from_event(
events: &ExtrinsicEvents<SubstrateConfig>,
) -> Result<u32, Error> {
let reserved_event = events.find_first::<Reserved>()?;
reserved_event
.map(|event| event.para_id)
.ok_or(Error::EventNotFound("Reserved".to_string()))
}
1 change: 1 addition & 0 deletions crates/pop-parachains/src/call/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::fmt::{Display, Formatter};
use subxt::{dynamic::Value, utils::to_hex, Metadata, OnlineClient, SubstrateConfig};

pub mod action;
pub mod events;
pub mod params;

/// Represents a pallet in the blockchain, including its dispatchable functions.
Expand Down
3 changes: 3 additions & 0 deletions crates/pop-parachains/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub enum Error {
CurrentDirAccess,
#[error("Failed to parse the endowment value")]
EndowmentError,
/// The specified event was not found in the extrinsic events.
#[error("Event {0} not found.")]
EventNotFound(String),
/// An error occurred during the submission of an extrinsic.
#[error("Extrinsic submission error: {0}")]
ExtrinsicSubmissionError(String),
Expand Down
1 change: 1 addition & 0 deletions crates/pop-parachains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub use call::{
construct_extrinsic, construct_sudo_extrinsic, decode_call_data, encode_call_data,
metadata::{
action::{supported_actions, Action},
events::extract_para_id_from_event,
find_dispatchable_by_name, find_pallet_by_name,
params::Param,
parse_chain_metadata, Function, Pallet,
Expand Down

0 comments on commit e8a05fa

Please sign in to comment.