From e3beb21c89cc08a34bc57210027b883d06dcef42 Mon Sep 17 00:00:00 2001 From: Sniezka Date: Mon, 18 Dec 2023 10:53:36 +0100 Subject: [PATCH] Add deploy.sh --- .gitignore | 3 +- Makefile | 17 +++++ scripts/addresses.json | 4 ++ scripts/deploy.sh | 102 +++++++++++++++++++++++++++ scripts/update_contract_addresses.sh | 8 +++ scripts/update_contract_metadata.sh | 17 +++++ 6 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 Makefile create mode 100644 scripts/addresses.json create mode 100755 scripts/deploy.sh create mode 100755 scripts/update_contract_addresses.sh create mode 100644 scripts/update_contract_metadata.sh diff --git a/.gitignore b/.gitignore index 5fe90ea2..4b5cef71 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ target **/*.rs.bk node_modules -.vscode/ \ No newline at end of file +.vscode/ +temp_file \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..f81d598a --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +PHONY: chain-clean chain-restart chain-start + +chain-stop: + @ps aux | grep substrate-contracts-node | tr -s ' ' | cut -d ' ' -f 2 | xargs -r kill -9 + +chain-clean: + @substrate-contracts-node purge-chain -y + +chain-restart: chain-clean chain-start + +chain-start: + substrate-contracts-node --rpc-port 9944 --dev + +setup: + @./scripts/deploy.sh + @./scripts/update_contract_addresses.sh + @./scripts/update_contract_metadata.sh \ No newline at end of file diff --git a/scripts/addresses.json b/scripts/addresses.json new file mode 100644 index 00000000..ff527033 --- /dev/null +++ b/scripts/addresses.json @@ -0,0 +1,4 @@ +{ + "INVARIANT_CODE_HASH": "0xf8df019beec758c3ce53f9b697ecf29c7b9749fe5f3cb7c21ace0f86609c44a4", + "INVARIANT_ADDRESS": "5FkbmjoQgz8XHmnj9BKLPnq8YsD5ZZP1RCTcwzyPta5LTri5" +} diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100755 index 00000000..ffd4fc80 --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,102 @@ +#!/usr/bin/env bash + +# This script does the following: +# * deploys Highlighted Posts and Bulletin Board contracts +# * instantiates them +# * stores addreses in the `addresses.json` file in the current directory +# +# What it does not do: +# * it doesn't build the contracts - assumes they're already built + +set -euo pipefail + +# Quiet versions of pushd and popd +pushd () { + command pushd "$@" > /dev/null +} + +popd () { + command popd "$@" > /dev/null +} + +cd .. + +CONTRACTS_PATH=$(pwd) +echo "Path ${CONTRACTS_PATH}" + +NODE_URL=${NODE_URL:="ws://localhost:9944"} +AUTHORITY_SEED=${AUTHORITY_SEED:="//Alice"} + +echo "node=${NODE_URL}" +echo "authority_seed=${AUTHORITY_SEED}" + +function upload_contract { + + local __resultvar=$1 + local contract_name=$2 + + echo "contract_name ${contract_name}" + + + pushd "$CONTRACTS_PATH"/$contract_name + + echo "Uploading ${contract_name}" + + # --- UPLOAD CONTRACT CODE + + code_hash=$(cargo contract upload --quiet --url "$NODE_URL" --suri "$AUTHORITY_SEED" --execute --skip-confirm) + code_hash=$(echo "${code_hash}" | grep hash | tail -1 | cut -c 14-) + + eval $__resultvar=${code_hash} + + popd +} + +function extract_contract_addresses { + jq '.events[] | select((.pallet == "Contracts") and (.name = "Instantiated")) | .fields[] | select(.name == "contract") | .value.Literal' +} + +function extract_from_quotes { + echo $1 | tr -d '"' +} + +upload_contract INVARIANT_CODE_HASH ./ +echo "Bulletin Board code hash: ${INVARIANT_CODE_HASH}" + +# --- instantiate contract + +pushd ${CONTRACTS_PATH} + +# Using temporary file as piping JSON from env variable crates problems with escaping. +temp_file=$(mktemp) +# Remove temporary file when finished. +trap "rm -f $temp_file" 0 2 3 15 + +SALT=${INVARIANT_VERSION:-12389012} +INVARIANT_CONTRACT_FILE="target/ink/contract.contract" + +echo "Instantiating Bulletin Board contract (version: ${SALT})" +# cargo contract instantiate --url "$NODE_URL" --salt ${SALT} --suri "$AUTHORITY_SEED" $INVARIANT_CONTRACT_FILE --constructor new --args "0" --execute --skip-confirm --output-json > temp_file + +# No salt instantiation +cargo contract instantiate --url "$NODE_URL" --suri "$AUTHORITY_SEED" $INVARIANT_CONTRACT_FILE --constructor new --args "0" --execute --skip-confirm --output-json > temp_file + +INVARIANT_ADDRESS=$(cat temp_file | jq '.events[] | select((.pallet == "Contracts") and (.name = "Instantiated")) | .fields[] | select(.name == "contract") | .value.Literal' | tail -1 | tr -d '"') +if [[ -z BULLETIN_BOARD_ADDRESS && -v BULLETIN_BOARD_ADDRESS ]]; then + echo "Empty BULLETIN_BOARD_ADDRESS" + exit 1 +fi + +echo "Invariant instance address: ${INVARIANT_ADDRESS}" + +popd + +jq -n --arg INVARIANT_CODE_HASH "$INVARIANT_CODE_HASH" \ + --arg INVARIANT_ADDRESS "$INVARIANT_ADDRESS" \ + '{ + INVARIANT_CODE_HASH: $INVARIANT_CODE_HASH, + INVARIANT_ADDRESS: $INVARIANT_ADDRESS + }' > ${PWD}/scripts/addresses.json + +echo "Contract addresses stored in addresses.json" +exit 0 \ No newline at end of file diff --git a/scripts/update_contract_addresses.sh b/scripts/update_contract_addresses.sh new file mode 100755 index 00000000..c97713a9 --- /dev/null +++ b/scripts/update_contract_addresses.sh @@ -0,0 +1,8 @@ +!/usr/bin/env bash + +# This script copies `addresses.json` files from ./scriptes directory into frontend metadata. +# This is required so that the frontend can interact with deployed contracts. +set -euo pipefail + +cp $(pwd)/scripts/addresses.json $(pwd)/frontend/src/metadata/ +exit 0 \ No newline at end of file diff --git a/scripts/update_contract_metadata.sh b/scripts/update_contract_metadata.sh new file mode 100644 index 00000000..ee35da19 --- /dev/null +++ b/scripts/update_contract_metadata.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +# This script copies `metadata.json` files from contracts' target directories into frontend metadata. +# This is required so that the frontend can interact with deployed contracts. +set -euo pipefail + +CONTRACTS_PATH=$(pwd)/contracts + +METADATA_FILE=/target/ink/metadata.json +BULLETIN_BOARD_METADATA=$(pwd)/frontend/src/metadata/metadata_bulletin_board.json +HIGHLIHTED_POSTS_METADATA=$(pwd)/frontend/src/metadata/metadata_highlighted_posts.json + +cp ${CONTRACTS_PATH}/bulletin_board/target/ink/bulletin_board.json ${BULLETIN_BOARD_METADATA} +cp ${CONTRACTS_PATH}/highlighted_posts/target/ink/highlighted_posts.json ${HIGHLIHTED_POSTS_METADATA} + +echo "Contract metadata updated" +exit 0 \ No newline at end of file