Skip to content

Commit c810eb0

Browse files
authored
Add single contract tutorial docs (#135)
* rename wasm script * add wasm build to CI * fix deployment imports on counter * re-export tokio when daemon feature is enabled * formatting * pass --all-features to docs.rs when building docs * default state file to `state.json` * update docs and add anchors * update further docs * format * update sections and add workspace placeholder. * format our stuff * fix doctest * bump tx query retries * add deny(missing_docs) * formatting * add #![deny(missing_docs)] and fix all the missing docs * fix deploy mod comment * update prelude doc comment * format * fix doc comment * remove bin folder from gitignore * remove unused deps and move executable to examples * fix docs * compile to wasm * format * group imports * fmt * add doc comment to re-exports * remove mdbook from test job * add mdbook-keeper to just install-docs command * build docs and add to gitattributes * fix git attribute * improve doc structure and reference * update html settings * formatting * fix edit url * build the docs * fix edit url
1 parent 976343e commit c810eb0

File tree

133 files changed

+10713
-780
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+10713
-780
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docs/book linguist-generated=true

.github/workflows/check.yml

+21
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,24 @@ jobs:
9191
toolchain: ${{ matrix.msrv }}
9292
- name: cargo +${{ matrix.msrv }} check
9393
run: cargo check
94+
95+
build-artifacts:
96+
runs-on: ubuntu-latest
97+
steps:
98+
- uses: actions/checkout@v3
99+
- name: Install latest stable
100+
uses: ATiltedTree/setup-rust@v1
101+
with:
102+
rust-version: stable
103+
- name: Setup Docker Buildx
104+
uses: docker/setup-buildx-action@v2
105+
- name: Generate Cargo.lock
106+
run: |
107+
cargo fetch --verbose
108+
- name: Build Artifacts
109+
run: |
110+
docker run --rm -v "$(pwd)":/code \
111+
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
112+
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
113+
cosmwasm/workspace-optimizer:0.12.13
114+
tar -zcvf cosmwasm-artifacts.tar.gz artifacts

.github/workflows/test.yml

-6
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ jobs:
4646
run: cargo generate-lockfile
4747
- name: cargo test --doc
4848
run: cargo test --doc --locked --all-features
49-
- name: install mdbook
50-
run: cargo install mdbook
51-
- name: install mdbook-keeper
52-
run: cargo install mdbook-keeper
53-
- name: mdbook build
54-
run: mdbook build docs
5549

5650
coverage:
5751
runs-on: ubuntu-latest

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Cargo.lock
1919
**/profraw_data/
2020
gen_cov_test
2121

22-
**/bin/
2322
**/logs/

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[workspace]
22
members = ["cw-orch", "packages/*", "contracts/*"]
33
version = "0.11.4"
4+
resolver = "2"
45

56
[workspace.package]
67
authors = ["CyberHoward <[email protected]>"]

artifacts/checksums.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ec347443a5ed4442b2cc166bce53ebab18d8a881543acc11fbca68a9bb092362 counter_contract-aarch64.wasm
2+
ed55bd2c18c7fbd20e9b15f56ff619a986a47a363c66358382c4c2fcf16e6389 mock_contract-aarch64.wasm
3+
bf962e11fdb38c2825c24046ae96577f23db422954165562c5ad80e499cfeabe mock_contract.wasm
4+
48856f5a5c169089cf289d31c14fd41d35bb298530577ff3c77ce08725f96496 mock_contract_u64-aarch64.wasm
5+
ab53bcea2fb6ef40a4df1c24640c8daa910b294b1ff185126b00b13b7fc700ee mock_contract_u64.wasm

artifacts/checksums_intermediate.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
eb5428b2c02c3e3950b0e8c3ab72d29b6f8163bf843fe03f21e8ffc448061d7e target/wasm32-unknown-unknown/release/counter_contract.wasm
2+
656026cb517e45bb0eee4545a2bf64406e18fc1c15391e1b6f1a6ee9d6e12107 target/wasm32-unknown-unknown/release/mock_contract.wasm
3+
906f015f89d79893f430bff8c7dc3168035aa4e0a2682987adc294bc11c371e9 target/wasm32-unknown-unknown/release/mock_contract_u64.wasm
149 KB
Binary file not shown.

artifacts/mock_contract-aarch64.wasm

143 KB
Binary file not shown.
143 KB
Binary file not shown.

contracts/counter/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ version = "0.11.0"
44
description = "counter constract"
55
keywords = ["cosmwasm", "blockchain"]
66
edition = { workspace = true }
7-
87
exclude = [".env"]
98

109
[lib]
@@ -22,13 +21,14 @@ cw2 = "1.0"
2221
cosmwasm-schema = "1.2"
2322
schemars = "0.8.10"
2423
thiserror = { version = "1.0.21" }
25-
anyhow = { workspace = true }
2624
serde = { workspace = true }
2725
serde_json = "1.0.79"
28-
log = "0.4.14"
29-
3026
cw-orch = { path = "../../cw-orch", optional = true }
3127

3228
[dev-dependencies]
3329
cw-multi-test = { workspace = true }
3430
counter-contract = { path = ".", features = ["interface"] }
31+
# Deps for deployment
32+
dotenv = { version = "0.15.0" }
33+
env_logger = { version = "0.10.0" }
34+
cw-orch = { path = "../../cw-orch", features = ["daemon"] }

contracts/counter/examples/deploy.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use counter_contract::{msg::InstantiateMsg, CounterContract};
2+
use cw_orch::{anyhow, prelude::*, tokio};
3+
use tokio::runtime::Runtime;
4+
5+
/// Script that registers the first Account in abstract (our Account)
6+
pub fn main() -> anyhow::Result<()> {
7+
dotenv::dotenv().ok();
8+
env_logger::init();
9+
10+
let rt = Runtime::new()?;
11+
let network = networks::LOCAL_JUNO;
12+
let chain = DaemonBuilder::default()
13+
.handle(rt.handle())
14+
.chain(network)
15+
.build()?;
16+
17+
let counter = CounterContract::new("counter_contract", chain);
18+
19+
counter.upload()?;
20+
counter.instantiate(&InstantiateMsg { count: 0 }, None, None)?;
21+
Ok(())
22+
}

contracts/counter/src/contract.rs

+13-40
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
use cosmwasm_std::entry_point;
2-
use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
1+
use cosmwasm_std::{
2+
entry_point, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult,
3+
};
34
use cw2::set_contract_version;
45

5-
use crate::{error::*, msg::*, state::*};
6+
use crate::{error::*, execute, msg::*, query, state::*};
67

78
// version info for migration info
89
pub const CONTRACT_NAME: &str = "crates.io:counter";
910
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
1011

1112
// ANCHOR: interface_entry
13+
// ANCHOR: entry_point_line
1214
#[cfg_attr(feature = "export", entry_point)]
15+
// ANCHOR_END: entry_point_line
16+
// ANCHOR: interface_line
1317
#[cfg_attr(feature = "interface", cw_orch::interface_entry_point)]
18+
// ANCHOR_END: interface_line
1419
pub fn instantiate(
1520
deps: DepsMut,
1621
_env: Env,
@@ -43,31 +48,6 @@ pub fn execute(
4348
ExecuteMsg::Reset { count } => execute::reset(deps, info, count),
4449
}
4550
}
46-
// ANCHOR_END: interface_entry
47-
48-
pub mod execute {
49-
use super::*;
50-
51-
pub fn increment(deps: DepsMut) -> Result<Response, ContractError> {
52-
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
53-
state.count += 1;
54-
Ok(state)
55-
})?;
56-
57-
Ok(Response::new().add_attribute("action", "increment"))
58-
}
59-
60-
pub fn reset(deps: DepsMut, info: MessageInfo, count: i32) -> Result<Response, ContractError> {
61-
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
62-
if info.sender != state.owner {
63-
return Err(ContractError::Unauthorized {});
64-
}
65-
state.count = count;
66-
Ok(state)
67-
})?;
68-
Ok(Response::new().add_attribute("action", "reset"))
69-
}
70-
}
7151

7252
#[cfg_attr(feature = "export", entry_point)]
7353
#[cfg_attr(feature = "interface", cw_orch::interface_entry_point)]
@@ -77,27 +57,20 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
7757
}
7858
}
7959

80-
pub mod query {
81-
use super::*;
82-
83-
pub fn count(deps: Deps) -> StdResult<GetCountResponse> {
84-
let state = STATE.load(deps.storage)?;
85-
Ok(GetCountResponse { count: state.count })
86-
}
87-
}
88-
8960
#[cfg_attr(feature = "export", entry_point)]
9061
#[cfg_attr(feature = "interface", cw_orch::interface_entry_point)]
9162
pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
92-
// TODO: Migrate state
9363
Ok(Response::default().add_attribute("action", "migrate"))
9464
}
65+
// ANCHOR_END: interface_entry
9566

9667
#[cfg(test)]
9768
mod tests {
9869
use super::*;
99-
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
100-
use cosmwasm_std::{coins, from_binary};
70+
use cosmwasm_std::{
71+
coins, from_binary,
72+
testing::{mock_dependencies, mock_env, mock_info},
73+
};
10174

10275
#[test]
10376
fn proper_initialization() {

contracts/counter/src/execute.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use cosmwasm_std::{DepsMut, MessageInfo, Response};
2+
3+
use crate::{error::*, state::*};
4+
5+
pub fn increment(deps: DepsMut) -> Result<Response, ContractError> {
6+
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
7+
state.count += 1;
8+
Ok(state)
9+
})?;
10+
11+
Ok(Response::new().add_attribute("action", "increment"))
12+
}
13+
14+
pub fn reset(deps: DepsMut, info: MessageInfo, count: i32) -> Result<Response, ContractError> {
15+
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
16+
if info.sender != state.owner {
17+
return Err(ContractError::Unauthorized {});
18+
}
19+
state.count = count;
20+
Ok(state)
21+
})?;
22+
Ok(Response::new().add_attribute("action", "reset"))
23+
}

contracts/counter/src/interface.rs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#![allow(unused)]
2+
// ANCHOR: custom_interface
3+
use cw_orch::{
4+
anyhow::Result,
5+
interface,
6+
prelude::{queriers::Node, *},
7+
};
8+
9+
use crate::{
10+
contract::CONTRACT_NAME,
11+
msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg},
12+
CounterContract,
13+
};
14+
15+
#[interface(InstantiateMsg, ExecuteMsg, QueryMsg, MigrateMsg)]
16+
pub struct Counter;
17+
18+
impl<Chain: CwEnv> Uploadable for Counter<Chain> {
19+
// Return the path to the wasm file
20+
fn wasm(&self) -> WasmPath {
21+
let crate_path = env!("CARGO_MANIFEST_DIR");
22+
let wasm_path = format!("{}/../artifacts/{}", crate_path, "mock.wasm");
23+
24+
WasmPath::new(wasm_path).unwrap()
25+
}
26+
// Return a CosmWasm contract wrapper
27+
fn wrapper(&self) -> Box<dyn MockContract<Empty>> {
28+
Box::new(
29+
ContractWrapper::new_with_empty(
30+
crate::contract::execute,
31+
crate::contract::instantiate,
32+
crate::contract::query,
33+
)
34+
.with_migrate(crate::contract::migrate),
35+
)
36+
}
37+
}
38+
// ANCHOR_END: custom_interface
39+
40+
// ANCHOR: daemon
41+
impl Counter<Daemon> {
42+
/// Deploys the counter contract at a specific block height
43+
pub fn await_launch(&self) -> Result<()> {
44+
let daemon = self.get_chain();
45+
let rt = daemon.rt_handle.clone();
46+
47+
rt.block_on(async {
48+
// Get the node query client, there are a lot of other clients available.
49+
let node = daemon.query_client::<Node>();
50+
let mut latest_block = node.latest_block().await.unwrap();
51+
52+
while latest_block.header.height.value() < 100 {
53+
// wait for the next block
54+
daemon.next_block().unwrap();
55+
latest_block = node.latest_block().await.unwrap();
56+
}
57+
});
58+
59+
let contract = CounterContract::new(CONTRACT_NAME, daemon.clone());
60+
61+
// Upload the contract
62+
contract.upload().unwrap();
63+
64+
// Instantiate the contract
65+
let msg = InstantiateMsg { count: 1i32 };
66+
contract.instantiate(&msg, None, None).unwrap();
67+
68+
Ok(())
69+
}
70+
}
71+
// ANCHOR_END: daemon

contracts/counter/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
pub mod contract;
22
mod error;
3+
pub(crate) mod execute;
34
pub mod msg;
5+
pub(crate) mod query;
46
pub mod state;
57

68
pub use crate::error::ContractError;
79
// ANCHOR: interface_reexport
810
#[cfg(feature = "interface")]
911
pub use crate::contract::CounterContract;
1012
// ANCHOR_END: interface_reexport
13+
// ANCHOR: fn_re_export
1114
#[cfg(feature = "interface")]
1215
pub use crate::msg::{ExecuteMsgFns as CounterExecuteMsgFns, QueryMsgFns as CounterQueryMsgFns};
16+
// ANCHOR_END: fn_re_export
17+
18+
// ANCHOR: custom_interface
19+
#[cfg(feature = "interface")]
20+
mod interface;
21+
// ANCHOR_END: custom_interface

contracts/counter/src/msg.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,31 @@ pub struct InstantiateMsg {
55
pub count: i32,
66
}
77

8+
// ANCHOR: exec_msg
89
#[cw_serde]
9-
#[cfg_attr(feature = "interface", derive(cw_orch::ExecuteFns))]
10+
#[cfg_attr(feature = "interface", derive(cw_orch::ExecuteFns))] // Function generation
1011
pub enum ExecuteMsg {
1112
Increment {},
1213
Reset { count: i32 },
1314
}
15+
// ANCHOR_END: exec_msg
1416

17+
// ANCHOR: query_msg
1518
#[cw_serde]
16-
#[cfg_attr(feature = "interface", derive(cw_orch::QueryFns))]
19+
#[cfg_attr(feature = "interface", derive(cw_orch::QueryFns))] // Function generation
1720
#[derive(QueryResponses)]
1821
pub enum QueryMsg {
1922
// GetCount returns the current count as a json-encoded number
2023
#[returns(GetCountResponse)]
2124
GetCount {},
2225
}
2326

24-
// We define a custom struct for each query response
27+
// Custom response for the query
2528
#[cw_serde]
2629
pub struct GetCountResponse {
2730
pub count: i32,
2831
}
32+
// ANCHOR_END: query_msg
2933

3034
#[cw_serde]
3135
pub struct MigrateMsg {

contracts/counter/src/query.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use cosmwasm_std::{Deps, StdResult};
2+
3+
use crate::{msg::GetCountResponse, state::STATE};
4+
5+
pub fn count(deps: Deps) -> StdResult<GetCountResponse> {
6+
let state = STATE.load(deps.storage)?;
7+
Ok(GetCountResponse { count: state.count })
8+
}

0 commit comments

Comments
 (0)