From 517a247741c659bccb3eb9de7f0c49bff1203745 Mon Sep 17 00:00:00 2001 From: suyao Date: Sat, 13 Jul 2024 23:36:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=9A=84=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- members/DeJeune/task3/.gitignore | 47 ++++++++++++++++++ .../contracts/nft_exchange/DeJeuneNFT.sol | 20 ++++++++ .../contracts/nft_exchange/NFTMarket.sol | 48 +++++++++++++++++++ .../contracts/nft_exchange/OpenBuildToken.sol | 14 ++++++ .../nft_exchange/interface/INFTMarket.sol | 21 ++++++++ members/DeJeune/task3/readme.md | 4 ++ 6 files changed, 154 insertions(+) create mode 100644 members/DeJeune/task3/.gitignore create mode 100644 members/DeJeune/task3/contracts/nft_exchange/DeJeuneNFT.sol create mode 100644 members/DeJeune/task3/contracts/nft_exchange/NFTMarket.sol create mode 100644 members/DeJeune/task3/contracts/nft_exchange/OpenBuildToken.sol create mode 100644 members/DeJeune/task3/contracts/nft_exchange/interface/INFTMarket.sol create mode 100644 members/DeJeune/task3/readme.md diff --git a/members/DeJeune/task3/.gitignore b/members/DeJeune/task3/.gitignore new file mode 100644 index 000000000..660da74f1 --- /dev/null +++ b/members/DeJeune/task3/.gitignore @@ -0,0 +1,47 @@ +# Ignore all dot files +.* + +# Except .gitignore +!.gitignore + +# Node modules +node_modules/ + +# Dependency directories +lib/ + +# Binaries for programs and plugins +bin/ +obj/ + +# Solidity output directories +build/ +artifacts/ + + +# Hardhat specific +cache/ +artifacts/ + +# Coverage +coverage/ +coverage.json +lcov-report/ + +# Miscellaneous +.DS_Store +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# build output +dist/ + +# environment variables +.env +.env.production + +# macOS-specific files +.DS_Store +._* diff --git a/members/DeJeune/task3/contracts/nft_exchange/DeJeuneNFT.sol b/members/DeJeune/task3/contracts/nft_exchange/DeJeuneNFT.sol new file mode 100644 index 000000000..13bf1b528 --- /dev/null +++ b/members/DeJeune/task3/contracts/nft_exchange/DeJeuneNFT.sol @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.18; + +import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; +import "@openzeppelin/contracts/utils/Counters.sol"; + +contract DeJeuneNFT is ERC721 { + using Counters for Counters.Counter; + Counters.Counter private _nftIDCounter; + + constructor() ERC721("DeJeuneNFT", "DeJeune") { + _nftIDCounter.increment(); + } + + function safeMint(address to) public { + uint256 tokenId = _nftIDCounter.current(); + _nftIDCounter.increment(); + _safeMint(to, tokenId); + } +} diff --git a/members/DeJeune/task3/contracts/nft_exchange/NFTMarket.sol b/members/DeJeune/task3/contracts/nft_exchange/NFTMarket.sol new file mode 100644 index 000000000..27edd20d0 --- /dev/null +++ b/members/DeJeune/task3/contracts/nft_exchange/NFTMarket.sol @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.18; + +import "./DeJeuneNFT.sol"; +import "./OpenBuildToken.sol"; +import "./interface/INFTMarket.sol"; +import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import "@openzeppelin/contracts/token/ERC721/IERC721.sol"; +import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; + +contract NFTMarket is INFTMarket, ReentrancyGuard { + struct Listing { + address seller; + address nftContract; + uint256 tokenId; + uint256 price; + bool isActivate; + } + + mapping(address => mapping(uint256 => Listing)) public listings; + IERC20 public paymentToken; + + constructor(address _paymentToken){ + paymentToken = IERC20(_paymentToken); + } + + function listNFT(address _nftContract, uint256 _tokenId, uint256 _price) external { + IERC721 nft = IERC721(_nftContract); + require(nft.ownerOf(_tokenId) == msg.sender, "Not the owner of the NFT"); + require(nft.isApprovedForAll(msg.sender, address(this)), "Contract not approved"); + listings[_nftContract][_tokenId] = Listing(msg.sender, _nftContract, _tokenId, _price, true); + + emit NFTListed(msg.sender, _nftContract, _tokenId, _price); + } + + function buyNFT(address _nftContract, uint256 _tokenId) external nonReentrant{ + Listing storage listing = listings[_nftContract][_tokenId]; + require(listing.isActivate, "NFT not listed for sale"); + + IERC721 nft = IERC721(_nftContract); + require(paymentToken.transferFrom(msg.sender, listing.seller, listing.price), "payment failed"); + nft.safeTransferFrom(listing.seller, msg.sender, _tokenId); + listing.isActivate = false; + + emit NFTPurchased(msg.sender, _nftContract, _tokenId, listing.price); + + } +} diff --git a/members/DeJeune/task3/contracts/nft_exchange/OpenBuildToken.sol b/members/DeJeune/task3/contracts/nft_exchange/OpenBuildToken.sol new file mode 100644 index 000000000..8697982a1 --- /dev/null +++ b/members/DeJeune/task3/contracts/nft_exchange/OpenBuildToken.sol @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.18; + +import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; + +contract OpenBuildToken is ERC20 { + constructor() ERC20("OpenBuildToken", "MTK") { + _mint(msg.sender, 10000000000 * 10 * 6); + } + + function decimals() public view virtual override returns(uint8){ + return 6; + } +} diff --git a/members/DeJeune/task3/contracts/nft_exchange/interface/INFTMarket.sol b/members/DeJeune/task3/contracts/nft_exchange/interface/INFTMarket.sol new file mode 100644 index 000000000..72335f9c2 --- /dev/null +++ b/members/DeJeune/task3/contracts/nft_exchange/interface/INFTMarket.sol @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.18; + +interface INFTMarket { + event NFTListed( + address indexed seller, + address indexed nftContract, + uint256 indexed tokenId, + uint256 price + ); + + event NFTPurchased( + address indexed buyer, + address indexed nftContract, + uint256 indexed tokenId, + uint256 price + ); + + function listNFT(address _nftContract, uint256 _tokenId, uint256 _price) external; + function buyNFT(address _nftContract, uint256 _tokenId) external; +} diff --git a/members/DeJeune/task3/readme.md b/members/DeJeune/task3/readme.md new file mode 100644 index 000000000..baf5d6ab7 --- /dev/null +++ b/members/DeJeune/task3/readme.md @@ -0,0 +1,4 @@ +1. 上架NFT交易哈希 +0x6a1d640e088a32db2b31edfbb5fea7102570382c7da0d153875ffaabc0317cf9 +2. 购买NFT交易哈希 +0x8bb6556a23791f9df7b829dc9deb6ef2248570f211f3a3d2558bd327e43df450