Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding cw-orchestrator to the CosmWasm template #153

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
wasm = "build --release --lib --target wasm32-unknown-unknown"
unit-test = "test --lib"
schema = "run --bin schema"
integration-test = "test --lib integration_tests"
deploy = "run --bin deploy --features deploy"
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copy this file to .env and fill in the values
# Learn about more env vars: http://orchestrator.abstract.money

# info, debug, trace (if using env_logger for logging)
RUST_LOG=info

MAIN_MNEMONIC="" # Necessary if interacting with a daemon on mainnet
TEST_MNEMONIC="" # Necessary if interacting with a daemon on testnet
LOCAL_MNEMONIC="" # Necessary if interacting with a daemon locally
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
# IDEs
*.iml
.idea
.env
20 changes: 13 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ panic = 'abort'
incremental = false
overflow-checks = true

[[bin]]
name = "deploy"
path = "src/bin/deploy.rs"
required-features = ["deploy"]

[features]
# use library feature to disable all instantiate/execute/query exports
library = []

[package.metadata.scripts]
optimize = """docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/optimizer:0.15.0
"""
deploy = ["cw-orch/daemon", "dotenv", "env_logger"]

[dependencies]
cosmwasm-schema = "2.1.0"
Expand All @@ -44,5 +43,12 @@ schemars = "0.8.16"
serde = { version = "1.0.197", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.58" }

cw-orch = { version = "0.26.0" }

env_logger = { version = "0.11.5", optional = true }
dotenv = { version = "0.15.0", optional = true }

[dev-dependencies]
{% unless minimal %}
cw-multi-test = "2.0.0"
{% endunless %}
21 changes: 19 additions & 2 deletions Developing.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,18 @@ To solve both these issues, we have produced `rust-optimizer`, a docker image to
produce an extremely small build output in a consistent manner. The suggest way
to run it is this:

```sh
chmod +x ./optimize.sh
./optimize.sh
```

Which is equivalent to:

```sh
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/optimizer:0.15.0
cosmwasm/optimizer:0.16.1
```

Or, If you're on an arm64 machine, you should use a docker image built with arm64.
Expand All @@ -83,7 +90,7 @@ Or, If you're on an arm64 machine, you should use a docker image built with arm6
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/optimizer-arm64:0.15.0
cosmwasm/optimizer-arm64:0.16.1
```

We must mount the contract code to `/code`. You can use an absolute path instead
Expand All @@ -102,3 +109,13 @@ The wasm file is compiled deterministically (anyone else running the same
docker on the same git commit should get the identical file with the same Sha256 hash).
It is also stripped and minimized for upload to a blockchain (we will also
gzip it in the uploading process to make it even smaller).

## Deploying

Fill in the mnemonic that you want to use for deployment in the `.env` file. Then run the deploy script:

```sh
cargo deploy
```

You can change the network to deploy to in the `deploy.rs` file.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Unless you did that before, run this line now:

```sh
cargo install cargo-generate --features vendored-openssl
cargo install cargo-run-script
```

Now, use it to create your new contract.
Expand All @@ -27,13 +26,13 @@ Go to the folder in which you want to place it and run:
**Latest**

```sh
cargo generate --git https://github.com/CosmWasm/cw-template.git --name PROJECT_NAME
cargo generate --git https://github.com/AbstractSDK/cw-template.git --name PROJECT_NAME
```

For cloning minimal code repo:

```sh
cargo generate --git https://github.com/CosmWasm/cw-template.git --name PROJECT_NAME -d minimal=true
cargo generate --git https://github.com/AbstractSDK/cw-template.git --name PROJECT_NAME -d minimal=true
```

You will now have a new folder called `PROJECT_NAME` (I hope you changed that to something else)
Expand Down Expand Up @@ -75,6 +74,8 @@ more on how to run tests and develop code. Or go through the
[online tutorial](https://docs.cosmwasm.com/) to get a better feel
of how to develop.

This template includes [cw-orchestrator](https://docs.rs/cw-orch/latest/cw_orch/) by default. This library allows you to unit-test, integration-test as well as interact with your contracts on-chain using a common intuitive syntax that leverages rust type-safety to assist you throughout your development process. You can find the interface definitions in the [src/interface.rs](src/interface.rs) file. You can also find more information in the [`cw-orch` documentation](https://orchestrator.abstract.money/).

[Publishing](./Publishing.md) contains useful information on how to publish your contract
to the world, once you are ready to deploy it on a running blockchain. And
[Importing](./Importing.md) contains information about pulling in other contracts or crates
Expand Down
5 changes: 4 additions & 1 deletion cargo-generate.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ The minimal template assumes you already know how to write your own logic, and d
Would you like to generate the minimal template?"""
default = false

[hooks]
post = ["post-script.rhai"]

[conditional.'minimal']
ignore = [
"Developing.md",
"Importing.md",
"Publishing.md",
".gitpod.yml",
".gitpod.Dockerfile",
"src/integration_tests.rs",
"tests/",
]
7 changes: 4 additions & 3 deletions meta/test_generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ PROJECT_NAME="testgen-local"
cd "$TMP_DIR"

echo "Generating project from local repository ..."
cargo generate --path "$REPO_ROOT" --name "$PROJECT_NAME"
cargo generate --path "$REPO_ROOT" --name "$PROJECT_NAME" -d minimal=false

(
cd "$PROJECT_NAME"
Expand All @@ -24,8 +24,9 @@ PROJECT_NAME="testgen-local"
cargo fmt -- --check

# Debug builds first to fail fast
echo "Running unit tests ..."
cargo unit-test
echo "Running tests ..."
cargo test

echo "Creating schema ..."
cargo schema

Expand Down
16 changes: 16 additions & 0 deletions optimize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

# Delete all the current wasms first
rm -rf ./artifacts/*.wasm

if [[ $(arch) == "arm64" ]]; then
image="cosmwasm/optimizer-arm64:0.16.1"
else
image="cosmwasm/optimizer:0.16.1"
fi

# Optimized builds
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 \
${image}
1 change: 1 addition & 0 deletions post-script.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file::rename(".env.example", ".env")
25 changes: 25 additions & 0 deletions src/bin/deploy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use cw_orch::{anyhow, prelude::*};
use {{project-name | snake_case}}::{% raw %}{{% endraw %}
interface::{{project-name | upper_camel_case}}I,
msg::{ExecuteMsgFns, InstantiateMsg, QueryMsgFns},
};

pub fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok(); // Used to load the `.env` file if any
env_logger::init(); // Used to log contract and chain interactions

let network = networks::PION_1;
let chain = DaemonBuilder::new(network.clone()).build()?;

let counter = {{project-name | upper_camel_case}}I::new(chain);

counter.upload()?;

let msg = InstantiateMsg {% raw %}{{% endraw %}{% unless minimal %} count: 1i32 {% endunless %}};
counter.instantiate(&msg, None, &[])?;{% unless minimal %}

counter.increment()?;
let count = counter.get_count()?;
assert_eq!(count.count, 1);{% endunless %}
Ok(())
}
14 changes: 7 additions & 7 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ pub mod query {
#[cfg(test)]
mod tests {% raw %}{{% endraw %}{% unless minimal %}
use super::*;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::testing::{message_info, mock_dependencies, mock_env};
use cosmwasm_std::{coins, from_json};

#[test]
fn proper_initialization() {
let mut deps = mock_dependencies();

let msg = InstantiateMsg { count: 17 };
let info = mock_info("creator", &coins(1000, "earth"));
let info = message_info(&deps.api.addr_make("creator"), &coins(1000, "earth"));

// we can just call .unwrap() to assert this was a success
let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
Expand All @@ -114,11 +114,11 @@ mod tests {% raw %}{{% endraw %}{% unless minimal %}
let mut deps = mock_dependencies();

let msg = InstantiateMsg { count: 17 };
let info = mock_info("creator", &coins(2, "token"));
let info = message_info(&deps.api.addr_make("creator"), &coins(2, "token"));
let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();

// beneficiary can release it
let info = mock_info("anyone", &coins(2, "token"));
let info = message_info(&deps.api.addr_make("anyone"), &coins(2, "token"));
let msg = ExecuteMsg::Increment {};
let _res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();

Expand All @@ -133,11 +133,11 @@ mod tests {% raw %}{{% endraw %}{% unless minimal %}
let mut deps = mock_dependencies();

let msg = InstantiateMsg { count: 17 };
let info = mock_info("creator", &coins(2, "token"));
let info = message_info(&deps.api.addr_make("creator"), &coins(2, "token"));
let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();

// beneficiary can release it
let unauth_info = mock_info("anyone", &coins(2, "token"));
let unauth_info = message_info(&deps.api.addr_make("anyone"), &coins(2, "token"));
let msg = ExecuteMsg::Reset { count: 5 };
let res = execute(deps.as_mut(), mock_env(), unauth_info, msg);
match res {
Expand All @@ -146,7 +146,7 @@ mod tests {% raw %}{{% endraw %}{% unless minimal %}
}

// only the original creator can reset the counter
let auth_info = mock_info("creator", &coins(2, "token"));
let auth_info = message_info(&deps.api.addr_make("creator"), &coins(2, "token"));
let msg = ExecuteMsg::Reset { count: 5 };
let _res = execute(deps.as_mut(), mock_env(), auth_info, msg).unwrap();

Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::StdError;
use thiserror::Error;

#[derive(Error, Debug)]
#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),
Expand Down
78 changes: 0 additions & 78 deletions src/integration_tests.rs

This file was deleted.

Loading