Skip to content

Commit

Permalink
Fix problems with TokenMetadata, LookupMap
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Mar 22, 2024
1 parent 09d1d7d commit 5887f59
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 95 deletions.
5 changes: 3 additions & 2 deletions near-contract-standards/src/non_fungible_token/metadata.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -21,7 +21,7 @@ pub struct NFTContractMetadata {
pub reference_hash: Option<Base64VecU8>, // Base64-encoded sha256 hash of JSON from reference field. Required if `reference` is included.
}

#[my_tmp_macro]
#[near(serializers=[borsh, json])]
#[derive(
Debug,
Clone,
Expand All @@ -44,6 +44,7 @@ pub struct TokenMetadata {
pub reference_hash: Option<Base64VecU8>,
}


/// Metadata on the individual token level.
// #[derive(Debug, Clone, Default, PartialEq, Eq)]
// #[near(serializers=[borsh, json])]
Expand Down
6 changes: 3 additions & 3 deletions near-contract-standards/src/non_fungible_token/token.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
125 changes: 41 additions & 84 deletions near-sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ItemStruct>(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<IdentsVector>,
Expand Down Expand Up @@ -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

};
}

Expand All @@ -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::<ItemEnum>(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::<ItemImpl>(item) {
expanded = quote! {
#[#near_sdk_crate::near_bindgen]
Expand Down Expand Up @@ -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();

// <unspecified> or #[abi(json)]
let json_schema = json_schema || !borsh_schema;
Expand Down Expand Up @@ -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)]
Expand All @@ -783,7 +738,9 @@ pub fn derive_near_schema(#[allow(unused)] input: TokenStream) -> TokenStream {
#borsh_impl
};
};
})
};
// eprintln!("x: {}", x.to_string());
TokenStream::from(x)
}
}

Expand Down
2 changes: 1 addition & 1 deletion near-sdk/src/json_types/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use serde::{Deserialize, Deserializer, Serializer};

/// Helper class to serialize/deserialize `Vec<u8>` 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",
Expand Down
2 changes: 1 addition & 1 deletion near-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
3 changes: 2 additions & 1 deletion near-sdk/src/store/lookup_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<K, V, H = Identity>
where
K: BorshSerialize + Ord,
Expand Down
4 changes: 1 addition & 3 deletions near-sdk/src/store/unordered_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -102,7 +100,7 @@ where
values: LookupMap<K, ValueAndIndex<V>, H>,
}

#[my_tmp_macro]
#[near(inside_nearsdk)]
struct ValueAndIndex<V> {
value: V,
key_index: FreeListIndex,
Expand Down

0 comments on commit 5887f59

Please sign in to comment.