diff --git a/contracts/Land.sol b/contracts/Land.sol index 3a1c85f..927b179 100644 --- a/contracts/Land.sol +++ b/contracts/Land.sol @@ -33,7 +33,7 @@ contract LandNFT is ERC721, ERC721Enumerable, ERC721URIStorage, Ownable { { uint256 tokenId = _tokenIdCounter.current(); _tokenIdCounter.increment(); - _mint(msg.sender, tokenId); + _safeMint(msg.sender, tokenId); _setTokenURI(tokenId, uri); createLand(tokenId, price); @@ -45,13 +45,11 @@ contract LandNFT is ERC721, ERC721Enumerable, ERC721URIStorage, Ownable { require(price > 0, "Price must be at least 1 wei"); lands[tokenId] = Land( tokenId, + payable(address(0)), payable(msg.sender), - payable(address(this)), price, false ); - - _transfer(msg.sender, address(this), tokenId); } function buyLand(uint256 tokenId) public payable { @@ -81,6 +79,18 @@ contract LandNFT is ERC721, ERC721Enumerable, ERC721URIStorage, Ownable { _transfer(msg.sender, address(this), tokenId); } + function unSellLand(uint256 tokenId) public payable { + require( + lands[tokenId].seller == msg.sender, + "Only item owner can perform this operation" + ); + lands[tokenId].owner = payable(msg.sender); + lands[tokenId].sold = true; + lands[tokenId].seller = payable(address(0)); + + _transfer(address(this), msg.sender, tokenId); + } + function getLand(uint256 tokenId) public view returns (Land memory) { return lands[tokenId]; } @@ -89,7 +99,6 @@ contract LandNFT is ERC721, ERC721Enumerable, ERC721URIStorage, Ownable { return _tokenIdCounter.current(); } - // The following functions are overrides required by Solidity. function _beforeTokenTransfer( diff --git a/src/components/nfts.js b/src/components/nfts.js index 679f4bd..0725488 100644 --- a/src/components/nfts.js +++ b/src/components/nfts.js @@ -7,6 +7,7 @@ import { buyLand, fetchNftContractOwner, sellLand, + unSellLand } from "../utils/minter"; import { Badge, Card, Stack, Row, Col } from "react-bootstrap"; import { useContractKit } from "@celo-tools/use-contractkit"; @@ -89,6 +90,20 @@ const Nft = ({ minterContract }) => { } }; + const unSell = async (index) => { + try { + setLoading(true); + await unSellLand(minterContract, index, performActions); + toast(); + getAssets(); + } catch (error) { + console.log(error); + toast(); + } finally { + setLoading(false); + } + }; + useEffect(() => { try { if (address && minterContract) { @@ -136,6 +151,9 @@ const Nft = ({ minterContract }) => {
+ {parseInt(_nft.seller.slice(0, 24)) !== 0 ? +

Seller: { truncateAddress(_nft.seller) }

: <> + }

{_nft.address}

{_nft.description}

@@ -154,16 +172,28 @@ const Nft = ({ minterContract }) => {
- {!_nft.sold ? ( - + {(parseInt(_nft.seller.slice(0, 24)) !== 0) && !_nft.sold ? ( + (_nft.seller === defaultAccount) ? ( + + ) : ( + + ) ) : defaultAccount === _nft.owner ? (