-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarketplace.sol
103 lines (85 loc) · 2.23 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "hardhat/console.sol";
contract Marketplace is ReentrancyGuard {
address payable public immutable chairPerson;
uint256 public immutable feePercent;
uint256 public itemCount;
struct Item {
uint256 id;
uint256 tokenId;
IERC721 nft;
uint256 price;
bool sold;
address payable seller;
}
mapping(uint256 => Item) public items;
event Offered(
uint256 id,
uint256 tokenId,
uint256 price,
IERC721 indexed nft,
address indexed seller
);
event Bought(
uint256 id,
uint256 tokenId,
uint256 price,
IERC721 indexed nft,
address indexed seller,
address indexed buyer
);
constructor(uint256 _feePercent) {
chairPerson = payable(msg.sender);
feePercent = _feePercent;
}
function createOffer(
IERC721 _nft,
uint256 _tokenId,
uint256 _price
) external nonReentrant {
require(_price > 0, "Price must be greater than 0");
itemCount++;
_nft.transferFrom(msg.sender, address(this), _tokenId);
items[itemCount] = Item(
itemCount,
_tokenId,
_nft,
_price,
false,
payable(msg.sender)
);
emit Offered(itemCount, _tokenId, _price, _nft, msg.sender);
}
function purchaseItem(uint256 _id) external payable nonReentrant {
uint256 totalPrice = getTotalPrice(_id);
Item storage item = items[_id];
require(item.id > 0 && item.id <= itemCount, "Item does not exist");
require(!item.sold, "Item is already sold");
require(
msg.value >= totalPrice,
"not enough ether to cover item price and market fee"
);
payable(item.seller).transfer(item.price);
chairPerson.transfer(totalPrice - item.price);
item.sold = true;
item.nft.transferFrom(address(this), msg.sender, item.tokenId);
emit Bought(
_id,
item.tokenId,
item.price,
item.nft,
item.seller,
msg.sender
);
}
function getTotalPrice(uint256 _itemId)
public
view
returns (uint256 totalPrice)
{
totalPrice = ((items[_itemId].price * (100 + feePercent)) / 100);
}
}