Skip to content

Commit

Permalink
feat: custom token_data and check_external_transfer (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
encody authored May 6, 2024
1 parent 3df50f2 commit d540ed7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
18 changes: 13 additions & 5 deletions macros/src/standard/non_fungible_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct NonFungibleTokenMeta {
pub mint_hook: Option<Type>,
pub transfer_hook: Option<Type>,
pub burn_hook: Option<Type>,
pub token_data: Option<Type>,
pub check_external_transfer: Option<Type>,

// NEP-177 fields
pub metadata_storage_key: Option<Expr>,
Expand Down Expand Up @@ -56,6 +58,8 @@ pub fn expand(meta: NonFungibleTokenMeta) -> Result<TokenStream, darling::Error>
mint_hook,
transfer_hook,
burn_hook,
token_data,
check_external_transfer,

metadata_storage_key,

Expand Down Expand Up @@ -88,6 +92,8 @@ pub fn expand(meta: NonFungibleTokenMeta) -> Result<TokenStream, darling::Error>
near_sdk: near_sdk.clone(),
});

let token_data = unitify(token_data);

let expand_nep171 = nep171::expand(nep171::Nep171Meta {
storage_key: core_storage_key,
all_hooks: Some(parse_quote! { (
Expand All @@ -103,11 +109,13 @@ pub fn expand(meta: NonFungibleTokenMeta) -> Result<TokenStream, darling::Error>
mint_hook,
transfer_hook,
burn_hook,
check_external_transfer: Some(syn::parse_quote! { #me::standard::nep178::TokenApprovals }),

token_data: Some(
syn::parse_quote! { (#me::standard::nep177::TokenMetadata, #me::standard::nep178::TokenApprovals) },
),
check_external_transfer: Some(check_external_transfer.unwrap_or_else(|| {
parse_quote! { #me::standard::nep178::TokenApprovals }
})),
token_data: Some(syn::parse_quote! { (
#token_data,
(#me::standard::nep177::TokenMetadata, #me::standard::nep178::TokenApprovals),
) }),

generics: generics.clone(),
ident: ident.clone(),
Expand Down
47 changes: 45 additions & 2 deletions workspaces-tests/src/bin/non_fungible_token_full.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
workspaces_tests::predicate!();

use near_sdk::{env, log, near, PanicOnDefault};
use near_sdk_contract_tools::{hook::Hook, nft::*};
use near_sdk::{env, log, near, serde_json::json, PanicOnDefault};
use near_sdk_contract_tools::{
hook::Hook,
nft::{
nep171::{CheckExternalTransfer, LoadTokenMetadata},
*,
},
};

#[derive(NonFungibleToken, PanicOnDefault)]
#[non_fungible_token(
transfer_hook = "Self",
approve_hook = "Self",
revoke_hook = "Self",
revoke_all_hook = "Self",
token_data = "ExtraTokenData",
check_external_transfer = "ExtraCheckExternalTransfer"
)]
#[near(contract_state)]
pub struct Contract {}

pub struct ExtraCheckExternalTransfer;

impl CheckExternalTransfer<Contract> for ExtraCheckExternalTransfer {
fn check_external_transfer(
contract: &Contract,
transfer: &Nep171Transfer,
) -> Result<near_sdk::AccountId, nep171::error::Nep171TransferError> {
TokenApprovals::check_external_transfer(contract, transfer)
}
}

pub struct ExtraTokenData;

impl LoadTokenMetadata<Contract> for ExtraTokenData {
fn load(
_contract: &Contract,
_token_id: &TokenId,
metadata: &mut std::collections::HashMap<String, near_sdk::serde_json::Value>,
) -> Result<(), Box<dyn std::error::Error>> {
metadata.insert(
"funky_data".to_string(),
json!({
"funky": "data",
}),
);
Ok(())
}
}

impl Hook<Contract, Nep178Approve<'_>> for Contract {
fn hook<R>(
contract: &mut Contract,
Expand Down
13 changes: 9 additions & 4 deletions workspaces-tests/tests/non_fungible_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ async fn create_and_mint_with_metadata_and_enumeration() {
owner_id: alice.id().as_str().parse().unwrap(),
extensions_metadata: [
("metadata".to_string(), token_meta("token_0".to_string())),
("approved_account_ids".to_string(), json!({}),)
("approved_account_ids".to_string(), json!({})),
("funky_data".to_string(), json!({"funky": "data"})),
]
.into(),
}),
Expand All @@ -178,7 +179,8 @@ async fn create_and_mint_with_metadata_and_enumeration() {
owner_id: bob.id().as_str().parse().unwrap(),
extensions_metadata: [
("metadata".to_string(), token_meta("token_1".to_string())),
("approved_account_ids".to_string(), json!({}),)
("approved_account_ids".to_string(), json!({})),
("funky_data".to_string(), json!({"funky": "data"})),
]
.into(),
}),
Expand All @@ -190,7 +192,8 @@ async fn create_and_mint_with_metadata_and_enumeration() {
owner_id: charlie.id().as_str().parse().unwrap(),
extensions_metadata: [
("metadata".to_string(), token_meta("token_2".to_string())),
("approved_account_ids".to_string(), json!({}),)
("approved_account_ids".to_string(), json!({})),
("funky_data".to_string(), json!({"funky": "data"})),
]
.into(),
}),
Expand Down Expand Up @@ -809,6 +812,7 @@ async fn transfer_approval_success() {
bob.id().to_string(): 0,
}),
),
("funky_data".to_string(), json!({"funky": "data"})),
]
.into(),
};
Expand Down Expand Up @@ -847,7 +851,8 @@ async fn transfer_approval_success() {
owner_id: charlie.id().as_str().parse().unwrap(),
extensions_metadata: [
("metadata".to_string(), token_meta("token_0".to_string())),
("approved_account_ids".to_string(), json!({}))
("approved_account_ids".to_string(), json!({})),
("funky_data".to_string(), json!({"funky": "data"})),
]
.into(),
}),
Expand Down

0 comments on commit d540ed7

Please sign in to comment.