Skip to content

Commit

Permalink
feat: Implement Querier on App (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Mar 18, 2024
1 parent 5255942 commit c5a5635
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 30 deletions.
2 changes: 1 addition & 1 deletion examples/contracts/cw1-subkeys/src/multitest/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn get_contract_version_works() {
.unwrap();

let version: ContractVersion =
query_contract_info(&app.app_mut().wrap(), contract.contract_addr.to_string()).unwrap();
query_contract_info(&app.querier(), contract.contract_addr.to_string()).unwrap();

assert_eq!(
ContractVersion {
Expand Down
6 changes: 2 additions & 4 deletions sylvia-derive/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,7 @@ impl<'a> MsgVariant<'a> {
let msg = #enum_name :: #name ( #(#arguments),* );

(*self.app)
.app()
.wrap()
.querier()
.query_wasm_smart(self.contract_addr.clone(), &msg)
.map_err(Into::into)
}
Expand Down Expand Up @@ -763,8 +762,7 @@ impl<'a> MsgVariant<'a> {
let msg = #interface_api :: #type_name :: #name ( #(#arguments),* );

(*self.app)
.app()
.wrap()
.querier()
.query_wasm_smart(self.contract_addr.clone(), &msg)
.map_err(Into::into)
}
Expand Down
20 changes: 5 additions & 15 deletions sylvia-derive/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,8 @@ impl<'a> ContractMtHelpers<'a> {
for #sylvia ::multitest::Proxy <'app, #mt_app, #contract_name >
where
CustomT: #sylvia ::cw_multi_test::Module,
CustomT::ExecT: std::fmt::Debug
+ PartialEq
+ Clone
+ #sylvia ::schemars::JsonSchema
+ #sylvia ::serde::de::DeserializeOwned
+ 'static,
CustomT::QueryT: #sylvia ::cw_std::CustomQuery + #sylvia ::serde::de::DeserializeOwned + 'static,
CustomT::ExecT: #sylvia::types::CustomMsg + 'static,
CustomT::QueryT: #sylvia ::types::CustomQuery + 'static,
WasmT: #sylvia ::cw_multi_test::Wasm<CustomT::ExecT, CustomT::QueryT>,
BankT: #sylvia ::cw_multi_test::Bank,
ApiT: #sylvia ::cw_std::Api,
Expand Down Expand Up @@ -322,7 +317,7 @@ impl<'a> ContractMtHelpers<'a> {
let code_info = if cfg!(feature = "cosmwasm_1_2") {
quote! {
pub fn code_info(&self) -> #sylvia ::cw_std::StdResult< #sylvia ::cw_std::CodeInfoResponse> {
self.app.app().wrap().query_wasm_code_info(self.code_id)
self.app.querier().query_wasm_code_info(self.code_id)
}
}
} else {
Expand Down Expand Up @@ -719,13 +714,8 @@ impl<'a> TraitMtHelpers<'a> {
DistrT: #sylvia ::cw_multi_test::Distribution,
IbcT: #sylvia ::cw_multi_test::Ibc,
GovT: #sylvia ::cw_multi_test::Gov,
CustomT::ExecT: Clone
+ std::fmt::Debug
+ PartialEq
+ #sylvia ::schemars::JsonSchema
+ #sylvia ::serde::de::DeserializeOwned
+ 'static,
CustomT::QueryT: #sylvia:: cw_std::CustomQuery + #sylvia ::serde::de::DeserializeOwned + 'static,
CustomT::ExecT: #sylvia ::types::CustomMsg + 'static,
CustomT::QueryT: #sylvia:: types::CustomQuery + 'static,
#mt_app : #sylvia ::cw_multi_test::Executor< #custom_msg >,
#where_predicates
{
Expand Down
94 changes: 84 additions & 10 deletions sylvia/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ use std::cell::{Ref, RefCell, RefMut};
use std::fmt::{Debug, Display};
use std::marker::PhantomData;

use cosmwasm_std::{Addr, Api, BlockInfo, Coin, CustomQuery, Empty, Storage};
use cosmwasm_std::{
Addr, Api, BlockInfo, Coin, Empty, Querier, QuerierResult, QuerierWrapper, Storage,
};
#[cfg(feature = "cosmwasm_1_2")]
use cosmwasm_std::{CodeInfoResponse, StdResult};
use cw_multi_test::{
Bank, BankKeeper, Distribution, DistributionKeeper, Executor, FailingModule, Gov,
GovFailingModule, Ibc, IbcFailingModule, Module, Router, StakeKeeper, Staking, StargateFailing,
Wasm, WasmKeeper,
GovFailingModule, Ibc, IbcFailingModule, Module, Router, StakeKeeper, Staking, Stargate,
StargateFailing, Wasm, WasmKeeper,
};
use derivative::Derivative;
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::types::{CustomMsg, CustomQuery};

#[derive(Derivative)]
#[derivative(Debug)]
pub struct Proxy<'a, MtApp, Contract> {
Expand All @@ -29,7 +32,7 @@ impl<'a, MtApp, Contract> Proxy<'a, MtApp, Contract> {
Proxy {
contract_addr,
app,
_phantom: std::marker::PhantomData::<(MtApp, Contract)>::default(),
_phantom: std::marker::PhantomData::<(MtApp, Contract)>,
}
}
}
Expand Down Expand Up @@ -59,8 +62,8 @@ impl<ExecC, QueryC> App<cw_multi_test::BasicApp<ExecC, QueryC>> {
/// Creates new default `App` implementation working with customized exec and query messages.
pub fn custom<F>(init_fn: F) -> Self
where
ExecC: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static,
QueryC: Debug + CustomQuery + DeserializeOwned + 'static,
ExecC: CustomMsg + 'static,
QueryC: Debug + CustomQuery + 'static,
F: FnOnce(
&mut Router<
BankKeeper,
Expand Down Expand Up @@ -101,8 +104,8 @@ impl<MtApp> App<MtApp> {
impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>
App<cw_multi_test::App<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>>
where
CustomT::ExecT: std::fmt::Debug + PartialEq + Clone + JsonSchema + DeserializeOwned + 'static,
CustomT::QueryT: CustomQuery + DeserializeOwned + 'static,
CustomT::ExecT: CustomMsg + 'static,
CustomT::QueryT: CustomQuery + 'static,
WasmT: Wasm<CustomT::ExecT, CustomT::QueryT>,
BankT: Bank,
ApiT: Api,
Expand All @@ -127,7 +130,78 @@ where

#[cfg(feature = "cosmwasm_1_2")]
pub fn code_info(&self, code_id: u64) -> StdResult<CodeInfoResponse> {
self.app.borrow().wrap().query_wasm_code_info(code_id)
self.querier().query_wasm_code_info(code_id)
}
}

impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>
App<
cw_multi_test::App<
BankT,
ApiT,
StorageT,
CustomT,
WasmT,
StakingT,
DistrT,
IbcT,
GovT,
StargateT,
>,
>
where
CustomT::ExecT: CustomMsg + 'static,
CustomT::QueryT: CustomQuery + 'static,
WasmT: Wasm<CustomT::ExecT, CustomT::QueryT>,
BankT: Bank,
ApiT: Api,
StorageT: Storage,
CustomT: Module,
StakingT: Staking,
DistrT: Distribution,
IbcT: Ibc,
GovT: Gov,
StargateT: Stargate,
{
/// Initialize a new `cosmwasm_std::QuerierWrapper` used to call e.g. `query_wasm_smart` or
/// `query_all_balances`.
/// A counterpart to `cw_multi_test::App::wrap` method.
pub fn querier(&self) -> QuerierWrapper<'_, CustomT::QueryT> {
QuerierWrapper::new(self)
}
}

impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT> Querier
for App<
cw_multi_test::App<
BankT,
ApiT,
StorageT,
CustomT,
WasmT,
StakingT,
DistrT,
IbcT,
GovT,
StargateT,
>,
>
where
CustomT::ExecT: CustomMsg + 'static,
CustomT::QueryT: CustomQuery + 'static,
WasmT: Wasm<CustomT::ExecT, CustomT::QueryT>,
BankT: Bank,
ApiT: Api,
StorageT: Storage,
CustomT: Module,
StakingT: Staking,
DistrT: Distribution,
IbcT: Ibc,
GovT: Gov,
StargateT: Stargate,
{
fn raw_query(&self, bin_request: &[u8]) -> QuerierResult {
self.app.borrow().raw_query(bin_request)
}
}

Expand Down

0 comments on commit c5a5635

Please sign in to comment.