How to Deploy erc721 nft smart contract using Thirdweb cli 2022

how to deploy erc721 nft smart contract using thirdweb cli
how to deploy erc721 nft smart contract using thirdweb cli

We all know that just writing the erc721 smart contract is not enough for launching your NFT collection and after writing the contract we need to deploy to your choosen blockchain i.e ethereum , polygon , binance , solana etc

Then you can call that you have successfully launched your nft collection in (X) blockchain with the help of smart contract

So, Today we will learn how we can deploy our erc721 nft smart contract using Thirdweb cli

First of all we will understand about Thirdweb Cli

What is Thirdweb Cli ?

the thirdweb CLI is your all in one resource for distributing custom agreements for your group or the world to utilize. The CLI transfers generally essential information to decentralized capacity and makes it accessible to send by means of the thirdweb sdk or thirdweb dashboard.

This brings every one of the capacities of thirdweb to your own custom agreements

Features :

  • auto-detect any contracts in your project
  • compile your project
  • Upload ABIs to IPFS
  • Open the deploy flow in your thirdweb dashboard in a browser

How anyone can deploy erc721 nft smart contract using Thirdweb Cli ?

In order to deploy your smart contract we will need to run some commands as well as download some neccessary packages In order to deploy to your choosen blockchain

Let’s start Learning

Step 1) Open your Vs code Terminal and need to type this command

npx thirdweb create

Step 2) Now , you need to choose the “contract” option

how to deploy erc721 nft smart contract using thirdweb cli

Step 3) Now , you need to give a name of your project and after that choose the “hardhat” option

how to deploy erc721 nft smart contract using thirdweb cli

Step 4) In this step , you need to choose whether you want to create your erc721 nft smart contract from scratch or use thirdweb different smart contract bolier code

different types of smart contract are provided choose according to your need

how to deploy erc721 nft smart contract using thirdweb cli

Step 5) All set , you have successfully configured thirdweb for deploying your smart contract

So , now we will write our erc721 nft smart contract by going inside this project then contract folder after that you will get a file name “contract.sol” inside this file you will write your smart contract code

I am providing you the source code which you can copy and paste in your “contract.sol” file

// SPDX-License-Identifier: MIT


pragma solidity >=0.7.0 <0.9.0;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract NFT is ERC721Enumerable, Ownable {
  using Strings for uint256;

  string baseURI;
  string public baseExtension = ".json";
  uint256 public cost = 0.05 ether;
  uint256 public maxSupply = 10000;
  uint256 public maxMintAmount = 20;
  bool public paused = false;
  bool public revealed = false;
  string public notRevealedUri;

  constructor(
    string memory _name,
    string memory _symbol,
    string memory _initBaseURI,
    string memory _initNotRevealedUri
  ) ERC721(_name, _symbol) {
    setBaseURI(_initBaseURI);
    setNotRevealedURI(_initNotRevealedUri);
  }

  // internal
  function _baseURI() internal view virtual override returns (string memory) {
    return baseURI;
  }

  // public
  function mint(uint256 _mintAmount) public payable {
    uint256 supply = totalSupply();
    require(!paused);
    require(_mintAmount > 0);
    require(_mintAmount <= maxMintAmount);
    require(supply + _mintAmount <= maxSupply);

    if (msg.sender != owner()) {
      require(msg.value >= cost * _mintAmount);
    }

    for (uint256 i = 1; i <= _mintAmount; i++) {
      _safeMint(msg.sender, supply + i);
    }
  }

  function walletOfOwner(address _owner)
    public
    view
    returns (uint256[] memory)
  {
    uint256 ownerTokenCount = balanceOf(_owner);
    uint256[] memory tokenIds = new uint256[](ownerTokenCount);
    for (uint256 i; i < ownerTokenCount; i++) {
      tokenIds[i] = tokenOfOwnerByIndex(_owner, i);
    }
    return tokenIds;
  }

  function tokenURI(uint256 tokenId)
    public
    view
    virtual
    override
    returns (string memory)
  {
    require(
      _exists(tokenId),
      "ERC721Metadata: URI query for nonexistent token"
    );
    
    if(revealed == false) {
        return notRevealedUri;
    }

    string memory currentBaseURI = _baseURI();
    return bytes(currentBaseURI).length > 0
        ? string(abi.encodePacked(currentBaseURI, tokenId.toString(), baseExtension))
        : "";
  }

  //only owner
  function reveal() public onlyOwner {
      revealed = true;
  }
  
  function setCost(uint256 _newCost) public onlyOwner {
    cost = _newCost;
  }

  function setmaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner {
    maxMintAmount = _newmaxMintAmount;
  }
  
  function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
    notRevealedUri = _notRevealedURI;
  }

  function setBaseURI(string memory _newBaseURI) public onlyOwner {
    baseURI = _newBaseURI;
  }

  function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
    baseExtension = _newBaseExtension;
  }

  function pause(bool _state) public onlyOwner {
    paused = _state;
  }
 
  function withdraw() public payable onlyOwner {
    
    (bool os, ) = payable(owner()).call{value: address(this).balance}("");
    require(os);
   
  }
}

Step 6) Finally you need to type this command in the terminal

npm thirdweb deploy

Now , it will provide you a link which you can click and then it will redirect to thirdweb website from where you can choose your preferred blockchain and click deploy button and then thirdweb will automatically deploy your erc721 nft smart contract

hope you have understood all the process clearly and if you get any doubts comment below

Also , this video will help you to undertstand better while executing all the steps

My previous posts :

how do I launch nft collection in solana using sugar cli Best Websites for custom made nft minting dapp and smart contract for free How to Create google ads for nft collection

Related Posts

Leave a Reply

Your email address will not be published.