Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `verify` command - [#1306](https://github.com/paritytech/cargo-contract/pull/1306)
- Add `--binary` flag for `info` command - [#1311](https://github.com/paritytech/cargo-contract/pull/1311/)
- Add `--all` flag for `info` command - [#1319](https://github.com/paritytech/cargo-contract/pull/1319)
- Fix for a Url to String conversion in `info` command - [#1330](https://github.com/paritytech/cargo-contract/pull/1330)

## [4.0.0-alpha]

Expand Down
4 changes: 3 additions & 1 deletion crates/cargo-contract/src/cmd/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use contract_extrinsics::{
fetch_all_contracts,
fetch_contract_info,
fetch_wasm_code,
url_to_string,
ErrorVariant,
};
use std::{
Expand Down Expand Up @@ -70,7 +71,8 @@ pub struct InfoCommand {

impl InfoCommand {
pub async fn run(&self) -> Result<(), ErrorVariant> {
let client = OnlineClient::<DefaultConfig>::from_url(&self.url).await?;
let client =
OnlineClient::<DefaultConfig>::from_url(url_to_string(&self.url)).await?;

// All flag applied
if self.all {
Expand Down
5 changes: 3 additions & 2 deletions crates/extrinsics/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use super::{
state,
state_call,
submit_extrinsic,
url_to_string,
AccountId32,
Balance,
BalanceVariant,
Expand Down Expand Up @@ -178,7 +179,7 @@ impl CallCommandBuilder<state::Message, state::ExtrinsicOptions> {

let signer = self.opts.extrinsic_opts.signer()?;

let url = self.opts.extrinsic_opts.url_to_string();
let url = url_to_string(self.opts.extrinsic_opts.url());
let client = OnlineClient::from_url(url.clone()).await?;
Ok(CallExec {
contract: self.opts.contract.clone(),
Expand Down Expand Up @@ -221,7 +222,7 @@ impl CallExec {
/// Returns the dry run simulation result of type [`ContractExecResult`], which
/// includes information about the simulated call, or an error in case of failure.
pub async fn call_dry_run(&self) -> Result<ContractExecResult<Balance, ()>> {
let url = self.opts.url_to_string();
let url = url_to_string(self.opts.url());
let token_metadata = TokenMetadata::query(&self.client).await?;
let storage_deposit_limit = self
.opts
Expand Down
12 changes: 0 additions & 12 deletions crates/extrinsics/src/extrinsic_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,6 @@ impl ExtrinsicOpts {
Ok(keypair)
}

/// Convert URL to String without omitting the default port
pub fn url_to_string(&self) -> String {
let mut res = self.url.to_string();
match (self.url.port(), self.url.port_or_known_default()) {
(None, Some(port)) => {
res.insert_str(res.len() - 1, &format!(":{port}"));
res
}
_ => res,
}
}

/// Return the file path of the contract artifact.
pub fn file(&self) -> Option<&PathBuf> {
self.file.as_ref()
Expand Down
3 changes: 2 additions & 1 deletion crates/extrinsics/src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use super::{
state,
state_call,
submit_extrinsic,
url_to_string,
Balance,
BalanceVariant,
Client,
Expand Down Expand Up @@ -165,7 +166,7 @@ impl InstantiateCommandBuilder<state::ExtrinsicOptions> {
let transcoder = artifacts.contract_transcoder()?;
let data = transcoder.encode(&self.opts.constructor, &self.opts.args)?;
let signer = self.opts.extrinsic_opts.signer()?;
let url = self.opts.extrinsic_opts.url_to_string();
let url = url_to_string(self.opts.extrinsic_opts.url());
let code = if let Some(code) = artifacts.code {
Code::Upload(code.0)
} else {
Expand Down
38 changes: 38 additions & 0 deletions crates/extrinsics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,21 @@ pub async fn fetch_all_contracts(
Ok(contracts)
}

// Converts a Url into a String representation without excluding the default port.
pub fn url_to_string(url: &url::Url) -> String {
match (url.port(), url.port_or_known_default()) {
(None, Some(port)) => {
format!(
"{}:{port}{}",
&url[..url::Position::AfterHost],
&url[url::Position::BeforePath..]
)
.to_string()
}
_ => url.to_string(),
}
}

/// Copy of `pallet_contracts_primitives::StorageDeposit` which implements `Serialize`,
/// required for json output.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, serde::Serialize)]
Expand Down Expand Up @@ -473,4 +488,27 @@ mod tests {
)
.is_ok())
}

#[test]
fn url_to_string_works() {
// with custom port
let url = url::Url::parse("ws://127.0.0.1:9944").unwrap();
assert_eq!(url_to_string(&url), "ws://127.0.0.1:9944/");

// with default port
let url = url::Url::parse("wss://127.0.0.1:443").unwrap();
assert_eq!(url_to_string(&url), "wss://127.0.0.1:443/");

// with default port and path
let url = url::Url::parse("wss://127.0.0.1:443/test/1").unwrap();
assert_eq!(url_to_string(&url), "wss://127.0.0.1:443/test/1");

// with default port and domain
let url = url::Url::parse("wss://test.io:443").unwrap();
assert_eq!(url_to_string(&url), "wss://test.io:443/");

// with default port, domain and path
let url = url::Url::parse("wss://test.io/test/1").unwrap();
assert_eq!(url_to_string(&url), "wss://test.io:443/test/1");
}
}
3 changes: 2 additions & 1 deletion crates/extrinsics/src/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use super::{
},
state,
submit_extrinsic,
url_to_string,
Client,
ContractMessageTranscoder,
DefaultConfig,
Expand Down Expand Up @@ -118,7 +119,7 @@ impl RemoveCommandBuilder<state::ExtrinsicOptions> {
artifacts_path.display()
)),
}?;
let url = self.opts.extrinsic_opts.url_to_string();
let url = url_to_string(self.opts.extrinsic_opts.url());
let client = OnlineClient::from_url(url.clone()).await?;

Ok(RemoveExec {
Expand Down
5 changes: 3 additions & 2 deletions crates/extrinsics/src/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use super::{
state,
state_call,
submit_extrinsic,
url_to_string,
Balance,
Client,
CodeHash,
Expand Down Expand Up @@ -105,7 +106,7 @@ impl UploadCommandBuilder<state::ExtrinsicOptions> {
artifacts_path.display()
)
})?;
let url = self.opts.extrinsic_opts.url_to_string();
let url = url_to_string(self.opts.extrinsic_opts.url());
let client = OnlineClient::from_url(url.clone()).await?;
Ok(UploadExec {
opts: self.opts.extrinsic_opts.clone(),
Expand All @@ -131,7 +132,7 @@ impl UploadExec {
/// then sends the request using the provided URL. This operation does not modify
/// the state of the blockchain.
pub async fn upload_code_rpc(&self) -> Result<CodeUploadResult<CodeHash, Balance>> {
let url = self.opts.url_to_string();
let url = url_to_string(self.opts.url());
let token_metadata = TokenMetadata::query(&self.client).await?;
let storage_deposit_limit = self
.opts
Expand Down