Skip to content

Commit

Permalink
refactor: docs and remove unneded functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexD10S committed Dec 13, 2024
1 parent 73126fe commit 44ad374
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
29 changes: 16 additions & 13 deletions crates/pop-cli/src/commands/call/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use crate::{
use anyhow::{anyhow, Result};
use clap::Args;
use pop_parachains::{
call_data, construct_extrinsic, construct_sudo_extrinsic, decode_call_data, encode_call_data,
construct_extrinsic, construct_sudo_extrinsic, decode_call_data, encode_call_data,
find_dispatchable_by_name, find_pallet_by_name, parse_chain_metadata, set_up_client,
sign_and_submit_extrinsic, sign_and_submit_extrinsic_with_call_data, submit_signed_extrinsic,
supported_actions, Action, DynamicPayload, Function, OnlineClient, Pallet, Param,
supported_actions, Action, DynamicPayload, Function, OnlineClient, Pallet, Param, Payload,
SubstrateConfig,
};
use url::Url;
Expand Down Expand Up @@ -97,8 +97,10 @@ impl CallChainCommand {

if self.use_wallet {
// Sign and submit the extrinsic.
let call_data = call_data(&chain.client, &xt)?;
if let Err(e) = submit_extrinsic_secure_signing(&chain, call_data, &mut cli).await {
let call_data = xt.encode_call_data(&chain.client.metadata())?;
if let Err(e) =
submit_extrinsic_with_secure_signing(&chain, call_data, &mut cli).await
{
display_message(&e.to_string(), false, &mut cli)?;
break;
}
Expand Down Expand Up @@ -211,20 +213,21 @@ impl CallChainCommand {
// skip the prompt.
let suri = match self.suri.as_ref() {
Some(suri) => suri.clone(),
None =>
None => {
if !self.use_wallet {
if cli.confirm("Do you want to use your browser wallet to sign the transaction? (Selecting 'No' will prompt you to manually enter the secret key URI for signing, e.g., '//Alice')")
.initial_value(true)
.interact()? {
self.use_wallet = true;
DEFAULT_URI.to_string()
DEFAULT_URI.to_string() // Default value because the user is using the browser wallet.
}
else {
cli.input("Signer of the extrinsic:").default_input(DEFAULT_URI).interact()?
}
} else {
DEFAULT_URI.to_string()
},
DEFAULT_URI.to_string() // Default value because the user is using the browser wallet.
}
},
};

return Ok(Call {
Expand Down Expand Up @@ -265,11 +268,11 @@ impl CallChainCommand {
DEFAULT_URI.to_string()
},
};
// Return early
// Perform signing steps with wallet integration and return early.
if use_wallet {
let call_data_bytes =
decode_call_data(call_data).map_err(|err| anyhow!("{}", format!("{err:?}")))?;
submit_extrinsic_secure_signing(chain, call_data_bytes, cli)
submit_extrinsic_with_secure_signing(chain, call_data_bytes, cli)
.await
.map_err(|err| anyhow!("{}", format!("{err:?}")))?;
display_message("Call complete.", true, cli)?;
Expand Down Expand Up @@ -476,8 +479,8 @@ impl Call {
}
}

// Sign and submit an extrinsic.
async fn submit_extrinsic_secure_signing(
// Sign and submit an extrinsic using wallet integration.
async fn submit_extrinsic_with_secure_signing(
chain: &Chain,
call_data: Vec<u8>,
cli: &mut impl Cli,
Expand All @@ -496,7 +499,7 @@ async fn submit_extrinsic_secure_signing(

spinner.stop(format!("Extrinsic submitted with hash: {:?}", result));
} else {
display_message("Signed payload doesn't exist.", false, cli)?;
display_message("No signed payload received.", false, cli)?;
}
Ok(())
}
Expand Down
20 changes: 3 additions & 17 deletions crates/pop-parachains/src/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ pub async fn sign_and_submit_extrinsic(
Ok(format!("{:?}", result.extrinsic_hash()))
}

/// TODO: Signs and submits a given extrinsic.
/// Submits a signed extrinsic.
///
/// # Arguments
/// * `client` - The client used to interact with the chain.
/// * `xt` - The extrinsic to be signed and submitted.
/// * `suri` - The secret URI (e.g., mnemonic or private key) for signing the extrinsic.
/// * `payload` - The signed payload string to be submitted.
pub async fn submit_signed_extrinsic(
client: OnlineClient<SubstrateConfig>,
payload: String,
Expand All @@ -87,6 +86,7 @@ pub async fn submit_signed_extrinsic(
.map_err(|e| Error::ExtrinsicSubmissionError(format!("{:?}", e)))?;
Ok(format!("{:?}", result.extrinsic_hash()))
}

/// Encodes the call data for a given extrinsic into a hexadecimal string.
///
/// # Arguments
Expand All @@ -102,20 +102,6 @@ pub fn encode_call_data(
Ok(format!("0x{}", hex::encode(call_data)))
}

/// Encodes the call data for a given extrinsic into a hexadecimal string.
///
/// # Arguments
/// * `client` - The client used to interact with the chain.
/// * `xt` - The extrinsic whose call data will be encoded and returned.
pub fn call_data(
client: &OnlineClient<SubstrateConfig>,
xt: &DynamicPayload,
) -> Result<Vec<u8>, Error> {
Ok(xt
.encode_call_data(&client.metadata())
.map_err(|e| Error::CallDataEncodingError(e.to_string()))?)
}

/// Decodes a hex-encoded string into a vector of bytes representing the call data.
///
/// # Arguments
Expand Down
7 changes: 5 additions & 2 deletions crates/pop-parachains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use build::{
generate_plain_chain_spec, generate_raw_chain_spec, is_supported, ChainSpec,
};
pub use call::{
call_data, construct_extrinsic, construct_sudo_extrinsic, decode_call_data, encode_call_data,
construct_extrinsic, construct_sudo_extrinsic, decode_call_data, encode_call_data,
metadata::{
action::{supported_actions, Action},
find_dispatchable_by_name, find_pallet_by_name,
Expand All @@ -32,7 +32,10 @@ pub use indexmap::IndexSet;
pub use new_pallet::{create_pallet_template, new_pallet_options::*, TemplatePalletConfig};
pub use new_parachain::instantiate_template_dir;
// External export from subxt.
pub use subxt::{tx::DynamicPayload, OnlineClient, SubstrateConfig};
pub use subxt::{
tx::{DynamicPayload, Payload},
OnlineClient, SubstrateConfig,
};
pub use templates::{Config, Parachain, Provider};
pub use up::Zombienet;
pub use utils::helpers::is_initial_endowment_valid;
Expand Down

0 comments on commit 44ad374

Please sign in to comment.