From 08aae815d7d90c18800b2aee6dc9f0b948d0e97e Mon Sep 17 00:00:00 2001 From: Night Owl <91905208+nightowl121@users.noreply.github.com> Date: Tue, 6 Jun 2023 16:48:47 +0545 Subject: [PATCH] feat: add cosmwasm deployment workflow (#11) * feat: add schema of cw-hub-bnusd * feat: add archway as submodule * feat: add cosmwasm-check as dev dependencies * feat: add scripts for common tasks * feat: add workflow to deploy wasm contracts * feat: remove cosmwasm-check from dev dependencies and install as a tool * feat: use 1.69.0 rust version * install wasm-opt and cw-check in ci * fix: add missing rust flags * update package versions * feat: add installation packages as components * feat: use correct container name * feat: separate out copy command * feat: use sh command to run the script * feat: move caching option to first step * feat: install wasm-opt using downloaded file --- .github/workflows/deploy-cw-contracts.yml | 69 +++++++++++++++++++ .gitignore | 3 - .gitmodules | 3 + Cargo.lock | 48 ++++++------- contracts/archway | 1 + .../token-contracts/cw-hub-bnusd/Cargo.toml | 23 +++++-- .../cw-hub-bnusd/schema/cw-hub-bnusd.json | 26 +++++++ .../cw-hub-bnusd/schema/raw/execute.json | 6 ++ .../cw-hub-bnusd/schema/raw/instantiate.json | 6 ++ .../cw-hub-bnusd/schema/raw/query.json | 6 ++ scripts/deploy_cw.sh | 28 ++++++++ scripts/generate_wasm.sh | 25 +++++++ scripts/pre_commit.sh | 7 ++ 13 files changed, 218 insertions(+), 33 deletions(-) create mode 100644 .github/workflows/deploy-cw-contracts.yml create mode 100644 .gitmodules create mode 160000 contracts/archway create mode 100644 contracts/token-contracts/cw-hub-bnusd/schema/cw-hub-bnusd.json create mode 100644 contracts/token-contracts/cw-hub-bnusd/schema/raw/execute.json create mode 100644 contracts/token-contracts/cw-hub-bnusd/schema/raw/instantiate.json create mode 100644 contracts/token-contracts/cw-hub-bnusd/schema/raw/query.json create mode 100755 scripts/deploy_cw.sh create mode 100755 scripts/generate_wasm.sh create mode 100755 scripts/pre_commit.sh diff --git a/.github/workflows/deploy-cw-contracts.yml b/.github/workflows/deploy-cw-contracts.yml new file mode 100644 index 0000000..b3c8c70 --- /dev/null +++ b/.github/workflows/deploy-cw-contracts.yml @@ -0,0 +1,69 @@ +name: CW Contracts Test Deployment +on: + push: + branches: + - main + pull_request: + branches: + - main + paths: + - contracts/** + - .github/workflows/deploy-cw-contracts.yml +jobs: + Build: + name: Build & Deploy CW Contracts + runs-on: ubuntu-latest + steps: + - name: Checkout sources + uses: actions/checkout@v3 + with: + submodules: true + + - name: Start local Archway Blockchain + run: | + cd contracts/archway + sed -i 's/latest/v0.4.0/' docker-compose.yaml + docker compose -f docker-compose.yaml up -d + # git clean submodule directory + git checkout . + git clean -fdx + + - name: Cache Rust dependencies + uses: Swatinem/rust-cache@v2 + + - name: Install stable toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: 1.69.0 + target: wasm32-unknown-unknown + override: true + profile: minimal + + - name: Install cw-check + run: | + cargo install cosmwasm-check + + - name: Compile WASM + run: bash ./scripts/generate_wasm.sh + + - name: Check WASM Size + run: | + max_size=800 + echo "Check if size of wasm file exceeds $max_size kilobytes..." + for file in artifacts/*.wasm; do + size=$(du -k "$file" | awk '{print $1}') + if [[ $size -gt $max_size ]]; then + echo "Error: $file : $size has exceeded maximum contract size limit of 800KB." + exit 1 + fi + echo "$file : $size" + done + echo "The size of all contracts is well within the 800 KB limit." + + - name: Deploy WASM + run: | + container=$(docker ps --format '{{.Names}}') + cp scripts/deploy_cw.sh contracts/archway/contracts + cp -r artifacts contracts/archway/contracts + docker exec $container chmod +x /contracts/deploy_cw.sh + docker exec $container sh /contracts/deploy_cw.sh diff --git a/.gitignore b/.gitignore index e6df254..0b2075f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ # Build results /target -/schema # Cargo+Git helper file (https://github.com/rust-lang/cargo/blob/0.44.1/src/cargo/sources/git/utils.rs#L320-L327) .cargo-ok @@ -27,5 +26,3 @@ artifacts/ # code coverage tarpaulin-report.* - -contracts/**/**/schema diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..38b84fa --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "contracts/archway"] + path = contracts/archway + url = https://github.com/archway-network/archway.git diff --git a/Cargo.lock b/Cargo.lock index c6ea372..88498e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -81,9 +81,9 @@ checksum = "520fbf3c07483f94e3e3ca9d0cfd913d7718ef2483d2cfd91c0d9e91474ab913" [[package]] name = "cosmwasm-crypto" -version = "1.2.5" +version = "1.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75836a10cb9654c54e77ee56da94d592923092a10b369cdb0dbd56acefc16340" +checksum = "41c0e41be7e6c7d7ab3c61cdc32fcfaa14f948491a401cbc1c74bb33b6f4b851" dependencies = [ "digest 0.10.7", "ed25519-zebra", @@ -94,18 +94,18 @@ dependencies = [ [[package]] name = "cosmwasm-derive" -version = "1.2.5" +version = "1.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c9f7f0e51bfc7295f7b2664fe8513c966428642aa765dad8a74acdab5e0c773" +checksum = "3a7ee2798c92c00dd17bebb4210f81d5f647e5e92d847959b7977e0fd29a3500" dependencies = [ "syn 1.0.109", ] [[package]] name = "cosmwasm-schema" -version = "1.2.5" +version = "1.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f00b363610218eea83f24bbab09e1a7c3920b79f068334fdfcc62f6129ef9fc" +checksum = "407aca6f1671a08b60db8167f03bb7cb6b2378f0ddd9a030367b66ba33c2fd41" dependencies = [ "cosmwasm-schema-derive", "schemars", @@ -116,9 +116,9 @@ dependencies = [ [[package]] name = "cosmwasm-schema-derive" -version = "1.2.5" +version = "1.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae38f909b2822d32b275c9e2db9728497aa33ffe67dd463bc67c6a3b7092785c" +checksum = "e6d1e00b8fd27ff923c10303023626358e23a6f9079f8ebec23a8b4b0bfcd4b3" dependencies = [ "proc-macro2", "quote", @@ -127,9 +127,9 @@ dependencies = [ [[package]] name = "cosmwasm-std" -version = "1.2.5" +version = "1.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a49b85345e811c8e80ec55d0d091e4fcb4f00f97ab058f9be5f614c444a730cb" +checksum = "92d5fdfd112b070055f068fad079d490117c8e905a588b92a5a7c9276d029930" dependencies = [ "base64", "cosmwasm-crypto", @@ -147,9 +147,9 @@ dependencies = [ [[package]] name = "cosmwasm-storage" -version = "1.2.5" +version = "1.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3737a3aac48f5ed883b5b73bfb731e77feebd8fc6b43419844ec2971072164d" +checksum = "af9e21c4f58986fd20184d7685e1c43c5732c9309337b09307d5952fd34dba6e" dependencies = [ "cosmwasm-std", "serde", @@ -479,15 +479,15 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.144" +version = "0.2.145" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" +checksum = "fc86cde3ff845662b8f4ef6cb50ea0e20c524eb3d29ae048287e06a1b3fa6a81" [[package]] name = "once_cell" -version = "1.17.1" +version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] name = "opaque-debug" @@ -507,9 +507,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.58" +version = "1.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa1fb82fc0c281dd9671101b66b771ebbe1eaf967b96ac8740dcba4b70005ca8" +checksum = "6aeca18b86b413c660b781aa319e4e2648a3e6f9eadc9b47e9038e6fe9f3451b" dependencies = [ "unicode-ident", ] @@ -539,9 +539,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.27" +version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" +checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488" dependencies = [ "proc-macro2", ] @@ -648,7 +648,7 @@ checksum = "8c805777e3930c8883389c602315a24224bcc738b63905ef87cd1420353ea93e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.18", ] [[package]] @@ -742,9 +742,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.16" +version = "2.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6f671d4b5ffdb8eadec19c0ae67fe2639df8684bd7bc4b83d986b8db549cf01" +checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e" dependencies = [ "proc-macro2", "quote", @@ -768,7 +768,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.18", ] [[package]] diff --git a/contracts/archway b/contracts/archway new file mode 160000 index 0000000..dd58dde --- /dev/null +++ b/contracts/archway @@ -0,0 +1 @@ +Subproject commit dd58dde7fb8e7baf184a6253d4004b2e88d4085c diff --git a/contracts/token-contracts/cw-hub-bnusd/Cargo.toml b/contracts/token-contracts/cw-hub-bnusd/Cargo.toml index 937e946..4224de1 100644 --- a/contracts/token-contracts/cw-hub-bnusd/Cargo.toml +++ b/contracts/token-contracts/cw-hub-bnusd/Cargo.toml @@ -29,14 +29,25 @@ optimize = """docker run --rm -v "$(pwd)":/code \ """ [dependencies] -cosmwasm-schema = "1.1.3" -cosmwasm-std = "1.1.3" -cosmwasm-storage = "1.1.3" +cosmwasm-schema = "1.2.6" +cosmwasm-std = "1.2.6" +cosmwasm-storage = "1.2.6" cw-storage-plus = "1.0.1" cw2 = "1.0.1" -schemars = "0.8.10" -serde = { version = "1.0.145", default-features = false, features = ["derive"] } -thiserror = { version = "1.0.31" } +schemars = "0.8.12" +serde = { version = "1.0.163", default-features = false, features = ["derive"] } +thiserror = { version = "1.0.40" } [dev-dependencies] cw-multi-test = "0.16.2" + +[profile.release] +# Do not perform backtrace for panic on release builds. +panic = 'abort' +# Perform optimizations on all codegen units. +codegen-units = 1 +# Optimize for size. +opt-level = 'z' # or 'z' to optimize "aggressively" for size +# Enable link time optimization. +lto = true +strip = true diff --git a/contracts/token-contracts/cw-hub-bnusd/schema/cw-hub-bnusd.json b/contracts/token-contracts/cw-hub-bnusd/schema/cw-hub-bnusd.json new file mode 100644 index 0000000..329662a --- /dev/null +++ b/contracts/token-contracts/cw-hub-bnusd/schema/cw-hub-bnusd.json @@ -0,0 +1,26 @@ +{ + "contract_name": "cw-hub-bnusd", + "contract_version": "0.1.0", + "idl_version": "1.0.0", + "instantiate": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "InstantiateMsg", + "type": "object", + "additionalProperties": false + }, + "execute": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "ExecuteMsg", + "type": "string", + "enum": [] + }, + "query": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "QueryMsg", + "type": "string", + "enum": [] + }, + "migrate": null, + "sudo": null, + "responses": {} +} diff --git a/contracts/token-contracts/cw-hub-bnusd/schema/raw/execute.json b/contracts/token-contracts/cw-hub-bnusd/schema/raw/execute.json new file mode 100644 index 0000000..b3d18b4 --- /dev/null +++ b/contracts/token-contracts/cw-hub-bnusd/schema/raw/execute.json @@ -0,0 +1,6 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "ExecuteMsg", + "type": "string", + "enum": [] +} diff --git a/contracts/token-contracts/cw-hub-bnusd/schema/raw/instantiate.json b/contracts/token-contracts/cw-hub-bnusd/schema/raw/instantiate.json new file mode 100644 index 0000000..1352613 --- /dev/null +++ b/contracts/token-contracts/cw-hub-bnusd/schema/raw/instantiate.json @@ -0,0 +1,6 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "InstantiateMsg", + "type": "object", + "additionalProperties": false +} diff --git a/contracts/token-contracts/cw-hub-bnusd/schema/raw/query.json b/contracts/token-contracts/cw-hub-bnusd/schema/raw/query.json new file mode 100644 index 0000000..0f592a1 --- /dev/null +++ b/contracts/token-contracts/cw-hub-bnusd/schema/raw/query.json @@ -0,0 +1,6 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "QueryMsg", + "type": "string", + "enum": [] +} diff --git a/scripts/deploy_cw.sh b/scripts/deploy_cw.sh new file mode 100755 index 0000000..be8f35f --- /dev/null +++ b/scripts/deploy_cw.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +WALLET=fd # This account is default while running the chain on docker. +### LOCALNET ### +ENDPOINT=http://localhost:26657 +CHAIN_ID=localnet +TOKEN=stake + +deploy_wasm() { + RES=$(archwayd tx wasm store "$CONTRACT_WASM" \ + --keyring-backend test \ + --from $WALLET \ + --node $ENDPOINT \ + --chain-id $CHAIN_ID \ + --gas-prices 0.02$TOKEN \ + --gas auto \ + --gas-adjustment 1.3 -y \ + --output json -b block) + + echo "Result: " + echo "$RES" +} + +# This wasm directory is inside the docker container +for CONTRACT_WASM in /contracts/artifacts/*.wasm; do + echo "=> Deploying $CONTRACT_WASM" + deploy_wasm "$CONTRACT_WASM" +done diff --git a/scripts/generate_wasm.sh b/scripts/generate_wasm.sh new file mode 100755 index 0000000..3e34392 --- /dev/null +++ b/scripts/generate_wasm.sh @@ -0,0 +1,25 @@ +#!/bin/bash +set -e + +# install wasm-opt +BINARYEN_VERS=110 +BINARYEN_DWN="https://github.com/WebAssembly/binaryen/releases/download/version_${BINARYEN_VERS}/binaryen-version_${BINARYEN_VERS}-x86_64-linux.tar.gz" + +if ! which wasm-opt; then + curl -OL $BINARYEN_DWN + tar xf binaryen-version_${BINARYEN_VERS}-x86_64-linux.tar.gz + export PATH=$PATH:$PWD/binaryen-version_${BINARYEN_VERS}/bin +fi + +# Generate optimized wasm files and verify generated wasm with cosmwasm-check +mkdir -p artifacts +RUSTFLAGS='-C link-arg=-s' cargo wasm +for WASM in ./target/wasm32-unknown-unknown/release/*.wasm; do + NAME=$(basename "$WASM" .wasm)${SUFFIX}.wasm + echo "########Creating intermediate hash for $NAME ...########" + sha256sum -- "$WASM" | tee -a artifacts/checksums_intermediate.txt + echo "########Optimizing $NAME ...########" + wasm-opt -Oz "$WASM" -o "artifacts/$NAME" + echo "########Verifying $NAME file with cosmwasm-check ...########" + cosmwasm-check "artifacts/$NAME" +done diff --git a/scripts/pre_commit.sh b/scripts/pre_commit.sh new file mode 100755 index 0000000..a697d68 --- /dev/null +++ b/scripts/pre_commit.sh @@ -0,0 +1,7 @@ +#!/bin/bash +set -e + +cargo fmt --all +#cargo clippy --fix +source ./scripts/run_in_subprojects.sh ./contracts/token-contracts/cw-hub-bnusd +cargo clean