diff --git a/near-contract-standards/src/non_fungible_token/metadata.rs b/near-contract-standards/src/non_fungible_token/metadata.rs index 7cc5d8517..693532d86 100644 --- a/near-contract-standards/src/non_fungible_token/metadata.rs +++ b/near-contract-standards/src/non_fungible_token/metadata.rs @@ -1,7 +1,7 @@ use near_sdk::borsh::{BorshDeserialize, BorshSerialize}; use near_sdk::json_types::Base64VecU8; use near_sdk::serde::{Deserialize, Serialize}; -use near_sdk::{my_tmp_macro, near, require}; +use near_sdk::{near, require}; /// This spec can be treated like a version of the standard. pub const NFT_METADATA_SPEC: &str = "nft-1.0.0"; @@ -21,7 +21,7 @@ pub struct NFTContractMetadata { pub reference_hash: Option, // Base64-encoded sha256 hash of JSON from reference field. Required if `reference` is included. } -#[my_tmp_macro] +#[near(serializers=[borsh, json])] #[derive( Debug, Clone, @@ -44,6 +44,7 @@ pub struct TokenMetadata { pub reference_hash: Option, } + /// Metadata on the individual token level. // #[derive(Debug, Clone, Default, PartialEq, Eq)] // #[near(serializers=[borsh, json])] diff --git a/near-contract-standards/src/non_fungible_token/token.rs b/near-contract-standards/src/non_fungible_token/token.rs index 829681533..6969146d7 100644 --- a/near-contract-standards/src/non_fungible_token/token.rs +++ b/near-contract-standards/src/non_fungible_token/token.rs @@ -1,12 +1,12 @@ use crate::non_fungible_token::metadata::TokenMetadata; -use near_sdk::{AccountId, near}; +use near_sdk::{serde::{Deserialize, Serialize}, AccountId, NearSchema}; 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; /// In this implementation, the Token struct takes two extensions standards (metadata and approval) as optional fields, as they are frequently used in modern NFTs. -#[near(serializers=[json])] -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(NearSchema, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] +#[serde(crate = "near_sdk::serde")] pub struct Token { pub token_id: TokenId, pub owner_id: AccountId, diff --git a/near-sdk-macros/src/lib.rs b/near-sdk-macros/src/lib.rs index 37fea660b..cca99b837 100644 --- a/near-sdk-macros/src/lib.rs +++ b/near-sdk-macros/src/lib.rs @@ -35,55 +35,6 @@ impl FromMeta for IdentsVector { } } -#[proc_macro_attribute] -pub fn my_tmp_macro(attr: TokenStream, item: TokenStream) -> TokenStream { - let expanded; - if let Ok(input) = syn::parse::(item.clone()) { - if (input.ident.eq("ValueAndIndex")) { - // #[cfg(feature = "abi")] - { - expanded = quote! { - #[cfg(feature = "abi")] - #[derive(crate :: borsh :: BorshSchema)] - #[derive(crate :: borsh :: BorshSerialize, crate::borsh::BorshDeserialize)] - #[borsh(crate = "crate :: borsh")] - #input - - #[cfg(not(feature = "abi"))] - #[derive(crate :: borsh :: BorshSerialize, crate::borsh::BorshDeserialize)] - #[borsh(crate = "crate :: borsh")] - #input - }; - } - // #[cfg(not(feature = "abi"))] - // { - // expanded = quote! { - // #[cfg(not(feature = "abi"))] - // #[derive(crate :: borsh :: BorshSerialize, crate::borsh::BorshDeserialize)] - // #[borsh(crate = "crate :: borsh")] - // #input - // }; - // } - } else if (input.ident.eq("TokenMetadata")) { - expanded = quote! { - // #[cfg(feature = "abi")] - // #[derive(crate::schemars::JsonSchema)] - #[derive(Serialize, Deserialize, BorshDeserialize, BorshSerialize, near_sdk::NearSchema)] - #[serde(crate = "near_sdk::serde")] - #[borsh(crate = "near_sdk::borsh")] - // #[schemars(crate = "near_sdk::schemars")] - #input - }; - } else { - expanded = quote! {}; - } - TokenStream::from(expanded) - } else { - TokenStream::from(quote! {}) - } - -} - #[derive(FromMeta)] struct NearMacroArgs { serializers: Option, @@ -246,13 +197,19 @@ pub fn near(attr: TokenStream, item: TokenStream) -> TokenStream { #[cfg(feature = "abi")] { expanded = quote! { + #[cfg(not(target_arch = "wasm32"))] #near_bindgen_annotation - #[derive(#near_sdk_crate::NearSchema)] - #inside_nearsdk_attr + #schema_derive #borsh #json - #abis #input + + #[cfg(target_arch = "wasm32")] + #near_bindgen_annotation + #borsh + #json + #input + }; } @@ -265,35 +222,35 @@ pub fn near(attr: TokenStream, item: TokenStream) -> TokenStream { #input }; } - - // eprintln!("expanded: {}", expanded.to_string()); - // eprintln!("") - - // expanded = quote! { - // #near_bindgen_annotation - // // #schema_derive - // #[derive(#near_sdk_crate::NearSchema)] - // #inside_nearsdk_attr - // #borsh - // #json - // #abis - // #input - // }; - - // eprintln!("expanded: {}", expanded.to_string()); - - } else if let Ok(input) = syn::parse::(item.clone()) { - expanded = quote! { - #near_bindgen_annotation - // #schema_derive - #[derive(#near_sdk_crate::NearSchema)] - #inside_nearsdk_attr - #borsh - #json - #abis - #input - }; + #[cfg(feature = "abi")] + { + expanded = quote! { + #[cfg(not(target_arch = "wasm32"))] + #near_bindgen_annotation + #schema_derive + #borsh + #json + #input + + #[cfg(target_arch = "wasm32")] + #near_bindgen_annotation + #borsh + #json + #input + + }; + } + + #[cfg(not(feature = "abi"))] + { + expanded = quote! { + #near_bindgen_annotation + #borsh + #json + #input + }; + } } else if let Ok(input) = syn::parse::(item) { expanded = quote! { #[#near_sdk_crate::near_bindgen] @@ -708,8 +665,6 @@ pub fn derive_near_schema(#[allow(unused)] input: TokenStream) -> TokenStream { } else { quote! {::near_sdk} }; - let string_borsh_crate = quote! {#near_sdk_crate::borsh}.to_string(); - let string_schemars_crate = quote! {#near_sdk_crate::schemars}.to_string(); // or #[abi(json)] let json_schema = json_schema || !borsh_schema; @@ -769,7 +724,7 @@ pub fn derive_near_schema(#[allow(unused)] input: TokenStream) -> TokenStream { quote! {} }; - TokenStream::from(quote! { + let x = quote! { #[cfg(not(target_arch = "wasm32"))] const _: () = { #[allow(non_camel_case_types)] @@ -783,7 +738,9 @@ pub fn derive_near_schema(#[allow(unused)] input: TokenStream) -> TokenStream { #borsh_impl }; }; - }) + }; + // eprintln!("x: {}", x.to_string()); + TokenStream::from(x) } } diff --git a/near-sdk/src/json_types/vector.rs b/near-sdk/src/json_types/vector.rs index 5b8ee443b..b69e484ae 100644 --- a/near-sdk/src/json_types/vector.rs +++ b/near-sdk/src/json_types/vector.rs @@ -3,8 +3,8 @@ use serde::{Deserialize, Deserializer, Serializer}; /// Helper class to serialize/deserialize `Vec` to base64 string. -#[derive(Debug, Clone, PartialEq, Eq)] #[near(inside_nearsdk, serializers=[borsh, json])] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Base64VecU8( #[serde( serialize_with = "base64_bytes::serialize", diff --git a/near-sdk/src/lib.rs b/near-sdk/src/lib.rs index 281305133..8a219a258 100644 --- a/near-sdk/src/lib.rs +++ b/near-sdk/src/lib.rs @@ -8,7 +8,7 @@ extern crate quickcheck; pub use near_sdk_macros::{ - ext_contract, near, my_tmp_macro, near_bindgen, BorshStorageKey, EventMetadata, FunctionError, NearSchema, + ext_contract, near, near_bindgen, BorshStorageKey, EventMetadata, FunctionError, NearSchema, PanicOnDefault, }; diff --git a/near-sdk/src/store/lookup_map/mod.rs b/near-sdk/src/store/lookup_map/mod.rs index 6aada3a59..bd73f8d5a 100644 --- a/near-sdk/src/store/lookup_map/mod.rs +++ b/near-sdk/src/store/lookup_map/mod.rs @@ -6,6 +6,7 @@ use std::fmt; use borsh::{BorshDeserialize, BorshSerialize}; use once_cell::unsync::OnceCell; +use near_sdk_macros::near; use super::ERR_NOT_EXIST; use crate::store::key::{Identity, ToKey}; @@ -76,7 +77,7 @@ const ERR_ELEMENT_SERIALIZATION: &str = "Cannot serialize element"; /// /// [`with_hasher`]: Self::with_hasher -#[derive(BorshSerialize, BorshDeserialize)] +#[near(inside_nearsdk)] pub struct LookupMap where K: BorshSerialize + Ord, diff --git a/near-sdk/src/store/unordered_map/mod.rs b/near-sdk/src/store/unordered_map/mod.rs index 4670703e0..35d5331b0 100644 --- a/near-sdk/src/store/unordered_map/mod.rs +++ b/near-sdk/src/store/unordered_map/mod.rs @@ -11,8 +11,6 @@ use std::{fmt, mem}; use borsh::{BorshDeserialize, BorshSerialize}; use near_sdk_macros::near; -use near_sdk_macros::NearSchema; -use near_sdk_macros::my_tmp_macro; use crate::store::key::{Sha256, ToKey}; use crate::{env, IntoStorageKey}; @@ -102,7 +100,7 @@ where values: LookupMap, H>, } -#[my_tmp_macro] +#[near(inside_nearsdk)] struct ValueAndIndex { value: V, key_index: FreeListIndex,