-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarketPlace.sol
129 lines (97 loc) · 3.59 KB
/
MarketPlace.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract NFTMarketplace is ERC721 {
// Incremental ID for minting NFTs
uint256 private _nextTokenId = 1;
address private owner;
bool private paused;
// Struct to represent each item for sale
struct NFTItem {
uint256 tokenId;
address owner;
uint256 price;
bool isForSale;
}
// Mapping of tokenId to NFTItem
mapping(uint256 => NFTItem) public nftItems;
// Event emitted when a new item is created
event NFTCreated(uint256 indexed tokenId, address owner, uint256 price);
// Event emitted when an item is bought
event NFTBought(uint256 indexed tokenId, address buyer, uint256 price);
constructor() ERC721("MyNFT", "MNFT") {}
// Function to create a new NFT
function createNFT(uint256 price) public returns (uint256) {
require(price > 0, "Price must be greater than zero");
uint256 newItemId = _nextTokenId;
// Mint the NFT
_mint(msg.sender, newItemId);
// Add the NFT to the marketplace
nftItems[newItemId] = NFTItem({
tokenId: newItemId,
owner: msg.sender,
price: price,
isForSale: true
});
emit NFTCreated(newItemId, msg.sender, price);
// Increment the token ID for the next NFT
_nextTokenId++;
return newItemId;
}
// Function to buy an NFT
function buyNFT(uint256 tokenId) public payable {
NFTItem storage item = nftItems[tokenId];
require(item.isForSale, "This NFT is not for sale");
require(msg.value >= item.price, "Insufficient funds to buy this NFT");
address previousOwner = item.owner;
address newOwner = msg.sender;
// Transfer ownership of the NFT
_transfer(previousOwner, newOwner, tokenId);
// Update the NFT item
item.owner = newOwner;
item.isForSale = false;
// Transfer the funds to the previous owner
payable(previousOwner).transfer(msg.value);
emit NFTBought(tokenId, newOwner, item.price);
}
// Function to list an NFT for sale
function listNFTForSale(uint256 tokenId, uint256 price) public {
require(ownerOf(tokenId) == msg.sender, "Only the owner can list this NFT");
require(price > 0, "Price must be greater than zero");
NFTItem storage item = nftItems[tokenId];
item.isForSale = true;
item.price = price;
}
// Function to cancel the sale of an NFT
function cancelNFTSale(uint256 tokenId) public {
require(ownerOf(tokenId) == msg.sender, "Only the owner can cancel this sale");
NFTItem storage item = nftItems[tokenId];
item.isForSale = false;
//Emergency STOP Design Pattern
}
modifier onlyOwner() {
require(msg.sender == owner, "You are not the owner");
_;
}
modifier whenPaused() {
require(paused, "The contract is not paused");
_;
}
event Paused();
event Unpaused();
event FundsWithdrawn(address owner, uint amount);
function pause() public onlyOwner {
paused = true;
emit Paused();
}
function unpause() public onlyOwner {
paused = false;
emit Unpaused();
}
function emergencyWithdraw() public onlyOwner whenPaused {
uint balance = address(this).balance;
(bool sent, ) = owner.call{value: balance}("");
require(sent, "Failed to send Ether");
emit FundsWithdrawn(owner, balance);
}
}