diff --git a/CHANGELOG.md b/CHANGELOG.md index 1926bec1a..020ec1c89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## [Unreleased] +### Removed +- Deleted `metadata` macro. Use https://github.com/near/abi instead. [PR 920](https://github.com/near/near-sdk-rs/pull/920) + +### Fixes +- Updated the associated error type for `Base58CryptoHash` parsing through `TryFrom` to concrete type. [PR 919](https://github.com/near/near-sdk-rs/pull/919) + ## [4.1.0-pre.3] - 2022-08-30 ### Added diff --git a/examples/adder/Cargo.lock b/examples/adder/Cargo.lock index aae2db086..dbd828ef2 100644 --- a/examples/adder/Cargo.lock +++ b/examples/adder/Cargo.lock @@ -58,7 +58,7 @@ name = "adder" version = "0.1.0" dependencies = [ "anyhow", - "near-abi", + "near-abi 0.1.0-pre.0", "near-sdk", "schemars", "serde", @@ -1288,6 +1288,18 @@ dependencies = [ "serde", ] +[[package]] +name = "near-abi" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85f9816032a8688b52e2e0e51e1a5513e7e05310eb2eebe1d913fcf82be1e1d7" +dependencies = [ + "borsh", + "schemars", + "semver", + "serde", +] + [[package]] name = "near-account-id" version = "0.14.0" @@ -1530,7 +1542,7 @@ dependencies = [ "base64 0.13.0", "borsh", "bs58", - "near-abi", + "near-abi 0.2.0", "near-crypto", "near-primitives", "near-primitives-core", diff --git a/examples/adder/res/adder_abi.json b/examples/adder/res/adder_abi.json index 28c34891a..6734a12aa 100644 --- a/examples/adder/res/adder_abi.json +++ b/examples/adder/res/adder_abi.json @@ -1,11 +1,16 @@ { - "schema_version": "0.1.0", + "schema_version": "0.2.0", "metadata": { "name": "adder", "version": "0.1.0", "authors": [ "Near Inc " - ] + ], + "build": { + "compiler": "rustc 1.61.0", + "builder": "cargo-near 0.2.0" + }, + "wasm_hash": "B4XgA4rGVyaCWyDv2h1XAN5QMbtdJN1frRwoxELUMvDe" }, "body": { "functions": [ @@ -13,22 +18,23 @@ "name": "add", "doc": " Adds two pairs point-wise.", "is_view": true, - "params": [ - { - "name": "a", - "serialization_type": "json", - "type_schema": { - "$ref": "#/definitions/Pair" - } - }, - { - "name": "b", - "serialization_type": "json", - "type_schema": { - "$ref": "#/definitions/Pair" + "params": { + "serialization_type": "json", + "args": [ + { + "name": "a", + "type_schema": { + "$ref": "#/definitions/Pair" + } + }, + { + "name": "b", + "type_schema": { + "$ref": "#/definitions/Pair" + } } - } - ], + ] + }, "result": { "serialization_type": "json", "type_schema": { @@ -39,38 +45,39 @@ { "name": "add_borsh", "is_view": true, - "params": [ - { - "name": "a", - "serialization_type": "borsh", - "type_schema": { - "declaration": "Pair", - "definitions": { - "Pair": { - "Struct": [ - "u32", - "u32" - ] + "params": { + "serialization_type": "borsh", + "args": [ + { + "name": "a", + "type_schema": { + "declaration": "Pair", + "definitions": { + "Pair": { + "Struct": [ + "u32", + "u32" + ] + } } } - } - }, - { - "name": "b", - "serialization_type": "borsh", - "type_schema": { - "declaration": "Pair", - "definitions": { - "Pair": { - "Struct": [ - "u32", - "u32" - ] + }, + { + "name": "b", + "type_schema": { + "declaration": "Pair", + "definitions": { + "Pair": { + "Struct": [ + "u32", + "u32" + ] + } } } } - } - ], + ] + }, "result": { "serialization_type": "borsh", "type_schema": { diff --git a/examples/fungible-token/Cargo.lock b/examples/fungible-token/Cargo.lock index f019dc3be..d88ec0ac2 100644 --- a/examples/fungible-token/Cargo.lock +++ b/examples/fungible-token/Cargo.lock @@ -1303,9 +1303,9 @@ dependencies = [ [[package]] name = "near-abi" -version = "0.1.0-pre.0" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d5281adc3a63d798b0e35683dee66095cbb94e431960ec859d5a8397b093f39" +checksum = "85f9816032a8688b52e2e0e51e1a5513e7e05310eb2eebe1d913fcf82be1e1d7" dependencies = [ "borsh", "schemars", diff --git a/examples/status-message/src/lib.rs b/examples/status-message/src/lib.rs index a7da6f58e..f127bd5dd 100644 --- a/examples/status-message/src/lib.rs +++ b/examples/status-message/src/lib.rs @@ -1,9 +1,7 @@ use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize}; -use near_sdk::{env, log, metadata, near_bindgen, AccountId}; - +use near_sdk::{env, log, near_bindgen, AccountId}; use std::collections::HashMap; -metadata! { #[near_bindgen] #[derive(Default, BorshDeserialize, BorshSerialize)] pub struct StatusMessage { @@ -19,12 +17,11 @@ impl StatusMessage { self.records.insert(account_id, message); } - pub fn get_status(&self, account_id: AccountId) -> Option:: { + pub fn get_status(&self, account_id: AccountId) -> Option { log!("get_status for account_id {}", account_id); self.records.get(&account_id).cloned() } } -} #[cfg(not(target_arch = "wasm32"))] #[cfg(test)] diff --git a/near-sdk-macros/src/core_impl/abi/abi_generator.rs b/near-sdk-macros/src/core_impl/abi/abi_generator.rs index 9786c65c9..4210089d5 100644 --- a/near-sdk-macros/src/core_impl/abi/abi_generator.rs +++ b/near-sdk-macros/src/core_impl/abi/abi_generator.rs @@ -97,13 +97,21 @@ impl ImplItemMethodInfo { let arg_name = arg.ident.to_string(); match arg.bindgen_ty { BindgenArgType::Regular => { - let abi_type = generate_abi_type(typ, &arg.serializer_ty); - params.push(quote! { - near_sdk::__private::AbiParameter { - name: #arg_name.to_string(), - typ: #abi_type - } - }); + let schema = generate_schema(typ, &arg.serializer_ty); + match arg.serializer_ty { + SerializerType::JSON => params.push(quote! { + near_sdk::__private::AbiJsonParameter { + name: #arg_name.to_string(), + type_schema: #schema, + } + }), + SerializerType::Borsh => params.push(quote! { + near_sdk::__private::AbiBorshParameter { + name: #arg_name.to_string(), + type_schema: #schema, + } + }), + }; } BindgenArgType::CallbackArg => { callbacks.push(generate_abi_type(typ, &arg.serializer_ty)); @@ -146,6 +154,18 @@ impl ImplItemMethodInfo { } }; } + let params = match self.attr_signature_info.input_serializer { + SerializerType::JSON => quote! { + near_sdk::__private::AbiParameters::Json { + args: vec![#(#params),*] + } + }, + SerializerType::Borsh => quote! { + near_sdk::__private::AbiParameters::Borsh { + args: vec![#(#params),*] + } + }, + }; let callback_vec = callback_vec.unwrap_or(quote! { None }); let result = match self.attr_signature_info.method_type { @@ -198,7 +218,7 @@ impl ImplItemMethodInfo { is_init: #is_init, is_payable: #is_payable, is_private: #is_private, - params: vec![#(#params),*], + params: #params, callbacks: vec![#(#callbacks),*], callbacks_vec: #callback_vec, result: #result @@ -207,16 +227,28 @@ impl ImplItemMethodInfo { } } +fn generate_schema(ty: &Type, serializer_type: &SerializerType) -> TokenStream2 { + match serializer_type { + SerializerType::JSON => quote! { + gen.subschema_for::<#ty>() + }, + SerializerType::Borsh => quote! { + <#ty>::schema_container() + }, + } +} + fn generate_abi_type(ty: &Type, serializer_type: &SerializerType) -> TokenStream2 { + let schema = generate_schema(ty, serializer_type); match serializer_type { SerializerType::JSON => quote! { near_sdk::__private::AbiType::Json { - type_schema: gen.subschema_for::<#ty>(), + type_schema: #schema, } }, SerializerType::Borsh => quote! { near_sdk::__private::AbiType::Borsh { - type_schema: <#ty>::schema_container(), + type_schema: #schema, } }, } diff --git a/near-sdk-macros/src/core_impl/metadata/metadata_generator.rs b/near-sdk-macros/src/core_impl/metadata/metadata_generator.rs index c6eb132c7..cac060405 100644 --- a/near-sdk-macros/src/core_impl/metadata/metadata_generator.rs +++ b/near-sdk-macros/src/core_impl/metadata/metadata_generator.rs @@ -33,7 +33,7 @@ impl ImplItemMethodInfo { /// } /// ``` /// If args are serialized with Borsh it will not include `#[derive(borsh::BorshSchema)]`. - pub fn metadata_struct(&self) -> TokenStream2 { + 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_type, &MethodType::View); let is_init = matches!( diff --git a/near-sdk-macros/src/core_impl/metadata/metadata_visitor.rs b/near-sdk-macros/src/core_impl/metadata/metadata_visitor.rs index 80c1e4455..5f4720f61 100644 --- a/near-sdk-macros/src/core_impl/metadata/metadata_visitor.rs +++ b/near-sdk-macros/src/core_impl/metadata/metadata_visitor.rs @@ -11,7 +11,7 @@ use syn::{Error, ItemImpl}; /// Information relevant to metadata extracted from the `impl` section decorated with `#[near_bindgen]`. #[derive(Default)] -pub struct MetadataVisitor { +pub(crate) struct MetadataVisitor { impl_item_infos: Vec, /// Errors that occured while extracting the data. errors: Vec, diff --git a/near-sdk-macros/src/core_impl/metadata/mod.rs b/near-sdk-macros/src/core_impl/metadata/mod.rs index 74119aa5b..5c26001ff 100644 --- a/near-sdk-macros/src/core_impl/metadata/mod.rs +++ b/near-sdk-macros/src/core_impl/metadata/mod.rs @@ -1,2 +1,2 @@ -pub mod metadata_generator; -pub mod metadata_visitor; +pub(crate) mod metadata_generator; +pub(crate) mod metadata_visitor; diff --git a/near-sdk-macros/src/core_impl/mod.rs b/near-sdk-macros/src/core_impl/mod.rs index 4f1c8d561..47a0b33a9 100644 --- a/near-sdk-macros/src/core_impl/mod.rs +++ b/near-sdk-macros/src/core_impl/mod.rs @@ -4,6 +4,6 @@ mod code_generator; mod info_extractor; mod metadata; mod utils; -pub use code_generator::*; -pub use info_extractor::*; -pub use metadata::metadata_visitor::MetadataVisitor; +pub(crate) use code_generator::*; +pub(crate) use info_extractor::*; +pub(crate) use metadata::metadata_visitor::MetadataVisitor; diff --git a/near-sdk-macros/src/lib.rs b/near-sdk-macros/src/lib.rs index b55d77503..4d698513e 100644 --- a/near-sdk-macros/src/lib.rs +++ b/near-sdk-macros/src/lib.rs @@ -205,6 +205,10 @@ pub fn init(_attr: TokenStream, item: TokenStream) -> TokenStream { /// `metadata` generates the metadata method and should be placed at the very end of the `lib.rs` file. // TODO: Once Rust allows inner attributes and custom procedural macros for modules we should switch this // to be `#![metadata]` attribute at the top of the contract file instead. https://github.com/rust-lang/rust/issues/54727 +#[deprecated( + since = "4.1.0", + note = "metadata macro is no longer used. Use https://github.com/near/abi to generate a contract schema" +)] #[proc_macro] pub fn metadata(item: TokenStream) -> TokenStream { if let Ok(input) = syn::parse::(item) { diff --git a/near-sdk/Cargo.toml b/near-sdk/Cargo.toml index 1d35be4d0..2ef9bc857 100644 --- a/near-sdk/Cargo.toml +++ b/near-sdk/Cargo.toml @@ -32,7 +32,7 @@ wee_alloc = { version = "0.4.5", default-features = false, optional = true } # Used for caching, might be worth porting only functionality needed. once_cell = { version = "1.8", default-features = false } -near-abi = { version = "0.1.0-pre.0", features = ["__chunked-entries"], optional = true } +near-abi = { version = "0.2.0", features = ["__chunked-entries"], optional = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] # alt_bn128 feature will need to be removed on the next version update (now stabilized) diff --git a/near-sdk/compilation_tests/all.rs b/near-sdk/compilation_tests/all.rs index 6415da53b..1ca203bbe 100644 --- a/near-sdk/compilation_tests/all.rs +++ b/near-sdk/compilation_tests/all.rs @@ -6,8 +6,6 @@ fn compilation_tests() { t.pass("compilation_tests/regular.rs"); t.pass("compilation_tests/private.rs"); t.pass("compilation_tests/trait_impl.rs"); - t.pass("compilation_tests/metadata.rs"); - t.compile_fail("compilation_tests/metadata_invalid_rust.rs"); t.compile_fail("compilation_tests/bad_argument.rs"); t.pass("compilation_tests/complex.rs"); t.compile_fail("compilation_tests/impl_generic.rs"); diff --git a/near-sdk/compilation_tests/metadata.rs b/near-sdk/compilation_tests/metadata.rs deleted file mode 100644 index 223ad44ed..000000000 --- a/near-sdk/compilation_tests/metadata.rs +++ /dev/null @@ -1,18 +0,0 @@ -use near_sdk::{near_bindgen, metadata}; -use borsh::{BorshDeserialize, BorshSerialize}; -metadata! { -#[near_bindgen] -#[derive(Default, BorshDeserialize, BorshSerialize)] -struct Incrementer { - value: u32, -} - -#[near_bindgen] -impl Incrementer { - pub fn inc(&mut self, by: u32) { - self.value += by; - } -} -} - -fn main() {} diff --git a/near-sdk/compilation_tests/metadata_invalid_rust.rs b/near-sdk/compilation_tests/metadata_invalid_rust.rs deleted file mode 100644 index 10998ad17..000000000 --- a/near-sdk/compilation_tests/metadata_invalid_rust.rs +++ /dev/null @@ -1,22 +0,0 @@ -#![allow(unused_imports)] - -use borsh::{BorshDeserialize, BorshSerialize}; -use near_sdk::{metadata, near_bindgen}; -metadata! { -FOOBAR - -#[near_bindgen] -#[derive(Default, BorshDeserialize, BorshSerialize)] -struct Incrementer { - value: u32, -} - -#[near_bindgen] -impl Incrementer { - pub fn inc(&mut self, by: u32) { - self.value += by; - } -} -} - -fn main() {} diff --git a/near-sdk/compilation_tests/metadata_invalid_rust.stderr b/near-sdk/compilation_tests/metadata_invalid_rust.stderr deleted file mode 100644 index 39ae98986..000000000 --- a/near-sdk/compilation_tests/metadata_invalid_rust.stderr +++ /dev/null @@ -1,13 +0,0 @@ -error: Failed to parse code decorated with `metadata!{}` macro. Only valid Rust is supported. - --> compilation_tests/metadata_invalid_rust.rs:5:1 - | -5 | / metadata! { -6 | | FOOBAR -7 | | -8 | | #[near_bindgen] -... | -19 | | } -20 | | } - | |_^ - | - = note: this error originates in the macro `metadata` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/near-sdk/src/json_types/hash.rs b/near-sdk/src/json_types/hash.rs index f4bd4a087..242640a13 100644 --- a/near-sdk/src/json_types/hash.rs +++ b/near-sdk/src/json_types/hash.rs @@ -62,18 +62,18 @@ impl From<&Base58CryptoHash> for String { } impl TryFrom for Base58CryptoHash { - type Error = Box; + type Error = ParseCryptoHashError; fn try_from(value: String) -> Result { - Self::try_from(value.as_str()) + value.parse() } } impl TryFrom<&str> for Base58CryptoHash { - type Error = Box; + type Error = ParseCryptoHashError; fn try_from(value: &str) -> Result { - Ok(value.parse()?) + value.parse() } } diff --git a/near-sdk/src/lib.rs b/near-sdk/src/lib.rs index 8d0139fc4..2bdbd8301 100644 --- a/near-sdk/src/lib.rs +++ b/near-sdk/src/lib.rs @@ -6,7 +6,7 @@ extern crate quickcheck; pub use near_sdk_macros::{ - ext_contract, metadata, near_bindgen, BorshStorageKey, FunctionError, PanicOnDefault, + ext_contract, near_bindgen, BorshStorageKey, FunctionError, PanicOnDefault, }; pub mod store; diff --git a/near-sdk/src/private/mod.rs b/near-sdk/src/private/mod.rs index 35780ca18..805c9c0b4 100644 --- a/near-sdk/src/private/mod.rs +++ b/near-sdk/src/private/mod.rs @@ -1,7 +1,7 @@ #[cfg(feature = "abi")] pub use near_abi::__private::ChunkedAbiEntry; #[cfg(feature = "abi")] -pub use near_abi::{AbiFunction, AbiParameter, AbiType}; +pub use near_abi::{AbiBorshParameter, AbiFunction, AbiJsonParameter, AbiParameters, AbiType}; #[cfg(feature = "abi")] pub use schemars;