Skip to content

Commit

Permalink
Update ELEN_E6883_NFT.sol
Browse files Browse the repository at this point in the history
//*** VK added code k3jph#2 ***
  • Loading branch information
vaibhaws1 authored May 6, 2023
1 parent 2d822eb commit a4bbe2a
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions contracts/ELEN_E6883_NFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,73 @@ contract ELEN_E6883_NFT is ERC721URIStorage, Ownable {
_mintSingleNFT();
}
}

//*** VK added code #2 ***
function getOwner (uint256 tokenId) public returns (address owner){
emit nftdetails ( msg.sender, tokenId);
return _contractOwner;
}

function setOwner (uint256 tokenId, address newAddress) public {
_contractOwner = newAddress;
emit nftdetails ( msg.sender, tokenId);
}

function createSale(uint256 tokenId, uint256 price) external {
require(nftContract.ownerOf(tokenId) == msg.sender, "Only token owner can create sale");
require(price > 0, "Price must be greater than zero");
require(tokenIdToSale[tokenId].active == false, "Token already listed for sale");

Sale memory sale = Sale({
seller: msg.sender,
tokenId: tokenId,
price: price,
active: true
});
tokenIdToSale[tokenId] = sale;
_price = sale.price;
emit SaleCreated(msg.sender, tokenId, price);
}

function getPrice (uint256 tokenId) public returns (uint256 price){
return _price;
}

function cancelSale(uint256 tokenId) external {
require(tokenIdToSale[tokenId].seller == msg.sender, "Only seller can cancel sale");
require(tokenIdToSale[tokenId].active == true, "Sale already cancelled");

delete tokenIdToSale[tokenId];

emit SaleCancelled(msg.sender, tokenId);
}

function sellNFT(uint256 tokenId) external payable {
Sale memory sale = tokenIdToSale[tokenId];
require(sale.active == true, "Sale is not active");
require(msg.value == sale.price, "Incorrect amount sent");

address seller = sale.seller;
uint256 price = sale.price;

delete tokenIdToSale[tokenId];

// Transfer the NFT to the buyer
nftContract.safeTransferFrom(seller, msg.sender, tokenId);

// Transfer the payment to the seller
(bool success,) = seller.call{value: price}("");
require(success, "Transfer failed");

emit SaleSuccessful(msg.sender, tokenId, price);
}

function delistNFT(uint256 tokenID) public {
delete _tokenIds ;
delete _contractOwner ;
}

//*** VK end code #2 ***

}

0 comments on commit a4bbe2a

Please sign in to comment.