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

near_bindgen methods with no self check for attached deposit #718

Open
austinabell opened this issue Jan 27, 2022 · 1 comment
Open

near_bindgen methods with no self check for attached deposit #718

austinabell opened this issue Jan 27, 2022 · 1 comment

Comments

@austinabell
Copy link
Contributor

austinabell commented Jan 27, 2022

Methods like this will add the codegen assumed when a contract mutates state. A slightly strange pattern we have that defaults to checking this only when the parameter is &mut self even though in any function there can be state changes not tied to the contract state.

Tying the check for attached deposit to whether or not contract state changes is a strange pattern IMO but the band-aid fix is to just remove this check when contract state isn't used. This needs to be done so that these methods can be called with a view operation, otherwise, the runtime will fail when trying to call env::attached_deposit from within a view call.

#[near_bindgen]
pub struct SomeContract;

#[near_bindgen]
impl SomeContract {
    pub fn get_val(s: &String) -> Option<u8> 

    { // ... } 

// ...

Test for this looks like:

#[test]
fn no_self_no_args() {
    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());
}

which is currently failing because it adds the extra check

@itegulov
Copy link
Contributor

Just to be clear, codegen having a check for the attached deposit is a side effect of such functions being identified as call functions.

Also, the markdown is all messed up again, curse you exalate

@exalate-issue-sync exalate-issue-sync bot added good_first_issue and removed good first issue Good for newcomers labels Oct 12, 2022
@frol frol added the good first issue Good for newcomers label Jun 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

3 participants