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

fix: remove payable check for non-mutable functions #774

Closed
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,19 @@ impl ImplItemMethodInfo {
is_handles_result,
..
} = attr_signature_info;
let deposit_check = if *is_payable || matches!(method_type, &MethodType::View) {
// No check if the method is payable or a view method
quote! {}
} else {
// If method is not payable, do a check to make sure that it doesn't consume deposit
let error = format!("Method {} doesn't accept deposit", ident);
quote! {
if near_sdk::env::attached_deposit() != 0 {
near_sdk::env::panic_str(#error);
let deposit_check =
if *is_payable || matches!(method_type, &MethodType::View) || receiver.is_none() {
// No check if the method is payable or doesn't mutate state
quote! {}
} else {
// If method is not payable, do a check to make sure that it doesn't consume deposit
let error = format!("Method {} doesn't accept deposit", ident);
quote! {
if near_sdk::env::attached_deposit() != 0 {
near_sdk::env::panic_str(#error);
}
}
}
};
};
let is_private_check = if *is_private {
let error = format!("Method {} is private", ident);
quote! {
Expand Down
19 changes: 19 additions & 0 deletions near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ mod tests {
assert_eq!(expected.to_string(), actual.to_string());
}


#[test]
fn no_args_no_return_no_self() {
let impl_type: Type = syn::parse_str("Hello").unwrap();
let mut method: ImplItemMethod = syn::parse_str("pub fn method() { }").unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, impl_type).unwrap();
let actual = method_info.method_wrapper();
let expected = quote!(
#[cfg(target_arch = "wasm32")]
#[no_mangle]
pub extern "C" fn method() {
near_sdk::env::setup_panic_hook();
Hello::method();
}
);
assert_eq!(expected.to_string(), actual.to_string());
}


#[test]
fn owned_no_args_no_return_no_mut() {
let impl_type: Type = syn::parse_str("Hello").unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl AttrSigInfo {
"Init methods can't have `self` attribute",
));
}
};
}

if let Some(payable_attr) = payable_attr {
if matches!(method_type, MethodType::View) {
Expand Down