KCC Documents
  • Quickstart
  • Overview
    • Introduction
    • Features
    • Our Goal
    • Consensus Engine
    • Marketing Guide
      • Requirements
      • KCC Official Website Resource
      • KCC Social Media Promotion
      • PR & Third-Party Marketing Support
  • Developers
    • Network Endpoints
    • Deploy Smart Contract
      • Using Hardhat
      • Using Remix
    • Verify Smart Contract
      • Using Hardhat
      • Using Foundry
      • Using Remix
    • Issue ERC20 Token
    • Deploy NFTs
      • Create A Foundry Project
      • Create Your ERC721 Contract
      • Add Metadata
    • Run a Node
      • Hardware & System Requirements
      • Install KCC
      • Run A Full Node
      • Run A Validator Node
        • Block Rewards
        • Apply For Running A Validator
        • Manager Account And Validator Account
        • Run A Validator
      • Troubleshooting
    • Explorer
    • Oracles
      • KCC Oracle
    • Bridge
    • Dev Toolkit
    • Data
      • Indexing and Querying
        • The Graph
        • Using KCC's hosted subgraph
    • Gas Revenue Program
      • Rules for Calculation of Gas Revenue
      • How to Join the KCC Gas Revenue Program
  • Individuals
    • Wallet
      • Supported Wallet
      • Tutorial on how to set up wallet
      • Mutisig Wallet
        • Create New Safe
        • Send KCS From Your Safe
        • Use A DApp With Your Safe
    • Network Configuration
      • Configure Value
      • Tutorial on how to config KCC network in Metamask
      • Tutorial on how to config KCC network in Chainlist
    • Bridge Assets
      • Bridge assets from others chains
      • KCC Bridge
      • Bridge assets form Exchange
    • Find a dApp
      • Add Your dAPP
    • KCS Token
      • Get KCS
      • Stake KCS
        • How to participate in KCS Staking
        • How to redeem staked KCS
        • How to check or claim staking rewards
  • FAQs
    • FAQs
      • General FAQs
      • KCS and Staking FAQs
      • Validator FAQs
  • Future Developments
    • Milestone
  • CONTACT US
    • Contact Us
  • Disclosure
    • Disclaimers
    • Risk Statement
    • Media Kit
Powered by GitBook
On this page
  • Install Dependencies
  • The ERC721 Contract
  • Deploy Your ERC721 Contract
  • Mint An NFT
  1. Developers
  2. Deploy NFTs

Create Your ERC721 Contract

PreviousCreate A Foundry ProjectNextAdd Metadata

Last updated 2 years ago

Install Dependencies

We will build your ERC721 contract based on the Openzeppelin contracts. Let's install Openzeppelin contracts for our foundry project:

forge install openzeppelin/openzeppelin-contracts 

Foundry installs all dependencies as git submodules under the directory of "lib". And to make it easier to import the dependencies, let's create a remappings.txt file with the following contents:

openzeppelin-contracts=lib/openzeppelin-contracts

The remapping.txt file should be put in the project root directory (i.e., the directory that contains thefoundry.toml file. )

With the remappings.txt file above, we can import the ERC721 contract from Openzeppelin by the following import statement :

import "openzeppelin-contracts/contracts/token/ERC721/ERC721.sol";

The ERC721 Contract

Create a new file named "MyNFT.sol" under the directory of "src". The content in the file is as follows:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "openzeppelin-contracts/contracts/token/ERC721/ERC721.sol";
import "openzeppelin-contracts/contracts/access/Ownable.sol";

contract MyNFT is ERC721, Ownable{

    string private _URIBase;

    constructor(string memory name_, string memory symbol_) ERC721(name_, symbol_) {
        _transferOwnership(msg.sender);
    }

    function mint(address to, uint256 tokenId) public onlyOwner {
        _safeMint(to, tokenId);
    }

    function setBaseURI(string memory baseURI_) public onlyOwner {
        _URIBase = baseURI_;
    }

    function _baseURI() internal view override returns (string memory) {
        return _URIBase;
    }
}

In this toy ERC721 contract, we have added some customized logics:

  • The deployer of the contract will be the owner of the contract.

  • Only the owner can mint a new NFT token. However, he can mint the token to another address.

  • Only the owner can change the base URI of the Token URI.

Deploy Your ERC721 Contract

FOUNDRY_PROFILE=kcc_testnet \
    forge create \
    --constructor-args "my-nft" "M" 
    --private-key=XXXXXXXXXXXXXXXXX  \
    --legacy  src/MyNFT.sol:MyNFT

We use --constructor-args to specify the arguments for our constructor.

If everything goes well, forge will print out the address of the deployed contract and the transaction hash for the deployment.

Mint An NFT

We can use cast from foundry to interact with our ERC721 contract. Let's mint an NFT with cast:

FOUNDRY_PROFILE=kcc_testnet cast send  \
    --private-key=XXXXXXXXXXXX  --legacy \
    0xd382De234f8d3f1DB54C856498a9DbC84c \
    'mint(address,uint256)'  \
    0x6Dfc34a4a2FC03Ba5d5ff95566781bD2b39702fc 1337

  • You should replace 0xd382De234f8d3f1DB54C856498a9DbC84c with the address of your ERC721 contract.

  • And 0x6Dfc34a4a2FC03Ba5d5ff95566781bD2b39702fc is the address that receives the newly minted NFT.

  • Finally, 1337 is the id of the newly minted NFT.

We can also verify the owner of this newly minted NFT by using cast again:

FOUNDRY_PROFILE=kcc_testnet \
        cast call  0xd382De234f8d3f1DB54C856498a9DbC84c \
        'ownerOf(uint256)(address)' 1337