-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNftMarketplace.sol
109 lines (95 loc) · 3.44 KB
/
NftMarketplace.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
// SPDX-License-Identifier: MIT
// Credit https://gist.github.com/dabit3/eb8866adc22bd86cabf5e6604b408e4a
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
contract NftMarketplace is ERC721URIStorage {
uint256 public listingPrice;
uint256 tokenId;
uint256 itemsListed;
struct MarketItem {
uint256 id;
address owner;
bool IsListed;
uint256 price;
}
mapping(uint256 => MarketItem) public MarketItems;
address payable public owner;
constructor(string memory _name, string memory _symbol) ERC721(_name, _symbol) {
owner = payable(msg.sender);
}
modifier onlyOwner() {
require(msg.sender == owner, "only Owner");
_;
}
function changeListingPrice(uint256 _price) external onlyOwner {
listingPrice = _price;
}
function createToken(string calldata url) external returns(uint256) {
uint256 _tokenId = tokenId;
_mint(msg.sender, _tokenId);
_setTokenURI(_tokenId, url);
MarketItem memory temp = MarketItem(_tokenId, msg.sender, false, 0);
MarketItems[_tokenId] = temp;
tokenId++;
return _tokenId;
}
function listItem(uint256 _tokenId, uint256 _price) payable external{
require(msg.sender == ownerOf(_tokenId), "only owner can list");
require(msg.value == listingPrice, "please pay platform listing price");
MarketItem memory temp = MarketItems[_tokenId];
require(temp.IsListed == false, "Already Listed");
temp.IsListed = true;
temp.price = _price;
MarketItems[_tokenId] = temp;
_transfer(msg.sender, address(this), _tokenId);
itemsListed++;
}
function removeListing(uint256 _tokenId) external {
require(msg.sender == ownerOf(_tokenId), "only owner can unlist");
MarketItem memory temp = MarketItems[_tokenId];
require(temp.IsListed == true, "Not Listed");
temp.IsListed = false;
temp.price = 0;
MarketItems[_tokenId] = temp;
_transfer(address(this), msg.sender, _tokenId);
itemsListed--;
}
function editListing(uint256 _tokenId, uint256 _price) external {
require(msg.sender == ownerOf(_tokenId), "only onwer can edit");
require(_price != 0, "call removeListing");
MarketItem memory temp = MarketItems[_tokenId];
require(temp.IsListed == true, "Not Listed");
temp.price = _price;
MarketItems[_tokenId] = temp;
}
function buy(uint256 _tokenId) external payable returns(bool, bytes memory) {
MarketItem memory temp = MarketItems[_tokenId];
require(msg.value == temp.price, "plz transfer listing price");
(bool success, bytes memory data) = payable(temp.owner).call{value:msg.value}("");
if(success == true) {
_transfer(address(this), msg.sender, _tokenId);
temp.owner = msg.sender;
temp.IsListed = false;
temp.price = 0;
MarketItems[_tokenId] = temp;
}
return (success, data);
}
function getAllListedItems() public view returns(MarketItem[] memory) {
MarketItem[] memory list = new MarketItem[](itemsListed);
for(uint256 i=0; i < tokenId; i++) {
if(MarketItems[i].IsListed == true) {
list[i] = MarketItems[i];
}
}
return list;
}
function transferFundtoPlatform(uint256 _val) external onlyOwner returns(bool success, bytes memory data) {
require(_val <= address(this).balance , "not enough fund available");
(success, data) = owner.call{value:_val}("");
}
// Never use this ! RUGPULL !!!
function destroy() external onlyOwner {
selfdestruct(owner);
}
}