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

Added Unit variant to execute and query fns #196

Merged
merged 7 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 23 additions & 1 deletion contracts/mock_contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ where
/// test doc-comment
t: T,
},
FourthMessage,
#[cfg_attr(feature = "interface", payable)]
FifthMessage,
}

#[cw_serde]
Expand All @@ -43,6 +46,8 @@ where
/// test doc-comment
t: T,
},
#[returns(String)]
ThirdQuery,
}

#[cw_serde]
Expand All @@ -66,7 +71,7 @@ pub fn instantiate(
pub fn execute(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
info: MessageInfo,
msg: ExecuteMsg,
) -> StdResult<Response> {
match msg {
Expand All @@ -77,6 +82,15 @@ pub fn execute(
ExecuteMsg::ThirdMessage { .. } => {
Ok(Response::new().add_attribute("action", "third message passed"))
}
ExecuteMsg::FourthMessage => {
Ok(Response::new().add_attribute("action", "fourth message passed"))
}
ExecuteMsg::FifthMessage => {
if info.funds.is_empty() {
return Err(StdError::generic_err("Coins missing"));
}
Ok(Response::new().add_attribute("action", "fourth message passed"))
}
}
}

Expand All @@ -86,6 +100,7 @@ pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::FirstQuery {} => to_binary("first query passed"),
QueryMsg::SecondQuery { .. } => Err(StdError::generic_err("Query not available")),
QueryMsg::ThirdQuery => to_binary("third query passed"),
}
}

Expand All @@ -105,6 +120,7 @@ pub fn migrate(_deps: DepsMut, _env: Env, msg: MigrateMsg) -> StdResult<Response
mod test {
use super::MockContract as LocalMockContract;
use super::*;
use cosmwasm_std::coins;
use cw_orch::prelude::*;
#[test]
fn compiles() -> Result<(), CwOrchError> {
Expand All @@ -117,6 +133,12 @@ mod test {
contract.instantiate(&InstantiateMsg {}, None, None)?;
contract.first_message()?;
contract.second_message("s".to_string(), &[]).unwrap_err();
contract.fourth_message().unwrap();
Kayanski marked this conversation as resolved.
Show resolved Hide resolved
contract.fifth_message(&coins(156, "ujuno")).unwrap();

contract.first_query().unwrap();
contract.second_query("arg".to_string()).unwrap_err();
contract.third_query().unwrap();

Ok(())
}
Expand Down
12 changes: 11 additions & 1 deletion contracts/mock_contract_u64/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn instantiate(
pub fn execute(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
info: MessageInfo,
msg: ExecuteMsg<u64>,
) -> StdResult<Response> {
match msg {
Expand All @@ -31,6 +31,15 @@ pub fn execute(
ExecuteMsg::ThirdMessage { .. } => {
Ok(Response::new().add_attribute("action", "third message passed"))
}
ExecuteMsg::FourthMessage => {
Ok(Response::new().add_attribute("action", "fourth message passed"))
}
ExecuteMsg::FifthMessage => {
if info.funds.is_empty() {
return Err(StdError::generic_err("Coins missing"));
}
Ok(Response::new().add_attribute("action", "fourth message passed"))
}
}
}

Expand All @@ -40,6 +49,7 @@ pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::FirstQuery {} => to_binary("first query passed"),
QueryMsg::SecondQuery { .. } => Err(StdError::generic_err("Query not available")),
QueryMsg::ThirdQuery => to_binary("third query passed"),
}
}

Expand Down
22 changes: 16 additions & 6 deletions packages/cw-orch-fns-derive/src/execute_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,24 @@ pub fn execute_fns_derive(input: DeriveInput) -> TokenStream {
variant_func_name.set_span(variant_name.span());

let is_payable = payable(&variant);

let (maybe_coins_attr, passed_coins) = if is_payable {
(quote!(coins: &[::cosmwasm_std::Coin]),quote!(Some(coins)))
} else {
(quote!(),quote!(None))
};

match &mut variant.fields {
Fields::Unnamed(_) => None,
Fields::Unit => None,
Fields::Unit => {

Some(quote!(
fn #variant_func_name(&self, #maybe_coins_attr) -> Result<::cw_orch::prelude::TxResponse<Chain>, ::cw_orch::prelude::CwOrchError> {
let msg = #name::#variant_name;
<Self as ::cw_orch::prelude::CwOrchExecute<Chain>>::execute(self, &msg #maybe_into,#passed_coins)
}
))
}
Fields::Named(variant_fields) => {
// sort fields on field name
LexiographicMatching::default().visit_fields_named_mut(variant_fields);
Expand All @@ -55,11 +70,6 @@ pub fn execute_fns_derive(input: DeriveInput) -> TokenStream {

let variant_ident_content_names = variant_idents.iter().map(|f|f.ident.clone().unwrap());

let (maybe_coins_attr, passed_coins) = if is_payable {
(quote!(coins: &[::cosmwasm_std::Coin]),quote!(Some(coins)))
} else {
(quote!(),quote!(None))
};
let variant_attr = variant_idents.iter();
Some(quote!(
#[allow(clippy::too_many_arguments)]
Expand Down
28 changes: 17 additions & 11 deletions packages/cw-orch-fns-derive/src/query_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ pub fn query_fns_derive(input: ItemEnum) -> TokenStream {

match &mut variant.fields {
Fields::Unnamed(_) => panic!("Expected named variant"),
Fields::Unit => panic!("Expected named variant"),
Fields::Unit => {
quote!(
fn #variant_func_name(&self) -> ::core::result::Result<#response, ::cw_orch::prelude::CwOrchError> {
let msg = #name::#variant_name;
self.query(&msg #maybe_into)
}
)
},
Fields::Named(variant_fields) => {
// sort fields on field name
LexiographicMatching::default().visit_fields_named_mut(variant_fields);
Expand All @@ -53,18 +60,17 @@ pub fn query_fns_derive(input: ItemEnum) -> TokenStream {

let variant_attr = variant_fields.iter();
quote!(
#[allow(clippy::too_many_arguments)]
fn #variant_func_name(&self, #(#variant_attr,)*) -> ::core::result::Result<#response, ::cw_orch::prelude::CwOrchError> {
let msg = #name::#variant_name {
#(#variant_idents,)*
};
self.query(&msg #maybe_into)
}
)
}
#[allow(clippy::too_many_arguments)]
fn #variant_func_name(&self, #(#variant_attr,)*) -> ::core::result::Result<#response, ::cw_orch::prelude::CwOrchError> {
let msg = #name::#variant_name {
#(#variant_idents,)*
};
self.query(&msg #maybe_into)
}
)
}
}
);
});

let derived_trait = quote!(
pub trait #bname<Chain: ::cw_orch::prelude::CwEnv, #type_generics>: ::cw_orch::prelude::CwOrchQuery<Chain, QueryMsg = #entrypoint_msg_type #ty_generics > #where_clause {
Expand Down
Loading