From 2e81e032c3a43bec10298f81e2875a863c50f76d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Tue, 11 Jul 2023 23:04:37 +0300 Subject: [PATCH 01/10] Rename workflow. --- .github/workflows/{publish.yml => publish-template.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{publish.yml => publish-template.yml} (100%) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish-template.yml similarity index 100% rename from .github/workflows/publish.yml rename to .github/workflows/publish-template.yml From bd3a411e453e42dd7c0c14d062b9772b7ca41b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Tue, 11 Jul 2023 23:17:03 +0300 Subject: [PATCH 02/10] Add some comments (workflow). --- .../{publish-template.yml => publish-templates.yml} | 6 ++++++ 1 file changed, 6 insertions(+) rename .github/workflows/{publish-template.yml => publish-templates.yml} (57%) diff --git a/.github/workflows/publish-template.yml b/.github/workflows/publish-templates.yml similarity index 57% rename from .github/workflows/publish-template.yml rename to .github/workflows/publish-templates.yml index be9201f..898ca08 100644 --- a/.github/workflows/publish-template.yml +++ b/.github/workflows/publish-templates.yml @@ -1,3 +1,9 @@ +# Publishes devcontainer template artifacts. See them here: +# https://github.com/orgs/multiversx/packages?repo_name=mx-template-devcontainers +# +# - Package "mx-template-devcontainers": not versioned, tagged as "latest" +# - Package "smart-contracts-rust": versioned according to src/smart-contracts-rust/devcontainer-template.json + name: Publish templates on: From fd65464e4b05361624358f0d07326f73f1840c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Tue, 11 Jul 2023 23:30:05 +0300 Subject: [PATCH 03/10] Add workflow to publish docker image. --- .github/workflows/publish-image-rust.yml | 53 ++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 .github/workflows/publish-image-rust.yml diff --git a/.github/workflows/publish-image-rust.yml b/.github/workflows/publish-image-rust.yml new file mode 100644 index 0000000..2fcde8b --- /dev/null +++ b/.github/workflows/publish-image-rust.yml @@ -0,0 +1,53 @@ +name: Build & publish Docker images + +on: + pull_request: + workflow_dispatch: + inputs: + latest: + description: "Push with tag 'latest'" + required: false + type: boolean + +jobs: + push_to_registry: + name: Push Docker image to Docker Hub + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v2 + + - name: Set environment variables + run: | + VERSION=v$(jq -r '.["version"]' ./src/smart-contracts-rust/devcontainer-template.json) + echo "VERSION=$VERSION" >> $GITHUB_ENV + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push + uses: docker/build-push-action@v3 + with: + context: ./resources/smart-contracts-rust + push: true + platforms: linux/amd64 + file: ./resources/smart-contracts-rust/Dockerfile + tags: multiversx/devcontainer-smart-contracts-rust:${{ env.VERSION }} + + - name: Push to "latest" + uses: docker/build-push-action@v3 + with: + context: ./resources/smart-contracts-rust + push: ${{ inputs.latest }} + platforms: linux/amd64 + file: ./resources/smart-contracts-rust/Dockerfile + tags: multiversx/devcontainer-smart-contracts-rust:latest From 9fe328a9fd24317fd3978710cbbf126a0eaffd5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Tue, 11 Jul 2023 23:41:07 +0300 Subject: [PATCH 04/10] Update readme, workflows. --- .github/workflows/publish-image-rust.yml | 3 +-- README.md | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/publish-image-rust.yml b/.github/workflows/publish-image-rust.yml index 2fcde8b..0bf53d8 100644 --- a/.github/workflows/publish-image-rust.yml +++ b/.github/workflows/publish-image-rust.yml @@ -1,7 +1,6 @@ -name: Build & publish Docker images +name: Publish Docker image (rust) on: - pull_request: workflow_dispatch: inputs: latest: diff --git a/README.md b/README.md index 3fd56c4..74731fb 100644 --- a/README.md +++ b/README.md @@ -46,13 +46,15 @@ Then, in VSCode, launch the command `Dev Containers: Rebuild and Reopen in Conta ### Publish images -Build and publish the Docker images: +Locally: ``` docker build ./resources/smart-contracts-rust -t multiversx/devcontainer-smart-contracts-rust:next -f ./resources/smart-contracts-rust/Dockerfile docker push multiversx/devcontainer-smart-contracts-rust:next ``` +On Github, trigger the GitHub workflow(s) `publish-image-*.yml` to publish the image(s). Ideally, do this on the `main` branch. + ### Publish templates -Trigger the GitHub workflow `publish.yml` to publish the templates. Ideally, do this on the `main` branch. +Trigger the GitHub workflow `publish-templates.yml` to publish the templates. Ideally, do this on the `main` branch. From d4fa57de92eb3e5f3bfef497a002953b8aef11b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Tue, 11 Jul 2023 23:41:21 +0300 Subject: [PATCH 05/10] Update reference to mxpy, rust etc. --- resources/smart-contracts-rust/Dockerfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/smart-contracts-rust/Dockerfile b/resources/smart-contracts-rust/Dockerfile index 3641657..0e94ba7 100644 --- a/resources/smart-contracts-rust/Dockerfile +++ b/resources/smart-contracts-rust/Dockerfile @@ -4,9 +4,9 @@ ARG USERNAME=developer ARG USER_UID=1000 ARG USER_GID=$USER_UID -ARG VERSION_MXPY="v6.1.3" -ARG VERSION_RUST="nightly-2022-10-16" -ARG VERSION_WASM_OPT="version_105" +ARG VERSION_MXPY="v7.0.0" +ARG VERSION_RUST="nightly-2023-04-24" +ARG VERSION_WASM_OPT="version_112" ARG VERSION_VMTOOLS="v1.4.60" # Create the user From ebd6d8aee58b3e634191f6422d0f2090f2870fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Tue, 11 Jul 2023 23:58:37 +0300 Subject: [PATCH 06/10] Add more contract samples. --- .github/workflows/publish-templates.yml | 2 +- README.md | 2 +- resources/smart-contracts-rust/post_create_command.py | 10 +++++++++- src/smart-contracts-rust/devcontainer-template.json | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/workflows/publish-templates.yml b/.github/workflows/publish-templates.yml index 898ca08..5da4eaf 100644 --- a/.github/workflows/publish-templates.yml +++ b/.github/workflows/publish-templates.yml @@ -1,4 +1,4 @@ -# Publishes devcontainer template artifacts. See them here: +# Publish devcontainer template artifacts. See them here: # https://github.com/orgs/multiversx/packages?repo_name=mx-template-devcontainers # # - Package "mx-template-devcontainers": not versioned, tagged as "latest" diff --git a/README.md b/README.md index 74731fb..50606d7 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ cp -R "src/smart-contracts-rust/.devcontainer" "/tmp/test-workspace" && \ code "/tmp/test-workspace/" ``` -Then, in VSCode, launch the command `Dev Containers: Rebuild and Reopen in Container`, wait, then inspect the environment. +Then, in VSCode, launch the command `Dev Containers: Rebuild and Reopen in Container`, wait, then inspect the environment (e.g. check version of `mxpy`, `rust`, build the sample smart contracts, verify output of rust-analyzer). ### Publish images diff --git a/resources/smart-contracts-rust/post_create_command.py b/resources/smart-contracts-rust/post_create_command.py index e886e99..ff10003 100644 --- a/resources/smart-contracts-rust/post_create_command.py +++ b/resources/smart-contracts-rust/post_create_command.py @@ -57,7 +57,15 @@ def prepare_gitignore(): def add_examples_if_empty_workspace(): subfolders = glob('*/') if len(subfolders) == 0: - subprocess.check_output(["mxpy", "contract", "new", "--template", "adder", "adder"]) + sample_contracts = [ + "adder", + "ping-pong-egld", + "lottery-esdt", + "crowdfunding-esdt", + ] + + for contract in sample_contracts: + subprocess.check_output(["mxpy", "contract", "new", "--template", contract, contract]) if __name__ == "__main__": diff --git a/src/smart-contracts-rust/devcontainer-template.json b/src/smart-contracts-rust/devcontainer-template.json index b5f646c..eeaa60e 100644 --- a/src/smart-contracts-rust/devcontainer-template.json +++ b/src/smart-contracts-rust/devcontainer-template.json @@ -1,6 +1,6 @@ { "id": "smart-contracts-rust", - "version": "0.1.2", + "version": "0.1.3", "name": "MultiversX: Smart Contracts Development (Rust)", "description": "Develop smart contracts for MultiversX. Includes Rust, mxpy, VSCode extensions etc.", "documentationURL": "https://github.com/multiversx/mx-template-devcontainers/blob/main/src/smart-contracts-rust", From 7e19e15d564024c40c4d8552070ce0ae3de8a590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Wed, 12 Jul 2023 00:56:01 +0300 Subject: [PATCH 07/10] In Docker image, install testwallets, as well. --- resources/smart-contracts-rust/Dockerfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/resources/smart-contracts-rust/Dockerfile b/resources/smart-contracts-rust/Dockerfile index 0e94ba7..0c6493b 100644 --- a/resources/smart-contracts-rust/Dockerfile +++ b/resources/smart-contracts-rust/Dockerfile @@ -45,6 +45,9 @@ RUN mxpy deps install rust --tag=${VERSION_RUST} && rm -rf ${MULTIVERSX}/vendor- RUN mxpy deps install wasm-opt --tag=${VERSION_WASM_OPT} && rm ${MULTIVERSX}/*.tar.gz RUN mxpy deps install vmtools --tag=${VERSION_VMTOOLS} && rm ${MULTIVERSX}/*.tar.gz && sudo rm -rf ${MULTIVERSX}/golang +# Install test wallets +RUN mxpy deps install testwallets && rm ${MULTIVERSX}/*.tar.gz + ENV PATH="${MULTIVERSX}/vendor-rust/bin:${MULTIVERSX}/vmtools:${PATH}" ENV CARGO_HOME="${MULTIVERSX}/vendor-rust" ENV RUSTUP_HOME="${MULTIVERSX}/vendor-rust" From 5dcd250e022730f9dd66b4cfa7b8221d3d666eb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Wed, 12 Jul 2023 01:16:04 +0300 Subject: [PATCH 08/10] Update readme - samples for running without VSCode. --- README.md | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/README.md b/README.md index 50606d7..d50498a 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,101 @@ In Visual Studio code, the following MultiversX dev containers are available: - [MultiversX: Smart Contracts Development (Rust)](src/smart-contracts-rust) - ... +### Using the Docker images without VSCode + +If you'd like to use the Docker image(s) to invoke `mxpy` commands and build contracts directly from a terminal, without VSCode's devcontainers feature, below are a few examples. + +First, let's export some environment variables: + +``` +export IMAGE=multiversx/devcontainer-smart-contracts-rust:next +export DOCKER_USER=$(id -u):$(id -g) + +# Mandatory: run the container as the current user (should be 1000:1000), not as root. +# Suggestion: use a stateless container; remove it after use (--rm). +# Suggestion: map the current directory to "/data" in the container. +export RUN="docker run --user=${DOCKER_USER} --rm -it --volume $(pwd):/data" +``` + +Run the container and do a quick inspection: + +``` +${RUN} ${IMAGE} whoami +${RUN} ${IMAGE} mxpy --version +${RUN} ${IMAGE} mxpy deps check rust +``` + +Clone `mx-contracts-rs` locally, then build a few contracts within the container: + +``` +git clone https://github.com/multiversx/mx-contracts-rs.git --single-branch --depth=1 + +${RUN} ${IMAGE} mxpy contract build /data/mx-contracts-rs/contracts/adder +stat ./mx-contracts-rs/contracts/adder/output/adder.wasm + +${RUN} ${IMAGE} mxpy contract build /data/mx-contracts-rs/contracts/ping-pong-egld +stat ./mx-contracts-rs/contracts/ping-pong-egld/output/ping-pong-egld.wasm +``` + +Deploy a previously-built smart contract on Testnet: + +``` +${RUN} ${IMAGE} mxpy contract deploy \ + --bytecode /data/mx-contracts-rs/contracts/adder/output/adder.wasm \ + --arguments 0 \ + --pem /home/developer/multiversx-sdk/testwallets/latest/users/frank.pem \ + --recall-nonce \ + --gas-limit 5000000 \ + --chain T \ + --proxy https://testnet-gateway.multiversx.com \ + --send +``` + +Call a function of a previously-deployed smart contract: + +``` +${RUN} ${IMAGE} mxpy contract call \ + erd1qqqqqqqqqqqqqpgq5v3ra8mxjkv6g2pues9tdkkzwmtm9fdht7asp8wtnr \ + --function "add" \ + --arguments 42 \ + --pem /home/developer/multiversx-sdk/testwallets/latest/users/frank.pem \ + --recall-nonce \ + --gas-limit 5000000 \ + --chain T \ + --proxy https://testnet-gateway.multiversx.com \ + --send +``` + +Query a smart contract: + +``` +${RUN} ${IMAGE} mxpy contract query \ + erd1qqqqqqqqqqqqqpgq5v3ra8mxjkv6g2pues9tdkkzwmtm9fdht7asp8wtnr \ + --function "getSum" \ + --proxy https://testnet-gateway.multiversx.com +``` + +Setup a localnet (make sure to set the `--workdir`, as well), then inspect the generated files (on the mapped volume): + +``` +${RUN} --workdir /data ${IMAGE} mxpy localnet setup +cat ./localnet.toml +tree -L 1 ./localnet +``` + +Start the localnet (make sure to publish the necessary ports; see the generated `localnet.toml`): + +``` +${RUN} --workdir /data --publish 7950:7950 ${IMAGE} mxpy localnet start +``` + +You can pause the localnet by simply stopping the container, then restart it by invoking the `start` command again. + +In a separate terminal, inspect an endpoint of the localnet's Proxy (as a smoke test): + +``` +curl http://127.0.0.1:7950/network/config | jq +``` ## For maintainers of the templates From 1557e2cb1777066fec532e9fa17e4e41343d8f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Wed, 12 Jul 2023 01:18:31 +0300 Subject: [PATCH 09/10] Adjust readme headers. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d50498a..277949a 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ In Visual Studio code, the following MultiversX dev containers are available: - [MultiversX: Smart Contracts Development (Rust)](src/smart-contracts-rust) - ... -### Using the Docker images without VSCode +## Using the Docker images without VSCode If you'd like to use the Docker image(s) to invoke `mxpy` commands and build contracts directly from a terminal, without VSCode's devcontainers feature, below are a few examples. From 766aa1cc2d09aa54be39737fa496c7a144c13913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Wed, 12 Jul 2023 13:24:14 +0300 Subject: [PATCH 10/10] Fix after self review. --- README.md | 8 ++++---- src/smart-contracts-rust/.devcontainer/devcontainer.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 277949a..8b6cc83 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ If you'd like to use the Docker image(s) to invoke `mxpy` commands and build con First, let's export some environment variables: ``` -export IMAGE=multiversx/devcontainer-smart-contracts-rust:next +export IMAGE=multiversx/devcontainer-smart-contracts-rust:latest export DOCKER_USER=$(id -u):$(id -g) # Mandatory: run the container as the current user (should be 1000:1000), not as root. @@ -126,7 +126,7 @@ Resources: Build the Docker images for local testing: ``` -docker build ./resources/smart-contracts-rust -t multiversx/devcontainer-smart-contracts-rust:next -f ./resources/smart-contracts-rust/Dockerfile +docker build ./resources/smart-contracts-rust -t multiversx/devcontainer-smart-contracts-rust:latest -f ./resources/smart-contracts-rust/Dockerfile ``` ### Test the templates @@ -144,8 +144,8 @@ Then, in VSCode, launch the command `Dev Containers: Rebuild and Reopen in Conta Locally: ``` -docker build ./resources/smart-contracts-rust -t multiversx/devcontainer-smart-contracts-rust:next -f ./resources/smart-contracts-rust/Dockerfile -docker push multiversx/devcontainer-smart-contracts-rust:next +docker build ./resources/smart-contracts-rust -t multiversx/devcontainer-smart-contracts-rust:latest -f ./resources/smart-contracts-rust/Dockerfile +docker push multiversx/devcontainer-smart-contracts-rust:latest ``` On Github, trigger the GitHub workflow(s) `publish-image-*.yml` to publish the image(s). Ideally, do this on the `main` branch. diff --git a/src/smart-contracts-rust/.devcontainer/devcontainer.json b/src/smart-contracts-rust/.devcontainer/devcontainer.json index 476b15d..2592fc1 100644 --- a/src/smart-contracts-rust/.devcontainer/devcontainer.json +++ b/src/smart-contracts-rust/.devcontainer/devcontainer.json @@ -1,6 +1,6 @@ { "name": "MultiversX Smart Contracts", - "image": "multiversx/devcontainer-smart-contracts-rust:next", + "image": "multiversx/devcontainer-smart-contracts-rust:latest", // Features to add to the dev container. More info: https://containers.dev/features. "features": { "ghcr.io/devcontainers/features/docker-in-docker:2": {