Skip to content

Commit

Permalink
feat: Allow usage of ExecC and QueryC in other params then `Respo…
Browse files Browse the repository at this point in the history
…nse` and `Ctx` types
  • Loading branch information
jawoznia committed Jul 31, 2024
1 parent ef48022 commit 0007a19
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 50 deletions.
1 change: 0 additions & 1 deletion examples/contracts/custom/src/cw1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ impl Cw1 for CustomContract {
type Error = StdError;
type ExecC = Empty;
type QueryC = Empty;
type CosmosCustomMsg = Empty;

fn execute(&self, _ctx: ExecCtx, _msgs: Vec<CosmosMsg>) -> StdResult<Response> {
Ok(Response::new())
Expand Down
5 changes: 2 additions & 3 deletions examples/contracts/cw1-subkeys/src/cw1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ where
type Error = ContractError;
type ExecC = E;
type QueryC = Q;
type CosmosCustomMsg = E;

fn execute(
&self,
ctx: ExecCtx<Self::QueryC>,
msgs: Vec<CosmosMsg<Self::CosmosCustomMsg>>,
msgs: Vec<CosmosMsg<Self::ExecC>>,
) -> Result<Response<Self::ExecC>, Self::Error> {
let authorized: StdResult<_> = msgs.iter().try_fold(true, |acc, msg| {
Ok(acc & self.is_authorized(ctx.deps.as_ref(), &ctx.env, &ctx.info.sender, msg)?)
Expand All @@ -37,7 +36,7 @@ where
&self,
ctx: QueryCtx<Self::QueryC>,
sender: String,
msg: CosmosMsg<Self::CosmosCustomMsg>,
msg: CosmosMsg<Self::ExecC>,
) -> StdResult<CanExecuteResp> {
let sender = Addr::unchecked(sender);

Expand Down
5 changes: 2 additions & 3 deletions examples/contracts/cw1-whitelist/src/cw1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ where
type Error = ContractError;
type ExecC = E;
type QueryC = Q;
type CosmosCustomMsg = E;

fn execute(
&self,
ctx: ExecCtx<Self::QueryC>,
msgs: Vec<CosmosMsg<Self::CosmosCustomMsg>>,
msgs: Vec<CosmosMsg<Self::ExecC>>,
) -> Result<Response<Self::ExecC>, ContractError> {
if !self.is_admin(ctx.deps.as_ref(), &ctx.info.sender) {
return Err(ContractError::Unauthorized);
Expand All @@ -34,7 +33,7 @@ where
&self,
ctx: QueryCtx<Self::QueryC>,
sender: String,
_msg: CosmosMsg<Self::CosmosCustomMsg>,
_msg: CosmosMsg<Self::ExecC>,
) -> StdResult<CanExecuteResp> {
let resp = CanExecuteResp {
can_execute: self.is_admin(ctx.deps, &Addr::unchecked(sender)),
Expand Down
1 change: 0 additions & 1 deletion examples/contracts/generic_contract/src/cw1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ impl<
type Error = StdError;
type ExecC = Empty;
type QueryC = Empty;
type CosmosCustomMsg = Empty;

fn execute(&self, _ctx: ExecCtx, _msgs: Vec<CosmosMsg>) -> StdResult<Response> {
Ok(Response::new())
Expand Down
1 change: 0 additions & 1 deletion examples/contracts/generic_iface_on_contract/src/cw1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ impl Cw1 for crate::contract::NonGenericContract {
type Error = StdError;
type ExecC = Empty;
type QueryC = Empty;
type CosmosCustomMsg = Empty;

fn execute(&self, _ctx: ExecCtx, _msgs: Vec<CosmosMsg>) -> StdResult<Response> {
Ok(Response::new())
Expand Down
1 change: 0 additions & 1 deletion examples/contracts/generics_forwarded/src/cw1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ where
type Error = ContractError;
type ExecC = Empty;
type QueryC = Empty;
type CosmosCustomMsg = Empty;

fn execute(&self, _ctx: ExecCtx, _msgs: Vec<CosmosMsg>) -> Result<Response, Self::Error> {
Ok(Response::new())
Expand Down
17 changes: 15 additions & 2 deletions examples/interfaces/custom-and-generic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ mod tests {
#[cosmwasm_schema::cw_serde]
pub struct SvCustomMsg;
impl cosmwasm_std::CustomMsg for SvCustomMsg {}
#[cosmwasm_schema::cw_serde]
pub struct SvCustomQuery;
impl cosmwasm_std::CustomQuery for SvCustomQuery {}

#[test]
fn construct_messages() {
Expand Down Expand Up @@ -121,8 +124,8 @@ mod tests {
Sudo2T = SvCustomMsg,
Sudo3T = SvCustomMsg,
Error = (),
ExecC = (),
QueryC = (),
ExecC = SvCustomMsg,
QueryC = SvCustomQuery,
>,
>::borrowed(&contract, &querier_wrapper);

Expand All @@ -147,6 +150,8 @@ mod tests {
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomQuery,
SvCustomMsg,
> as InterfaceApi>::Query::custom_generic_query_one(
SvCustomMsg {}, SvCustomMsg {}
);
Expand All @@ -162,6 +167,8 @@ mod tests {
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomQuery,
SvCustomMsg,
> as InterfaceApi>::Exec::custom_generic_execute_one(
vec![CosmosMsg::Custom(SvCustomMsg {})],
vec![CosmosMsg::Custom(SvCustomMsg {})],
Expand All @@ -178,6 +185,8 @@ mod tests {
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomQuery,
SvCustomMsg,
> as InterfaceApi>::Exec::custom_generic_execute_two(
vec![CosmosMsg::Custom(SvCustomMsg {})],
vec![CosmosMsg::Custom(SvCustomMsg {})],
Expand All @@ -194,6 +203,8 @@ mod tests {
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomQuery,
SvCustomMsg,
> as InterfaceApi>::Sudo::custom_generic_sudo_one(
CosmosMsg::Custom(SvCustomMsg {}),
CosmosMsg::Custom(SvCustomMsg {}),
Expand All @@ -210,6 +221,8 @@ mod tests {
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomQuery,
SvCustomMsg,
> as InterfaceApi>::Sudo::custom_generic_sudo_one(
CosmosMsg::Custom(SvCustomMsg {}),
CosmosMsg::Custom(SvCustomMsg {}),
Expand Down
5 changes: 2 additions & 3 deletions examples/interfaces/cw1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub trait Cw1 {
type Error: From<StdError>;
type ExecC: CustomMsg;
type QueryC: CustomQuery;
type CosmosCustomMsg: CustomMsg;

/// Execute requests the contract to re-dispatch all these messages with the
/// contract's address as sender. Every implementation has it's own logic to
Expand All @@ -24,7 +23,7 @@ pub trait Cw1 {
fn execute(
&self,
ctx: ExecCtx<Self::QueryC>,
msgs: Vec<CosmosMsg<Self::CosmosCustomMsg>>,
msgs: Vec<CosmosMsg<Self::ExecC>>,
) -> Result<Response<Self::ExecC>, Self::Error>;

/// Checks permissions of the caller on this proxy.
Expand All @@ -35,7 +34,7 @@ pub trait Cw1 {
&self,
ctx: QueryCtx<Self::QueryC>,
sender: String,
msg: CosmosMsg<Self::CosmosCustomMsg>,
msg: CosmosMsg<Self::ExecC>,
) -> StdResult<CanExecuteResp>;
}

Expand Down
29 changes: 1 addition & 28 deletions sylvia-derive/src/associated_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use syn::{parse_quote, ItemTrait, TraitItem, TraitItemType, WhereClause, WherePr
pub const ERROR_TYPE: &str = "Error";
pub const EXEC_TYPE: &str = "ExecC";
pub const QUERY_TYPE: &str = "QueryC";
pub const RESERVED_TYPES: [&str; 3] = [ERROR_TYPE, EXEC_TYPE, QUERY_TYPE];

#[derive(Default)]
pub struct AssociatedTypes<'a>(Vec<&'a TraitItemType>);
Expand Down Expand Up @@ -35,19 +34,8 @@ impl<'a> AssociatedTypes<'a> {
.cloned()
}

pub fn without_special(&self) -> impl Iterator<Item = &TraitItemType> {
self.0
.iter()
.filter(|associated| {
!RESERVED_TYPES
.iter()
.any(|reserved| reserved == &associated.ident.to_string().as_str())
})
.cloned()
}

pub fn as_where_predicates(&self) -> Vec<WherePredicate> {
self.without_special()
self.without_error()
.map(|associated| {
let name = &associated.ident;
let colon = &associated.colon_token;
Expand All @@ -66,10 +54,6 @@ impl<'a> AssociatedTypes<'a> {
}
}

pub fn as_filtered_names(&self) -> impl Iterator<Item = &Ident> {
self.filtered().map(|associated| &associated.ident)
}

pub fn emit_contract_predicate(&self, trait_name: &Ident) -> TokenStream {
let predicate = quote! { ContractT: #trait_name };
if self.0.is_empty() {
Expand All @@ -85,17 +69,6 @@ impl<'a> AssociatedTypes<'a> {
#predicate < #(#bounds,)* >
}
}

pub fn filtered(&self) -> impl Iterator<Item = &TraitItemType> {
self.0
.iter()
.filter(|associated| {
!RESERVED_TYPES
.iter()
.any(|reserved| reserved == &associated.ident.to_string().as_str())
})
.cloned()
}
}

pub trait ItemType {
Expand Down
4 changes: 2 additions & 2 deletions sylvia-derive/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
} = self;

let generics: Vec<_> = associated_types
.without_special()
.without_error()
.map(ItemType::as_name)
.collect();
let all_generics: Vec<_> = associated_types.all_names().collect();
Expand All @@ -64,7 +64,7 @@ where
.variants()
.map(|variant| variant.emit_executor_method_declaration());

let types_declaration = associated_types.filtered().collect::<Vec<_>>();
let types_declaration = associated_types.without_error().collect::<Vec<_>>();
let where_clause = associated_types.as_where_clause();

quote! {
Expand Down
5 changes: 4 additions & 1 deletion sylvia-derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ impl<'a> TraitInput<'a> {
custom,
} = self;
let messages = self.emit_messages();
let associated_names: Vec<_> = associated_types.as_filtered_names().collect();
let associated_names: Vec<_> = associated_types
.without_error()
.map(ItemType::as_name)
.collect();

let executor_variants =
MsgVariants::new(item.as_variants(), MsgType::Exec, &associated_names, &None);
Expand Down
2 changes: 1 addition & 1 deletion sylvia-derive/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ impl<'a> InterfaceApi<'a> {

let interface_name = &source.ident;
let generics: Vec<_> = associated_types
.without_special()
.without_error()
.map(ItemType::as_name)
.collect();
let exec_variants = MsgVariants::new(
Expand Down
2 changes: 1 addition & 1 deletion sylvia-derive/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ impl<'a> TraitMtHelpers<'a> {
.collect();

let associated_args_for_api: Vec<_> = associated_types
.without_special()
.without_error()
.map(|associated| {
let assoc = &associated.ident;
quote! { Self:: #assoc }
Expand Down
4 changes: 2 additions & 2 deletions sylvia-derive/src/querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
} = self;

let generics: Vec<_> = associated_types
.without_special()
.without_error()
.map(ItemType::as_name)
.collect();
let all_generics: Vec<_> = associated_types.all_names().collect();
Expand All @@ -63,7 +63,7 @@ where
.variants()
.map(|variant| variant.emit_querier_method_declaration());

let types_declaration = associated_types.filtered().collect::<Vec<_>>();
let types_declaration = associated_types.without_error().collect::<Vec<_>>();
let where_clause = associated_types.as_where_clause();

quote! {
Expand Down

0 comments on commit 0007a19

Please sign in to comment.