Skip to content

Commit

Permalink
feat: Generate sudo multitest helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Mar 4, 2024
1 parent c3692ee commit 3a60605
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 58 deletions.
72 changes: 43 additions & 29 deletions sylvia-derive/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,16 @@ impl<'a> MsgVariant<'a> {
.map_err(Into::into)
}
},
MsgType::Sudo => quote! {
pub fn #name (&self, #(#params,)* ) -> Result< #sylvia ::cw_multi_test::AppResponse, #error_type> {
let msg = #enum_name :: #name ( #(#arguments),* );

(*self.app)
.app_mut()
.wasm_sudo(self.contract_addr.clone(), &msg)
.map_err(|err| err.downcast().unwrap())
}
},
_ => quote! {},
}
}
Expand Down Expand Up @@ -719,6 +729,16 @@ impl<'a> MsgVariant<'a> {
}
}
}
MsgType::Sudo => quote! {
fn #name (&self, #(#params,)* ) -> Result< #sylvia ::cw_multi_test::AppResponse, #error_type> {
let msg = #interface_api :: #type_name :: #name ( #(#arguments),* );

(*self.app)
.app_mut()
.wasm_sudo(self.contract_addr.clone(), &msg)
.map_err(|err| err.downcast().unwrap())
}
},
_ => quote! {},
}
}
Expand Down Expand Up @@ -757,6 +777,10 @@ impl<'a> MsgVariant<'a> {
fn #name (&self, #(#params,)* ) -> Result<#return_type, #error_type>;
}
}
,
MsgType::Sudo => quote! {
fn #name (&self, #(#params,)* ) -> Result< #sylvia ::cw_multi_test::AppResponse, #error_type>;
},
_ => quote! {},
}
}
Expand Down Expand Up @@ -1665,20 +1689,25 @@ impl<'a> EntryPoints<'a> {

#[cfg(not(tarpaulin_include))]
{
let entry_points = [instantiate_variants, exec_variants, query_variants]
.into_iter()
.map(
|variants| match override_entry_points.get_entry_point(variants.msg_ty) {
Some(_) => quote! {},
None => variants.emit_default_entry_point(
&custom_msg,
&custom_query,
name,
error,
&attrs.generics,
),
},
);
let entry_points = [
instantiate_variants,
exec_variants,
query_variants,
sudo_variants,
]
.into_iter()
.map(|variants| {
match override_entry_points.get_entry_point(variants.msg_ty) {
Some(_) => quote! {},
None => variants.emit_default_entry_point(
&custom_msg,
&custom_query,
name,
error,
&attrs.generics,
),
}
});

let migrate_not_overridden = override_entry_points
.get_entry_point(MsgType::Migrate)
Expand Down Expand Up @@ -1714,19 +1743,6 @@ impl<'a> EntryPoints<'a> {
_ => quote! {},
});

let sudo = override_entry_points
.get_entry_point(MsgType::Sudo)
.map(|_| quote! {})
.unwrap_or_else(|| {
sudo_variants.emit_default_entry_point(
&custom_msg,
&custom_query,
name,
error,
&attrs.generics,
)
});

quote! {
pub mod entry_points {
use super::*;
Expand All @@ -1736,8 +1752,6 @@ impl<'a> EntryPoints<'a> {
#migrate

#reply_ep

#sudo
}
}
}
Expand Down
47 changes: 39 additions & 8 deletions sylvia-derive/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct ContractMtHelpers<'a> {
query_variants: MsgVariants<'a, GenericParam>,
migrate_variants: MsgVariants<'a, GenericParam>,
reply_variants: MsgVariants<'a, GenericParam>,
sudo_variants: MsgVariants<'a, GenericParam>,
}

impl<'a> ContractMtHelpers<'a> {
Expand Down Expand Up @@ -89,6 +90,12 @@ impl<'a> ContractMtHelpers<'a> {
generic_params,
where_clause,
);
let sudo_variants = MsgVariants::new(
source.as_variants(),
MsgType::Sudo,
generic_params,
where_clause,
);

let error_type: Type = if is_trait(source) {
let associated_error = source.items.iter().find_map(|item| match item {
Expand Down Expand Up @@ -123,6 +130,7 @@ impl<'a> ContractMtHelpers<'a> {
instantiate_variants,
exec_variants,
query_variants,
sudo_variants,
migrate_variants,
reply_variants,
}
Expand All @@ -136,6 +144,7 @@ impl<'a> ContractMtHelpers<'a> {
exec_variants,
query_variants,
migrate_variants,
sudo_variants,
generic_params,
where_clause,
..
Expand All @@ -162,6 +171,8 @@ impl<'a> ContractMtHelpers<'a> {
exec_variants.emit_multitest_proxy_methods(&custom_msg, &mt_app, error_type);
let query_methods =
query_variants.emit_multitest_proxy_methods(&custom_msg, &mt_app, error_type);
let sudo_methods =
sudo_variants.emit_multitest_proxy_methods(&custom_msg, &mt_app, error_type);
let migrate_methods =
migrate_variants.emit_multitest_proxy_methods(&custom_msg, &mt_app, error_type);
let where_predicates = where_clause
Expand Down Expand Up @@ -215,6 +226,7 @@ impl<'a> ContractMtHelpers<'a> {
#( #exec_methods )*
#( #migrate_methods )*
#( #query_methods )*
#( #sudo_methods )*
}

impl<'app, BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, #(#generic_params,)* >
Expand Down Expand Up @@ -498,6 +510,7 @@ impl<'a> ContractMtHelpers<'a> {

let bracketed_generics = emit_bracketed_generics(generic_params);
let full_where_clause = &source.generics.where_clause;

let instantiate_body = override_entry_points
.get_entry_point(MsgType::Instantiate)
.map(OverrideEntryPoint::emit_multitest_dispatch)
Expand All @@ -516,20 +529,14 @@ impl<'a> ContractMtHelpers<'a> {
let sudo_body = override_entry_points
.get_entry_point(MsgType::Sudo)
.map(OverrideEntryPoint::emit_multitest_dispatch)
.unwrap_or_else(|| {
quote! {
#sylvia ::anyhow::bail!("sudo not implemented for contract")
}
});
.unwrap_or_else(|| emit_default_dispatch(&MsgType::Sudo, contract));

let migrate_body = match override_entry_points.get_entry_point(MsgType::Migrate) {
Some(entry_point) => entry_point.emit_multitest_dispatch(),
None if migrate_variants.get_only_variant().is_some() => {
emit_default_dispatch(&MsgType::Migrate, contract)
}
None => quote! {
#sylvia ::anyhow::bail!("migrate not implemented for contract")
},
None => quote! { #sylvia ::anyhow::bail!("migrate not implemented for contract") },
};

let reply_body = match override_entry_points.get_entry_point(MsgType::Reply) {
Expand Down Expand Up @@ -630,6 +637,7 @@ pub struct ImplMtHelpers<'a> {
generic_params: &'a [&'a GenericParam],
exec_variants: MsgVariants<'a, GenericParam>,
query_variants: MsgVariants<'a, GenericParam>,
sudo_variants: MsgVariants<'a, GenericParam>,
where_clause: &'a Option<syn::WhereClause>,
contract_module: &'a Option<&'a Path>,
contract_name: &'a Ident,
Expand All @@ -656,6 +664,12 @@ impl<'a> ImplMtHelpers<'a> {
generic_params,
where_clause,
);
let sudo_variants = MsgVariants::new(
source.as_variants(),
MsgType::Sudo,
generic_params,
where_clause,
);
let associated_error = source.items.iter().find_map(|item| match item {
ImplItem::Type(ty) if ty.ident == "Error" => Some(&ty.ty),
_ => None,
Expand All @@ -679,6 +693,7 @@ impl<'a> ImplMtHelpers<'a> {
interfaces,
exec_variants,
query_variants,
sudo_variants,
contract_module,
}
}
Expand All @@ -692,6 +707,7 @@ impl<'a> ImplMtHelpers<'a> {
generic_params,
exec_variants,
query_variants,
sudo_variants,
where_clause,
contract_module,
contract_name,
Expand Down Expand Up @@ -753,6 +769,13 @@ impl<'a> ImplMtHelpers<'a> {
&interface_api,
&associated_items,
);
let sudo_methods = sudo_variants.emit_interface_multitest_proxy_methods(
&custom_msg,
&mt_app,
error_type,
&interface_api,
&associated_items,
);
let exec_methods_declarations = exec_variants.emit_proxy_methods_declarations(
&custom_msg,
error_type,
Expand All @@ -765,6 +788,12 @@ impl<'a> ImplMtHelpers<'a> {
&interface_api,
&associated_items,
);
let sudo_methods_declarations = sudo_variants.emit_proxy_methods_declarations(
&custom_msg,
error_type,
&interface_api,
&associated_items,
);

let contract_module = match contract_module {
Some(contract_module) => quote! { #contract_module :: },
Expand All @@ -785,6 +814,7 @@ impl<'a> ImplMtHelpers<'a> {
pub trait #trait_name<MtApp, #(#generic_params,)* > #where_clause {
#(#query_methods_declarations)*
#(#exec_methods_declarations)*
#(#sudo_methods_declarations)*
}

impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, #(#generic_params,)* > #trait_name< #mt_app, #(#generic_params,)* > for #contract_module sv::multitest_utils:: #contract_proxy <'_, #mt_app, #(#generic_params,)* >
Expand All @@ -811,6 +841,7 @@ impl<'a> ImplMtHelpers<'a> {
{
#(#query_methods)*
#(#exec_methods)*
#(#sudo_methods)*
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion sylvia/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<'a, C: cosmwasm_std::CustomQuery> From<(Deps<'a, C>, Env)> for QueryCtx<'a,
}
}

impl<'a, C: CustomQuery> From<(DepsMut<'a, C>, Env)> for SudoCtx<'a, C> {
impl<'a, C: cosmwasm_std::CustomQuery> From<(DepsMut<'a, C>, Env)> for SudoCtx<'a, C> {
fn from((deps, env): (DepsMut<'a, C>, Env)) -> Self {
Self { deps, env }
}
Expand Down
Loading

0 comments on commit 3a60605

Please sign in to comment.