From 43c97925e25822d85f16b75dfd2c35a682dd2cee Mon Sep 17 00:00:00 2001 From: hu55a1n1 Date: Tue, 14 Nov 2023 16:16:14 -0800 Subject: [PATCH] Add scripts --- bisenzone-cw-mvp/scripts/build-contract.sh | 23 ++++++++ bisenzone-cw-mvp/scripts/deploy-contract.sh | 60 +++++++++++++++++++++ bisenzone-cw-mvp/scripts/init-node.sh | 26 +++++++++ bisenzone-cw-mvp/scripts/keygen.sh | 10 ++++ bisenzone-cw-mvp/scripts/run-node.sh | 26 +++++++++ 5 files changed, 145 insertions(+) create mode 100755 bisenzone-cw-mvp/scripts/build-contract.sh create mode 100755 bisenzone-cw-mvp/scripts/deploy-contract.sh create mode 100755 bisenzone-cw-mvp/scripts/init-node.sh create mode 100755 bisenzone-cw-mvp/scripts/keygen.sh create mode 100755 bisenzone-cw-mvp/scripts/run-node.sh diff --git a/bisenzone-cw-mvp/scripts/build-contract.sh b/bisenzone-cw-mvp/scripts/build-contract.sh new file mode 100755 index 0000000..eec2412 --- /dev/null +++ b/bisenzone-cw-mvp/scripts/build-contract.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# Build and optimize the contract to output a WASM binary that can be deployed to a CosmWasm chain. + +set -euo pipefail + +if ! [ -f "Cargo.toml" ]; then + echo "❌ Error: Cannot find 'Cargo.toml' in current directory. Make sure this command is run from the contract's source directory" + exit 1 +fi + +echo "👷 Building and optimizing the contract..." +echo "===========================================" + +RUSTFLAGS='-C link-arg=-s' cargo wasm + +docker run --rm -v "$(pwd)":/code \ + --mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \ + --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \ + cosmwasm/rust-optimizer:0.15.0 + +echo "" +echo "🎉 Contract build and optimization completed successfully!" diff --git a/bisenzone-cw-mvp/scripts/deploy-contract.sh b/bisenzone-cw-mvp/scripts/deploy-contract.sh new file mode 100755 index 0000000..ab8db9e --- /dev/null +++ b/bisenzone-cw-mvp/scripts/deploy-contract.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +# Deploy the specified contract's `WASM_BIN` to the chain specified by `CHAIN_ID` using the `USER_ADDR` account. + +set -eo pipefail + +usage() { + echo "Usage: $0 WASM_BIN [COUNT]" + echo "Example: $0 artifacts/cw_mtcs.wasm" + exit 1 +} + +if [ -z "$1" ]; then + echo "❌ Error: Missing WASM_BIN parameter. Please check if all parameters were specified." + usage +fi + +if [ "$#" -gt 9 ]; then + echo "❌ Error: Incorrect number of parameters." + usage +fi + +USER_ADDR=${USER_ADDR:-$(wasmd keys show -a admin)} +WASM_BIN="$1" +CHAIN_ID=${CHAIN_ID:-testing} +LABEL=${LABEL:-bisenzone-mvp} +COUNT=${COUNT:-0} + +TXFLAG="--chain-id ${CHAIN_ID} --gas-prices 0.0025ucosm --gas auto --gas-adjustment 1.3" + +echo "🚀 Deploying WASM contract '${WASM_BIN}' on chain '${CHAIN_ID}' using account '${USER_ADDR}'..." +echo "====================================================================" + +RES=$(wasmd tx wasm store "$WASM_BIN" --from "$USER_ADDR" $TXFLAG -y --output json) +sleep 8 +TX_HASH=$(echo $RES | jq -r '.["txhash"]') +RES=$(wasmd query tx "$TX_HASH" --output json) +CODE_ID=$(echo $RES | jq -r '.logs[0].events[1].attributes[1].value') + +echo "" +echo "🚀 Instantiating contract with the following parameters:" +echo "--------------------------------------------------------" +echo "Label: ${LABEL}" +echo "Count: ${COUNT}" +echo "--------------------------------------------------------" + +wasmd tx wasm instantiate "$CODE_ID" "{\"count\":${COUNT}}" --from "$USER_ADDR" --label $LABEL $TXFLAG -y --no-admin 2>&1 > /dev/null + +echo "" +echo "🕐 Waiting for contract to be queryable..." +sleep 5 + +CONTRACT=$(wasmd query wasm list-contract-by-code "$CODE_ID" --output json | jq -r '.contracts[0]') +echo "🚀 Successfully deployed and instantiated contract!" +echo "🔗 Chain ID: ${CHAIN_ID}" +echo "🆔 Code ID: ${CODE_ID}" +echo "📌 Contract Address: ${CONTRACT}" +echo "🔑 Contract Key: ${KEY}" +echo "🔖 Contract Label: ${LABEL}" +echo "🏷️ Count: ${COUNT}" diff --git a/bisenzone-cw-mvp/scripts/init-node.sh b/bisenzone-cw-mvp/scripts/init-node.sh new file mode 100755 index 0000000..445b2fd --- /dev/null +++ b/bisenzone-cw-mvp/scripts/init-node.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Initialize a wasmd node that can host the MVP CosmWasm smart contract. +# Also creates a validator account and adds default genesis accounts with sufficient tokens for testing (stake and fees) + +set -euo pipefail + +ADMIN=${ADMIN:-$(wasmd keys show -a admin)} +ALICE=${ALICE:-$(wasmd keys show -a alice)} +BOB=${BOB:-$(wasmd keys show -a bob)} +CHARLIE=${CHARLIE:-$(wasmd keys show -a charlie)} + +echo "Remove old docker volume (if it exists)..." +docker volume rm -f wasmd_data + + +echo "" +echo "Setup wasmd (with validator and default genesis accounts)..." +docker run --rm -it \ + --mount type=volume,source=wasmd_data,target=/root \ + --name wasmd \ + cosmwasm/wasmd:v0.44.0 \ + /bin/sh -c "sed -i 's/1000000000/12000000000000/g' /opt/setup_wasmd.sh; + /opt/setup_wasmd.sh "$ADMIN" "$ALICE" "$BOB" "$CHARLIE";" \ + + diff --git a/bisenzone-cw-mvp/scripts/keygen.sh b/bisenzone-cw-mvp/scripts/keygen.sh new file mode 100755 index 0000000..00a7789 --- /dev/null +++ b/bisenzone-cw-mvp/scripts/keygen.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# Generate keys for testing. + +set -euo pipefail + +wasmd keys add admin +wasmd keys add alice +wasmd keys add bob +wasmd keys add charlie diff --git a/bisenzone-cw-mvp/scripts/run-node.sh b/bisenzone-cw-mvp/scripts/run-node.sh new file mode 100755 index 0000000..61c62bf --- /dev/null +++ b/bisenzone-cw-mvp/scripts/run-node.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Run a previously initialized wasmd node. + +set -uo pipefail + +docker volume inspect wasmd_data >/dev/null 2>&1 +RESULT=$? +if [ $RESULT -eq 1 ]; then + echo "wasmd isn't initialized - run 'init-node.sh' first" + exit 1 +fi + +echo "Starting wasmd node" + +docker run --rm -it -p 26657:26657 -p 26656:26656 -p 1317:1317 -p 9090:9090 \ + --mount type=volume,source=wasmd_data,target=/root \ + --name wasmd \ + cosmwasm/wasmd:v0.44.0 \ + /bin/sh -c "sed -i 's/enabled-unsafe-cors = false/enabled-unsafe-cors = true/g' /root/.wasmd/config/app.toml; + sed -i 's/enable = false/enable = true/g' /root/.wasmd/config/app.toml; + sed -i 's/rpc-max-body-bytes = 1000000$/rpc-max-body-bytes = 1000000000/g' /root/.wasmd/config/app.toml; + sed -i 's/cors_allowed_origins = \[\]/cors_allowed_origins = \[\"*\"\]/g' /root/.wasmd/config/config.toml; + sed -i 's/max_body_bytes = 1000000$/max_body_bytes = 1000000000/g' /root/.wasmd/config/config.toml; + sed -i 's/max_tx_bytes = 1048576$/max_tx_bytes = 104857600/g' /root/.wasmd/config/config.toml; + /opt/run_wasmd.sh"