Skip to content

Instantly share code, notes, and snippets.

@Warkanlock
Forked from dabit3/marketplace.sol
Created February 26, 2022 04:05
Show Gist options
  • Save Warkanlock/d8bdd0f96aa7fa214fcb4bf800dea5b8 to your computer and use it in GitHub Desktop.
Save Warkanlock/d8bdd0f96aa7fa214fcb4bf800dea5b8 to your computer and use it in GitHub Desktop.

Revisions

  1. @dabit3 dabit3 revised this gist Feb 26, 2022. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions marketplace.sol
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ import "hardhat/console.sol";
    contract NFTMarketplace is ERC721URIStorage {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;
    Counters.Counter private _itemsForSale;
    Counters.Counter private _itemsSold;

    uint256 listingPrice = 0.025 ether;
    address payable owner;
    @@ -69,7 +69,6 @@ contract NFTMarketplace is ERC721URIStorage {
    false
    );

    _itemsForSale.increment();
    _transfer(msg.sender, address(this), tokenId);
    emit MarketItemCreated(
    tokenId,
    @@ -87,7 +86,7 @@ contract NFTMarketplace is ERC721URIStorage {
    idToMarketItem[tokenId].price = price;
    idToMarketItem[tokenId].seller = payable(msg.sender);
    idToMarketItem[tokenId].owner = payable(address(this));
    _itemsForSale.increment();
    _itemsSold.decrement();

    _transfer(msg.sender, address(this), tokenId);
    }
    @@ -101,7 +100,7 @@ contract NFTMarketplace is ERC721URIStorage {
    require(msg.value == price, "Please submit the asking price in order to complete the purchase");
    idToMarketItem[tokenId].owner = payable(msg.sender);
    idToMarketItem[tokenId].sold = true;
    _itemsForSale.decrement();
    _itemsSold.increment();
    _transfer(address(this), msg.sender, tokenId);
    payable(owner).transfer(listingPrice);
    payable(idToMarketItem[tokenId].seller).transfer(msg.value);
    @@ -110,7 +109,7 @@ contract NFTMarketplace is ERC721URIStorage {
    /* Returns all unsold market items */
    function fetchMarketItems() public view returns (MarketItem[] memory) {
    uint itemCount = _tokenIds.current();
    uint unsoldItemCount = _tokenIds.current() - _itemsForSale.current();
    uint unsoldItemCount = _tokenIds.current() - _itemsSold.current();
    uint currentIndex = 0;

    MarketItem[] memory items = new MarketItem[](unsoldItemCount);
  2. @dabit3 dabit3 revised this gist Feb 26, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion marketplace.sol
    Original file line number Diff line number Diff line change
    @@ -69,7 +69,7 @@ contract NFTMarketplace is ERC721URIStorage {
    false
    );

    _itemsForSale.increment()
    _itemsForSale.increment();
    _transfer(msg.sender, address(this), tokenId);
    emit MarketItemCreated(
    tokenId,
  3. @dabit3 dabit3 revised this gist Feb 26, 2022. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions marketplace.sol
    Original file line number Diff line number Diff line change
    @@ -69,6 +69,7 @@ contract NFTMarketplace is ERC721URIStorage {
    false
    );

    _itemsForSale.increment()
    _transfer(msg.sender, address(this), tokenId);
    emit MarketItemCreated(
    tokenId,
    @@ -86,7 +87,7 @@ contract NFTMarketplace is ERC721URIStorage {
    idToMarketItem[tokenId].price = price;
    idToMarketItem[tokenId].seller = payable(msg.sender);
    idToMarketItem[tokenId].owner = payable(address(this));
    _itemsForSale.decrement();
    _itemsForSale.increment();

    _transfer(msg.sender, address(this), tokenId);
    }
    @@ -100,7 +101,7 @@ contract NFTMarketplace is ERC721URIStorage {
    require(msg.value == price, "Please submit the asking price in order to complete the purchase");
    idToMarketItem[tokenId].owner = payable(msg.sender);
    idToMarketItem[tokenId].sold = true;
    _itemsForSale.increment();
    _itemsForSale.decrement();
    _transfer(address(this), msg.sender, tokenId);
    payable(owner).transfer(listingPrice);
    payable(idToMarketItem[tokenId].seller).transfer(msg.value);
  4. @dabit3 dabit3 revised this gist Feb 26, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions marketplace.sol
    Original file line number Diff line number Diff line change
    @@ -148,8 +148,8 @@ contract NFTMarketplace is ERC721URIStorage {
    return items;
    }

    /* Returns only items a user has created */
    function fetchItemsCreated() public view returns (MarketItem[] memory) {
    /* Returns only items a user has listed */
    function fetchItemsListed() public view returns (MarketItem[] memory) {
    uint totalItemCount = _tokenIds.current();
    uint itemCount = 0;
    uint currentIndex = 0;
  5. @dabit3 dabit3 revised this gist Feb 26, 2022. No changes.
  6. @dabit3 dabit3 revised this gist Feb 25, 2022. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions marketplace.sol
    Original file line number Diff line number Diff line change
    @@ -98,13 +98,12 @@ contract NFTMarketplace is ERC721URIStorage {
    ) public payable {
    uint price = idToMarketItem[tokenId].price;
    require(msg.value == price, "Please submit the asking price in order to complete the purchase");

    payable(idToMarketItem[tokenId].seller).transfer(msg.value);
    _transfer(address(this), msg.sender, tokenId);
    idToMarketItem[tokenId].owner = payable(msg.sender);
    idToMarketItem[tokenId].sold = true;
    _itemsForSale.increment();
    _transfer(address(this), msg.sender, tokenId);
    payable(owner).transfer(listingPrice);
    payable(idToMarketItem[tokenId].seller).transfer(msg.value);
    }

    /* Returns all unsold market items */
  7. @dabit3 dabit3 revised this gist Feb 25, 2022. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions marketplace.sol
    Original file line number Diff line number Diff line change
    @@ -5,11 +5,10 @@ pragma solidity ^0.8.3;
    import "@openzeppelin/contracts/utils/Counters.sol";
    import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
    import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
    import "@openzeppelin/contracts/security/ReentrancyGuard.sol";

    import "hardhat/console.sol";

    contract NFTMarketplace is ERC721URIStorage, ReentrancyGuard {
    contract NFTMarketplace is ERC721URIStorage {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;
    Counters.Counter private _itemsForSale;
    @@ -58,7 +57,7 @@ contract NFTMarketplace is ERC721URIStorage, ReentrancyGuard {
    function createMarketItem(
    uint256 tokenId,
    uint256 price
    ) private nonReentrant {
    ) private {
    require(price > 0, "Price must be at least 1 wei");
    require(msg.value == listingPrice, "Price must be equal to listing price");

    @@ -80,7 +79,7 @@ contract NFTMarketplace is ERC721URIStorage, ReentrancyGuard {
    );
    }

    function resellToken(uint256 tokenId, uint256 price) public payable nonReentrant {
    function resellToken(uint256 tokenId, uint256 price) public payable {
    require(idToMarketItem[tokenId].owner == msg.sender, "Only item owner can perform this operation");
    require(msg.value == listingPrice, "Price must be equal to listing price");
    idToMarketItem[tokenId].sold = false;
    @@ -96,7 +95,7 @@ contract NFTMarketplace is ERC721URIStorage, ReentrancyGuard {
    /* Transfers ownership of the item, as well as funds between parties */
    function createMarketSale(
    uint256 tokenId
    ) public payable nonReentrant {
    ) public payable {
    uint price = idToMarketItem[tokenId].price;
    require(msg.value == price, "Please submit the asking price in order to complete the purchase");

  8. @dabit3 dabit3 created this gist Feb 25, 2022.
    177 changes: 177 additions & 0 deletions marketplace.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,177 @@
    // SPDX-License-Identifier: MIT

    pragma solidity ^0.8.3;

    import "@openzeppelin/contracts/utils/Counters.sol";
    import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
    import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
    import "@openzeppelin/contracts/security/ReentrancyGuard.sol";

    import "hardhat/console.sol";

    contract NFTMarketplace is ERC721URIStorage, ReentrancyGuard {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;
    Counters.Counter private _itemsForSale;

    uint256 listingPrice = 0.025 ether;
    address payable owner;

    constructor() ERC721("Metaverse Tokens", "METT") {
    owner = payable(msg.sender);
    }

    function createToken(string memory tokenURI, uint256 price) public payable returns (uint) {
    _tokenIds.increment();
    uint256 newTokenId = _tokenIds.current();

    _mint(msg.sender, newTokenId);
    _setTokenURI(newTokenId, tokenURI);
    createMarketItem(newTokenId, price);
    return newTokenId;
    }

    mapping(uint256 => MarketItem) private idToMarketItem;

    struct MarketItem {
    uint256 tokenId;
    address payable seller;
    address payable owner;
    uint256 price;
    bool sold;
    }

    event MarketItemCreated (
    uint256 indexed tokenId,
    address seller,
    address owner,
    uint256 price,
    bool sold
    );

    /* Returns the listing price of the contract */
    function getListingPrice() public view returns (uint256) {
    return listingPrice;
    }

    /* Places an item for sale on the marketplace */
    function createMarketItem(
    uint256 tokenId,
    uint256 price
    ) private nonReentrant {
    require(price > 0, "Price must be at least 1 wei");
    require(msg.value == listingPrice, "Price must be equal to listing price");

    idToMarketItem[tokenId] = MarketItem(
    tokenId,
    payable(msg.sender),
    payable(address(0)),
    price,
    false
    );

    _transfer(msg.sender, address(this), tokenId);
    emit MarketItemCreated(
    tokenId,
    msg.sender,
    address(0),
    price,
    false
    );
    }

    function resellToken(uint256 tokenId, uint256 price) public payable nonReentrant {
    require(idToMarketItem[tokenId].owner == msg.sender, "Only item owner can perform this operation");
    require(msg.value == listingPrice, "Price must be equal to listing price");
    idToMarketItem[tokenId].sold = false;
    idToMarketItem[tokenId].price = price;
    idToMarketItem[tokenId].seller = payable(msg.sender);
    idToMarketItem[tokenId].owner = payable(address(this));
    _itemsForSale.decrement();

    _transfer(msg.sender, address(this), tokenId);
    }

    /* Creates the sale of a marketplace item */
    /* Transfers ownership of the item, as well as funds between parties */
    function createMarketSale(
    uint256 tokenId
    ) public payable nonReentrant {
    uint price = idToMarketItem[tokenId].price;
    require(msg.value == price, "Please submit the asking price in order to complete the purchase");

    payable(idToMarketItem[tokenId].seller).transfer(msg.value);
    _transfer(address(this), msg.sender, tokenId);
    idToMarketItem[tokenId].owner = payable(msg.sender);
    idToMarketItem[tokenId].sold = true;
    _itemsForSale.increment();
    payable(owner).transfer(listingPrice);
    }

    /* Returns all unsold market items */
    function fetchMarketItems() public view returns (MarketItem[] memory) {
    uint itemCount = _tokenIds.current();
    uint unsoldItemCount = _tokenIds.current() - _itemsForSale.current();
    uint currentIndex = 0;

    MarketItem[] memory items = new MarketItem[](unsoldItemCount);
    for (uint i = 0; i < itemCount; i++) {
    if (idToMarketItem[i + 1].owner == address(0)) {
    uint currentId = i + 1;
    MarketItem storage currentItem = idToMarketItem[currentId];
    items[currentIndex] = currentItem;
    currentIndex += 1;
    }
    }
    return items;
    }

    /* Returns only items that a user has purchased */
    function fetchMyNFTs() public view returns (MarketItem[] memory) {
    uint totalItemCount = _tokenIds.current();
    uint itemCount = 0;
    uint currentIndex = 0;

    for (uint i = 0; i < totalItemCount; i++) {
    if (idToMarketItem[i + 1].owner == msg.sender) {
    itemCount += 1;
    }
    }

    MarketItem[] memory items = new MarketItem[](itemCount);
    for (uint i = 0; i < totalItemCount; i++) {
    if (idToMarketItem[i + 1].owner == msg.sender) {
    uint currentId = i + 1;
    MarketItem storage currentItem = idToMarketItem[currentId];
    items[currentIndex] = currentItem;
    currentIndex += 1;
    }
    }
    return items;
    }

    /* Returns only items a user has created */
    function fetchItemsCreated() public view returns (MarketItem[] memory) {
    uint totalItemCount = _tokenIds.current();
    uint itemCount = 0;
    uint currentIndex = 0;

    for (uint i = 0; i < totalItemCount; i++) {
    if (idToMarketItem[i + 1].seller == msg.sender) {
    itemCount += 1;
    }
    }

    MarketItem[] memory items = new MarketItem[](itemCount);
    for (uint i = 0; i < totalItemCount; i++) {
    if (idToMarketItem[i + 1].seller == msg.sender) {
    uint currentId = i + 1;
    MarketItem storage currentItem = idToMarketItem[currentId];
    items[currentIndex] = currentItem;
    currentIndex += 1;
    }
    }
    return items;
    }

    }