Skip to content

Commit

Permalink
test: Add sudo to custom example
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Mar 4, 2024
1 parent 6397a4a commit 8996fbe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
31 changes: 27 additions & 4 deletions examples/contracts/custom/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
use cosmwasm_std::{CosmosMsg, QueryRequest, Response, StdResult};
use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx};
use cw_storage_plus::Item;
use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx, SudoCtx};
use sylvia::{contract, schemars};

#[cfg(not(feature = "library"))]
use sylvia::entry_points;

use crate::messages::{CountResponse, CounterMsg, CounterQuery};

pub struct CustomContract;
pub struct CustomContract {
pub(crate) sudo_counter: Item<'static, u64>,
}

#[cfg_attr(not(feature = "library"), entry_points)]
#[contract]
#[messages(cw1 as Cw1: custom(msg, query))]
#[sv::custom(query=CounterQuery, msg=CounterMsg)]
impl CustomContract {
pub const fn new() -> Self {
Self
Self {
sudo_counter: Item::new("sudo_counter"),
}
}

#[msg(instantiate)]
pub fn instantiate(
&self,
_ctx: InstantiateCtx<CounterQuery>,
ctx: InstantiateCtx<CounterQuery>,
) -> StdResult<Response<CounterMsg>> {
self.sudo_counter.save(ctx.deps.storage, &0)?;
Ok(Response::default())
}

Expand All @@ -42,4 +48,21 @@ impl CustomContract {

Ok(resp)
}

#[msg(query)]
pub fn sudo_counter(&self, ctx: QueryCtx<CounterQuery>) -> StdResult<CountResponse> {
let count = self.sudo_counter.load(ctx.deps.storage)?;

Ok(CountResponse { count })
}

#[msg(sudo)]
pub fn increment_sudo_counter(
&self,
ctx: SudoCtx<CounterQuery>,
) -> StdResult<Response<CounterMsg>> {
self.sudo_counter
.update(ctx.deps.storage, |value| -> StdResult<_> { Ok(value + 1) })?;
Ok(Response::new())
}
}
5 changes: 5 additions & 0 deletions examples/contracts/custom/src/multitest/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ fn test_custom() {

let count = contract.query_custom().unwrap().count;
assert_eq!(count, 1);

contract.increment_sudo_counter().unwrap();

let count = contract.sudo_counter().unwrap().count;
assert_eq!(count, 1);
}

0 comments on commit 8996fbe

Please sign in to comment.