From a39f81e615d48a94f9794eb141b888b01aaaee82 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Tue, 14 Nov 2023 20:40:59 +0200 Subject: [PATCH 1/9] start --- cargo-near/src/commands/mod.rs | 6 ++++++ cargo-near/src/commands/new/mod.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 cargo-near/src/commands/new/mod.rs diff --git a/cargo-near/src/commands/mod.rs b/cargo-near/src/commands/mod.rs index 691ffca3..6594afde 100644 --- a/cargo-near/src/commands/mod.rs +++ b/cargo-near/src/commands/mod.rs @@ -4,6 +4,7 @@ pub mod abi_command; pub mod build_command; pub mod create_dev_account; pub mod deploy; +pub mod new; #[derive(Debug, EnumDiscriminants, Clone, interactive_clap::InteractiveClap)] #[interactive_clap(context = near_cli_rs::GlobalContext)] @@ -12,6 +13,11 @@ pub mod deploy; #[non_exhaustive] /// What are you up to? (select one of the options with the up-down arrows on your keyboard and press Enter) pub enum NearCommand { + #[strum_discriminants(strum( + message = "new - Initializes a new project to create a contract" + ))] + /// Initializes a new project to create a contract + New(self::new::New), #[strum_discriminants(strum( message = "build - Build a NEAR contract and optionally embed ABI" ))] diff --git a/cargo-near/src/commands/new/mod.rs b/cargo-near/src/commands/new/mod.rs new file mode 100644 index 00000000..c5e748c6 --- /dev/null +++ b/cargo-near/src/commands/new/mod.rs @@ -0,0 +1,26 @@ +#[derive(Debug, Clone, interactive_clap::InteractiveClap)] +#[interactive_clap(input_context = near_cli_rs::GlobalContext)] +#[interactive_clap(output_context = NewContext)] +pub struct New { + /// Enter a new project name to create a contract: + pub project_dir: near_cli_rs::types::path_buf::PathBuf, +} + +#[derive(Debug, Clone)] +pub struct NewContext; + +impl NewContext { + pub fn from_previous_context( + _previous_context: near_cli_rs::GlobalContext, + scope: &::InteractiveClapContextScope, + ) -> color_eyre::eyre::Result { + let project_dir = scope.project_dir.clone(); + std::process::Command::new("cargo") + .arg("new") + .arg(&project_dir) + .arg("--lib") + .output() + .expect("failed to execute process"); + Ok(Self) + } +} From 7805d58275dfe2973ef5c31c7fa7cc0c41b95b69 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Fri, 17 Nov 2023 10:33:08 +0200 Subject: [PATCH 2/9] added prototype for the project --- cargo-near/src/commands/new/mod.rs | 26 +++++-- .../.github/workflows/deploy-production.yml | 27 +++++++ .../.github/workflows/deploy-staging.yml | 43 ++++++++++ .../.github/workflows/test.yml | 32 ++++++++ .../.github/workflows/undeploy-staging.yml | 25 ++++++ .../new/prototype_for_project/.gitignore | 1 + .../new/prototype_for_project/Cargo.toml | 27 +++++++ .../new/prototype_for_project/README.md | 37 +++++++++ .../prototype_for_project/rust-toolchain.toml | 4 + .../new/prototype_for_project/src/lib.rs | 78 +++++++++++++++++++ .../tests/test_basics.rs | 33 ++++++++ 11 files changed, 327 insertions(+), 6 deletions(-) create mode 100644 cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-production.yml create mode 100644 cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-staging.yml create mode 100644 cargo-near/src/commands/new/prototype_for_project/.github/workflows/test.yml create mode 100644 cargo-near/src/commands/new/prototype_for_project/.github/workflows/undeploy-staging.yml create mode 100644 cargo-near/src/commands/new/prototype_for_project/.gitignore create mode 100644 cargo-near/src/commands/new/prototype_for_project/Cargo.toml create mode 100644 cargo-near/src/commands/new/prototype_for_project/README.md create mode 100644 cargo-near/src/commands/new/prototype_for_project/rust-toolchain.toml create mode 100644 cargo-near/src/commands/new/prototype_for_project/src/lib.rs create mode 100644 cargo-near/src/commands/new/prototype_for_project/tests/test_basics.rs diff --git a/cargo-near/src/commands/new/mod.rs b/cargo-near/src/commands/new/mod.rs index c5e748c6..050b7a9a 100644 --- a/cargo-near/src/commands/new/mod.rs +++ b/cargo-near/src/commands/new/mod.rs @@ -2,7 +2,7 @@ #[interactive_clap(input_context = near_cli_rs::GlobalContext)] #[interactive_clap(output_context = NewContext)] pub struct New { - /// Enter a new project name to create a contract: + /// Enter a new project name (path to the project) to create a contract: pub project_dir: near_cli_rs::types::path_buf::PathBuf, } @@ -14,13 +14,27 @@ impl NewContext { _previous_context: near_cli_rs::GlobalContext, scope: &::InteractiveClapContextScope, ) -> color_eyre::eyre::Result { - let project_dir = scope.project_dir.clone(); - std::process::Command::new("cargo") - .arg("new") - .arg(&project_dir) - .arg("--lib") + const SOURCE_DIR: &str = "./cargo-near/src/commands/new/prototype_for_project/"; + let new_project_dir = scope.project_dir.clone(); + + std::process::Command::new("mkdir") + .arg(&new_project_dir) + .output() + .expect("failed to execute process"); + + std::process::Command::new("cp") + .arg("-r") + .arg(SOURCE_DIR) + .arg(&new_project_dir) .output() .expect("failed to execute process"); + + std::process::Command::new("git") + .arg("init") + .current_dir(&new_project_dir) + .output() + .expect("failed to execute process"); + Ok(Self) } } diff --git a/cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-production.yml b/cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-production.yml new file mode 100644 index 00000000..16620218 --- /dev/null +++ b/cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-production.yml @@ -0,0 +1,27 @@ +name: Deploy to production +on: + push: + branches: [main] + +jobs: + test: + uses: ./.github/workflows/test.yml + + deploy-staging: + name: Deploy to production + needs: [test] + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Install cargo-near CLI + run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/download/cargo-near-v0.4.1/cargo-near-installer.sh | sh + - name: Deploy to production + run: | + cargo near deploy --no-abi "${{ vars.NEAR_CONTRACT_PRODUCTION_ACCOUNT_ID }}" \ + without-init-call \ + network-config "${{ vars.NEAR_CONTRACT_PRODUCTION_NETWORK }}" \ + sign-with-plaintext-private-key \ + --signer-public-key "${{ vars.NEAR_CONTRACT_PRODUCTION_ACCOUNT_PUBLIC_KEY }}" \ + --signer-private-key "${{ secrets.NEAR_CONTRACT_PRODUCTION_ACCOUNT_PRIVATE_KEY }}" \ + send diff --git a/cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-staging.yml b/cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-staging.yml new file mode 100644 index 00000000..4efa6e5a --- /dev/null +++ b/cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-staging.yml @@ -0,0 +1,43 @@ +name: Deploy to staging +on: + pull_request: + +jobs: + test: + uses: ./.github/workflows/test.yml + + deploy-staging: + name: Deploy to staging subaccount + needs: [test] + runs-on: ubuntu-latest + env: + NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID: gh-${{ github.event.number }}.${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_ID }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install near CLI + run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/near-cli-rs/releases/download/v0.7.0/near-cli-rs-installer.sh | sh + - name: Create staging account + run: | + near account create-account fund-myself "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" '10 NEAR' \ + use-manually-provided-public-key "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_PUBLIC_KEY }}" \ + sign-as "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_ID }}" \ + network-config "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \ + sign-with-plaintext-private-key \ + --signer-public-key "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_PUBLIC_KEY }}" \ + --signer-private-key "${{ secrets.NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY }}" \ + send + + - name: Install cargo-near CLI + run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/download/cargo-near-v0.4.1/cargo-near-installer.sh | sh + - name: Deploy to staging + run: | + cargo near deploy --no-abi "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \ + without-init-call \ + network-config "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \ + sign-with-plaintext-private-key \ + --signer-public-key "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_PUBLIC_KEY }}" \ + --signer-private-key "${{ secrets.NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY }}" \ + send diff --git a/cargo-near/src/commands/new/prototype_for_project/.github/workflows/test.yml b/cargo-near/src/commands/new/prototype_for_project/.github/workflows/test.yml new file mode 100644 index 00000000..7f847afd --- /dev/null +++ b/cargo-near/src/commands/new/prototype_for_project/.github/workflows/test.yml @@ -0,0 +1,32 @@ +name: Test +on: + workflow_call: + +jobs: + code-formatting: + name: Code Formatting + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - run: cargo fmt --check + + code-linter: + name: Code Linter + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Run cargo clippy + run: | + rustup component add clippy + cargo clippy --all-features --workspace --tests -- --warn clippy::all --warn clippy::nursery + + tests: + name: Tests + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Run cargo test + run: cargo test diff --git a/cargo-near/src/commands/new/prototype_for_project/.github/workflows/undeploy-staging.yml b/cargo-near/src/commands/new/prototype_for_project/.github/workflows/undeploy-staging.yml new file mode 100644 index 00000000..48a8fc7a --- /dev/null +++ b/cargo-near/src/commands/new/prototype_for_project/.github/workflows/undeploy-staging.yml @@ -0,0 +1,25 @@ +name: Undeploy staging +on: + pull_request: + types: [closed] + +jobs: + cleanup-staging: + name: Cleanup staging account + runs-on: ubuntu-latest + env: + NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID: gh-${{ github.event.number }}.${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_ID }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Install near CLI + run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/near-cli-rs/releases/download/v0.7.0/near-cli-rs-installer.sh | sh + - name: Remove staging account + run: | + near account delete-account "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \ + beneficiary "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_ID }}" \ + network-config "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \ + sign-with-plaintext-private-key \ + --signer-public-key "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_PUBLIC_KEY }}" \ + --signer-private-key "${{ secrets.NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY }}" \ + send diff --git a/cargo-near/src/commands/new/prototype_for_project/.gitignore b/cargo-near/src/commands/new/prototype_for_project/.gitignore new file mode 100644 index 00000000..ea8c4bf7 --- /dev/null +++ b/cargo-near/src/commands/new/prototype_for_project/.gitignore @@ -0,0 +1 @@ +/target diff --git a/cargo-near/src/commands/new/prototype_for_project/Cargo.toml b/cargo-near/src/commands/new/prototype_for_project/Cargo.toml new file mode 100644 index 00000000..80b597be --- /dev/null +++ b/cargo-near/src/commands/new/prototype_for_project/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "cargo-near-new-project" +description = "New NEAR Protocol smart contract" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib", "rlib"] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dependencies] +near-sdk = { git = "https://github.com/near/near-sdk-rs" } +serde_json = "1.0.108" + +[dev-dependencies] +near-workspaces = { version = "0.9.0", features = ["unstable"] } +tokio = { version = "1.12.0", features = ["full"] } + +[profile.release] +codegen-units = 1 +# Tell `rustc` to optimize for small code size. +opt-level = "z" +lto = true +debug = false +panic = "abort" +# Opt into extra safety checks on arithmetic operations https://stackoverflow.com/a/64136471/249801 +overflow-checks = true diff --git a/cargo-near/src/commands/new/prototype_for_project/README.md b/cargo-near/src/commands/new/prototype_for_project/README.md new file mode 100644 index 00000000..34b53e82 --- /dev/null +++ b/cargo-near/src/commands/new/prototype_for_project/README.md @@ -0,0 +1,37 @@ +# + + + +## How to Build Locally? + +Install [`cargo-near`](https://github.com/near/cargo-near) and run: + +```bash +cargo near build +``` + +## How to Test Locally? + +```bash +cargo test +``` + +## How to Deploy? + +Deployment is automated with GitHub Actions CI/CD pipeline. +To deploy manually, install [`cargo-near`](https://github.com/near/cargo-near) and run: + +```bash +cargo near deploy +``` + +## Useful Links + +- [cargo-near](https://github.com/near/cargo-near) - NEAR smart contract development toolkit for Rust +- [near CLI](https://near.cli.rs) - Iteract with NEAR blockchain from command line +- [NEAR Rust SDK Documentation](https://docs.near.org/sdk/rust/introduction) +- [NEAR Documentation](https://docs.near.org) +- [NEAR StackOverflow](https://stackoverflow.com/questions/tagged/nearprotocol) +- [NEAR Discord](https://near.chat) +- [NEAR Telegram Developers Community Group](https://t.me/neardev) +- NEAR DevHub: [Telegram](https://t.me/neardevhub), [Twitter](https://twitter.com/neardevhub) diff --git a/cargo-near/src/commands/new/prototype_for_project/rust-toolchain.toml b/cargo-near/src/commands/new/prototype_for_project/rust-toolchain.toml new file mode 100644 index 00000000..5879410d --- /dev/null +++ b/cargo-near/src/commands/new/prototype_for_project/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "1.73.0" +components = ["rustfmt"] +targets = ["wasm32-unknown-unknown"] diff --git a/cargo-near/src/commands/new/prototype_for_project/src/lib.rs b/cargo-near/src/commands/new/prototype_for_project/src/lib.rs new file mode 100644 index 00000000..b80b9327 --- /dev/null +++ b/cargo-near/src/commands/new/prototype_for_project/src/lib.rs @@ -0,0 +1,78 @@ +use near_sdk::borsh::{BorshDeserialize, BorshSerialize}; +use near_sdk::collections::LookupMap; +use near_sdk::{env, near_bindgen, AccountId, BorshStorageKey}; + +#[near_bindgen] +#[derive(BorshDeserialize, BorshSerialize)] +#[borsh(crate = "near_sdk::borsh")] +pub struct StatusMessage { + records: LookupMap, +} + +#[derive(BorshSerialize, BorshStorageKey)] +#[borsh(crate = "near_sdk::borsh")] +enum StorageKey { + StatusMessageRecords, +} + +impl Default for StatusMessage { + fn default() -> Self { + Self { + records: LookupMap::new(StorageKey::StatusMessageRecords), + } + } +} + +#[near_bindgen] +impl StatusMessage { + pub fn set_status(&mut self, message: String) { + let account_id = env::predecessor_account_id(); + self.records.insert(&account_id, &message); + } + + pub fn get_status(&self, account_id: AccountId) -> Option { + self.records.get(&account_id) + } +} + +#[cfg(not(target_arch = "wasm32"))] +#[cfg(test)] +mod tests { + use near_sdk::test_utils::{accounts, VMContextBuilder}; + use near_sdk::testing_env; + + use super::*; + + // Allows for modifying the environment of the mocked blockchain + fn get_context(predecessor_account_id: AccountId) -> VMContextBuilder { + let mut builder = VMContextBuilder::new(); + builder + .current_account_id(accounts(0)) + .signer_account_id(predecessor_account_id.clone()) + .predecessor_account_id(predecessor_account_id); + builder + } + + #[test] + fn set_get_message() { + let mut context = get_context(accounts(1)); + // Initialize the mocked blockchain + testing_env!(context.build()); + + // Set the testing environment for the subsequent calls + testing_env!(context.predecessor_account_id(accounts(1)).build()); + + let mut contract = StatusMessage::default(); + contract.set_status("hello".to_string()); + assert_eq!( + "hello".to_string(), + contract.get_status(accounts(1)).unwrap() + ); + } + + #[test] + fn get_nonexistent_message() { + let contract = StatusMessage::default(); + assert_eq!(None, contract.get_status("francis.near".parse().unwrap())); + } +} diff --git a/cargo-near/src/commands/new/prototype_for_project/tests/test_basics.rs b/cargo-near/src/commands/new/prototype_for_project/tests/test_basics.rs new file mode 100644 index 00000000..de6963fd --- /dev/null +++ b/cargo-near/src/commands/new/prototype_for_project/tests/test_basics.rs @@ -0,0 +1,33 @@ +use serde_json::json; + +#[tokio::test] +async fn test_contract_is_operational() -> Result<(), Box> { + let sandbox = near_workspaces::sandbox().await?; + let contract_wasm = near_workspaces::compile_project("./").await?; + + let contract = sandbox.dev_deploy(&contract_wasm).await?; + + let user1_account = sandbox.dev_create_account().await?; + let user2_account = sandbox.dev_create_account().await?; + + let outcome = user1_account + .call(contract.id(), "set_status") + .args_json(json!({"message": "test status"})) + .transact() + .await?; + assert!(outcome.is_success()); + + let user1_message_outcome = contract + .view("get_status") + .args_json(json!({"account_id": user1_account.id()})) + .await?; + assert_eq!(user1_message_outcome.json::()?, "test status"); + + let user2_message_outcome = contract + .view("get_status") + .args_json(json!({"account_id": user2_account.id()})) + .await?; + assert_eq!(user2_message_outcome.result, b"null"); + + Ok(()) +} From 1cce1732979c2518f0c3fa8cbf5cfadc26250fea Mon Sep 17 00:00:00 2001 From: FroVolod Date: Fri, 17 Nov 2023 10:51:51 +0200 Subject: [PATCH 3/9] updated: near-abi = "0.4.1" --- Cargo.lock | 90 +++++++++++++++++++++++++++++++++++++++---- cargo-near/Cargo.toml | 2 +- 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9dd472f1..d18603f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -606,6 +606,16 @@ dependencies = [ "hashbrown 0.13.2", ] +[[package]] +name = "borsh" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18a744ac76a433734df0902926ed12edd997391a8da3add87f6d706afc2dcbea" +dependencies = [ + "borsh-derive 1.1.2", + "cfg_aliases", +] + [[package]] name = "borsh-derive" version = "0.9.3" @@ -632,6 +642,20 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "borsh-derive" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b22bf794b9f8c87b51ea4d9e2710907ce13aa81dd2b8ac18a78fcca68ac738ef" +dependencies = [ + "once_cell", + "proc-macro-crate 2.0.0", + "proc-macro2", + "quote", + "syn 2.0.38", + "syn_derive", +] + [[package]] name = "borsh-derive-internal" version = "0.9.3" @@ -789,7 +813,7 @@ dependencies = [ "env_logger", "libloading", "log", - "near-abi", + "near-abi 0.3.0", "rustc_version", "schemars", "serde_json", @@ -819,7 +843,7 @@ dependencies = [ "linked-hash-map", "log", "names", - "near-abi", + "near-abi 0.4.1", "near-cli-rs", "near-crypto 0.17.0", "near-jsonrpc-client", @@ -849,7 +873,7 @@ dependencies = [ "function_name", "git2", "minifier", - "near-abi", + "near-abi 0.3.0", "near-workspaces", "prettyplease", "proc-macro2", @@ -928,6 +952,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + [[package]] name = "chrono" version = "0.4.31" @@ -2794,6 +2824,18 @@ dependencies = [ "serde", ] +[[package]] +name = "near-abi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4ac4e2d843390b1a007ad206022b4252d0fe3756d8b6609bc025cce07949bc5" +dependencies = [ + "borsh 1.1.2", + "schemars", + "semver", + "serde", +] + [[package]] name = "near-account-id" version = "0.14.0" @@ -3219,7 +3261,7 @@ dependencies = [ "base64 0.13.1", "borsh 0.9.3", "bs58 0.4.0", - "near-abi", + "near-abi 0.3.0", "near-crypto 0.14.0", "near-primitives 0.14.0", "near-primitives-core 0.14.0", @@ -3986,7 +4028,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" dependencies = [ "once_cell", - "toml_edit", + "toml_edit 0.19.15", +] + +[[package]] +name = "proc-macro-crate" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e8366a6159044a37876a2b9817124296703c586a5c92e2c53751fa06d8d43e8" +dependencies = [ + "toml_edit 0.20.7", ] [[package]] @@ -5050,6 +5101,18 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1329189c02ff984e9736652b1631330da25eaa6bc639089ed4915d25446cbe7b" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.38", +] + [[package]] name = "system-configuration" version = "0.5.1" @@ -5319,14 +5382,14 @@ dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit", + "toml_edit 0.19.15", ] [[package]] name = "toml_datetime" -version = "0.6.3" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" +checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" dependencies = [ "serde", ] @@ -5344,6 +5407,17 @@ dependencies = [ "winnow", ] +[[package]] +name = "toml_edit" +version = "0.20.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70f427fce4d84c72b5b732388bf4a9f4531b53f74e2887e3ecb2481f68f66d81" +dependencies = [ + "indexmap 2.0.2", + "toml_datetime", + "winnow", +] + [[package]] name = "tonic" version = "0.6.2" diff --git a/cargo-near/Cargo.toml b/cargo-near/Cargo.toml index a4083f66..fd4eb294 100644 --- a/cargo-near/Cargo.toml +++ b/cargo-near/Cargo.toml @@ -28,7 +28,7 @@ serde_json = "1.0" sha2 = "0.10" symbolic-debuginfo = "8.8" schemars = "0.8" -near-abi = { version = "0.3.0", features = ["__chunked-entries"] } +near-abi = { version = "0.4.1", features = ["__chunked-entries"] } libloading = "0.7.3" zstd = "0.11" atty = "0.2.14" From b83de797e1832ba27664e086517f0739adae29ba Mon Sep 17 00:00:00 2001 From: FroVolod Date: Fri, 17 Nov 2023 12:05:10 +0200 Subject: [PATCH 4/9] updated gitignore for "prototype_for_project" --- .../src/commands/new/prototype_for_project/.gitignore | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cargo-near/src/commands/new/prototype_for_project/.gitignore b/cargo-near/src/commands/new/prototype_for_project/.gitignore index ea8c4bf7..0251ae90 100644 --- a/cargo-near/src/commands/new/prototype_for_project/.gitignore +++ b/cargo-near/src/commands/new/prototype_for_project/.gitignore @@ -1 +1,9 @@ +# Build files /target + +# Code Editor related files +.idea +.vscode + +# OS X +.DS_Store From 06445e120742a3653d001288efa28a341e785f88 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Fri, 17 Nov 2023 22:10:14 +0200 Subject: [PATCH 5/9] fixed --- Cargo.lock | 86 ++-------------------------- cargo-near/Cargo.toml | 2 +- cargo-near/src/commands/new/mod.rs | 91 ++++++++++++++++++++++++------ 3 files changed, 82 insertions(+), 97 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d18603f3..519b8db1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -606,16 +606,6 @@ dependencies = [ "hashbrown 0.13.2", ] -[[package]] -name = "borsh" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18a744ac76a433734df0902926ed12edd997391a8da3add87f6d706afc2dcbea" -dependencies = [ - "borsh-derive 1.1.2", - "cfg_aliases", -] - [[package]] name = "borsh-derive" version = "0.9.3" @@ -642,20 +632,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "borsh-derive" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b22bf794b9f8c87b51ea4d9e2710907ce13aa81dd2b8ac18a78fcca68ac738ef" -dependencies = [ - "once_cell", - "proc-macro-crate 2.0.0", - "proc-macro2", - "quote", - "syn 2.0.38", - "syn_derive", -] - [[package]] name = "borsh-derive-internal" version = "0.9.3" @@ -813,7 +789,7 @@ dependencies = [ "env_logger", "libloading", "log", - "near-abi 0.3.0", + "near-abi", "rustc_version", "schemars", "serde_json", @@ -843,7 +819,7 @@ dependencies = [ "linked-hash-map", "log", "names", - "near-abi 0.4.1", + "near-abi", "near-cli-rs", "near-crypto 0.17.0", "near-jsonrpc-client", @@ -873,7 +849,7 @@ dependencies = [ "function_name", "git2", "minifier", - "near-abi 0.3.0", + "near-abi", "near-workspaces", "prettyplease", "proc-macro2", @@ -952,12 +928,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "cfg_aliases" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" - [[package]] name = "chrono" version = "0.4.31" @@ -2824,18 +2794,6 @@ dependencies = [ "serde", ] -[[package]] -name = "near-abi" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4ac4e2d843390b1a007ad206022b4252d0fe3756d8b6609bc025cce07949bc5" -dependencies = [ - "borsh 1.1.2", - "schemars", - "semver", - "serde", -] - [[package]] name = "near-account-id" version = "0.14.0" @@ -3261,7 +3219,7 @@ dependencies = [ "base64 0.13.1", "borsh 0.9.3", "bs58 0.4.0", - "near-abi 0.3.0", + "near-abi", "near-crypto 0.14.0", "near-primitives 0.14.0", "near-primitives-core 0.14.0", @@ -4028,16 +3986,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" dependencies = [ "once_cell", - "toml_edit 0.19.15", -] - -[[package]] -name = "proc-macro-crate" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e8366a6159044a37876a2b9817124296703c586a5c92e2c53751fa06d8d43e8" -dependencies = [ - "toml_edit 0.20.7", + "toml_edit", ] [[package]] @@ -5101,18 +5050,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "syn_derive" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1329189c02ff984e9736652b1631330da25eaa6bc639089ed4915d25446cbe7b" -dependencies = [ - "proc-macro-error", - "proc-macro2", - "quote", - "syn 2.0.38", -] - [[package]] name = "system-configuration" version = "0.5.1" @@ -5382,7 +5319,7 @@ dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.19.15", + "toml_edit", ] [[package]] @@ -5407,17 +5344,6 @@ dependencies = [ "winnow", ] -[[package]] -name = "toml_edit" -version = "0.20.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70f427fce4d84c72b5b732388bf4a9f4531b53f74e2887e3ecb2481f68f66d81" -dependencies = [ - "indexmap 2.0.2", - "toml_datetime", - "winnow", -] - [[package]] name = "tonic" version = "0.6.2" diff --git a/cargo-near/Cargo.toml b/cargo-near/Cargo.toml index fd4eb294..a4083f66 100644 --- a/cargo-near/Cargo.toml +++ b/cargo-near/Cargo.toml @@ -28,7 +28,7 @@ serde_json = "1.0" sha2 = "0.10" symbolic-debuginfo = "8.8" schemars = "0.8" -near-abi = { version = "0.4.1", features = ["__chunked-entries"] } +near-abi = { version = "0.3.0", features = ["__chunked-entries"] } libloading = "0.7.3" zstd = "0.11" atty = "0.2.14" diff --git a/cargo-near/src/commands/new/mod.rs b/cargo-near/src/commands/new/mod.rs index 050b7a9a..15b58301 100644 --- a/cargo-near/src/commands/new/mod.rs +++ b/cargo-near/src/commands/new/mod.rs @@ -1,3 +1,6 @@ +use color_eyre::eyre::{ContextCompat, WrapErr}; +use std::io::Write; + #[derive(Debug, Clone, interactive_clap::InteractiveClap)] #[interactive_clap(input_context = near_cli_rs::GlobalContext)] #[interactive_clap(output_context = NewContext)] @@ -14,27 +17,83 @@ impl NewContext { _previous_context: near_cli_rs::GlobalContext, scope: &::InteractiveClapContextScope, ) -> color_eyre::eyre::Result { - const SOURCE_DIR: &str = "./cargo-near/src/commands/new/prototype_for_project/"; - let new_project_dir = scope.project_dir.clone(); - - std::process::Command::new("mkdir") - .arg(&new_project_dir) - .output() - .expect("failed to execute process"); - - std::process::Command::new("cp") - .arg("-r") - .arg(SOURCE_DIR) - .arg(&new_project_dir) - .output() - .expect("failed to execute process"); + for new_project_file in NEW_PROJECT_FILES { + let mut folder_path = std::path::PathBuf::from(&scope.project_dir); + let file_path = std::path::PathBuf::from(new_project_file.file_path); + folder_path.push(file_path.parent().wrap_err_with(|| { + format!("Impossible to get parent for `{}`", file_path.display()) + })?); + std::fs::create_dir_all(&folder_path)?; + let path = folder_path.join(file_path.file_name().wrap_err_with(|| { + format!("Impossible to get filename for `{}`", file_path.display()) + })?); + std::fs::File::create(&path) + .wrap_err_with(|| format!("Failed to create file: {:?}", path))? + .write(new_project_file.content.as_bytes()) + .wrap_err_with(|| format!("Failed to write to file: {:?}", path))?; + } std::process::Command::new("git") .arg("init") - .current_dir(&new_project_dir) + .current_dir(&scope.project_dir) .output() - .expect("failed to execute process"); + .wrap_err("Failed to execute process: `git init`")?; + + println!("New project is created at '{}'\n", scope.project_dir); + println!("Now you can build, deploy, and finish CI setup for automatic deployment:"); + println!("1. `cargo near build`"); + println!("2. `cargo test`"); + println!("3. `cargo near deploy`"); + println!("4. Configure `NEAR_CONTRACT_STAGING_*` and `NEAR_CONTRACT_PRODUCTION_*` variables and secrets on GitHub to enable automatic deployment to staging and production. See more details in `.github/workflow/*` files."); Ok(Self) } } + +struct NewProjectFile { + file_path: &'static str, + content: &'static str, +} + +const NEW_PROJECT_FILES: &[NewProjectFile] = &[ + NewProjectFile { + file_path: ".github/workflows/deploy-production.yml", + content: include_str!("prototype_for_project/.github/workflows/deploy-production.yml"), + }, + NewProjectFile { + file_path: ".github/workflows/deploy-staging.yml", + content: include_str!("prototype_for_project/.github/workflows/deploy-staging.yml"), + }, + NewProjectFile { + file_path: ".github/workflows/test.yml", + content: include_str!("prototype_for_project/.github/workflows/test.yml"), + }, + NewProjectFile { + file_path: ".github/workflows/undeploy-staging.yml", + content: include_str!("prototype_for_project/.github/workflows/undeploy-staging.yml"), + }, + NewProjectFile { + file_path: "src/lib.rs", + content: include_str!("prototype_for_project/src/lib.rs"), + }, + NewProjectFile { + file_path: "tests/test_basics.rs", + content: include_str!("prototype_for_project/tests/test_basics.rs"), + }, + NewProjectFile { + file_path: ".gitignore", + content: include_str!("prototype_for_project/.gitignore"), + }, + NewProjectFile { + file_path: "Cargo.toml", + content: include_str!("prototype_for_project/Cargo.toml"), + }, + NewProjectFile { + file_path: "README.md", + content: include_str!("prototype_for_project/README.md"), + }, + NewProjectFile { + file_path: "rust-toolchain.toml", + content: include_str!("prototype_for_project/rust-toolchain.toml"), + }, +]; From f0930fcc2009f1f13d729246e1c8e137882eaa08 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Sat, 18 Nov 2023 18:26:15 +0200 Subject: [PATCH 6/9] refactored --- cargo-near/src/commands/new/mod.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/cargo-near/src/commands/new/mod.rs b/cargo-near/src/commands/new/mod.rs index 15b58301..e9f2f6bf 100644 --- a/cargo-near/src/commands/new/mod.rs +++ b/cargo-near/src/commands/new/mod.rs @@ -17,8 +17,11 @@ impl NewContext { _previous_context: near_cli_rs::GlobalContext, scope: &::InteractiveClapContextScope, ) -> color_eyre::eyre::Result { + let project_dir = scope.project_dir.to_string(); + let project_name = project_dir.rsplit('/').next().wrap_err("Internal error!")?; + for new_project_file in NEW_PROJECT_FILES { - let mut folder_path = std::path::PathBuf::from(&scope.project_dir); + let mut folder_path = std::path::PathBuf::from(&project_dir); let file_path = std::path::PathBuf::from(new_project_file.file_path); folder_path.push(file_path.parent().wrap_err_with(|| { format!("Impossible to get parent for `{}`", file_path.display()) @@ -28,18 +31,23 @@ impl NewContext { format!("Impossible to get filename for `{}`", file_path.display()) })?); std::fs::File::create(&path) - .wrap_err_with(|| format!("Failed to create file: {:?}", path))? - .write(new_project_file.content.as_bytes()) - .wrap_err_with(|| format!("Failed to write to file: {:?}", path))?; + .wrap_err_with(|| format!("Failed to create file: {}", path.display()))? + .write( + new_project_file + .content + .replace("cargo-near-new-project", project_name) + .as_bytes(), + ) + .wrap_err_with(|| format!("Failed to write to file: {}", path.display()))?; } std::process::Command::new("git") .arg("init") - .current_dir(&scope.project_dir) + .current_dir(&project_dir) .output() .wrap_err("Failed to execute process: `git init`")?; - println!("New project is created at '{}'\n", scope.project_dir); + println!("New project is created at '{project_dir}'\n"); println!("Now you can build, deploy, and finish CI setup for automatic deployment:"); println!("1. `cargo near build`"); println!("2. `cargo test`"); From 5cec518419b4466dad1e048b9db5b819359089cb Mon Sep 17 00:00:00 2001 From: Vlad Frolov Date: Mon, 20 Nov 2023 00:47:15 +0100 Subject: [PATCH 7/9] switch to using git submodule to new-project-template --- .github/workflows/test.yml | 18 +++-- .gitmodules | 3 + README.md | 6 ++ cargo-near/src/commands/new/mod.rs | 60 +++++++------- .../src/commands/new/new-project-template | 1 + .../.github/workflows/deploy-production.yml | 27 ------- .../.github/workflows/deploy-staging.yml | 43 ---------- .../.github/workflows/test.yml | 32 -------- .../.github/workflows/undeploy-staging.yml | 25 ------ .../new/prototype_for_project/.gitignore | 9 --- .../new/prototype_for_project/Cargo.toml | 27 ------- .../new/prototype_for_project/README.md | 37 --------- .../prototype_for_project/rust-toolchain.toml | 4 - .../new/prototype_for_project/src/lib.rs | 78 ------------------- .../tests/test_basics.rs | 33 -------- 15 files changed, 51 insertions(+), 352 deletions(-) create mode 100644 .gitmodules create mode 160000 cargo-near/src/commands/new/new-project-template delete mode 100644 cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-production.yml delete mode 100644 cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-staging.yml delete mode 100644 cargo-near/src/commands/new/prototype_for_project/.github/workflows/test.yml delete mode 100644 cargo-near/src/commands/new/prototype_for_project/.github/workflows/undeploy-staging.yml delete mode 100644 cargo-near/src/commands/new/prototype_for_project/.gitignore delete mode 100644 cargo-near/src/commands/new/prototype_for_project/Cargo.toml delete mode 100644 cargo-near/src/commands/new/prototype_for_project/README.md delete mode 100644 cargo-near/src/commands/new/prototype_for_project/rust-toolchain.toml delete mode 100644 cargo-near/src/commands/new/prototype_for_project/src/lib.rs delete mode 100644 cargo-near/src/commands/new/prototype_for_project/tests/test_basics.rs diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3e516a70..cbbe741e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,7 +2,7 @@ name: CI on: push: - branches: [ main ] + branches: [main] pull_request: jobs: @@ -15,7 +15,9 @@ jobs: steps: - name: Checkout Sources - uses: actions/checkout@v2 + uses: actions/checkout@v4 + with: + submodules: recursive - name: Get MSRV run: | @@ -45,7 +47,9 @@ jobs: steps: - name: Checkout Sources - uses: actions/checkout@v2 + uses: actions/checkout@v4 + with: + submodules: recursive - name: "Install stable Rust toolchain" uses: actions-rs/toolchain@v1 @@ -69,7 +73,9 @@ jobs: steps: - name: Checkout Sources - uses: actions/checkout@v2 + uses: actions/checkout@v4 + with: + submodules: recursive - name: Check Formatting run: cargo fmt -- --check @@ -86,7 +92,9 @@ jobs: steps: - name: Checkout Sources - uses: actions/checkout@v2 + uses: actions/checkout@v4 + with: + submodules: recursive - name: Install Audit Tool run: cargo install cargo-audit diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..20e41d75 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "cargo-near/src/commands/new/new-project-template"] + path = cargo-near/src/commands/new/new-project-template + url = https://github.com/near/cargo-near-new-project-template diff --git a/README.md b/README.md index 4199e424..20eb493f 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,12 @@ Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as below, without any additional terms or conditions. +Note, fetching the git repository of cargo-near, don't forget to pull the submodules after cloning: + +```console +git submodule update --init +``` + ## License Licensed under either of diff --git a/cargo-near/src/commands/new/mod.rs b/cargo-near/src/commands/new/mod.rs index e9f2f6bf..af28a73f 100644 --- a/cargo-near/src/commands/new/mod.rs +++ b/cargo-near/src/commands/new/mod.rs @@ -1,5 +1,4 @@ use color_eyre::eyre::{ContextCompat, WrapErr}; -use std::io::Write; #[derive(Debug, Clone, interactive_clap::InteractiveClap)] #[interactive_clap(input_context = near_cli_rs::GlobalContext)] @@ -17,28 +16,25 @@ impl NewContext { _previous_context: near_cli_rs::GlobalContext, scope: &::InteractiveClapContextScope, ) -> color_eyre::eyre::Result { - let project_dir = scope.project_dir.to_string(); - let project_name = project_dir.rsplit('/').next().wrap_err("Internal error!")?; + let project_dir: &std::path::Path = scope.project_dir.as_ref(); + let project_name = project_dir + .file_name() + .wrap_err("Could not extract project name from project path")? + .to_str() + .wrap_err("Project name has to be a valid UTF-8 string")?; for new_project_file in NEW_PROJECT_FILES { - let mut folder_path = std::path::PathBuf::from(&project_dir); - let file_path = std::path::PathBuf::from(new_project_file.file_path); - folder_path.push(file_path.parent().wrap_err_with(|| { - format!("Impossible to get parent for `{}`", file_path.display()) - })?); - std::fs::create_dir_all(&folder_path)?; - let path = folder_path.join(file_path.file_name().wrap_err_with(|| { - format!("Impossible to get filename for `{}`", file_path.display()) - })?); - std::fs::File::create(&path) - .wrap_err_with(|| format!("Failed to create file: {}", path.display()))? - .write( - new_project_file - .content - .replace("cargo-near-new-project", project_name) - .as_bytes(), - ) - .wrap_err_with(|| format!("Failed to write to file: {}", path.display()))?; + let new_file_path = project_dir.join(new_project_file.file_path); + std::fs::create_dir_all(&new_file_path.parent().wrap_err_with(|| { + format!("Impossible to get parent for `{}`", new_file_path.display()) + })?)?; + std::fs::write( + &new_file_path, + new_project_file + .content + .replace("cargo-near-new-project-name", project_name), + ) + .wrap_err_with(|| format!("Failed to write to file: {}", new_file_path.display()))?; } std::process::Command::new("git") @@ -47,7 +43,7 @@ impl NewContext { .output() .wrap_err("Failed to execute process: `git init`")?; - println!("New project is created at '{project_dir}'\n"); + println!("New project is created at '{}'\n", project_dir.display()); println!("Now you can build, deploy, and finish CI setup for automatic deployment:"); println!("1. `cargo near build`"); println!("2. `cargo test`"); @@ -66,42 +62,42 @@ struct NewProjectFile { const NEW_PROJECT_FILES: &[NewProjectFile] = &[ NewProjectFile { file_path: ".github/workflows/deploy-production.yml", - content: include_str!("prototype_for_project/.github/workflows/deploy-production.yml"), + content: include_str!("new-project-template/.github/workflows/deploy-production.yml"), }, NewProjectFile { file_path: ".github/workflows/deploy-staging.yml", - content: include_str!("prototype_for_project/.github/workflows/deploy-staging.yml"), + content: include_str!("new-project-template/.github/workflows/deploy-staging.yml"), }, NewProjectFile { file_path: ".github/workflows/test.yml", - content: include_str!("prototype_for_project/.github/workflows/test.yml"), + content: include_str!("new-project-template/.github/workflows/test.yml"), }, NewProjectFile { file_path: ".github/workflows/undeploy-staging.yml", - content: include_str!("prototype_for_project/.github/workflows/undeploy-staging.yml"), + content: include_str!("new-project-template/.github/workflows/undeploy-staging.yml"), }, NewProjectFile { file_path: "src/lib.rs", - content: include_str!("prototype_for_project/src/lib.rs"), + content: include_str!("new-project-template/src/lib.rs"), }, NewProjectFile { file_path: "tests/test_basics.rs", - content: include_str!("prototype_for_project/tests/test_basics.rs"), + content: include_str!("new-project-template/tests/test_basics.rs"), }, NewProjectFile { file_path: ".gitignore", - content: include_str!("prototype_for_project/.gitignore"), + content: include_str!("new-project-template/.gitignore"), }, NewProjectFile { file_path: "Cargo.toml", - content: include_str!("prototype_for_project/Cargo.toml"), + content: include_str!("new-project-template/Cargo.toml"), }, NewProjectFile { file_path: "README.md", - content: include_str!("prototype_for_project/README.md"), + content: include_str!("new-project-template/README.md"), }, NewProjectFile { file_path: "rust-toolchain.toml", - content: include_str!("prototype_for_project/rust-toolchain.toml"), + content: include_str!("new-project-template/rust-toolchain.toml"), }, ]; diff --git a/cargo-near/src/commands/new/new-project-template b/cargo-near/src/commands/new/new-project-template new file mode 160000 index 00000000..9d53496b --- /dev/null +++ b/cargo-near/src/commands/new/new-project-template @@ -0,0 +1 @@ +Subproject commit 9d53496b4d2bd207d5e815e350a7c28696592068 diff --git a/cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-production.yml b/cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-production.yml deleted file mode 100644 index 16620218..00000000 --- a/cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-production.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Deploy to production -on: - push: - branches: [main] - -jobs: - test: - uses: ./.github/workflows/test.yml - - deploy-staging: - name: Deploy to production - needs: [test] - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - name: Install cargo-near CLI - run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/download/cargo-near-v0.4.1/cargo-near-installer.sh | sh - - name: Deploy to production - run: | - cargo near deploy --no-abi "${{ vars.NEAR_CONTRACT_PRODUCTION_ACCOUNT_ID }}" \ - without-init-call \ - network-config "${{ vars.NEAR_CONTRACT_PRODUCTION_NETWORK }}" \ - sign-with-plaintext-private-key \ - --signer-public-key "${{ vars.NEAR_CONTRACT_PRODUCTION_ACCOUNT_PUBLIC_KEY }}" \ - --signer-private-key "${{ secrets.NEAR_CONTRACT_PRODUCTION_ACCOUNT_PRIVATE_KEY }}" \ - send diff --git a/cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-staging.yml b/cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-staging.yml deleted file mode 100644 index 4efa6e5a..00000000 --- a/cargo-near/src/commands/new/prototype_for_project/.github/workflows/deploy-staging.yml +++ /dev/null @@ -1,43 +0,0 @@ -name: Deploy to staging -on: - pull_request: - -jobs: - test: - uses: ./.github/workflows/test.yml - - deploy-staging: - name: Deploy to staging subaccount - needs: [test] - runs-on: ubuntu-latest - env: - NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID: gh-${{ github.event.number }}.${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_ID }} - - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Install near CLI - run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/near-cli-rs/releases/download/v0.7.0/near-cli-rs-installer.sh | sh - - name: Create staging account - run: | - near account create-account fund-myself "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" '10 NEAR' \ - use-manually-provided-public-key "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_PUBLIC_KEY }}" \ - sign-as "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_ID }}" \ - network-config "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \ - sign-with-plaintext-private-key \ - --signer-public-key "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_PUBLIC_KEY }}" \ - --signer-private-key "${{ secrets.NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY }}" \ - send - - - name: Install cargo-near CLI - run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/download/cargo-near-v0.4.1/cargo-near-installer.sh | sh - - name: Deploy to staging - run: | - cargo near deploy --no-abi "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \ - without-init-call \ - network-config "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \ - sign-with-plaintext-private-key \ - --signer-public-key "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_PUBLIC_KEY }}" \ - --signer-private-key "${{ secrets.NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY }}" \ - send diff --git a/cargo-near/src/commands/new/prototype_for_project/.github/workflows/test.yml b/cargo-near/src/commands/new/prototype_for_project/.github/workflows/test.yml deleted file mode 100644 index 7f847afd..00000000 --- a/cargo-near/src/commands/new/prototype_for_project/.github/workflows/test.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: Test -on: - workflow_call: - -jobs: - code-formatting: - name: Code Formatting - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - run: cargo fmt --check - - code-linter: - name: Code Linter - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - name: Run cargo clippy - run: | - rustup component add clippy - cargo clippy --all-features --workspace --tests -- --warn clippy::all --warn clippy::nursery - - tests: - name: Tests - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - name: Run cargo test - run: cargo test diff --git a/cargo-near/src/commands/new/prototype_for_project/.github/workflows/undeploy-staging.yml b/cargo-near/src/commands/new/prototype_for_project/.github/workflows/undeploy-staging.yml deleted file mode 100644 index 48a8fc7a..00000000 --- a/cargo-near/src/commands/new/prototype_for_project/.github/workflows/undeploy-staging.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: Undeploy staging -on: - pull_request: - types: [closed] - -jobs: - cleanup-staging: - name: Cleanup staging account - runs-on: ubuntu-latest - env: - NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID: gh-${{ github.event.number }}.${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_ID }} - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - name: Install near CLI - run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/near-cli-rs/releases/download/v0.7.0/near-cli-rs-installer.sh | sh - - name: Remove staging account - run: | - near account delete-account "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \ - beneficiary "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_ID }}" \ - network-config "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \ - sign-with-plaintext-private-key \ - --signer-public-key "${{ vars.NEAR_CONTRACT_STAGING_ACCOUNT_PUBLIC_KEY }}" \ - --signer-private-key "${{ secrets.NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY }}" \ - send diff --git a/cargo-near/src/commands/new/prototype_for_project/.gitignore b/cargo-near/src/commands/new/prototype_for_project/.gitignore deleted file mode 100644 index 0251ae90..00000000 --- a/cargo-near/src/commands/new/prototype_for_project/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Build files -/target - -# Code Editor related files -.idea -.vscode - -# OS X -.DS_Store diff --git a/cargo-near/src/commands/new/prototype_for_project/Cargo.toml b/cargo-near/src/commands/new/prototype_for_project/Cargo.toml deleted file mode 100644 index 80b597be..00000000 --- a/cargo-near/src/commands/new/prototype_for_project/Cargo.toml +++ /dev/null @@ -1,27 +0,0 @@ -[package] -name = "cargo-near-new-project" -description = "New NEAR Protocol smart contract" -version = "0.1.0" -edition = "2021" - -[lib] -crate-type = ["cdylib", "rlib"] - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] -near-sdk = { git = "https://github.com/near/near-sdk-rs" } -serde_json = "1.0.108" - -[dev-dependencies] -near-workspaces = { version = "0.9.0", features = ["unstable"] } -tokio = { version = "1.12.0", features = ["full"] } - -[profile.release] -codegen-units = 1 -# Tell `rustc` to optimize for small code size. -opt-level = "z" -lto = true -debug = false -panic = "abort" -# Opt into extra safety checks on arithmetic operations https://stackoverflow.com/a/64136471/249801 -overflow-checks = true diff --git a/cargo-near/src/commands/new/prototype_for_project/README.md b/cargo-near/src/commands/new/prototype_for_project/README.md deleted file mode 100644 index 34b53e82..00000000 --- a/cargo-near/src/commands/new/prototype_for_project/README.md +++ /dev/null @@ -1,37 +0,0 @@ -# - - - -## How to Build Locally? - -Install [`cargo-near`](https://github.com/near/cargo-near) and run: - -```bash -cargo near build -``` - -## How to Test Locally? - -```bash -cargo test -``` - -## How to Deploy? - -Deployment is automated with GitHub Actions CI/CD pipeline. -To deploy manually, install [`cargo-near`](https://github.com/near/cargo-near) and run: - -```bash -cargo near deploy -``` - -## Useful Links - -- [cargo-near](https://github.com/near/cargo-near) - NEAR smart contract development toolkit for Rust -- [near CLI](https://near.cli.rs) - Iteract with NEAR blockchain from command line -- [NEAR Rust SDK Documentation](https://docs.near.org/sdk/rust/introduction) -- [NEAR Documentation](https://docs.near.org) -- [NEAR StackOverflow](https://stackoverflow.com/questions/tagged/nearprotocol) -- [NEAR Discord](https://near.chat) -- [NEAR Telegram Developers Community Group](https://t.me/neardev) -- NEAR DevHub: [Telegram](https://t.me/neardevhub), [Twitter](https://twitter.com/neardevhub) diff --git a/cargo-near/src/commands/new/prototype_for_project/rust-toolchain.toml b/cargo-near/src/commands/new/prototype_for_project/rust-toolchain.toml deleted file mode 100644 index 5879410d..00000000 --- a/cargo-near/src/commands/new/prototype_for_project/rust-toolchain.toml +++ /dev/null @@ -1,4 +0,0 @@ -[toolchain] -channel = "1.73.0" -components = ["rustfmt"] -targets = ["wasm32-unknown-unknown"] diff --git a/cargo-near/src/commands/new/prototype_for_project/src/lib.rs b/cargo-near/src/commands/new/prototype_for_project/src/lib.rs deleted file mode 100644 index b80b9327..00000000 --- a/cargo-near/src/commands/new/prototype_for_project/src/lib.rs +++ /dev/null @@ -1,78 +0,0 @@ -use near_sdk::borsh::{BorshDeserialize, BorshSerialize}; -use near_sdk::collections::LookupMap; -use near_sdk::{env, near_bindgen, AccountId, BorshStorageKey}; - -#[near_bindgen] -#[derive(BorshDeserialize, BorshSerialize)] -#[borsh(crate = "near_sdk::borsh")] -pub struct StatusMessage { - records: LookupMap, -} - -#[derive(BorshSerialize, BorshStorageKey)] -#[borsh(crate = "near_sdk::borsh")] -enum StorageKey { - StatusMessageRecords, -} - -impl Default for StatusMessage { - fn default() -> Self { - Self { - records: LookupMap::new(StorageKey::StatusMessageRecords), - } - } -} - -#[near_bindgen] -impl StatusMessage { - pub fn set_status(&mut self, message: String) { - let account_id = env::predecessor_account_id(); - self.records.insert(&account_id, &message); - } - - pub fn get_status(&self, account_id: AccountId) -> Option { - self.records.get(&account_id) - } -} - -#[cfg(not(target_arch = "wasm32"))] -#[cfg(test)] -mod tests { - use near_sdk::test_utils::{accounts, VMContextBuilder}; - use near_sdk::testing_env; - - use super::*; - - // Allows for modifying the environment of the mocked blockchain - fn get_context(predecessor_account_id: AccountId) -> VMContextBuilder { - let mut builder = VMContextBuilder::new(); - builder - .current_account_id(accounts(0)) - .signer_account_id(predecessor_account_id.clone()) - .predecessor_account_id(predecessor_account_id); - builder - } - - #[test] - fn set_get_message() { - let mut context = get_context(accounts(1)); - // Initialize the mocked blockchain - testing_env!(context.build()); - - // Set the testing environment for the subsequent calls - testing_env!(context.predecessor_account_id(accounts(1)).build()); - - let mut contract = StatusMessage::default(); - contract.set_status("hello".to_string()); - assert_eq!( - "hello".to_string(), - contract.get_status(accounts(1)).unwrap() - ); - } - - #[test] - fn get_nonexistent_message() { - let contract = StatusMessage::default(); - assert_eq!(None, contract.get_status("francis.near".parse().unwrap())); - } -} diff --git a/cargo-near/src/commands/new/prototype_for_project/tests/test_basics.rs b/cargo-near/src/commands/new/prototype_for_project/tests/test_basics.rs deleted file mode 100644 index de6963fd..00000000 --- a/cargo-near/src/commands/new/prototype_for_project/tests/test_basics.rs +++ /dev/null @@ -1,33 +0,0 @@ -use serde_json::json; - -#[tokio::test] -async fn test_contract_is_operational() -> Result<(), Box> { - let sandbox = near_workspaces::sandbox().await?; - let contract_wasm = near_workspaces::compile_project("./").await?; - - let contract = sandbox.dev_deploy(&contract_wasm).await?; - - let user1_account = sandbox.dev_create_account().await?; - let user2_account = sandbox.dev_create_account().await?; - - let outcome = user1_account - .call(contract.id(), "set_status") - .args_json(json!({"message": "test status"})) - .transact() - .await?; - assert!(outcome.is_success()); - - let user1_message_outcome = contract - .view("get_status") - .args_json(json!({"account_id": user1_account.id()})) - .await?; - assert_eq!(user1_message_outcome.json::()?, "test status"); - - let user2_message_outcome = contract - .view("get_status") - .args_json(json!({"account_id": user2_account.id()})) - .await?; - assert_eq!(user2_message_outcome.result, b"null"); - - Ok(()) -} From 72697a0cbb524cf6c2a250d12efc77c94284eee7 Mon Sep 17 00:00:00 2001 From: Vlad Frolov Date: Mon, 20 Nov 2023 17:20:31 +0100 Subject: [PATCH 8/9] addressed linter warnings --- cargo-near/src/commands/new/mod.rs | 4 ++-- cargo-near/src/util/mod.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cargo-near/src/commands/new/mod.rs b/cargo-near/src/commands/new/mod.rs index af28a73f..af02511f 100644 --- a/cargo-near/src/commands/new/mod.rs +++ b/cargo-near/src/commands/new/mod.rs @@ -25,7 +25,7 @@ impl NewContext { for new_project_file in NEW_PROJECT_FILES { let new_file_path = project_dir.join(new_project_file.file_path); - std::fs::create_dir_all(&new_file_path.parent().wrap_err_with(|| { + std::fs::create_dir_all(new_file_path.parent().wrap_err_with(|| { format!("Impossible to get parent for `{}`", new_file_path.display()) })?)?; std::fs::write( @@ -39,7 +39,7 @@ impl NewContext { std::process::Command::new("git") .arg("init") - .current_dir(&project_dir) + .current_dir(project_dir) .output() .wrap_err("Failed to execute process: `git init`")?; diff --git a/cargo-near/src/util/mod.rs b/cargo-near/src/util/mod.rs index 5c9ec441..c425eced 100644 --- a/cargo-near/src/util/mod.rs +++ b/cargo-near/src/util/mod.rs @@ -213,12 +213,12 @@ pub(crate) fn compile_project( let dylib_files = compile_artifact .filenames .iter() - .cloned() .filter(|f| { f.extension() .map(|e| e == artifact_extension) .unwrap_or(false) }) + .cloned() .collect(); let mut dylib_files_iter = Vec::into_iter(dylib_files); match (dylib_files_iter.next(), dylib_files_iter.next()) { From 0fa3cacc33e49bc28e85b47f3f5c931c5ecc0a59 Mon Sep 17 00:00:00 2001 From: Vlad Frolov Date: Mon, 20 Nov 2023 21:19:00 +0100 Subject: [PATCH 9/9] fix CI issues --- .github/workflows/release-plz.yml | 3 ++- integration-tests/src/lib.rs | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release-plz.yml b/.github/workflows/release-plz.yml index c9f20c24..631e5b0d 100644 --- a/.github/workflows/release-plz.yml +++ b/.github/workflows/release-plz.yml @@ -15,9 +15,10 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 + submodules: recursive token: ${{ secrets.MY_GITHUB_TOKEN }} - name: Install Rust toolchain uses: dtolnay/rust-toolchain@stable diff --git a/integration-tests/src/lib.rs b/integration-tests/src/lib.rs index 45554663..de023253 100644 --- a/integration-tests/src/lib.rs +++ b/integration-tests/src/lib.rs @@ -74,8 +74,7 @@ macro_rules! invoke_cargo_near { }; cargo_near::commands::build_command::build::run(args)?; }, - Some(cargo_near::commands::CliNearCommand::CreateDevAccount(_)) => todo!(), - Some(cargo_near::commands::CliNearCommand::Deploy(_)) => todo!(), + Some(_) => todo!(), None => () }