From 33ecf5b730a5587394096df8f516e6ed9395e21a Mon Sep 17 00:00:00 2001 From: david weil Date: Tue, 21 Nov 2023 12:24:30 -0300 Subject: [PATCH] Add workspace-enabled example contracts (was PR:#44) (#52) * feature: check contracts in workspace * fixes in CI * fixes in CI * fixes in CI: s/steps/step * fixes in CI: enable examples step * wip CI/CD: run contracts along regular ones, but with different step * wip CI/CD: log changed files also use the right variable to check for them * wip CI/CD: hopefuly preventing error on invalid character.. * wip CI/CD: changed approach to run both workspace contracts on same runner.. * wip CI/CD: whole workspace at once * wip CI/CD: specify workspace cargo * wip CI/CD: no test on workspace * wip CI/CD: removed debug info/vars * wip CI/CD: removed debug info/vars * updated the CI/CD to be ready when using/having cargo-contract v 4.0. * fix unnoticed error in contract * Update ci.yml removed redundant configuration Update ci.yml removed redundant configuration --- .github/workflows/ci.yml | 47 ++++++++++++++++++++-- workspace-contracts/.gitignore | 9 +++++ workspace-contracts/Cargo.toml | 10 +++++ workspace-contracts/flipper/Cargo.toml | 28 +++++++++++++ workspace-contracts/flipper/lib.rs | 35 ++++++++++++++++ workspace-contracts/incrementer/Cargo.toml | 24 +++++++++++ workspace-contracts/incrementer/lib.rs | 31 ++++++++++++++ 7 files changed, 181 insertions(+), 3 deletions(-) create mode 100644 workspace-contracts/.gitignore create mode 100644 workspace-contracts/Cargo.toml create mode 100644 workspace-contracts/flipper/Cargo.toml create mode 100644 workspace-contracts/flipper/lib.rs create mode 100644 workspace-contracts/incrementer/Cargo.toml create mode 100644 workspace-contracts/incrementer/lib.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4b866dc1..f44e7a6e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -54,6 +54,7 @@ jobs: **/*.md multi-contract-caller/** upgradeable-contracts/** + workspace-contracts/** ui/** **/frontend/** json: true @@ -71,8 +72,32 @@ jobs: files_ignore: | **/.gitignore **/*.md + workspace-contracts/** json: true + - name: Look for changes in workspace contracts + id: CHANGED_WORKSPACE_CONTRACTS + uses: tj-actions/changed-files@v39 + with: + dir_names: "true" + dir_names_exclude_current_dir: "true" + dir_names_max_depth: 2 + files: | + workspace-contracts/** + files_ignore: | + **/.gitignore + **/*.md + json: true + + + - name: List all changed files + run: | + echo " Changed workspace contracts " + for file in ${{ steps.CHANGED_WORKSPACE_CONTRACTS.outputs.all_changed_files }}; do + echo "$file was changed" + done + echo "no more files.." + - name: Build matrix id: build_matrix run: | @@ -80,6 +105,15 @@ jobs: <(echo "${{ steps.CHANGED_CONTRACTS.outputs.all_changed_files }}") \ <(echo "${{ steps.CHANGED_MULTI_AND_UPGRADEABLE_CONTRACTS.outputs.all_changed_files }}")) + if [ ${{steps.CHANGED_WORKSPACE_CONTRACTS.outputs.all_changed_files }} = "[]" ] + then + echo "Nothing important changed. Workspace contracts job will be skipped." + else + ALL_CHANGED_CONTRACTS=$(jq -s 'add' \ + <(echo "$ALL_CHANGED_CONTRACTS") \ + <(echo "[\"workspace-contracts\"]")) + fi + if [ $ALL_CHANGED_CONTRACTS = "[]" ] then echo "Nothing important changed. Checks job will be skipped." @@ -181,9 +215,16 @@ jobs: cargo contract --version - name: Build ${{ matrix.contract }} on ${{ matrix.platform }}-${{ matrix.toolchain }} - if: runner.os != 'Windows' + if: runner.os != 'Windows' && matrix.contract != 'workspace-contracts' run: cargo contract build --verbose --manifest-path=${{ matrix.contract }}/Cargo.toml; - name: Test ${{ matrix.contract }} on ${{ matrix.platform }}-${{ matrix.toolchain }} - if: runner.os != 'Windows' - run: cargo test --verbose --manifest-path=${{ matrix.contract }}/Cargo.toml; \ No newline at end of file + if: runner.os != 'Windows' && matrix.contract != 'workspace-contracts' + run: cargo test --verbose --manifest-path=${{ matrix.contract }}/Cargo.toml; + + - name: Build workspace ${{ matrix.contract }} on ${{ matrix.platform }}-${{ matrix.toolchain }} + if: runner.os != 'Windows' && matrix.contract == 'workspace-contracts' + run: | + echo FOLLOWING SEEMS TO WORK WITH CARGO-CONTRACT 4.0 + cargo contract build --verbose --manifest-path=workspace-contracts/flipper/Cargo.toml + cargo contract build --verbose --manifest-path=workspace-contracts/incrementer/Cargo.toml diff --git a/workspace-contracts/.gitignore b/workspace-contracts/.gitignore new file mode 100644 index 00000000..bf910de1 --- /dev/null +++ b/workspace-contracts/.gitignore @@ -0,0 +1,9 @@ +# Ignore build artifacts from the local tests sub-crate. +/target/ + +# Ignore backup files creates by cargo fmt. +**/*.rs.bk + +# Remove Cargo.lock when creating an executable, leave it for libraries +# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock +Cargo.lock \ No newline at end of file diff --git a/workspace-contracts/Cargo.toml b/workspace-contracts/Cargo.toml new file mode 100644 index 00000000..dd7cd9df --- /dev/null +++ b/workspace-contracts/Cargo.toml @@ -0,0 +1,10 @@ +[workspace] +members = ["*"] +exclude = [".cargo", "target"] +resolver = "2" + +[workspace.dependencies] +ink = { version = "4.3.0", default-features = false } +ink_e2e = "4.3.0" +scale = { package = "parity-scale-codec", version = "=3.6.5", default-features = false, features = ["derive"] } +scale-info = { version = "2.6", default-features = false, features = ["derive"] } diff --git a/workspace-contracts/flipper/Cargo.toml b/workspace-contracts/flipper/Cargo.toml new file mode 100644 index 00000000..78e55629 --- /dev/null +++ b/workspace-contracts/flipper/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "flipper" +version = "4.3.0" +authors = ["Parity Technologies "] +edition = "2021" +publish = false + +[dependencies] +ink = { workspace = true } + +scale = { workspace = true, package = "parity-scale-codec" } +scale-info = { workspace = true, optional = true } + +[dev-dependencies] +ink_e2e = { workspace = true } + +[lib] +path = "lib.rs" + +[features] +default = ["std"] +std = [ + "ink/std", + "scale/std", + "scale-info/std", +] +ink-as-dependency = [] +e2e-tests = [] diff --git a/workspace-contracts/flipper/lib.rs b/workspace-contracts/flipper/lib.rs new file mode 100644 index 00000000..a0dc3655 --- /dev/null +++ b/workspace-contracts/flipper/lib.rs @@ -0,0 +1,35 @@ +#![cfg_attr(not(feature = "std"), no_std, no_main)] + +#[ink::contract] +pub mod flipper { + #[ink(storage)] + pub struct Flipper { + value: bool, + } + + impl Flipper { + /// Creates a new flipper smart contract initialized with the given value. + #[ink(constructor)] + pub fn new(init_value: bool) -> Self { + Self { value: init_value } + } + + /// Creates a new flipper smart contract initialized to `false`. + #[ink(constructor)] + pub fn new_default() -> Self { + Self::new(Default::default()) + } + + /// Flips the current value of the Flipper's boolean. + #[ink(message)] + pub fn flip(&mut self) { + self.value = !self.value; + } + + /// Returns the current value of the Flipper's boolean. + #[ink(message)] + pub fn get(&self) -> bool { + self.value + } + } +} diff --git a/workspace-contracts/incrementer/Cargo.toml b/workspace-contracts/incrementer/Cargo.toml new file mode 100644 index 00000000..f7c42407 --- /dev/null +++ b/workspace-contracts/incrementer/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "incrementer" +version = "4.3.0" +authors = ["Parity Technologies "] +edition = "2021" +publish = false + +[dependencies] +ink = { workspace = true } + +scale = { workspace = true, package = "parity-scale-codec" } +scale-info = { workspace = true, optional = true } + +[lib] +path = "lib.rs" + +[features] +default = ["std"] +std = [ + "ink/std", + "scale/std", + "scale-info/std", +] +ink-as-dependency = [] diff --git a/workspace-contracts/incrementer/lib.rs b/workspace-contracts/incrementer/lib.rs new file mode 100644 index 00000000..ccddf463 --- /dev/null +++ b/workspace-contracts/incrementer/lib.rs @@ -0,0 +1,31 @@ +#![cfg_attr(not(feature = "std"), no_std, no_main)] + +#[ink::contract] +mod incrementer { + #[ink(storage)] + pub struct Incrementer { + value: i32, + } + + impl Incrementer { + #[ink(constructor)] + pub fn new(init_value: i32) -> Self { + Self { value: init_value } + } + + #[ink(constructor)] + pub fn new_default() -> Self { + Self::new(Default::default()) + } + + #[ink(message)] + pub fn inc(&mut self, by: i32) { + self.value = self.value.saturating_add(by); + } + + #[ink(message)] + pub fn get(&self) -> i32 { + self.value + } + } +}