Skip to content

Commit

Permalink
Add metadata to nft-simple (#137)
Browse files Browse the repository at this point in the history
* nft-simple: add metadata for token and contract, for init and minting fns, and nft_metadata fn

* update nft_simple WASM file
  • Loading branch information
mikedotexe committed Mar 26, 2021
1 parent 1a5843b commit 75b02a4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
Binary file modified nft-simple/res/nft_simple.wasm
Binary file not shown.
11 changes: 8 additions & 3 deletions nft-simple/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ use std::collections::HashSet;

use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::{LookupMap, UnorderedMap, UnorderedSet};
use near_sdk::json_types::ValidAccountId;
use near_sdk::json_types::{ValidAccountId, Base64VecU8, U64};
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{env, near_bindgen, AccountId, Balance, PanicOnDefault, Promise, StorageUsage};

use crate::internal::*;
pub use crate::mint::*;
pub use crate::nft_core::*;
use crate::nft_metadata::{TokenMetadata, NFTMetadata};

mod internal;
mod mint;
mod nft_core;
mod nft_metadata;

#[global_allocator]
static ALLOC: near_sdk::wee_alloc::WeeAlloc<'_> = near_sdk::wee_alloc::WeeAlloc::INIT;
Expand All @@ -23,7 +25,7 @@ pub type TokenId = String;
#[serde(crate = "near_sdk::serde")]
pub struct Token {
pub owner_id: AccountId,
pub metadata: String,
pub metadata: TokenMetadata,
pub approved_account_ids: HashSet<AccountId>,
}

Expand All @@ -38,18 +40,21 @@ pub struct Contract {

/// The storage size in bytes for one account.
pub extra_storage_in_bytes_per_token: StorageUsage,

pub metadata: NFTMetadata
}

#[near_bindgen]
impl Contract {
#[init]
pub fn new(owner_id: ValidAccountId) -> Self {
pub fn new(owner_id: ValidAccountId, metadata: NFTMetadata) -> Self {
assert!(!env::state_exists(), "Already initialized");
let mut this = Self {
tokens_per_owner: LookupMap::new(b"a".to_vec()),
tokens_by_id: UnorderedMap::new(b"t".to_vec()),
owner_id: owner_id.into(),
extra_storage_in_bytes_per_token: 0,
metadata
};

this.measure_min_token_storage_cost();
Expand Down
2 changes: 1 addition & 1 deletion nft-simple/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::*;
#[near_bindgen]
impl Contract {
#[payable]
pub fn nft_mint(&mut self, token_id: TokenId, metadata: String) {
pub fn nft_mint(&mut self, token_id: TokenId, metadata: TokenMetadata) {
let initial_storage_usage = env::storage_usage();
self.assert_owner();
let token = Token {
Expand Down
41 changes: 41 additions & 0 deletions nft-simple/src/nft_metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::*;

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone)]
#[serde(crate = "near_sdk::serde")]
pub struct NFTMetadata {
spec: String, // required, essentially a version like "nft-1.0.0"
name: String, // required, ex. "Mosaics"
symbol: String, // required, ex. "MOSIAC"
icon: Option<String>, // Data URL
base_uri: Option<String>, // Centralized gateway known to have reliable access to decentralized storage assets referenced by `reference` or `media` URLs
reference: Option<String>, // URL to a JSON file with more info
reference_hash: Option<Base64VecU8>, // Base64-encoded sha256 hash of JSON from reference field. Required if `reference` is included.
}

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize)]
#[serde(crate = "near_sdk::serde")]
pub struct TokenMetadata {
title: Option<String>, // ex. "Arch Nemesis: Mail Carrier" or "Parcel #5055"
description: Option<String>, // free-form description
media: Option<String>, // URL to associated media, preferably to decentralized, content-addressed storage
media_hash: Option<Base64VecU8>, // Base64-encoded sha256 hash of content referenced by the `media` field. Required if `media` is included.
copies: Option<U64>, // number of copies of this set of metadata in existence when token was minted.
issued_at: Option<String>, // ISO 8601 datetime when token was issued or minted
expires_at: Option<String>, // ISO 8601 datetime when token expires
starts_at: Option<String>, // ISO 8601 datetime when token starts being valid
updated_at: Option<String>, // ISO 8601 datetime when token was last updated
extra: Option<String>, // anything extra the NFT wants to store on-chain. Can be stringified JSON.
reference: Option<String>, // URL to an off-chain JSON file with more info.
reference_hash: Option<Base64VecU8>, // Base64-encoded sha256 hash of JSON from reference field. Required if `reference` is included.
}

pub trait NonFungibleTokenMetadata {
fn nft_metadata(&self) -> NFTMetadata;
}

#[near_bindgen]
impl NonFungibleTokenMetadata for Contract {
fn nft_metadata(&self) -> NFTMetadata {
self.metadata.clone()
}
}

1 comment on commit 75b02a4

@zavodil
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we rename reference => reference_url?

Please sign in to comment.