Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# my-nft-project
Thanks to dacade, after finishing the tutorial I was able to come up with this basic nft market place. your feedbacks would be much appreciated


An NFTMarketplace where users can mint NFTs to be listed for sale, for others to buy. New buyers can relist the NFTs.
44 changes: 23 additions & 21 deletions pro.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ contract NFTMarketplace is ERC721URIStorage {
//The structure to store info about a listed token
struct ListedToken {
uint256 tokenId;
address payable owner;
address payable seller;
address owner;
address seller;
uint256 price;
bool currentlyListed;
}
Expand Down Expand Up @@ -78,22 +78,23 @@ contract NFTMarketplace is ERC721URIStorage {
_setTokenURI(newTokenId, tokenURI);

//Helper function to update Global variables and emit an event
createListedToken(newTokenId, price);
listToken(newTokenId, price);

return newTokenId;
}

function createListedToken(uint256 tokenId, uint256 price) private {
//Make sure the sender sent enough ETH to pay for listing
require(msg.value == listPrice, "Hopefully sending the correct price");
//Just sanity check
require(price > 0, "Make sure the price isn't negative");

modifier canListToken(uint256 tokenId, uint256 price){
require(ownerOf(tokenId) == msg.sender, "Cannot list token not owned by you");
require(msg.value == listPrice, "Attach the correct slisting Fee");
require(price > 0, "Price must be greater than 0");
_;
}
function listToken(uint256 tokenId, uint256 price) public payable canListToken(tokenId, price) {
//Update the mapping of tokenId's to Token details, useful for retrieval functions
idToListedToken[tokenId] = ListedToken(
tokenId,
payable(address(this)),
payable(msg.sender),
msg.sender,
msg.sender,
price,
true
);
Expand All @@ -102,7 +103,7 @@ contract NFTMarketplace is ERC721URIStorage {
//Emit the event for successful transfer. The frontend parses this message and updates the end user
emit TokenListedSuccess(
tokenId,
address(this),
msg.sender,
msg.sender,
price,
true
Expand Down Expand Up @@ -137,15 +138,15 @@ contract NFTMarketplace is ERC721URIStorage {
//Important to get a count of all the NFTs that belong to the user before we can make an array for them
for(uint i=0; i < totalItemCount; i++)
{
if(idToListedToken[i+1].owner == msg.sender || idToListedToken[i+1].seller == msg.sender){
if(idToListedToken[i+1].owner == msg.sender){
itemCount += 1;
}
}

//Once you have the count of relevant NFTs, create an array then store all the NFTs in it
ListedToken[] memory items = new ListedToken[](itemCount);
for(uint i=0; i < totalItemCount; i++) {
if(idToListedToken[i+1].owner == msg.sender || idToListedToken[i+1].seller == msg.sender) {
if(idToListedToken[i+1].owner == msg.sender) {
uint currentId = i+1;
ListedToken storage currentItem = idToListedToken[currentId];
items[currentIndex] = currentItem;
Expand All @@ -160,9 +161,10 @@ contract NFTMarketplace is ERC721URIStorage {
address seller = idToListedToken[tokenId].seller;
require(msg.value == price, "Please submit the asking price in order to complete the purchase");

//update the details of the token
idToListedToken[tokenId].currentlyListed = true;
idToListedToken[tokenId].seller = payable(msg.sender);
//update the details of the token - change owner to the buyer and seller to 0 account
idToListedToken[tokenId].currentlyListed = false;
idToListedToken[tokenId].owner = payable(msg.sender);
idToListedToken[tokenId].seller = address(0);
_itemsSold.increment();

//Actually transfer the token to the new owner
Expand All @@ -171,13 +173,13 @@ contract NFTMarketplace is ERC721URIStorage {
approve(address(this), tokenId);

//Transfer the listing fee to the marketplace creator
payable(owner).transfer(listPrice);
//Transfer the proceeds from the sale to the seller of the NFT
payable(seller).transfer(msg.value);
(bool sentListPrice, ) = payable(owner).call{value: listPrice}("");
//Transfer the proceeds from the sale to the seller of the NFT
(bool sentSalesValue, ) = payable(seller).call{value: msg.value}("");
}

//We might add a resell token function in the future
//In that case, tokens won't be listed by default but users can send a request to actually list a token
//Currently NFTs are listed by default
}