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
19 changes: 14 additions & 5 deletions contracts/Land.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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];
}
Expand All @@ -89,7 +99,6 @@ contract LandNFT is ERC721, ERC721Enumerable, ERC721URIStorage, Ownable {
return _tokenIdCounter.current();
}


// The following functions are overrides required by Solidity.

function _beforeTokenTransfer(
Expand Down
50 changes: 40 additions & 10 deletions src/components/nfts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -89,6 +90,20 @@ const Nft = ({ minterContract }) => {
}
};

const unSell = async (index) => {
try {
setLoading(true);
await unSellLand(minterContract, index, performActions);
toast(<NotificationSuccess text="Updating NFT list...." />);
getAssets();
} catch (error) {
console.log(error);
toast(<NotificationError text="Failed to send NFT." />);
} finally {
setLoading(false);
}
};

useEffect(() => {
try {
if (address && minterContract) {
Expand Down Expand Up @@ -136,6 +151,9 @@ const Nft = ({ minterContract }) => {
</Card.Header>
<img src={_nft.image} alt="" />
<div className="card-body">
{parseInt(_nft.seller.slice(0, 24)) !== 0 ?
<p className="card-text">Seller: { truncateAddress(_nft.seller) }</p> : <></>
}
<p className="card-text">{_nft.address}</p>
<p className="card-text">{_nft.description}</p>
<Row className="mt-2 mb-2">
Expand All @@ -154,16 +172,28 @@ const Nft = ({ minterContract }) => {
</Row>
<div className="d-flex justify-content-between align-items-center">
<div className="btn-group">
{!_nft.sold ? (
<button
type="button"
className="btn btn-outline-primary"
onClick={() =>
buy(_nft.index, _nft.tokenId)
}
>
Buy
</button>
{(parseInt(_nft.seller.slice(0, 24)) !== 0) && !_nft.sold ? (
(_nft.seller === defaultAccount) ? (
<button
type="button"
className="btn btn-outline-primary"
onClick={() =>
unSell(_nft.index, _nft.tokenId)
}
>
Cancel Sale
</button>
) : (
<button
type="button"
className="btn btn-outline-primary"
onClick={() =>
buy(_nft.index, _nft.tokenId)
}
>
Buy
</button>
)
) : defaultAccount === _nft.owner ? (
<button
type="button"
Expand Down
17 changes: 15 additions & 2 deletions src/contracts/Land.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/contracts/LandAddress.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"Land": "0xba51090402c850758e8cB3B5984590379FE972f4"
"Land": "0x92E1d77F3282bA513D8411a13541606134cE9Ea0"
}
14 changes: 14 additions & 0 deletions src/utils/minter.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const getLands = async (minterContract) => {
index: i,
tokenId: i,
owner,
seller: land.seller,
price: land.price,
sold: land.sold,
address: meta.data.address,
Expand Down Expand Up @@ -151,3 +152,16 @@ export const sellLand = async (minterContract, tokenId, performActions) => {
console.log({ error });
}
};

export const unSellLand = async (minterContract, tokenId, performActions) => {
try {
await performActions(async (kit) => {
const { defaultAccount } = kit;
await minterContract.methods
.unSellLand(tokenId)
.send({ from: defaultAccount });
});
} catch (error) {
console.log({ error });
}
};