Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide internal multitest types #87

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions sylvia-derive/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl<'a> EnumMessage<'a> {
let enum_declaration = match name.to_string().as_str() {
"QueryMsg" => quote! {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(#sylvia ::serde::Serialize, #sylvia ::serde::Deserialize, Clone, Debug, PartialEq, #sylvia ::schemars::JsonSchema, cosmwasm_schema::QueryResponses)]
#[derive(#sylvia ::serde::Serialize, #sylvia ::serde::Deserialize, Clone, Debug, PartialEq, #sylvia ::schemars::JsonSchema, #sylvia ::cosmwasm_schema::QueryResponses)]
#[serde(rename_all="snake_case")]
pub enum #unique_enum_name #generics #where_clause {
#(#variants,)*
Expand Down Expand Up @@ -393,7 +393,7 @@ impl<'a> ContractEnumMessage<'a> {
let enum_declaration = match name.to_string().as_str() {
"QueryMsg" => quote! {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(#sylvia ::serde::Serialize, #sylvia ::serde::Deserialize, Clone, Debug, PartialEq, #sylvia ::schemars::JsonSchema, cosmwasm_schema::QueryResponses)]
#[derive(#sylvia ::serde::Serialize, #sylvia ::serde::Deserialize, Clone, Debug, PartialEq, #sylvia ::schemars::JsonSchema, #sylvia ::cosmwasm_schema::QueryResponses)]
#[serde(rename_all="snake_case")]
pub enum #name {
#(#variants,)*
Expand Down Expand Up @@ -719,7 +719,8 @@ impl<'a> GlueMessage<'a> {
quote! { #contract_name :: #variant(msg) => msg.dispatch(contract, ctx) }
});

let dispatch_arm = quote! {#contract_name :: #contract (msg) =>msg.dispatch(contract, ctx)};
let dispatch_arm =
quote! {#contract_name :: #contract (msg) => msg.dispatch(contract, ctx)};

let deserialization_attempts = interfaces.iter().map(|interface| {
let ContractMessageAttr {
Expand Down Expand Up @@ -765,7 +766,7 @@ impl<'a> GlueMessage<'a> {
"QueryMsg" => {
quote! {
#[cfg(not(target_arch = "wasm32"))]
impl cosmwasm_schema::QueryResponses for #contract_name {
impl #sylvia ::cosmwasm_schema::QueryResponses for #contract_name {
fn response_schemas_impl() -> std::collections::BTreeMap<String, #sylvia ::schemars::schema::RootSchema> {
let responses = [#(#response_schemas),*];
responses.into_iter().flatten().collect()
Expand Down
50 changes: 46 additions & 4 deletions sylvia-derive/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl<'a> MultitestHelpers<'a> {
generics: &'a [&'a GenericParam],
) -> Self {
let mut is_migrate = false;
let sylvia = crate_module();

let messages: Vec<_> = source
.items
Expand Down Expand Up @@ -91,7 +92,7 @@ impl<'a> MultitestHelpers<'a> {
}
}
} else {
quote! { cw_multi_test::AppResponse }
quote! { #sylvia ::cw_multi_test::AppResponse }
};

let name = &sig.ident;
Expand Down Expand Up @@ -196,6 +197,9 @@ impl<'a> MultitestHelpers<'a> {
error_type,
proxy_name,
source,
is_trait,
contract,
is_migrate,
..
} = self;
let sylvia = crate_module();
Expand All @@ -219,7 +223,7 @@ impl<'a> MultitestHelpers<'a> {
}
} else {
quote! {
pub fn #name (&self, #(#params,)* ) -> Result<#return_type, #error_type> {
pub fn #name (&self, #(#params,)* ) -> anyhow::Result<#return_type, #error_type> {
let msg = QueryMsg:: #name ( #(#arguments),* );

self.app
Expand Down Expand Up @@ -267,7 +271,28 @@ impl<'a> MultitestHelpers<'a> {
})
.collect();

let expose_types = if *is_trait {
quote! {}
} else {
let migrate_msg = if *is_migrate {
quote! {MigrateMsg}
} else {
quote! {#sylvia ::cw_std::Empty}
};
quote! {
impl #sylvia ::multitest::Contract for #contract {
type InstantiateMsg = InstantiateMsg;
type ExecMsg = ContractExecMsg;
type QueryMsg = ContractQueryMsg;
type MigrationMsg = #migrate_msg ;
}

}
};

quote! {
#expose_types

#[cfg(test)]
pub mod multitest_utils {
use super::*;
Expand Down Expand Up @@ -307,6 +332,7 @@ impl<'a> MultitestHelpers<'a> {
generics,
contract_name,
proxy_name,
contract,
..
} = self;

Expand Down Expand Up @@ -339,6 +365,21 @@ impl<'a> MultitestHelpers<'a> {
app: &'app #sylvia ::multitest::App,
}

impl<'app> #sylvia ::multitest::ContractCodeId<'app> for #code_id <'app> {
fn store_code(app: &'app mut #sylvia ::multitest::App) -> #code_id <'app> {
Self::store_code(app)
}
}

impl<'app> #sylvia ::multitest::Multitest<'app> for #contract {
type CodeId = #code_id <'app>;
type Contract = #proxy_name <'app>;

fn store_code(app: &'app mut #sylvia ::multitest::App) -> Self::CodeId {
Self::CodeId::store_code(app)
}
}

impl<'app> #code_id <'app> {
pub fn store_code(app: &'app mut #sylvia ::multitest::App) -> Self {
let code_id = app
Expand Down Expand Up @@ -389,7 +430,7 @@ impl<'a> MultitestHelpers<'a> {
}

#[track_caller]
pub fn call(self, sender: &str) -> Result<#proxy_name<'app>, #error_type> {
pub fn call(self, sender: &str) -> anyhow::Result<#proxy_name<'app>, #error_type> {
self.code_id
.app
.app
Expand All @@ -413,6 +454,7 @@ impl<'a> MultitestHelpers<'a> {
}

fn generate_impl_contract(&self) -> TokenStream {
let sylvia = crate_module();
let contract = &self.contract;

// MigrateMsg is not generated all the time in contrary to Exec, Query and Instantiate.
Expand All @@ -428,7 +470,7 @@ impl<'a> MultitestHelpers<'a> {
}
};
quote! {
impl cw_multi_test::Contract<cosmwasm_std::Empty> for #contract {
impl #sylvia ::cw_multi_test::Contract<cosmwasm_std::Empty> for #contract {
fn execute(
&self,
deps: cosmwasm_std::DepsMut<cosmwasm_std::Empty>,
Expand Down
3 changes: 3 additions & 0 deletions sylvia/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
pub mod multitest;
pub mod utils;

pub use cosmwasm_schema;
pub use cosmwasm_std as cw_std;
#[cfg(feature = "mt")]
pub use cw_multi_test;
pub use schemars;
pub use serde;
pub use serde_cw_value as serde_value;
Expand Down
20 changes: 20 additions & 0 deletions sylvia/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,23 @@ where
.map_err(|err| err.downcast().unwrap())
}
}

/// Trait to expose messages of the contract
pub trait Contract {
type InstantiateMsg;
type ExecMsg;
type QueryMsg;
type MigrationMsg;
}

/// Trait to expose multitest utils of the contract
pub trait Multitest<'app> {
type CodeId;
type Contract;

fn store_code(app: &'app mut App) -> Self::CodeId;
}

pub trait ContractCodeId<'app> {
fn store_code(app: &'app mut App) -> Self;
}