Skip to content

Commit 1fa3be7

Browse files
committed
update contracts&test
1 parent e1b0831 commit 1fa3be7

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

contracts/nft-market.sol

+63-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
pragma solidity ^0.8.20;
44

5-
// import "@openzeppelin/contracts/interfaces/IERC721Receiver.sol";
65
import "@openzeppelin/contracts/interfaces/IERC721.sol";
76
import "@openzeppelin/contracts/interfaces/IERC20.sol";
87

@@ -21,14 +20,30 @@ contract Market {
2120
uint256 public currentOrderId;
2221
mapping(uint256 => NFTListing) public listings;
2322

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+
2429
constructor(IERC20 _erc20, IERC721 _erc721) {
2530
erc20 = _erc20;
2631
erc721 = _erc721;
2732
}
2833

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+
);
3247

3348
NFTListing memory listing;
3449
listing.nftAddress = _nft;
@@ -37,17 +52,32 @@ contract Market {
3752
listing.isListed = true;
3853

3954
listings[currentOrderId] = listing;
55+
56+
emit NFTListed(
57+
currentOrderId,
58+
address(_nft),
59+
_tokenId,
60+
_price
61+
);
62+
4063
currentOrderId++;
4164
}
4265

4366
function unlistNFTFromMarket(uint256 _orderId) external {
4467
require(listings[_orderId].nftAddress.ownerOf(listings[_orderId].tokenId) == msg.sender, "You are not the owner of this NFT");
4568
listings[_orderId].isListed = false;
69+
70+
emit NFTUnlisted(_orderId);
4671
}
4772

48-
function changeNFTPrice(uint256 _orderId, uint256 _newPrice) external {
73+
function changeNFTPrice(
74+
uint256 _orderId,
75+
uint256 _newPrice
76+
) external {
4977
require(listings[_orderId].nftAddress.ownerOf(listings[_orderId].tokenId) == msg.sender, "You are not the owner of this NFT");
5078
listings[_orderId].price = _newPrice;
79+
80+
emit NFTPriceChanged(_orderId, _newPrice);
5181
}
5282

5383
function buyNFT(uint256 _orderId) external payable{
@@ -59,6 +89,34 @@ contract Market {
5989
payable(owner).transfer(listings[_orderId].price);
6090

6191
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;
62120
}
63121

64122
receive() external payable {}

test/market.js

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ describe("Market", function () {
8484
await listNFT(tokenId);
8585
}
8686

87+
expect(await market.getListLength()).to.equal(2);
8788
await market.connect(accountB).buyNFT(1, { value: ethers.parseUnits('500000000000000', 'wei') })
8889

8990
expect(await nft.ownerOf(1)).to.equal(accountB.address);

0 commit comments

Comments
 (0)