Skip to content

Commit

Permalink
chore: replacements in near-sdk-macros for removed/deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Sep 5, 2023
1 parent 4f02338 commit 9ed89a8
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 39 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 @@ -75,7 +75,7 @@ impl ImplItemMethodInfo {
/// })
/// }
/// ```
/// If args are serialized with Borsh it will not include `#[derive(borsh::BorshSchema)]`.
/// If args are serialized with Borsh it will not include `#[derive(::near_sdk::borsh::BorshSchema)]`.
pub fn abi_struct(&self) -> TokenStream2 {
let attr_signature_info = &self.attr_signature_info;

Expand Down Expand Up @@ -246,7 +246,7 @@ fn generate_schema(ty: &Type, serializer_type: &SerializerType) -> TokenStream2
gen.subschema_for::<#ty>()
},
SerializerType::Borsh => quote! {
<#ty as ::near_sdk::borsh::BorshSchema>::schema_container()
::near_sdk::borsh::schema_container_of::<#ty>()
},
}
}
Expand Down Expand Up @@ -361,14 +361,14 @@ mod tests {
args: ::std::vec![
::near_sdk::__private::AbiBorshParameter {
name: ::std::string::String::from("arg0"),
type_schema: <FancyStruct as ::near_sdk::borsh::BorshSchema>::schema_container(),
type_schema: ::near_sdk::borsh::schema_container_of::<FancyStruct>(),
}
]
},
callbacks: ::std::vec![],
callbacks_vec: ::std::option::Option::None,
result: ::std::option::Option::Some(::near_sdk::__private::AbiType::Borsh {
type_schema: <IsOk as ::near_sdk::borsh::BorshSchema>::schema_container(),
type_schema: ::near_sdk::borsh::schema_container_of::<IsOk>(),
})
}
};
Expand Down Expand Up @@ -430,13 +430,13 @@ mod tests {
args: ::std::vec! [
::near_sdk::__private::AbiBorshParameter {
name: ::std::string::String::from("y"),
type_schema: < String as ::near_sdk::borsh::BorshSchema >::schema_container(),
type_schema: ::near_sdk::borsh::schema_container_of::<String>(),
}
]
},
callbacks: ::std::vec! [
::near_sdk::__private::AbiType::Borsh {
type_schema: <u64 as ::near_sdk::borsh::BorshSchema>::schema_container(),
type_schema: ::near_sdk::borsh::schema_container_of::<u64>(),
},
::near_sdk::__private::AbiType::Json {
type_schema: gen.subschema_for::< Vec<u8> >(),
Expand Down
2 changes: 2 additions & 0 deletions near-sdk-macros/src/core_impl/code_generator/attr_sig_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl AttrSigInfo {
},
SerializerType::Borsh => quote! {
#[derive(::near_sdk::borsh::BorshSerialize)]
#[borsh(crate = "::near_sdk::borsh")]
},
};
let mut fields = TokenStream2::new();
Expand Down Expand Up @@ -94,6 +95,7 @@ impl AttrSigInfo {
},
SerializerType::Borsh => quote! {
#[derive(::near_sdk::borsh::BorshDeserialize)]
#[borsh(crate = "::near_sdk::borsh")]
},
};
let mut fields = TokenStream2::new();
Expand Down
3 changes: 2 additions & 1 deletion near-sdk-macros/src/core_impl/code_generator/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,12 @@ mod tests {
pub fn borsh_test(self, a: String,) -> ::near_sdk::Promise {
let __args = {
#[derive(::near_sdk :: borsh :: BorshSerialize)]
#[borsh(crate = "::near_sdk::borsh")]
struct Input<'nearinput> {
a: &'nearinput String,
}
let __args = Input { a: &a, };
::near_sdk::borsh::BorshSerialize::try_to_vec(&__args)
::near_sdk::borsh::to_vec(&__args)
.expect("Failed to serialize the cross contract args using Borsh.")
};
::near_sdk::Promise::new(self.account_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl ImplItemMethodInfo {
let result = ::near_sdk::serde_json::to_vec(&result).expect("Failed to serialize the return value using JSON.");
},
SerializerType::Borsh => quote! {
let result = ::near_sdk::borsh::BorshSerialize::try_to_vec(&result).expect("Failed to serialize the return value using Borsh.");
let result = ::near_sdk::borsh::to_vec(&result).expect("Failed to serialize the return value using Borsh.");
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ mod tests {
::near_sdk::env::panic_str("Method method doesn't accept deposit");
}
#[derive(::near_sdk :: borsh :: BorshDeserialize)]
#[borsh(crate = "::near_sdk::borsh")]
struct Input {
k: u64,
m: Bar,
Expand All @@ -595,7 +596,7 @@ mod tests {
.expect("Failed to deserialize input from Borsh.");
let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
let result = contract.method(k, m, );
let result = ::near_sdk::borsh::BorshSerialize::try_to_vec(&result)
let result = ::near_sdk::borsh::to_vec(&result)
.expect("Failed to serialize the return value using Borsh.");
::near_sdk::env::value_return(&result);
::near_sdk::env::state_write(&contract);
Expand All @@ -621,6 +622,7 @@ mod tests {
::near_sdk::env::panic_str("Method method is private");
}
#[derive(::near_sdk :: borsh :: BorshDeserialize)]
#[borsh(crate = "::near_sdk::borsh")]
struct Input {
y: ::std::string::String,
}
Expand Down Expand Up @@ -773,7 +775,7 @@ mod tests {
match result {
::std::result::Result::Ok(result) => {
let result =
::near_sdk::borsh::BorshSerialize::try_to_vec(&result).expect("Failed to serialize the return value using Borsh.");
::near_sdk::borsh::to_vec(&result).expect("Failed to serialize the return value using Borsh.");
::near_sdk::env::value_return(&result);
}
::std::result::Result::Err(err) => ::near_sdk::FunctionError::panic(&err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,12 @@ mod tests {
) -> ::near_sdk::Promise {
let __args = {
#[derive(::near_sdk :: borsh :: BorshSerialize)]
#[borsh(crate = "::near_sdk::borsh")]
struct Input<'nearinput> {
v: &'nearinput Vec<String>,
}
let __args = Input { v: &v, };
::near_sdk::borsh::BorshSerialize::try_to_vec(&__args)
::near_sdk::borsh::to_vec(&__args)
.expect("Failed to serialize the cross contract args using Borsh.")
};
::near_sdk::Promise::new(self.account_id)
Expand Down
2 changes: 1 addition & 1 deletion near-sdk-macros/src/core_impl/code_generator/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn generate_serializer(
::near_sdk::serde_json::to_vec(&__args).expect("Failed to serialize the cross contract args using JSON.")
},
SerializerType::Borsh => quote! {
::near_sdk::borsh::BorshSerialize::try_to_vec(&__args).expect("Failed to serialize the cross contract args using Borsh.")
::near_sdk::borsh::to_vec(&__args).expect("Failed to serialize the cross contract args using Borsh.")
},
};

Expand Down
20 changes: 11 additions & 9 deletions near-sdk-macros/src/core_impl/metadata/metadata_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ impl ImplItemMethodInfo {
/// is_view: false,
/// is_init: false,
/// args: {
/// #[derive(borsh::BorshSchema)]
/// #[derive(::near_sdk::borsh::BorshSchema)]
/// #[borsh(crate = "::near_sdk::borsh")]
/// #[derive(serde :: Deserialize, serde :: Serialize)]
/// struct Input {
/// arg0: FancyStruct,
/// arg1: u64,
/// }
/// Some(Input::schema_container())
/// Some(::near_sdk::borsh::schema_container_of::<Input>())
/// },
/// callbacks: vec![],
/// callbacks_vec: None,
/// result: Some(Result < IsOk, Error > ::schema_container())
/// result: Some(::near_sdk::borsh::schema_container_of::<Result < IsOk, Error >>())
/// }
/// ```
/// If args are serialized with Borsh it will not include `#[derive(borsh::BorshSchema)]`.
/// If args are serialized with Borsh it will not include `#[derive(::near_sdk::borsh::BorshSchema)]`.
pub(crate) fn metadata_struct(&self) -> TokenStream2 {
let method_name_str = self.attr_signature_info.ident.to_string();
let is_view = matches!(&self.attr_signature_info.method_kind, &MethodKind::View(_));
Expand All @@ -44,15 +45,16 @@ impl ImplItemMethodInfo {
let additional_schema = match &self.attr_signature_info.input_serializer {
SerializerType::Borsh => TokenStream2::new(),
SerializerType::JSON => quote! {
#[derive(::borsh::BorshSchema)]
#[derive(::near_sdk::borsh::BorshSchema)]
#[borsh(crate = "::near_sdk::borsh")]
},
};
quote! {
{
#additional_schema
#[allow(dead_code)]
#input_struct
::std::option::Option::Some(<Input as ::near_sdk::borsh::BorshSchema>::schema_container())
::std::option::Option::Some(::near_sdk::borsh::schema_container_of::<Input>())
}
}
} else {
Expand All @@ -68,7 +70,7 @@ impl ImplItemMethodInfo {
.map(|arg| {
let ty = &arg.ty;
quote! {
<#ty as ::near_sdk::borsh::BorshSchema>::schema_container()
::near_sdk::borsh::schema_container_of::<#ty>()
}
})
.collect();
Expand All @@ -87,7 +89,7 @@ impl ImplItemMethodInfo {
Some(arg) => {
let ty = &arg.ty;
quote! {
::std::option::Option::Some(<#ty as ::near_sdk::borsh::BorshSchema>::schema_container())
::std::option::Option::Some(::near_sdk::borsh::schema_container_of::<#ty>())
}
}
};
Expand All @@ -99,7 +101,7 @@ impl ImplItemMethodInfo {
}
ReturnType::Type(_, ty) => {
quote! {
::std::option::Option::Some(<#ty as ::near_sdk::borsh::BorshSchema>::schema_container())
::std::option::Option::Some(::near_sdk::borsh::schema_container_of::<#ty>())
}
}
};
Expand Down
20 changes: 11 additions & 9 deletions near-sdk-macros/src/core_impl/metadata/metadata_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ impl MetadataVisitor {
#[no_mangle]
pub extern "C" fn metadata() {
#panic_hook
use ::borsh::*;
use ::near_sdk::borsh::*;
let metadata = ::near_sdk::__private::Metadata::new(::std::vec![
#(#methods),*
]);
let data = ::near_sdk::borsh::BorshSerialize::try_to_vec(&metadata).expect("Failed to serialize the metadata using Borsh");
let data = ::near_sdk::borsh::to_vec(&metadata).expect("Failed to serialize the metadata using Borsh");
::near_sdk::env::value_return(&data);
}
})
Expand Down Expand Up @@ -99,7 +99,7 @@ mod tests {
#[no_mangle]
pub extern "C" fn metadata() {
::near_sdk::env::setup_panic_hook();
use ::borsh::*;
use ::near_sdk::borsh::*;
let metadata = ::near_sdk::__private::Metadata::new(::std::vec![
::near_sdk::__private::MethodMetadata {
name: ::std::string::String::from("f1"),
Expand All @@ -115,15 +115,16 @@ mod tests {
is_view: false,
is_init: false,
args: {
#[derive(::borsh::BorshSchema)]
#[derive(::near_sdk::borsh::BorshSchema)]
#[borsh(crate = "::near_sdk::borsh")]
#[allow(dead_code)]
#[derive(::near_sdk :: serde :: Deserialize)]
#[serde(crate = "::near_sdk::serde")]
struct Input {
arg0: FancyStruct,
arg1: u64,
}
::std::option::Option::Some(<Input as ::near_sdk::borsh::BorshSchema>::schema_container())
::std::option::Option::Some(::near_sdk::borsh::schema_container_of::<Input>())
},
callbacks: ::std::vec![],
callbacks_vec: ::std::option::Option::None,
Expand All @@ -134,22 +135,23 @@ mod tests {
is_view: false,
is_init: false,
args: {
#[derive(::borsh::BorshSchema)]
#[derive(::near_sdk::borsh::BorshSchema)]
#[borsh(crate = "::near_sdk::borsh")]
#[allow(dead_code)]
#[derive(::near_sdk :: serde :: Deserialize)]
#[serde(crate = "::near_sdk::serde")]
struct Input {
arg0: FancyStruct,
arg1: u64,
}
::std::option::Option::Some(<Input as ::near_sdk::borsh::BorshSchema>::schema_container())
::std::option::Option::Some(::near_sdk::borsh::schema_container_of::<Input>())
},
callbacks: ::std::vec![],
callbacks_vec: ::std::option::Option::None,
result: ::std::option::Option::Some(<Either < IsOk, Error > as ::near_sdk::borsh::BorshSchema> ::schema_container())
result: ::std::option::Option::Some(::near_sdk::borsh::schema_container_of::<Either <IsOk , Error> >())
}
]);
let data = ::near_sdk::borsh::BorshSerialize::try_to_vec(&metadata)
let data = ::near_sdk::borsh::to_vec(&metadata)
.expect("Failed to serialize the metadata using Borsh");
::near_sdk::env::value_return(&data);
}
Expand Down
19 changes: 10 additions & 9 deletions near-sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,13 @@ pub fn derive_near_schema(input: TokenStream) -> TokenStream {
},
// #[abi(borsh)]
(false, true) => quote! {
#[derive(borsh::BorshSchema)]
#[derive(::near_sdk::borsh::BorshSchema)]
#[borsh(crate = "::near_sdk::borsh")]
},
// #[abi(json, borsh)]
(true, true) => quote! {
#[derive(schemars::JsonSchema, borsh::BorshSchema)]
#[derive(schemars::JsonSchema, ::near_sdk::borsh::BorshSchema)]
#[borsh(crate = "::near_sdk::borsh")]
},
};

Expand Down Expand Up @@ -408,18 +410,18 @@ pub fn derive_near_schema(input: TokenStream) -> TokenStream {
let borsh_impl = if borsh_schema {
quote! {
#[automatically_derived]
impl borsh::BorshSchema for #input_ident_proxy {
fn declaration() -> borsh::schema::Declaration {
impl ::near_sdk::borsh::BorshSchema for #input_ident_proxy {
fn declaration() -> ::near_sdk::borsh::schema::Declaration {
stringify!(#input_ident).to_string()
}

fn add_definitions_recursively(
definitions: &mut borsh::__private::maybestd::collections::BTreeMap<
borsh::schema::Declaration,
borsh::schema::Definition
definitions: &mut ::near_sdk::borsh::__private::maybestd::collections::BTreeMap<
::near_sdk::borsh::schema::Declaration,
::near_sdk::borsh::schema::Definition
>,
) {
<#input_ident as borsh::BorshSchema>::add_definitions_recursively(definitions);
<#input_ident as ::near_sdk::borsh::BorshSchema>::add_definitions_recursively(definitions);
}
}
}
Expand All @@ -433,7 +435,6 @@ pub fn derive_near_schema(input: TokenStream) -> TokenStream {
#[allow(non_camel_case_types)]
type #input_ident_proxy = #input_ident;
{
use ::near_sdk::borsh;
use ::near_sdk::__private::schemars;

#derive
Expand Down

0 comments on commit 9ed89a8

Please sign in to comment.