From ff0e4d2bea3e9404dff227c0dffaac786e4613c0 Mon Sep 17 00:00:00 2001 From: Esteban Date: Mon, 6 Mar 2023 15:01:36 -0300 Subject: [PATCH] feat(docker-container): create docker image from Dockerfile (#90) * feat(docker-container): Draft: create docker image from Dockerfile * feat(docker-container): Solved bug when building image #89 * feat(use-docker): run soroban-preview docker container in quickstart * feat(docker): add instructions to readme * feat(docker): solve soroban-cli config network bug * Review feedback * Pass cli args through to quickstart --------- Co-authored-by: Paul Bellamy --- Dockerfile | 19 +++++++++++++++++++ Makefile | 3 +++ README.md | 37 +++++++++++++++++++++++++++++++++---- docker/build.sh | 7 +++++++ initialize.sh | 21 +++++++++++++++------ quickstart.sh | 34 +++++++++++++++++++++++++++++++++- 6 files changed, 110 insertions(+), 11 deletions(-) create mode 100755 Dockerfile mode change 100644 => 100755 README.md create mode 100755 docker/build.sh diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 0000000..95971e5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +# Based on Preview 7 +# https://soroban.stellar.org/docs/reference/releases + +FROM ubuntu:20.04 + +RUN apt update && apt install -y curl + +RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rust_install.sh +RUN sh rust_install.sh -y +RUN echo $PATH +ENV PATH="$PATH:/root/.cargo/bin" +RUN rustup target add wasm32-unknown-unknown + +RUN apt install -y build-essential +RUN cargo install --locked --version 0.6.0 soroban-cli +# WORKDIR / +RUN mkdir /workspace +WORKDIR /workspace +ENV IS_USING_DOCKER=true diff --git a/Makefile b/Makefile index 6188b4f..cbf2787 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,9 @@ build-optimized: fmt ls -l "$$i"; \ done +build-docker: + docker build . --tag soroban-preview:7 + check: fmt cargo clippy --all-targets cargo clippy --release --target wasm32-unknown-unknown diff --git a/README.md b/README.md old mode 100644 new mode 100755 index b1b11ae..19b35bb --- a/README.md +++ b/README.md @@ -6,18 +6,39 @@ This is a [Next.js](https://nextjs.org/) project, demoing how to build a dapp fr backed by smart contracts on Stellar. ## Getting Started +___ ### Dependencies -1. `soroban-cli v0.4.0`. See https://soroban.stellar.org/docs/getting-started/setup#install-the-soroban-cli +1. `soroban-cli v0.6.0`. See https://soroban.stellar.org/docs/getting-started/setup#install-the-soroban-cli 2. `docker` (both Standalone and Futurenet backends require it). 3. `Node.js v17` 4. `Freighter wallet v2.9.1`. Download it from https://github.com/stellar/freighter/releases/tag/2.9.1 and Enable "Experimental Mode" in the settings (gear icon). +5. Build the `soroban-preview` docker image: +``` +make build-docker +``` +Building the docker image lets you avoid installing the specific version of soroban-cli in step (1), if desired. +___ ### Backend (Local Standalone Network) -1. Run the backend docker container with `./quickstart.sh standalone`, and wait for it to start. -2. Run `./initialize.sh standalone` to load the contracts and initialize it. +1. Run the backend docker containers and wait for them to start: + +``` +./quickstart.sh standalone +``` + +2. Load the contracts and initialize them: + +You can use your own local soroban-cli: +``` +./initialize.sh standalone +``` +Or run it inside the soroban-preview docker container: +``` +docker exec soroban-preview ./initialize.sh standalone +``` - Note: this state will be lost if the quickstart docker container is removed. 3. Add the Standalone custom network in Freighter | | | @@ -38,7 +59,15 @@ backed by smart contracts on Stellar. working by visiting http://localhost:8000/, and look at the `ingest_latest_ledger`, field. If it is `0`, the quickstart image is not ready yet. -2. Run `./initialize.sh futurenet` to load the contracts and initialize it. +2. Load the contracts and initialize it: +You can use your own local soroban-cli: +``` +./initialize.sh futurenet +``` +Or run it inside the soroban-preview docker container: +``` +docker exec soroban-preview ./initialize.sh futurenet +``` 3. Add the Futurenet custom network in Freighter (Note, the out-of-the-box "Future Net" network in Freighter will not work with a local quickstart container, so we need to add diff --git a/docker/build.sh b/docker/build.sh new file mode 100755 index 0000000..856bbe3 --- /dev/null +++ b/docker/build.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +# Build image and tag it with image name and version +docker build . \ + --tag soroban-preview:7 \ + --force-rm \ + --rm diff --git a/initialize.sh b/initialize.sh index 4eea4f2..0ea2b50 100755 --- a/initialize.sh +++ b/initialize.sh @@ -4,18 +4,24 @@ set -e NETWORK="$1" +# If soroban-cli is called inside the soroban-preview docker containter, +# it can call the stellar standalone container just using its name "stellar" +if [[ "$IS_USING_DOCKER" == "true" ]]; then + SOROBAN_RPC_HOST="http://stellar:8000" +else + SOROBAN_RPC_HOST="http://localhost:8000" +fi + +SOROBAN_RPC_URL="$SOROBAN_RPC_HOST/soroban/rpc" + case "$1" in standalone) echo "Using standalone network" - SOROBAN_RPC_HOST="http://localhost:8000" - SOROBAN_RPC_URL="$SOROBAN_RPC_HOST/soroban/rpc" SOROBAN_NETWORK_PASSPHRASE="Standalone Network ; February 2017" FRIENDBOT_URL="$SOROBAN_RPC_HOST/friendbot" ;; futurenet) echo "Using Futurenet network" - SOROBAN_RPC_HOST="http://localhost:8000" - SOROBAN_RPC_URL="$SOROBAN_RPC_HOST/soroban/rpc" SOROBAN_NETWORK_PASSPHRASE="Test SDF Future Network ; October 2022" FRIENDBOT_URL="https://friendbot-futurenet.stellar.org/" ;; @@ -25,12 +31,13 @@ futurenet) ;; esac -if !(soroban config network ls | grep "$NETWORK" 2>&1 >/dev/null); then +#if !(soroban config network ls | grep "$NETWORK" 2>&1 >/dev/null); then +# Always set a net configuration echo Add the $NETWORK network to cli client soroban config network add "$NETWORK" \ --rpc-url "$SOROBAN_RPC_URL" \ --network-passphrase "$SOROBAN_NETWORK_PASSPHRASE" -fi +#fi TOKEN_ADMIN_SECRET="SAKCFFFNCE7XAWYMYVRZQYKUK6KMUCDIINLWISJYTMYJLNR2QLCDLFVT" if !(soroban config identity ls | grep token-admin 2>&1 >/dev/null); then @@ -56,6 +63,8 @@ ARGS="--network $NETWORK --identity token-admin" echo Wrap the Stellar asset mkdir -p .soroban TOKEN_ID=$(soroban lab token wrap $ARGS --asset "EXT:$TOKEN_ADMIN_ADDRESS") +echo "Token wrapped succesfully with TOKEN_ID: $TOKEN_ID" + echo -n "$TOKEN_ID" > .soroban/token_id echo Build the crowdfund contract diff --git a/quickstart.sh b/quickstart.sh index 98970f0..211e1b8 100755 --- a/quickstart.sh +++ b/quickstart.sh @@ -17,11 +17,43 @@ futurenet) ;; esac +shift + +# Run the soroban-preview container +# Remember to do: +# make build-docker + +echo "Creating docker soroban network" +(docker network inspect soroban-network -f '{{.Id}}' 2>/dev/null) \ + || docker network create soroban-network + +echo "Searching for a previous soroban-preview docker container" +containerID=$(docker ps --filter="name=soroban-preview" --all --quiet) +if [[ ${containerID} ]]; then + echo "Start removing soroban-preview container." + docker rm --force soroban-preview + echo "Finished removing soroban-preview container." +else + echo "No previous soroban-preview container was found" +fi + +currentDir=$(pwd) +docker run -dti \ + --volume ${currentDir}:/workspace \ + --name soroban-preview \ + -p 8001:8000 \ + --ipc=host \ + --network soroban-network \ + soroban-preview:7 + +# Run the stellar quickstart image docker run --rm -ti \ --platform linux/amd64 \ --name stellar \ + --network soroban-network \ -p 8000:8000 \ stellar/quickstart:soroban-dev@sha256:81c23da078c90d0ba220f8fc93414d0ea44608adc616988930529c58df278739 \ $ARGS \ --enable-soroban-rpc \ - --protocol-version 20 + --protocol-version 20 \ + "$@" # Pass through args from the CLI