Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve contract experience #330

Merged
merged 7 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 22 additions & 4 deletions crates/pop-cli/src/commands/up/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,29 @@ impl UpContractCommand {
if !self.dry_run {
let spinner = spinner();
spinner.start("Uploading and instantiating the contract...");
let contract_address =
instantiate_smart_contract(instantiate_exec, weight_limit).await?;
let contract_info = instantiate_smart_contract(instantiate_exec, weight_limit).await?;
spinner.stop(format!(
"Contract deployed and instantiated: The Contract Address is {:?}",
contract_address
"Contract deployed and instantiated:\n{}",
style(format!(
"{}\n{}",
style(format!(
"{} The contract address is {:?}",
console::Emoji("●", ">"),
contract_info.address
))
.dim(),
contract_info
.code_hash
.map(|hash| style(format!(
"{} The contract code hash is {:?}",
console::Emoji("●", ">"),
hash
))
.dim()
.to_string())
.unwrap_or_default(),
))
.dim()
));
Self::terminate_node(process)?;
Cli.outro(COMPLETE)?;
Expand Down
6 changes: 3 additions & 3 deletions crates/pop-cli/tests/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async fn contract_lifecycle() -> Result<()> {
})
.await?;
let weight_limit = dry_run_gas_estimate_instantiate(&instantiate_exec).await?;
let contract_address = instantiate_smart_contract(instantiate_exec, weight_limit).await?;
let contract_info = instantiate_smart_contract(instantiate_exec, weight_limit).await?;
// Call contract (only query)
// pop call contract --contract $INSTANTIATED_CONTRACT_ADDRESS --message get --suri //Alice
Command::cargo_bin("pop")
Expand All @@ -96,7 +96,7 @@ async fn contract_lifecycle() -> Result<()> {
"call",
"contract",
"--contract",
&contract_address,
&contract_info.address,
"--message",
"get",
"--suri",
Expand All @@ -114,7 +114,7 @@ async fn contract_lifecycle() -> Result<()> {
"call",
"contract",
"--contract",
&contract_address,
&contract_info.address,
"--message",
"flip",
"--suri",
Expand Down
6 changes: 3 additions & 3 deletions crates/pop-contracts/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,11 @@ mod tests {
})
.await?;
let weight = dry_run_gas_estimate_instantiate(&instantiate_exec).await?;
let address = instantiate_smart_contract(instantiate_exec, weight).await?;
let contract_info = instantiate_smart_contract(instantiate_exec, weight).await?;
// Test querying a value.
let query_exec = set_up_call(CallOpts {
path: Some(temp_dir.path().join("testing")),
contract: address.clone(),
contract: contract_info.address.clone(),
message: "get".to_string(),
args: [].to_vec(),
value: "0".to_string(),
Expand All @@ -346,7 +346,7 @@ mod tests {
// Test extrinsic execution by flipping the value.
let call_exec = set_up_call(CallOpts {
path: Some(temp_dir.path().join("testing")),
contract: address,
contract: contract_info.address,
message: "flip".to_string(),
args: [].to_vec(),
value: "0".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion crates/pop-contracts/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub async fn run_contracts_node(
output: Option<&File>,
) -> Result<Child, Error> {
let mut command = Command::new(binary_path);

command.arg("-linfo,runtime::contracts=debug");
if let Some(output) = output {
command.stdout(Stdio::from(output.try_clone()?));
command.stderr(Stdio::from(output.try_clone()?));
Expand Down
20 changes: 16 additions & 4 deletions crates/pop-contracts/src/up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ pub async fn dry_run_upload(
}
}

/// Type to represent information about a deployed smart contract.
pub struct ContractInfo {
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
/// The on-chain address of the deployed contract.
pub address: String,
/// The hash of the contract's code
pub code_hash: Option<String>,
}

/// Instantiate a contract.
///
/// # Arguments
Expand All @@ -166,12 +174,15 @@ pub async fn dry_run_upload(
pub async fn instantiate_smart_contract(
instantiate_exec: InstantiateExec<DefaultConfig, DefaultEnvironment, Keypair>,
gas_limit: Weight,
) -> anyhow::Result<String, Error> {
) -> anyhow::Result<ContractInfo, Error> {
let instantiate_result = instantiate_exec
.instantiate(Some(gas_limit))
.await
.map_err(|error_variant| Error::InstantiateContractError(format!("{:?}", error_variant)))?;
Ok(instantiate_result.contract_address.to_string())
// If is upload + instantiate, return the code hash.
let hash = instantiate_result.code_hash.map(|code_hash| format!("{:?}", code_hash));

Ok(ContractInfo { address: instantiate_result.contract_address.to_string(), code_hash: hash })
}

/// Upload a contract.
Expand Down Expand Up @@ -391,8 +402,9 @@ mod tests {
assert!(weight.ref_time() > 0);
assert!(weight.proof_size() > 0);
// Instantiate smart contract
let address = instantiate_smart_contract(instantiate_exec, weight).await?;
assert!(address.starts_with("5"));
let contract_info = instantiate_smart_contract(instantiate_exec, weight).await?;
assert!(contract_info.address.starts_with("5"));
assert!(contract_info.code_hash.is_none());
// Stop the process contracts-node
Command::new("kill")
.args(["-s", "TERM", &process.id().to_string()])
Expand Down
Loading