Created
October 29, 2021 10:20
-
-
Save omeganoob/659b258f8c072e5b2b986c1dfb00051c to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.7; | |
import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; | |
import "@openzeppelin/contracts/utils/Counters.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; | |
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol"; | |
import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; | |
contract MedChainMarket is ReentrancyGuard{ | |
using Counters for Counters.Counter; | |
Counters.Counter private _itemIds; | |
Counters.Counter private _itemsSold; | |
Counters.Counter private _transactionIds; | |
address payable owner; | |
constructor(){ | |
owner = payable(msg.sender); | |
} | |
struct MarketItem{ | |
uint itemId; | |
address nftContract; | |
uint256 tokenId; | |
address payable seller; | |
address payable owner; | |
uint256 price; | |
bool sold; | |
} | |
struct Transaction{ | |
uint transId; | |
address nftContract; | |
uint256 tokenId; | |
address payable seller; | |
address payable owner; | |
uint256 price; | |
} | |
mapping(uint256 => MarketItem) private idToMarketItem; | |
mapping(uint256 => Transaction) private idToTransaction; | |
event MarketItemCreated( | |
uint indexed itemID, | |
address indexed nftContract, | |
uint256 indexed tokenId, | |
address seller, | |
address owner, | |
uint256 price, | |
bool sold | |
); | |
event TransactionCreated( | |
uint indexed transID, | |
address indexed nftContract, | |
uint256 indexed tokenId, | |
address seller, | |
address owner, | |
uint256 price | |
); | |
function createMarketItem( | |
address nftContract, | |
uint256 tokenId, | |
uint256 price | |
) public payable nonReentrant returns (MarketItem memory){ | |
require(price > 0, "Price must be at least 1 wei"); | |
_itemIds.increment(); | |
uint256 itemId = _itemIds.current(); | |
idToMarketItem[itemId] = MarketItem( | |
itemId, | |
nftContract, | |
tokenId, | |
payable(msg.sender), | |
payable(address(0)), | |
price, | |
false | |
); | |
IERC721(nftContract).transferFrom(msg.sender, address(this), tokenId); | |
emit MarketItemCreated( | |
itemId, | |
nftContract, | |
tokenId, | |
msg.sender, | |
address(0), | |
price, | |
false | |
); | |
return idToMarketItem[itemId]; | |
} | |
function createMarketSale(address nftContract, uint256 itemId) | |
public payable nonReentrant{ | |
_transactionIds.increment(); | |
uint256 transactionid = _transactionIds.current(); | |
uint price = idToMarketItem[itemId].price; | |
uint tokenId = idToMarketItem[itemId].tokenId; | |
require(msg.value == price, "Please submit the asking price in order to complete the purchase"); | |
idToMarketItem[itemId].seller.transfer(msg.value); | |
IERC721(nftContract).transferFrom(address(this), msg.sender, tokenId); | |
idToMarketItem[itemId].owner = payable(msg.sender); | |
idToMarketItem[itemId].sold = true; | |
_itemsSold.increment(); | |
payable(owner).transfer(price); | |
idToTransaction[transactionid] = Transaction( | |
transactionid, | |
nftContract, | |
tokenId, | |
idToMarketItem[itemId].seller, | |
idToMarketItem[itemId].owner, | |
price | |
); | |
emit TransactionCreated( | |
transactionid, | |
nftContract, | |
tokenId, | |
idToMarketItem[itemId].seller, | |
idToMarketItem[itemId].owner, | |
price | |
); | |
} | |
function fetchMarketItems() | |
public view returns (MarketItem[] memory){ | |
uint itemCount = _itemIds.current(); | |
uint unsoldItemCount = _itemIds.current() - _itemsSold.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 = idToMarketItem[i + 1].itemId; | |
MarketItem storage currentItem = idToMarketItem[currentId]; | |
items[currentIndex] = currentItem; | |
currentIndex += 1; | |
} | |
} | |
return items; | |
} | |
function fetchTransaction() | |
public view returns (Transaction[] memory){ | |
uint soldItemCount = _itemsSold.current(); | |
uint currentIndex = 0; | |
Transaction[] memory transactions = new Transaction[](soldItemCount); | |
for(uint i = 0; i < soldItemCount; i++){ | |
uint currentId = idToTransaction[i + 1].transId; | |
Transaction storage transaction = idToTransaction[currentId]; | |
transactions[currentIndex] = transaction; | |
currentIndex += 1; | |
} | |
return transactions; | |
} | |
function fetchMyNFTs() | |
public view returns (MarketItem[] memory){ | |
uint totalItemCount = _itemIds.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 = idToMarketItem[i + 1].itemId; | |
MarketItem storage currentItem = idToMarketItem[currentId]; | |
items[currentIndex] = currentItem; | |
currentIndex += 1; | |
} | |
} | |
return items; | |
} | |
function fetchItemsCreated() | |
public view returns (MarketItem[] memory){ | |
uint totalItemCount = _itemIds.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 = idToMarketItem[i + 1].itemId; | |
MarketItem storage currentItem = idToMarketItem[currentId]; | |
items[currentIndex] = currentItem; | |
currentIndex += 1; | |
} | |
} | |
return items; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment