-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Forward
CustomQuery
to entry points
- Loading branch information
Showing
15 changed files
with
273 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[alias] | ||
wasm = "build --release --target wasm32-unknown-unknown --lib" | ||
wasm-debug = "build --target wasm32-unknown-unknown --lib" | ||
unit-test = "test --lib" | ||
integration-test = "test --test integration" | ||
schema = "run --bin schema" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[package] | ||
name = "custom" | ||
version = { workspace = true } | ||
authors = ["Jan Woźniak <[email protected]>"] | ||
edition = { workspace = true } | ||
description = "Example of custom message usage." | ||
license = "Apache-2.0" | ||
repository = "https://github.com/CosmWasm/sylvia" | ||
homepage = "https://cosmwasm.com" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[features] | ||
library = [] | ||
mt = ["library"] | ||
|
||
[dependencies] | ||
cosmwasm-schema = "1.2" | ||
cosmwasm-std = { version = "1.3", features = ["staking"] } | ||
cw-storage-plus = "1.0" | ||
serde = { version = "1.0", default-features = false, features = ["derive"] } | ||
sylvia = { path = "../../../sylvia" } | ||
|
||
[dev-dependencies] | ||
anyhow = "1.0" | ||
cw-multi-test = "0.16" | ||
sylvia = { path = "../../../sylvia", features = ["mt"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use cosmwasm_schema::write_api; | ||
|
||
use custom::contract::{ContractExecMsg, ContractQueryMsg, InstantiateMsg}; | ||
|
||
#[cfg(not(tarpaulin_include))] | ||
fn main() { | ||
write_api! { | ||
instantiate: InstantiateMsg, | ||
execute: ContractExecMsg, | ||
query: ContractQueryMsg, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use cosmwasm_std::{CosmosMsg, QueryRequest, Response, StdResult}; | ||
use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx}; | ||
use sylvia::{contract, entry_points, schemars}; | ||
|
||
use crate::messages::{CountResponse, CounterMsg, CounterQuery}; | ||
|
||
pub struct CustomContract; | ||
|
||
#[cfg_attr(not(feature = "mt"), entry_points)] | ||
#[contract] | ||
#[sv::custom(query=CounterQuery, msg=CounterMsg)] | ||
impl CustomContract { | ||
pub const fn new() -> Self { | ||
Self | ||
} | ||
|
||
#[msg(instantiate)] | ||
pub fn instantiate( | ||
&self, | ||
_ctx: InstantiateCtx<CounterQuery>, | ||
) -> StdResult<Response<CounterMsg>> { | ||
Ok(Response::default()) | ||
} | ||
|
||
#[msg(exec)] | ||
pub fn send_custom(&self, _ctx: ExecCtx<CounterQuery>) -> StdResult<Response<CounterMsg>> { | ||
let msg = CosmosMsg::Custom(CounterMsg::Increment {}); | ||
let resp = Response::default().add_message(msg); | ||
Ok(resp) | ||
} | ||
|
||
#[msg(query)] | ||
pub fn query_custom(&self, ctx: QueryCtx<CounterQuery>) -> StdResult<CountResponse> { | ||
let resp = ctx | ||
.deps | ||
.querier | ||
.query::<CountResponse>(&QueryRequest::Custom(CounterQuery::Count {}))?; | ||
|
||
Ok(resp) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod contract; | ||
pub mod messages; | ||
#[cfg(any(test, feature = "mt"))] | ||
pub mod multitest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use cosmwasm_schema::cw_serde; | ||
use cosmwasm_std::{CustomMsg, CustomQuery}; | ||
|
||
#[cw_serde] | ||
pub struct CountResponse { | ||
pub count: u64, | ||
} | ||
|
||
#[cw_serde] | ||
pub enum CounterMsg { | ||
Increment {}, | ||
} | ||
|
||
#[cw_serde] | ||
pub enum CounterQuery { | ||
Count {}, | ||
} | ||
|
||
impl CustomMsg for CounterMsg {} | ||
|
||
impl CustomQuery for CounterQuery {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod custom_module; | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use cosmwasm_schema::schemars::JsonSchema; | ||
use cosmwasm_std::testing::{MockApi, MockStorage}; | ||
use cosmwasm_std::{ | ||
to_binary, Addr, Api, Binary, BlockInfo, CustomQuery, Empty, Querier, StdError, StdResult, | ||
Storage, | ||
}; | ||
use cw_multi_test::{AppResponse, BankKeeper, CosmosRouter, Module, WasmKeeper}; | ||
use cw_storage_plus::Item; | ||
use serde::de::DeserializeOwned; | ||
use std::fmt::Debug; | ||
|
||
use crate::messages::{CountResponse, CounterMsg, CounterQuery}; | ||
|
||
pub type CustomApp = cw_multi_test::App< | ||
BankKeeper, | ||
MockApi, | ||
MockStorage, | ||
CustomModule, | ||
WasmKeeper<CounterMsg, CounterQuery>, | ||
>; | ||
|
||
pub struct CustomModule { | ||
pub counter: Item<'static, u64>, | ||
} | ||
|
||
impl CustomModule { | ||
pub fn new() -> Self { | ||
Self { | ||
counter: Item::new("counter"), | ||
} | ||
} | ||
|
||
pub fn save_counter(&self, storage: &mut dyn Storage, value: u64) -> StdResult<()> { | ||
self.counter.save(storage, &value) | ||
} | ||
} | ||
|
||
impl Module for CustomModule { | ||
type ExecT = CounterMsg; | ||
type QueryT = CounterQuery; | ||
type SudoT = Empty; | ||
|
||
fn execute<ExecC, QueryC>( | ||
&self, | ||
_api: &dyn Api, | ||
storage: &mut dyn Storage, | ||
_router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>, | ||
_block: &BlockInfo, | ||
_sender: Addr, | ||
msg: Self::ExecT, | ||
) -> anyhow::Result<AppResponse> | ||
where | ||
ExecC: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static, | ||
QueryC: CustomQuery + DeserializeOwned + 'static, | ||
{ | ||
match msg { | ||
CounterMsg::Increment {} => { | ||
self.counter | ||
.update(storage, |value| Ok::<_, StdError>(value + 1))?; | ||
Ok(AppResponse::default()) | ||
} | ||
} | ||
} | ||
|
||
fn sudo<ExecC, QueryC>( | ||
&self, | ||
_api: &dyn Api, | ||
_storage: &mut dyn Storage, | ||
_router: &dyn CosmosRouter<ExecC = ExecC, QueryC = QueryC>, | ||
_block: &BlockInfo, | ||
_msg: Self::SudoT, | ||
) -> anyhow::Result<AppResponse> | ||
where | ||
ExecC: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static, | ||
QueryC: CustomQuery + DeserializeOwned + 'static, | ||
{ | ||
Ok(AppResponse::default()) | ||
} | ||
|
||
fn query( | ||
&self, | ||
_api: &dyn Api, | ||
storage: &dyn Storage, | ||
_querier: &dyn Querier, | ||
_block: &BlockInfo, | ||
request: Self::QueryT, | ||
) -> anyhow::Result<Binary> { | ||
match request { | ||
CounterQuery::Count {} => { | ||
let count = self.counter.load(storage)?; | ||
let res = CountResponse { count }; | ||
to_binary(&res).map_err(Into::into) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use sylvia::multitest::App; | ||
|
||
use crate::contract::multitest_utils::CodeId; | ||
use crate::messages::{CounterMsg, CounterQuery}; | ||
|
||
use super::custom_module::{CustomApp, CustomModule}; | ||
|
||
#[test] | ||
fn test_custom() { | ||
let owner = "owner"; | ||
|
||
let mt_app = cw_multi_test::BasicAppBuilder::<CounterMsg, CounterQuery>::new_custom() | ||
.with_custom(CustomModule::new()) | ||
.build(|router, _, storage| { | ||
router.custom.save_counter(storage, 0).unwrap(); | ||
}); | ||
|
||
let app = App::<CustomApp>::new(mt_app); | ||
|
||
let code_id = CodeId::store_code(&app); | ||
|
||
let contract = code_id.instantiate().call(owner).unwrap(); | ||
|
||
contract.send_custom().call(owner).unwrap(); | ||
|
||
let count = contract.query_custom().unwrap().count; | ||
assert_eq!(count, 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.