Skip to content

Commit

Permalink
fix: use FQDNs when calling contract methods to avoid method names co…
Browse files Browse the repository at this point in the history
…llision (#1186)
  • Loading branch information
mitinarseny authored Jun 1, 2024
1 parent 5a9acae commit 8a908c3
Show file tree
Hide file tree
Showing 29 changed files with 102 additions and 83 deletions.
12 changes: 6 additions & 6 deletions near-sdk-macros/src/core_impl/abi/abi_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ mod tests {
#[handle_result]
pub fn f3(&mut self, arg0: FancyStruct, arg1: u64) -> Result<IsOk, Error> { }
};
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, None, impl_type).unwrap().unwrap();
let actual = method_info.abi_struct();

local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual));
Expand All @@ -339,7 +339,7 @@ mod tests {
#[handle_result]
pub fn f3(&mut self, #[serializer(borsh)] arg0: FancyStruct) -> Result<IsOk, Error> { }
};
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, None, impl_type).unwrap().unwrap();
let actual = method_info.abi_struct();

local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual));
Expand All @@ -355,7 +355,7 @@ mod tests {
#[callback_vec] x: Vec<String>,
) -> bool { }
};
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, None, impl_type).unwrap().unwrap();
let actual = method_info.abi_struct();

local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual));
Expand All @@ -367,7 +367,7 @@ mod tests {
let mut method = parse_quote! {
pub fn method(&self, #[callback_unwrap] #[serializer(borsh)] x: &mut u64, #[serializer(borsh)] y: String, #[callback_unwrap] #[serializer(json)] z: Vec<u8>) { }
};
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, None, impl_type).unwrap().unwrap();
let actual = method_info.abi_struct();

local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual));
Expand All @@ -380,7 +380,7 @@ mod tests {
#[init(ignore_state)]
pub fn new() -> u64 { }
};
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, None, impl_type).unwrap().unwrap();
let actual = method_info.abi_struct();

local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual));
Expand All @@ -392,7 +392,7 @@ mod tests {
let mut method = parse_quote! {
pub fn method() { }
};
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, None, impl_type).unwrap().unwrap();
let actual = method_info.abi_struct();

local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual));
Expand Down
6 changes: 3 additions & 3 deletions near-sdk-macros/src/core_impl/code_generator/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mod tests {
#[warn(unused)]
pub fn method(&self) { }
};
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, None, impl_type).unwrap().unwrap();
let actual = generate_ext_function(&method_info.attr_signature_info);

local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap());
Expand All @@ -189,7 +189,7 @@ mod tests {
let mut method: ImplItemFn = parse_quote! {
pub fn method(&self, k: &String) { }
};
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, None, impl_type).unwrap().unwrap();
let actual = generate_ext_function(&method_info.attr_signature_info);

local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap());
Expand All @@ -201,7 +201,7 @@ mod tests {
let mut method: syn::ImplItemFn = parse_quote! {
pub fn borsh_test(&mut self, #[serializer(borsh)] a: String) {}
};
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, None, impl_type).unwrap().unwrap();
let actual = generate_ext_function(&method_info.attr_signature_info);

local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,24 +277,41 @@ impl ImplItemMethodInfo {

let ident = &self.attr_signature_info.ident;
let arg_list = self.attr_signature_info.arg_list();
let struct_type = &self.struct_type;

let method_invocation = || {
let method_fqdn = if let Some(impl_trait) = &self.impl_trait {
quote! {
contract.#ident(#arg_list)
<#struct_type as #impl_trait>::#ident
}
} else {
quote! {
#struct_type::#ident
}
};

let method_invocation = |receiver: &Receiver| {
if receiver.reference.is_some() {
let mutability = receiver.mutability;
quote! {
#method_fqdn(&#mutability contract, #arg_list)
}
} else {
quote! {
#method_fqdn(contract, #arg_list)
}
}
};

let static_invocation = || {
let struct_type = &self.struct_type;
quote! {
#struct_type::#ident(#arg_list)
#method_fqdn(#arg_list)
}
};

match &self.attr_signature_info.method_kind {
Call(call_method) => {
if call_method.receiver.is_some() {
method_invocation()
if let Some(receiver) = call_method.receiver.as_ref() {
method_invocation(receiver)
} else {
static_invocation()
}
Expand All @@ -304,8 +321,8 @@ impl ImplItemMethodInfo {
Init(_) => quote! {},

View(view_method) => {
if view_method.receiver.is_some() {
method_invocation()
if let Some(receiver) = view_method.receiver.as_ref() {
method_invocation(receiver)
} else {
static_invocation()
}
Expand Down
Loading

0 comments on commit 8a908c3

Please sign in to comment.