diff --git a/examples/adder/Cargo.toml b/examples/adder/Cargo.toml index 0d714335c..3e8e6aba5 100644 --- a/examples/adder/Cargo.toml +++ b/examples/adder/Cargo.toml @@ -9,7 +9,6 @@ crate-type = ["cdylib"] [dependencies] near-sdk = { path = "../../near-sdk", features = ["unstable"] } -schemars = "0.8" [dev-dependencies] near-workspaces = { version = "0.9.0", default-features = false, features = ["install"] } diff --git a/near-contract-standards/Cargo.toml b/near-contract-standards/Cargo.toml index 01e3ea3d6..34ec4bda8 100644 --- a/near-contract-standards/Cargo.toml +++ b/near-contract-standards/Cargo.toml @@ -14,10 +14,6 @@ NEAR smart contracts standard library. [dependencies] near-sdk = { path = "../near-sdk", version = "~5.0.0-alpha.1", default-features = false, features = ["legacy"] } -serde = "1" -serde_json = "1" -schemars = "0.8" -near-account-id = {version="1.0.0-alpha.3", features = ["abi", "serde", "borsh", "schemars"]} [features] default = ["abi"] diff --git a/near-contract-standards/src/contract_metadata.rs b/near-contract-standards/src/contract_metadata.rs index 102e91ae9..c75714482 100644 --- a/near-contract-standards/src/contract_metadata.rs +++ b/near-contract-standards/src/contract_metadata.rs @@ -1,7 +1,8 @@ -use serde::{Deserialize, Serialize}; +use near_sdk::serde::{Deserialize, Serialize}; /// The contract source metadata is a standard interface that allows auditing and viewing source code for a deployed smart contract. #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] +#[serde(crate = "near_sdk::serde")] pub struct ContractSourceMetadata { pub version: Option, pub link: Option, @@ -9,6 +10,7 @@ pub struct ContractSourceMetadata { } #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] +#[serde(crate = "near_sdk::serde")] pub struct Standard { pub standard: String, pub version: String, diff --git a/near-contract-standards/src/event.rs b/near-contract-standards/src/event.rs index 3f055dc5a..6179578b9 100644 --- a/near-contract-standards/src/event.rs +++ b/near-contract-standards/src/event.rs @@ -1,7 +1,9 @@ use near_sdk::env; -use serde::Serialize; +use near_sdk::serde::Serialize; +use near_sdk::serde_json; #[derive(Serialize, Debug)] +#[serde(crate = "near_sdk::serde")] #[serde(tag = "standard")] #[must_use = "don't forget to `.emit()` this event"] #[serde(rename_all = "snake_case")] diff --git a/near-contract-standards/src/fungible_token/events.rs b/near-contract-standards/src/fungible_token/events.rs index 5762f58de..51f74ddf1 100644 --- a/near-contract-standards/src/fungible_token/events.rs +++ b/near-contract-standards/src/fungible_token/events.rs @@ -15,12 +15,13 @@ use crate::event::NearEvent; use near_sdk::json_types::U128; +use near_sdk::serde::Serialize; use near_sdk::AccountIdRef; -use serde::Serialize; /// Data to log for an FT mint event. To log this event, call [`.emit()`](FtMint::emit). #[must_use] #[derive(Serialize, Debug, Clone)] +#[serde(crate = "near_sdk::serde")] pub struct FtMint<'a> { pub owner_id: &'a AccountIdRef, pub amount: U128, @@ -46,6 +47,7 @@ impl FtMint<'_> { /// call [`.emit()`](FtTransfer::emit). #[must_use] #[derive(Serialize, Debug, Clone)] +#[serde(crate = "near_sdk::serde")] pub struct FtTransfer<'a> { pub old_owner_id: &'a AccountIdRef, pub new_owner_id: &'a AccountIdRef, @@ -71,6 +73,7 @@ impl FtTransfer<'_> { /// Data to log for an FT burn event. To log this event, call [`.emit()`](FtBurn::emit). #[must_use] #[derive(Serialize, Debug, Clone)] +#[serde(crate = "near_sdk::serde")] pub struct FtBurn<'a> { pub owner_id: &'a AccountIdRef, pub amount: U128, @@ -93,6 +96,7 @@ impl FtBurn<'_> { } #[derive(Serialize, Debug)] +#[serde(crate = "near_sdk::serde")] pub(crate) struct Nep141Event<'a> { version: &'static str, #[serde(flatten)] @@ -100,6 +104,7 @@ pub(crate) struct Nep141Event<'a> { } #[derive(Serialize, Debug)] +#[serde(crate = "near_sdk::serde")] #[serde(tag = "event", content = "data")] #[serde(rename_all = "snake_case")] #[allow(clippy::enum_variant_names)] diff --git a/near-contract-standards/src/fungible_token/metadata.rs b/near-contract-standards/src/fungible_token/metadata.rs index a38a0f9f8..19c711af8 100644 --- a/near-contract-standards/src/fungible_token/metadata.rs +++ b/near-contract-standards/src/fungible_token/metadata.rs @@ -1,5 +1,6 @@ use near_sdk::borsh::{BorshDeserialize, BorshSerialize}; use near_sdk::json_types::Base64VecU8; +use near_sdk::schemars; use near_sdk::serde::{Deserialize, Serialize}; use near_sdk::{ext_contract, require}; diff --git a/near-contract-standards/src/non_fungible_token/events.rs b/near-contract-standards/src/non_fungible_token/events.rs index 94a7a219f..7836c97b7 100644 --- a/near-contract-standards/src/non_fungible_token/events.rs +++ b/near-contract-standards/src/non_fungible_token/events.rs @@ -14,12 +14,13 @@ //! or [`NftBurn::emit_many`] respectively. use crate::event::NearEvent; +use near_sdk::serde::Serialize; use near_sdk::AccountIdRef; -use serde::Serialize; /// Data to log for an NFT mint event. To log this event, call [`.emit()`](NftMint::emit). #[must_use] #[derive(Serialize, Debug, Clone)] +#[serde(crate = "near_sdk::serde")] pub struct NftMint<'a> { pub owner_id: &'a AccountIdRef, pub token_ids: &'a [&'a str], @@ -45,6 +46,7 @@ impl NftMint<'_> { /// call [`.emit()`](NftTransfer::emit). #[must_use] #[derive(Serialize, Debug, Clone)] +#[serde(crate = "near_sdk::serde")] pub struct NftTransfer<'a> { pub old_owner_id: &'a AccountIdRef, pub new_owner_id: &'a AccountIdRef, @@ -72,6 +74,7 @@ impl NftTransfer<'_> { /// Data to log for an NFT burn event. To log this event, call [`.emit()`](NftBurn::emit). #[must_use] #[derive(Serialize, Debug, Clone)] +#[serde(crate = "near_sdk::serde")] pub struct NftBurn<'a> { pub owner_id: &'a AccountIdRef, pub token_ids: &'a [&'a str], @@ -96,6 +99,7 @@ impl NftBurn<'_> { } #[derive(Serialize, Debug)] +#[serde(crate = "near_sdk::serde")] pub(crate) struct Nep171Event<'a> { version: &'static str, #[serde(flatten)] @@ -103,6 +107,7 @@ pub(crate) struct Nep171Event<'a> { } #[derive(Serialize, Debug)] +#[serde(crate = "near_sdk::serde")] #[serde(tag = "event", content = "data")] #[serde(rename_all = "snake_case")] #[allow(clippy::enum_variant_names)] diff --git a/near-contract-standards/src/non_fungible_token/metadata.rs b/near-contract-standards/src/non_fungible_token/metadata.rs index ec8cc4848..72f51c500 100644 --- a/near-contract-standards/src/non_fungible_token/metadata.rs +++ b/near-contract-standards/src/non_fungible_token/metadata.rs @@ -1,6 +1,7 @@ use near_sdk::borsh::{BorshDeserialize, BorshSerialize}; use near_sdk::json_types::Base64VecU8; use near_sdk::require; +use near_sdk::schemars; use near_sdk::serde::{Deserialize, Serialize}; /// This spec can be treated like a version of the standard. diff --git a/near-contract-standards/src/non_fungible_token/token.rs b/near-contract-standards/src/non_fungible_token/token.rs index 2a32530f6..efe3bf350 100644 --- a/near-contract-standards/src/non_fungible_token/token.rs +++ b/near-contract-standards/src/non_fungible_token/token.rs @@ -1,8 +1,8 @@ use crate::non_fungible_token::metadata::TokenMetadata; +use near_sdk::schemars; use near_sdk::serde::{Deserialize, Serialize}; use near_sdk::AccountId; use std::collections::HashMap; - /// Note that token IDs for NFTs are strings on NEAR. It's still fine to use autoincrementing numbers as unique IDs if desired, but they should be stringified. This is to make IDs more future-proof as chain-agnostic conventions and standards arise, and allows for more flexibility with considerations like bridging NFTs across chains, etc. pub type TokenId = String; diff --git a/near-contract-standards/src/storage_management/mod.rs b/near-contract-standards/src/storage_management/mod.rs index 154557b91..d39dc5278 100644 --- a/near-contract-standards/src/storage_management/mod.rs +++ b/near-contract-standards/src/storage_management/mod.rs @@ -1,7 +1,7 @@ use near_sdk::borsh::{BorshDeserialize, BorshSerialize}; +use near_sdk::schemars; use near_sdk::serde::{Deserialize, Serialize}; use near_sdk::{AccountId, NearToken}; - #[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize)] #[serde(crate = "near_sdk::serde")] #[borsh(crate = "near_sdk::borsh")] diff --git a/near-sdk-macros/src/lib.rs b/near-sdk-macros/src/lib.rs index 6d34fb0ae..4cc86a97b 100644 --- a/near-sdk-macros/src/lib.rs +++ b/near-sdk-macros/src/lib.rs @@ -368,23 +368,25 @@ pub fn derive_near_schema(input: TokenStream) -> TokenStream { } } + // or #[abi(json)] let json_schema = json_schema || !borsh_schema; - let derive = match (json_schema, borsh_schema) { - // or #[abi(json)] - (_, false) => quote! { - #[derive(schemars::JsonSchema)] - }, - // #[abi(borsh)] - (false, true) => quote! { - #[derive(::near_sdk::borsh::BorshSchema)] - #[borsh(crate = "::near_sdk::borsh")] - }, - // #[abi(json, borsh)] - (true, true) => quote! { - #[derive(schemars::JsonSchema, ::near_sdk::borsh::BorshSchema)] - #[borsh(crate = "::near_sdk::borsh")] - }, + let derive = { + let mut derive = quote! {}; + if borsh_schema { + derive = quote! { + #[derive(::near_sdk::borsh::BorshSchema)] + #[borsh(crate = "::near_sdk::borsh")] + }; + } + if json_schema { + derive = quote! { + #derive + #[derive(::near_sdk::schemars::JsonSchema)] + #[schemars(crate = "::near_sdk::schemars")] + }; + } + derive }; let input_ident = &input.ident; @@ -394,13 +396,13 @@ pub fn derive_near_schema(input: TokenStream) -> TokenStream { let json_impl = if json_schema { quote! { #[automatically_derived] - impl schemars::JsonSchema for #input_ident_proxy { + impl ::near_sdk::schemars::JsonSchema for #input_ident_proxy { fn schema_name() -> ::std::string::String { stringify!(#input_ident).to_string() } - fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { - <#input_ident as schemars::JsonSchema>::json_schema(gen) + fn json_schema(gen: &mut ::near_sdk::schemars::gen::SchemaGenerator) -> ::near_sdk::schemars::schema::Schema { + <#input_ident as ::near_sdk::schemars::JsonSchema>::json_schema(gen) } } } @@ -436,8 +438,6 @@ pub fn derive_near_schema(input: TokenStream) -> TokenStream { #[allow(non_camel_case_types)] type #input_ident_proxy = #input_ident; { - use ::near_sdk::__private::schemars; - #derive #input diff --git a/near-sdk/Cargo.toml b/near-sdk/Cargo.toml index 625f91752..d7fc4c5a1 100644 --- a/near-sdk/Cargo.toml +++ b/near-sdk/Cargo.toml @@ -34,9 +34,9 @@ wee_alloc = { version = "0.4.5", default-features = false, optional = true } once_cell = { version = "1.17", default-features = false } near-abi = { version = "0.4.0", features = ["__chunked-entries"], optional = true } -near-gas = { version = "0.2.3", features = ["serde", "borsh", "schemars"] } -near-token = { version = "0.2.0", features = ["serde", "borsh", "schemars"] } -near-account-id = {version="1.0.0-alpha.3", features = ["abi", "serde", "borsh", "schemars"]} +near-account-id = { version="1.0.0-alpha.4", features = ["serde", "borsh"] } +near-gas = { version = "0.2.3", features = ["serde", "borsh"] } +near-token = { version = "0.2.0", features = ["serde", "borsh"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] near-vm-logic = { version = "0.17", optional = true } @@ -62,7 +62,7 @@ default = ["wee_alloc", "unit-testing", "legacy", "abi"] expensive-debug = [] unstable = [] legacy = [] -abi = ["borsh/unstable__schema", "near-abi", "schemars", "near-sdk-macros/abi"] +abi = ["borsh/unstable__schema", "near-abi", "schemars", "near-sdk-macros/abi", "near-account-id/abi", "near-gas/abi", "near-token/abi"] unit-testing = ["near-vm-logic", "near-primitives-core", "near-primitives", "near-crypto"] __abi-embed = ["near-sdk-macros/__abi-embed"] diff --git a/near-sdk/compilation_tests/schema_derive.rs b/near-sdk/compilation_tests/schema_derive.rs index 37f1224e2..1bddac5d4 100644 --- a/near-sdk/compilation_tests/schema_derive.rs +++ b/near-sdk/compilation_tests/schema_derive.rs @@ -49,7 +49,7 @@ pub fn non_mod_scoped() { #[derive(NearSchema)] struct InnerValue; - const_assert_impls!(InnerValue: near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(InnerValue: near_sdk::schemars::JsonSchema); const_assert_impls!(InnerValue: !near_sdk::borsh::BorshSchema); #[derive(NearSchema)] @@ -57,7 +57,7 @@ pub fn non_mod_scoped() { field: InnerValue, } - const_assert_impls!(Value: near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(Value: near_sdk::schemars::JsonSchema); const_assert_impls!(Value: !near_sdk::borsh::BorshSchema); } @@ -66,14 +66,14 @@ pub fn no_schema_spec() { #[serde(rename = "UnitNoSchemaSpecSTRUCT")] struct UnitStructNoSchemaSpec; - const_assert_impls!(UnitStructNoSchemaSpec: near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(UnitStructNoSchemaSpec: near_sdk::schemars::JsonSchema); const_assert_impls!(UnitStructNoSchemaSpec: !near_sdk::borsh::BorshSchema); #[derive(NearSchema)] #[serde(rename = "UNITNoSchemaSpecENUM")] pub enum UnitEnumNoSchemaSpec {} - const_assert_impls!(UnitEnumNoSchemaSpec: near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(UnitEnumNoSchemaSpec: near_sdk::schemars::JsonSchema); const_assert_impls!(UnitEnumNoSchemaSpec: !near_sdk::borsh::BorshSchema); #[derive(NearSchema)] @@ -89,7 +89,7 @@ pub fn no_schema_spec() { }, } - const_assert_impls!(EnumNoSchemaSpec: near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(EnumNoSchemaSpec: near_sdk::schemars::JsonSchema); const_assert_impls!(EnumNoSchemaSpec: !near_sdk::borsh::BorshSchema); #[derive(NearSchema)] @@ -99,7 +99,7 @@ pub fn no_schema_spec() { var2: EnumNoSchemaSpec, } - const_assert_impls!(StructNoSchemaSpec: near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(StructNoSchemaSpec: near_sdk::schemars::JsonSchema); const_assert_impls!(StructNoSchemaSpec: !near_sdk::borsh::BorshSchema); } @@ -109,7 +109,7 @@ pub fn json_schema_spec() { #[serde(rename = "UnitNoSchemaSpecSTRUCT")] pub struct UnitStructNoSchemaSpec; - const_assert_impls!(UnitStructNoSchemaSpec: near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(UnitStructNoSchemaSpec: near_sdk::schemars::JsonSchema); const_assert_impls!(UnitStructNoSchemaSpec: !near_sdk::borsh::BorshSchema); #[derive(NearSchema)] @@ -117,7 +117,7 @@ pub fn json_schema_spec() { #[serde(rename = "UNITNoSchemaSpecENUM")] pub enum UnitEnumNoSchemaSpec {} - const_assert_impls!(UnitEnumNoSchemaSpec: near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(UnitEnumNoSchemaSpec: near_sdk::schemars::JsonSchema); const_assert_impls!(UnitEnumNoSchemaSpec: !near_sdk::borsh::BorshSchema); #[derive(NearSchema)] @@ -134,7 +134,7 @@ pub fn json_schema_spec() { }, } - const_assert_impls!(EnumNoSchemaSpec: near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(EnumNoSchemaSpec: near_sdk::schemars::JsonSchema); const_assert_impls!(EnumNoSchemaSpec: !near_sdk::borsh::BorshSchema); #[derive(NearSchema)] @@ -145,7 +145,7 @@ pub fn json_schema_spec() { var2: EnumNoSchemaSpec, } - const_assert_impls!(StructNoSchemaSpec: near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(StructNoSchemaSpec: near_sdk::schemars::JsonSchema); const_assert_impls!(StructNoSchemaSpec: !near_sdk::borsh::BorshSchema); } @@ -155,14 +155,14 @@ pub fn borsh_schema_spec() { pub struct UnitStructNoSchemaSpec; const_assert_impls!(UnitStructNoSchemaSpec: near_sdk::borsh::BorshSchema); - const_assert_impls!(UnitStructNoSchemaSpec: !near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(UnitStructNoSchemaSpec: !near_sdk::schemars::JsonSchema); #[derive(NearSchema)] #[abi(borsh)] pub enum UnitEnumNoSchemaSpec {} const_assert_impls!(UnitEnumNoSchemaSpec: near_sdk::borsh::BorshSchema); - const_assert_impls!(UnitEnumNoSchemaSpec: !near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(UnitEnumNoSchemaSpec: !near_sdk::schemars::JsonSchema); #[derive(NearSchema)] #[abi(borsh)] @@ -177,7 +177,7 @@ pub fn borsh_schema_spec() { } const_assert_impls!(EnumNoSchemaSpec: near_sdk::borsh::BorshSchema); - const_assert_impls!(EnumNoSchemaSpec: !near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(EnumNoSchemaSpec: !near_sdk::schemars::JsonSchema); #[derive(NearSchema)] #[abi(borsh)] @@ -188,7 +188,7 @@ pub fn borsh_schema_spec() { } const_assert_impls!(StructNoSchemaSpec: near_sdk::borsh::BorshSchema); - const_assert_impls!(StructNoSchemaSpec: !near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(StructNoSchemaSpec: !near_sdk::schemars::JsonSchema); } pub fn json_borsh_schema_spec() { @@ -197,7 +197,7 @@ pub fn json_borsh_schema_spec() { #[serde(rename = "UnitNoSchemaSpecSTRUCT")] pub struct UnitStructNoSchemaSpec; - const_assert_impls!(UnitStructNoSchemaSpec: near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(UnitStructNoSchemaSpec: near_sdk::schemars::JsonSchema); const_assert_impls!(UnitStructNoSchemaSpec: near_sdk::borsh::BorshSchema); #[derive(NearSchema)] @@ -205,7 +205,7 @@ pub fn json_borsh_schema_spec() { #[serde(rename = "UNITNoSchemaSpecENUM")] pub enum UnitEnumNoSchemaSpec {} - const_assert_impls!(UnitEnumNoSchemaSpec: near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(UnitEnumNoSchemaSpec: near_sdk::schemars::JsonSchema); const_assert_impls!(UnitEnumNoSchemaSpec: near_sdk::borsh::BorshSchema); #[derive(NearSchema)] @@ -231,7 +231,7 @@ pub fn json_borsh_schema_spec() { }, } - const_assert_impls!(EnumNoSchemaSpec: near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(EnumNoSchemaSpec: near_sdk::schemars::JsonSchema); const_assert_impls!(EnumNoSchemaSpec: near_sdk::borsh::BorshSchema); #[derive(NearSchema)] @@ -243,7 +243,7 @@ pub fn json_borsh_schema_spec() { var2: EnumNoSchemaSpec, } - const_assert_impls!(StructNoSchemaSpec: near_sdk::__private::schemars::JsonSchema); + const_assert_impls!(StructNoSchemaSpec: near_sdk::schemars::JsonSchema); const_assert_impls!(StructNoSchemaSpec: near_sdk::borsh::BorshSchema); } diff --git a/near-sdk/src/lib.rs b/near-sdk/src/lib.rs index e7dbc2039..0f9beab8b 100644 --- a/near-sdk/src/lib.rs +++ b/near-sdk/src/lib.rs @@ -7,7 +7,7 @@ #[cfg(test)] extern crate quickcheck; -#[cfg(all(feature = "unstable", feature = "abi"))] +#[cfg(feature = "abi")] pub use near_sdk_macros::NearSchema; pub use near_sdk_macros::{ ext_contract, near_bindgen, BorshStorageKey, EventMetadata, FunctionError, PanicOnDefault, @@ -60,17 +60,10 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; // Exporting common crates -#[doc(hidden)] -pub use borsh; - -#[doc(hidden)] pub use base64; - -#[doc(hidden)] +pub use borsh; pub use bs58; - -#[doc(hidden)] +#[cfg(feature = "abi")] +pub use schemars; pub use serde; - -#[doc(hidden)] pub use serde_json; diff --git a/near-sdk/src/private/mod.rs b/near-sdk/src/private/mod.rs index 16713a1cb..631d82dff 100644 --- a/near-sdk/src/private/mod.rs +++ b/near-sdk/src/private/mod.rs @@ -6,9 +6,6 @@ pub use near_abi::{ AbiParameters, AbiType, }; -#[cfg(feature = "abi")] -pub use schemars; - use crate::IntoStorageKey; use borsh::{to_vec, BorshSerialize}; diff --git a/near-sdk/src/types/mod.rs b/near-sdk/src/types/mod.rs index 549cc553d..aeaedfba4 100644 --- a/near-sdk/src/types/mod.rs +++ b/near-sdk/src/types/mod.rs @@ -8,7 +8,6 @@ mod primitives; pub use self::primitives::*; pub use near_account_id::{AccountId, AccountIdRef}; - pub use near_gas::NearGas as Gas; pub use near_token::NearToken;