From b7052d4f29a1d84a2a066e32fe45247994e952b1 Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 21 Aug 2024 14:11:16 +0200 Subject: [PATCH] Updates. --- src/pages/cw-multi-test/getting-started.mdx | 82 ++++++++++++++++++++- 1 file changed, 79 insertions(+), 3 deletions(-) diff --git a/src/pages/cw-multi-test/getting-started.mdx b/src/pages/cw-multi-test/getting-started.mdx index 8e75d863..066272f9 100644 --- a/src/pages/cw-multi-test/getting-started.mdx +++ b/src/pages/cw-multi-test/getting-started.mdx @@ -6,9 +6,15 @@ import { Tabs } from "nextra/components"; # Getting started -Counter example +```text +├── Cargo.toml +└── src + ├── contract.rs + └── lib.rs +``` + ```rust showLineNumbers copy @@ -16,11 +22,81 @@ Counter example ``` + -```rust showLineNumbers copy -// Sylvia +```toml copy filename="Cargo.toml" +[package] +name = "counter" +version = "0.0.1" +edition = "2021" + +[features] +library = [] + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +cosmwasm-std = { version = "2" } +cosmwasm-schema = "2" +cw-storage-plus = "2" +schemars = "0.8" +serde = "1.0" +sylvia = "1" + +[dev-dependencies] +sylvia = { version = "1", features = ["mt"] } +cw-multi-test = { version = "2", features = ["cosmwasm_2_0"] } +``` + +```rust showLineNumbers copy filename="lib.rs" +pub mod contract; +``` + +```rust showLineNumbers copy filename="contract.rs" +use cosmwasm_schema::cw_serde; +use cosmwasm_std::{Response, StdResult}; +use cw_storage_plus::Item; +use sylvia::contract; +use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx}; + +#[cw_serde] +pub struct CountResponse { + pub count: u8, +} + +pub struct CounterContract { + pub count: Item, +} + +#[cfg_attr(not(feature = "library"), sylvia::entry_points)] +#[contract] +impl CounterContract { + pub const fn new() -> Self { + Self { count: Item::new("count") } + } + + #[sv::msg(instantiate)] + fn instantiate(&self, ctx: InstantiateCtx) -> StdResult { + self.count.save(ctx.deps.storage, &0)?; + Ok(Response::new()) + } + + #[sv::msg(exec)] + fn increment(&self, ctx: ExecCtx) -> StdResult { + self.count.update(ctx.deps.storage, |count| -> StdResult { Ok(count.saturating_add(1)) })?; + Ok(Response::new()) + } + + #[sv::msg(query)] + fn count(&self, ctx: QueryCtx) -> StdResult { + let count = self.count.load(ctx.deps.storage)?; + Ok(CountResponse { count }) + } +} ``` +