Skip to content

Commit

Permalink
Merge pull request #10 from multiversx/next-11
Browse files Browse the repository at this point in the history
Update image, adjust Github Workflows etc.
  • Loading branch information
andreibancioiu committed Jul 12, 2023
2 parents b3e7580 + 766aa1c commit 9f186d5
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 12 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/publish-image-rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Publish Docker image (rust)

on:
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 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"
# - Package "smart-contracts-rust": versioned according to src/smart-contracts-rust/devcontainer-template.json

name: Publish templates

on:
Expand Down
109 changes: 103 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:latest
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

Expand All @@ -31,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
Expand All @@ -42,17 +137,19 @@ 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

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
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.

### 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.
9 changes: 6 additions & 3 deletions resources/smart-contracts-rust/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
10 changes: 9 additions & 1 deletion resources/smart-contracts-rust/post_create_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
2 changes: 1 addition & 1 deletion src/smart-contracts-rust/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/smart-contracts-rust/devcontainer-template.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 9f186d5

Please sign in to comment.