Make an NFT Minter on the NEAR Protocol | by Anmol Dhiman | Feb, 2022

Creating NFTs on the NEAR protocol using Rust

Anmol Dhiman

an introduction

What is the NEAR protocol?

Prerequisites

prove

cargo new nft --lib
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
near-sdk = "*"
near-contract-standards = "*"

start

use near_sdk::{
near_bindgen,
borsh::{self, BorshDeserialize, BorshSerialize}
};
near_sdk::setup_alloc!();#[near_bindgen]
#[derive(BorshDeserialize, BorshDeserialize)]
pub struct Contract {}
#[near_bindgen]
impl Contract {}
use near_contract_standards::non_fungible_token::{
metadata::NFTContractMetadata, NonFungibleToken,
};
pub struct Contract {
token: NonFungibleToken,
metadata: LazyOption<NFTContractMetadata>,
}
#[derive(BorshSerialize, BorshStorageKey)]
pub enum StorageKey {
NonFungibleToken,
Metadata,
TokenMetadata,
Enumeration,
Approval
}
#[near_bindgen]
impl Contract {
#[init]
pub fn new(owner_id: ValidAccountId) -> Self {
Self {
token: NonFungibleToken::new(
StorageKey::NonFungibleToken,
owner_id,
Some(StorageKey::TokenMetadata),
Some(StorageKey::Enumeration),
Some(StorageKey::Approval)
),
metadata: LazyOption::new(
StorageKey::Metadata,
Some(&NFTContractMetadata {
spec: NFT_METADATA_SPEC.to_string(),
name: "Example Name".to_string(),
symbol: "Example".to_string(),
icon: Some("ANY_SVG".to_string()),
base_uri: None,
reference: None,
reference_hash: None,
})
),
}
}
}
impl Contract {

......

#[payable]
pub fn nft_mint(
&mut self,
token_id: TokenId,
receiver_id: ValidAccountId,
token_metadata: TokenMetadata,
) -> Token {
self.token.mint(token_id, receiver_id, Some(token_metadata))
}
}

cargo build --target wasm32-unknown-unknown --release
near deploy --wasmFile target/wasm32-unknown-unknown/release/nft.wasm --accountId "your near account username"

Leave a Comment