My Image
CoursesQuizzesProblemsContestsSmartBooks
Contest!

No results found

LOGINREGISTER
My ProgressCoursesQuizzesProblemsContestsSmartbooks
Published on 20 Feb 2023
NFTMarketplace Smart Contract
which allows users to buy the NFT and list there already-minted NFT .
img
Rishabh Yadav
0
Like
65

also , The owner of the marketplace will get commission whenever an NFT is sold .

Note : withdraw function is for safety purpose , if some Ether got stuck in the contract then the contract owner can withdraw it.

// SPDX-License-Identifier: MIT
//By Rishabh Yadav
pragma solidity ^0.8.13;

import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract NFTmarketplace is Ownable{
    address payable public admin;

    struct Listing {
        address nftAddress;
        uint tokenId;
        uint price;
        address seller;
        bool sold;
    }
    
    uint256 public saleCommissionPercentage;
    mapping(uint => uint) tokenIdToListingId; 
    Listing[] listings;

    constructor() {
        admin = payable(msg.sender);
        saleCommissionPercentage = 10;
    }

    function listNFT(address _nftAddress , uint _tokenId , uint _price ) external {
        require(_price > 0 , "Invalid Value");
        require(msg.sender != address(0) , "Zero Address");
        require(IERC721(_nftAddress).ownerOf(_tokenId) == msg.sender , "only the owner of the nft can list it");

        IERC721(_nftAddress).transferFrom(msg.sender , address(this) , _tokenId);
        
         listings.push(Listing(_nftAddress , _tokenId , _price , msg.sender , false));
         tokenIdToListingId[_tokenId] = listings.length -1;

    }

    function buyNFT(uint _listingId) external payable {
         require(_listingId < listings.length , "Invalid listingId");

         Listing storage listing = listings[_listingId];

         require(listing.seller != msg.sender , " seller can not buy his own nft");
         require(msg.value >= listing.price , "Insufficient funds");
         require(!listing.sold  , "Already been Sold Out");
         require(msg.sender != address(0) , "Zero Address");

         listing.sold = true;
         uint256 commission = (listing.price * saleCommissionPercentage) / 100;
         admin.transfer(commission);
         payable(listing.seller).transfer(listing.price - commission);
         IERC721(listing.nftAddress).transferFrom(address(this) , msg.sender , listing.tokenId);
    }

    function getListing(uint _listingId) external view returns(Listing memory){
        return  listings[_listingId];
    }

  //In case if some amount is left in the contract
    function withdraw() external onlyOwner {
        uint bal = address(this).balance;
        admin.transfer(bal);
    }
}
Enjoyed the SmartBook?
Like
logo
contact@dapp-world.com
Katraj, Pune, Maharashtra, India - 411048

Follow Us

linkedintwitteryoutubediscordinstagram

Products

  • SmartBooks
  • Courses
  • Quizzes
  • Assessments

Support

  • Contact Us
  • FAQ
  • Privacy Policy
  • T&C

Backed By

ah! ventures

Copyright 2023 - All Rights Reserved.

Recommended from DAppWorld
img
1 May 2021
How to connect Ganache with Metamask and deploy Smart contracts on remix without
Set up your development environment with (Metamask + Ganache + Remix) and skip truffle :)
3 min read
11508
5
img
8 Jul 2021
How to interact with smart contarct from backend node js
call and send functions from backend server side using nodejs
3 min read
8103
2
img
18 Aug 2021
Send transaction with web3 using python
Introduction to web3.py and sending transaction on testnet
3 min read
6229
5
img
5 Aug 2021
Deploy Smart Contract on Polygon POS using Hardhat
how to deploy smart contracts on polygon pos chain using hardhat both mainnet and testnet ?
3 min read
5540
3