Skip to content

Commit

Permalink
feat(cli): integrate contracts and evm parachain template. (#137)
Browse files Browse the repository at this point in the history
* feat(cli): nfts template

* style(cli): assets parachain template

* style(cli): contracts parachain description

* fix(cli): ammend template tests

* fix(cli): ammend template tests

* fix(cli): ammend template url

* fix(cli): point to new assets template repo

* style(cli): fmt

* feat: add contracts and evm

* refactor: sort templates and renaming

* refactor: templates tests

* refactor: rename base to standard

* refactor: tests

* refactor: renaming base methods

* chore: fmt

* docs: add new parachains in the readme

* docs: alphabetically order for templates

* fix: merge

* fix: customizable flags in user guiding for pop templates

---------

Co-authored-by: Alejandro Martinez Andres <[email protected]>
  • Loading branch information
AlexD10S and al3mart authored May 10, 2024
1 parent 2b5a86f commit 738e0d6
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 60 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ If no guidance is needed, proceed with:
pop new parachain my-app
```

`pop-cli` supports diverse project templates, to use a specific one use the flag `--template`:
```sh
# Create an assets parachain
pop new parachain my-app pop -t assets
# Create a contracts parachain
pop new parachain my-app pop -t contracts
# Create a evm parachain
pop new parachain my-app pop -t evm
```

We also integrate other provider templates in the tool, check them running:

```sh
Expand Down
12 changes: 6 additions & 6 deletions crates/pop-cli/src/commands/new/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async fn guide_user_to_generate_parachain() -> Result<()> {
decimals: 12,
initial_endowment: "1u64 << 60".to_string(),
};
if matches!(template, Template::Base) {
if template.matches(&Provider::Pop) {
customizable_options = prompt_customizable_options()?;
}

Expand Down Expand Up @@ -215,7 +215,7 @@ fn get_customization_value(
decimals: Option<u8>,
initial_endowment: Option<String>,
) -> Result<Config> {
if !matches!(template, Template::Base)
if !matches!(template, Template::Standard)
&& (symbol.is_some() || decimals.is_some() || initial_endowment.is_some())
{
log::warning("Customization options are not available for this template")?;
Expand Down Expand Up @@ -329,7 +329,7 @@ mod tests {
let command = NewParachainCommand {
name: Some(dir.path().join("test_parachain").to_str().unwrap().to_string()),
provider: Some(Provider::Pop),
template: Some(Template::Base),
template: Some(Template::Standard),
symbol: Some("UNIT".to_string()),
decimals: Some(12),
initial_endowment: Some("1u64 << 60".to_string()),
Expand All @@ -347,19 +347,19 @@ mod tests {

#[test]
fn test_is_template_supported() -> Result<()> {
is_template_supported(&Provider::Pop, &Template::Base)?;
is_template_supported(&Provider::Pop, &Template::Standard)?;
assert!(is_template_supported(&Provider::Pop, &Template::ParityContracts).is_err());
assert!(is_template_supported(&Provider::Pop, &Template::ParityFPT).is_err());

assert!(is_template_supported(&Provider::Parity, &Template::Base).is_err());
assert!(is_template_supported(&Provider::Parity, &Template::Standard).is_err());
is_template_supported(&Provider::Parity, &Template::ParityContracts)?;
is_template_supported(&Provider::Parity, &Template::ParityFPT)
}

#[test]
fn test_get_customization_values() -> Result<()> {
let config = get_customization_value(
&Template::Base,
&Template::Standard,
Some("DOT".to_string()),
Some(6),
Some("10000".to_string()),
Expand Down
2 changes: 1 addition & 1 deletion crates/pop-parachains/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Generate a new Parachain:
```rust
use pop_parachains::{instantiate_template_dir, Config, Git, Template};

let template = Template::Base;
let template = Template::Standard;
let destination_path = ...;
let config = Config {
symbol: ...,
Expand Down
14 changes: 7 additions & 7 deletions crates/pop-parachains/src/new_parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
git::Git,
helpers::{sanitize, write_to_file},
},
Config, Template,
Config, Provider, Template,
};
use anyhow::Result;
use std::{fs, path::Path};
Expand All @@ -21,21 +21,21 @@ pub fn instantiate_template_dir(
) -> Result<Option<String>> {
sanitize(target)?;

if matches!(template, &Template::Base) {
return instantiate_base_template(target, config, tag_version);
if template.matches(&Provider::Pop) {
return instantiate_standard_template(template, target, config, tag_version);
}
let tag = Git::clone_and_degit(template.repository_url()?, target, tag_version)?;
Ok(tag)
}

pub fn instantiate_base_template(
pub fn instantiate_standard_template(
template: &Template,
target: &Path,
config: Config,
tag_version: Option<String>,
) -> Result<Option<String>> {
let temp_dir = ::tempfile::TempDir::new_in(std::env::temp_dir())?;
let source = temp_dir.path();
let template = crate::templates::Template::Base;

let tag = Git::clone_and_degit(template.repository_url()?, source, tag_version)?;

Expand Down Expand Up @@ -80,12 +80,12 @@ mod tests {
decimals: 18,
initial_endowment: "1000000".to_string(),
};
instantiate_base_template(temp_dir.path(), config, None)?;
instantiate_standard_template(&Template::Standard, temp_dir.path(), config, None)?;
Ok(temp_dir)
}

#[test]
fn test_parachain_instantiate_base_template() -> Result<()> {
fn test_parachain_instantiate_standard_template() -> Result<()> {
let temp_dir =
setup_template_and_instantiate().expect("Failed to setup template and instantiate");

Expand Down
120 changes: 74 additions & 46 deletions crates/pop-parachains/src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Provider {

pub fn default_template(&self) -> Template {
match &self {
Provider::Pop => Template::Base,
Provider::Pop => Template::Standard,
Provider::Parity => Template::ParityContracts,
}
}
Expand Down Expand Up @@ -78,19 +78,33 @@ pub enum Template {
// Pop
#[default]
#[strum(
serialize = "base",
serialize = "standard",
message = "Standard",
detailed_message = "A standard parachain",
props(Provider = "Pop", Repository = "https://github.com/r0gue-io/base-parachain")
)]
Base,
Standard,
#[strum(
serialize = "assets",
message = "Assets",
detailed_message = "Parachain configured with fungible and non-fungilble asset functionalities.",
props(Provider = "Pop", Repository = "https://github.com/r0gue-io/assets-parachain")
)]
Assets,
#[strum(
serialize = "contracts",
message = "Contracts",
detailed_message = "Parachain configured to supports Wasm-based contracts.",
props(Provider = "Pop", Repository = "https://github.com/r0gue-io/contracts-parachain")
)]
Contracts,
#[strum(
serialize = "evm",
message = "EVM",
detailed_message = "Parachain configured with frontier, enabling compatibility with the Ethereum Virtual Machine (EVM).",
props(Provider = "Pop", Repository = "https://github.com/r0gue-io/evm-parachain")
)]
EVM,
// Parity
#[strum(
serialize = "cpt",
Expand Down Expand Up @@ -141,72 +155,86 @@ pub enum Error {
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
use std::{collections::HashMap, str::FromStr};

fn templates_names() -> HashMap<String, Template> {
HashMap::from([
("standard".to_string(), Template::Standard),
("assets".to_string(), Template::Assets),
("contracts".to_string(), Template::Contracts),
("evm".to_string(), Template::EVM),
("cpt".to_string(), Template::ParityContracts),
("fpt".to_string(), Template::ParityFPT),
])
}
fn templates_urls() -> HashMap<String, &'static str> {
HashMap::from([
("standard".to_string(), "https://github.com/r0gue-io/base-parachain"),
("assets".to_string(), "https://github.com/r0gue-io/assets-parachain"),
("contracts".to_string(), "https://github.com/r0gue-io/contracts-parachain"),
("evm".to_string(), "https://github.com/r0gue-io/evm-parachain"),
("cpt".to_string(), "https://github.com/paritytech/substrate-contracts-node"),
("fpt".to_string(), "https://github.com/paritytech/frontier-parachain-template"),
])
}

#[test]
fn test_is_template_correct() {
let mut template = Template::Base;
assert_eq!(template.matches(&Provider::Pop), true);
assert_eq!(template.matches(&Provider::Parity), false);

template = Template::ParityContracts;
assert_eq!(template.matches(&Provider::Pop), false);
assert_eq!(template.matches(&Provider::Parity), true);

template = Template::ParityFPT;
assert_eq!(template.matches(&Provider::Pop), false);
assert_eq!(template.matches(&Provider::Parity), true);

template = Template::Assets;
assert_eq!(template.matches(&Provider::Pop), true);
assert_eq!(template.matches(&Provider::Parity), false);
for template in Template::VARIANTS {
if matches!(
template,
Template::Standard | Template::Assets | Template::Contracts | Template::EVM
) {
assert_eq!(template.matches(&Provider::Pop), true);
assert_eq!(template.matches(&Provider::Parity), false);
}
if matches!(template, Template::ParityContracts | Template::ParityFPT) {
assert_eq!(template.matches(&Provider::Pop), false);
assert_eq!(template.matches(&Provider::Parity), true);
}
}
}

#[test]
fn test_convert_string_to_template() {
assert_eq!(Template::from_str("base").unwrap(), Template::Base);
assert_eq!(Template::from_str("").unwrap_or_default(), Template::Base);
assert_eq!(Template::from_str("assets").unwrap(), Template::Assets);
assert_eq!(Template::from_str("cpt").unwrap(), Template::ParityContracts);
assert_eq!(Template::from_str("fpt").unwrap(), Template::ParityFPT);
let template_names = templates_names();
// Test the default
assert_eq!(Template::from_str("").unwrap_or_default(), Template::Standard);
// Test the rest
for template in Template::VARIANTS {
assert_eq!(
&Template::from_str(&template.to_string()).unwrap(),
template_names.get(&template.to_string()).unwrap()
);
}
}

#[test]
fn test_repository_url() {
let mut template = Template::Base;
assert_eq!(
template.repository_url().unwrap(),
"https://github.com/r0gue-io/base-parachain"
);
template = Template::ParityContracts;
assert_eq!(
template.repository_url().unwrap(),
"https://github.com/paritytech/substrate-contracts-node"
);
template = Template::ParityFPT;
assert_eq!(
template.repository_url().unwrap(),
"https://github.com/paritytech/frontier-parachain-template"
);
template = Template::Assets;
assert_eq!(
template.repository_url().unwrap(),
"https://github.com/r0gue-io/assets-parachain"
);
let template_urls = templates_urls();
for template in Template::VARIANTS {
assert_eq!(
&template.repository_url().unwrap(),
template_urls.get(&template.to_string()).unwrap()
);
}
}

#[test]
fn test_default_template_of_provider() {
let mut provider = Provider::Pop;
assert_eq!(provider.default_template(), Template::Base);
assert_eq!(provider.default_template(), Template::Standard);
provider = Provider::Parity;
assert_eq!(provider.default_template(), Template::ParityContracts);
}

#[test]
fn test_templates_of_provider() {
let mut provider = Provider::Pop;
assert_eq!(provider.templates(), [&Template::Base, &Template::Assets]);
assert_eq!(
provider.templates(),
[&Template::Standard, &Template::Assets, &Template::Contracts, &Template::EVM]
);
provider = Provider::Parity;
assert_eq!(provider.templates(), [&Template::ParityContracts, &Template::ParityFPT]);
}
Expand Down

0 comments on commit 738e0d6

Please sign in to comment.