2
2
3
3
pragma solidity ^ 0.8.20 ;
4
4
5
- // import "@openzeppelin/contracts/interfaces/IERC721Receiver.sol";
6
5
import "@openzeppelin/contracts/interfaces/IERC721.sol " ;
7
6
import "@openzeppelin/contracts/interfaces/IERC20.sol " ;
8
7
@@ -21,14 +20,30 @@ contract Market {
21
20
uint256 public currentOrderId;
22
21
mapping (uint256 => NFTListing) public listings;
23
22
23
+ // Event declarations
24
+ event NFTListed (uint256 indexed orderId , address indexed nftAddress , uint256 tokenId , uint256 price );
25
+ event NFTUnlisted (uint256 indexed orderId );
26
+ event NFTPriceChanged (uint256 indexed orderId , uint256 newPrice );
27
+ event NFTBought (uint256 indexed orderId , address indexed buyer , uint256 price );
28
+
24
29
constructor (IERC20 _erc20 , IERC721 _erc721 ) {
25
30
erc20 = _erc20;
26
31
erc721 = _erc721;
27
32
}
28
33
29
- function listNFTToMarket (IERC721 _nft , uint256 _tokenId , uint256 _price ) public {
30
- require (_nft.ownerOf (_tokenId) == msg .sender , "You are not the owner of this NFT " );
31
- require (_nft.getApproved (_tokenId) == address (this ), "Token is not approved for the market " );
34
+ function listNFTToMarket (
35
+ IERC721 _nft ,
36
+ uint256 _tokenId ,
37
+ uint256 _price
38
+ ) public {
39
+ require (
40
+ _nft.ownerOf (_tokenId) == msg .sender ,
41
+ "You are not the owner of this NFT "
42
+ );
43
+ require (
44
+ _nft.getApproved (_tokenId) == address (this ),
45
+ "Token is not approved for the market "
46
+ );
32
47
33
48
NFTListing memory listing;
34
49
listing.nftAddress = _nft;
@@ -37,17 +52,32 @@ contract Market {
37
52
listing.isListed = true ;
38
53
39
54
listings[currentOrderId] = listing;
55
+
56
+ emit NFTListed (
57
+ currentOrderId,
58
+ address (_nft),
59
+ _tokenId,
60
+ _price
61
+ );
62
+
40
63
currentOrderId++ ;
41
64
}
42
65
43
66
function unlistNFTFromMarket (uint256 _orderId ) external {
44
67
require (listings[_orderId].nftAddress.ownerOf (listings[_orderId].tokenId) == msg .sender , "You are not the owner of this NFT " );
45
68
listings[_orderId].isListed = false ;
69
+
70
+ emit NFTUnlisted (_orderId);
46
71
}
47
72
48
- function changeNFTPrice (uint256 _orderId , uint256 _newPrice ) external {
73
+ function changeNFTPrice (
74
+ uint256 _orderId ,
75
+ uint256 _newPrice
76
+ ) external {
49
77
require (listings[_orderId].nftAddress.ownerOf (listings[_orderId].tokenId) == msg .sender , "You are not the owner of this NFT " );
50
78
listings[_orderId].price = _newPrice;
79
+
80
+ emit NFTPriceChanged (_orderId, _newPrice);
51
81
}
52
82
53
83
function buyNFT (uint256 _orderId ) external payable {
@@ -59,6 +89,34 @@ contract Market {
59
89
payable (owner).transfer (listings[_orderId].price);
60
90
61
91
listings[_orderId].isSold = true ;
92
+
93
+ emit NFTBought (_orderId, msg .sender , listings[_orderId].price);
94
+ }
95
+
96
+ function getListNFTsOrderNumber () public view returns (
97
+ uint256 [] memory
98
+ ){
99
+ uint256 count = 0 ;
100
+ for (uint256 i = 0 ; i < currentOrderId; i++ ) {
101
+ if (listings[i].isListed) {
102
+ count++ ;
103
+ }
104
+ }
105
+
106
+ uint256 [] memory allListNFTs = new uint256 [](count);
107
+ uint256 index = 0 ;
108
+ for (uint256 i = 0 ; i < currentOrderId; i++ ) {
109
+ if (listings[i].isListed) {
110
+ allListNFTs[index] = i;
111
+ index++ ;
112
+ }
113
+ }
114
+
115
+ return allListNFTs;
116
+ }
117
+
118
+ function getListLength () public view returns (uint256 ) {
119
+ return getListNFTsOrderNumber ().length ;
62
120
}
63
121
64
122
receive () external payable {}
0 commit comments