Skip to content

Commit

Permalink
Add deploy.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
Sniezka1927 committed Dec 18, 2023
1 parent dd12da7 commit e3beb21
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
target
**/*.rs.bk
node_modules
.vscode/
.vscode/
temp_file
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions scripts/addresses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"INVARIANT_CODE_HASH": "0xf8df019beec758c3ce53f9b697ecf29c7b9749fe5f3cb7c21ace0f86609c44a4",
"INVARIANT_ADDRESS": "5FkbmjoQgz8XHmnj9BKLPnq8YsD5ZZP1RCTcwzyPta5LTri5"
}
102 changes: 102 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions scripts/update_contract_addresses.sh
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions scripts/update_contract_metadata.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e3beb21

Please sign in to comment.