Skip to content

Commit

Permalink
feat: add cosmwasm deployment workflow (#11)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nightowl121 authored Jun 6, 2023
1 parent 30ba970 commit 08aae81
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 33 deletions.
69 changes: 69 additions & 0 deletions .github/workflows/deploy-cw-contracts.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -27,5 +26,3 @@ artifacts/

# code coverage
tarpaulin-report.*

contracts/**/**/schema
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "contracts/archway"]
path = contracts/archway
url = https://github.com/archway-network/archway.git
48 changes: 24 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contracts/archway
Submodule archway added at dd58dd
23 changes: 17 additions & 6 deletions contracts/token-contracts/cw-hub-bnusd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions contracts/token-contracts/cw-hub-bnusd/schema/cw-hub-bnusd.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ExecuteMsg",
"type": "string",
"enum": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "InstantiateMsg",
"type": "object",
"additionalProperties": false
}
6 changes: 6 additions & 0 deletions contracts/token-contracts/cw-hub-bnusd/schema/raw/query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "QueryMsg",
"type": "string",
"enum": []
}
28 changes: 28 additions & 0 deletions scripts/deploy_cw.sh
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions scripts/generate_wasm.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions scripts/pre_commit.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 08aae81

Please sign in to comment.