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

feat(new): improve new contract with a ContractType for categorization #220

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ cargo test
Pop CLI would not be possible without these awesome crates!

- Local network deployment powered by [zombienet-sdk](https://github.com/paritytech/zombienet-sdk)
- [cargo contract](https://github.com/paritytech/cargo-contract) a setup and deployment tool for developing Wasm based
- [cargo contract](https://github.com/use-ink/cargo-contract) a setup and deployment tool for developing Wasm based
Smart Contracts via ink!

## License
Expand Down
62 changes: 54 additions & 8 deletions crates/pop-cli/src/commands/new/contract.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
// SPDX-License-Identifier: GPL-3.0

use crate::style::Theme;
use anyhow::Result;
use clap::{
builder::{PossibleValue, PossibleValuesParser, TypedValueParser},
Args,
};
use cliclack::{clear_screen, confirm, input, intro, log::success, outro, outro_cancel, set_theme};
use console::style;
use pop_contracts::{create_smart_contract, is_valid_contract_name, Template};
use pop_contracts::{create_smart_contract, is_valid_contract_name, ContractType, Template};
use std::{env::current_dir, fs, path::PathBuf, str::FromStr};
use strum::VariantArray;

#[derive(Args, Clone)]
pub struct NewContractCommand {
#[arg(help = "Name of the contract")]
pub(crate) name: Option<String>,
#[arg(
help = "Contract type.",
default_value = ContractType::Examples.as_ref(),
value_parser = crate::enum_variants!(ContractType)
)]
pub(crate) contract_type: Option<ContractType>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I received feedback on a pending PR suggesting that we should make the flags shorter. I like it, do you think makes sense to call it only type? #209 (comment)

Copy link
Contributor Author

@peterwht peterwht Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, type is a reserved keyword in Rust. Could maybe do category instead? Could also be c_type, but that's not super clear

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep it contract_type then, c_type is a bit confusing

#[arg(short = 'p', long, help = "Path for the contract project, [default: current directory]")]
pub(crate) path: Option<PathBuf>,
#[arg(
Expand Down Expand Up @@ -42,15 +49,29 @@ impl NewContractCommand {
.clone()
.expect("name can not be none as fallback above is interactive input; qed");
is_valid_contract_name(name)?;
let contract_type = &contract_config.contract_type.clone().unwrap_or_default();
let template = match &contract_config.template {
Some(template) => template.clone(),
None => Template::Standard, // Default template
None => contract_type.default_type(), // Default contract type
};
generate_contract_from_template(name, contract_config.path, &template)?;

is_template_supported(contract_type, &template)?;

generate_contract_from_template(name, contract_config.path, &contract_type, &template)?;
Ok(())
}
}

fn is_template_supported(contract_type: &ContractType, template: &Template) -> Result<()> {
if !template.matches(contract_type) {
return Err(anyhow::anyhow!(format!(
"The contract type \"{:?}\" doesn't support the {:?} template.",
contract_type, template
)));
};
return Ok(());
}

async fn guide_user_to_generate_contract() -> anyhow::Result<NewContractCommand> {
intro(format!("{}: Generate a contract", style(" Pop CLI ").black().on_magenta()))?;
let name: String = input("Name of your contract?")
Expand All @@ -61,25 +82,50 @@ async fn guide_user_to_generate_contract() -> anyhow::Result<NewContractCommand>
.placeholder("./")
.default_input("./")
.interact()?;
let mut prompt = cliclack::select("Select a template provider: ".to_string());
for (i, template) in Template::templates().iter().enumerate() {

let mut contract_type_prompt = cliclack::select("Select a contract type: ".to_string());
for (i, contract_type) in ContractType::types().iter().enumerate() {
if i == 0 {
prompt = prompt.initial_value(template);
contract_type_prompt = contract_type_prompt.initial_value(contract_type);
}
prompt = prompt.item(template, template.name(), format!("{}", template.description(),));
contract_type_prompt = contract_type_prompt.item(
contract_type,
contract_type.name(),
format!(
"{} {} available option(s)",
contract_type.description(),
contract_type.templates().len(),
),
);
}
let template = prompt.interact()?;
let contract_type = contract_type_prompt.interact()?;

let template = display_select_options(contract_type)?;

clear_screen()?;
Ok(NewContractCommand {
name: Some(name),
path: Some(PathBuf::from(path)),
contract_type: Some(contract_type.clone()),
template: Some(template.clone()),
})
}

fn display_select_options(contract_type: &ContractType) -> Result<&Template> {
let mut prompt = cliclack::select("Select the contract:".to_string());
for (i, template) in contract_type.templates().into_iter().enumerate() {
if i == 0 {
prompt = prompt.initial_value(template);
}
prompt = prompt.item(template, template.name(), template.description());
}
Ok(prompt.interact()?)
}

fn generate_contract_from_template(
name: &String,
path: Option<PathBuf>,
contract_type: &ContractType,
template: &Template,
) -> anyhow::Result<()> {
intro(format!(
Expand Down
2 changes: 1 addition & 1 deletion crates/pop-contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let template = Template::Standard;

let name = '...';
let contract_path = ...;
create_smart_contract(name, &contract_path, template)?;
create_smart_contract(name, &contract_path, &template)?;
```

Build an existing Smart Contract:
Expand Down
2 changes: 1 addition & 1 deletion crates/pop-contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use call::{
call_smart_contract, dry_run_call, dry_run_gas_estimate_call, set_up_call, CallOpts,
};
pub use new::{create_smart_contract, is_valid_contract_name};
pub use templates::Template;
pub use templates::{ContractType, Template};
pub use test::{test_e2e_smart_contract, test_smart_contract};
pub use up::{
dry_run_gas_estimate_instantiate, instantiate_smart_contract, set_up_deployment, UpOpts,
Expand Down
106 changes: 102 additions & 4 deletions crates/pop-contracts/src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,61 @@ use strum::{
};
use strum_macros::{AsRefStr, Display, EnumMessage, EnumProperty, EnumString, VariantArray};

/// Supported template providers.
#[derive(
AsRefStr, Clone, Default, Debug, Display, EnumMessage, EnumString, Eq, PartialEq, VariantArray,
)]
pub enum ContractType {
#[default]
#[strum(
ascii_case_insensitive,
serialize = "examples",
message = "Examples",
detailed_message = "Contract examples for ink!."
)]
Examples,
#[strum(
ascii_case_insensitive,
serialize = "erc",
message = "ERC",
detailed_message = "ERC-based contracts in ink!."
)]
Erc,
}

impl ContractType {
/// Get the list of providers supported.
pub fn types() -> &'static [ContractType] {
ContractType::VARIANTS
}

/// Get provider's name.
pub fn name(&self) -> &str {
self.get_message().unwrap_or_default()
}

/// Get the default template of the provider.
pub fn default_type(&self) -> Template {
match &self {
ContractType::Examples => Template::Standard,
ContractType::Erc => Template::ERC20,
}
}

/// Get the providers detailed description message.
pub fn description(&self) -> &str {
self.get_detailed_message().unwrap_or_default()
}

/// Get the list of templates of the provider.
pub fn templates(&self) -> Vec<&Template> {
Template::VARIANTS
.iter()
.filter(|t| t.get_str("ContractType") == Some(self.name()))
.collect()
}
}

#[derive(
AsRefStr,
Clone,
Expand All @@ -26,31 +81,32 @@ pub enum Template {
#[strum(
serialize = "standard",
message = "Standard",
detailed_message = "ink!'s 'Hello World': Flipper"
detailed_message = "ink!'s 'Hello World': Flipper",
props(ContractType = "Examples")
)]
Standard,
/// The implementation of the ERC-20 standard in ink!
#[strum(
serialize = "erc20",
message = "Erc20",
detailed_message = "The implementation of the ERC-20 standard in ink!",
props(Repository = "https://github.com/use-ink/ink-examples")
props(ContractType = "ERC", Repository = "https://github.com/use-ink/ink-examples")
)]
ERC20,
/// The implementation of the ERC-721 standard in ink!
#[strum(
serialize = "erc721",
message = "Erc721",
detailed_message = "The implementation of the ERC-721 standard in ink!",
props(Repository = "https://github.com/use-ink/ink-examples")
props(ContractType = "ERC", Repository = "https://github.com/use-ink/ink-examples")
)]
ERC721,
/// The implementation of the ERC-1155 standard in ink!
#[strum(
serialize = "erc1155",
message = "Erc1155",
detailed_message = "The implementation of the ERC-1155 standard in ink!",
props(Repository = "https://github.com/use-ink/ink-examples")
props(ContractType = "ERC", Repository = "https://github.com/use-ink/ink-examples")
)]
ERC1155,
}
Expand All @@ -74,6 +130,12 @@ impl Template {
pub fn templates() -> &'static [Template] {
Template::VARIANTS
}

/// Check the template belongs to a `provider`.
pub fn matches(&self, contract_type: &ContractType) -> bool {
// Match explicitly on provider name (message)
self.get_str("ContractType") == Some(contract_type.name())
}
}

#[cfg(test)]
Expand Down Expand Up @@ -107,6 +169,20 @@ mod tests {
])
}

#[test]
fn test_is_template_correct() {
for template in Template::VARIANTS {
if matches!(template, Template::Standard) {
assert_eq!(template.matches(&ContractType::Examples), true);
assert_eq!(template.matches(&ContractType::Erc), false);
}
if matches!(template, Template::ERC20 | Template::ERC721 | Template::ERC1155) {
assert_eq!(template.matches(&ContractType::Examples), false);
assert_eq!(template.matches(&ContractType::Erc), true);
}
}
}

#[test]
fn test_convert_string_to_template() {
let template_names = templates_names();
Expand Down Expand Up @@ -143,4 +219,26 @@ mod tests {
assert_eq!(template.description(), templates_description[template]);
}
}

#[test]
fn test_default_template_of_type() {
let mut contract_type = ContractType::Examples;
assert_eq!(contract_type.default_type(), Template::Standard);
contract_type = ContractType::Erc;
assert_eq!(contract_type.default_type(), Template::ERC20);
}

#[test]
fn test_templates_of_type() {
let mut provider = ContractType::Examples;
assert_eq!(provider.templates(), [&Template::Standard]);
provider = ContractType::Erc;
assert_eq!(provider.templates(), [&Template::ERC20, &Template::ERC721, &Template::ERC1155]);
}

#[test]
fn test_convert_string_to_type() {
assert_eq!(ContractType::from_str("Examples").unwrap(), ContractType::Examples);
assert_eq!(ContractType::from_str("Erc").unwrap_or_default(), ContractType::Erc);
}
}