Skip to content

Commit

Permalink
Merge pull request #1226 from DeJeune/main
Browse files Browse the repository at this point in the history
task3: DeJeune
  • Loading branch information
linghuccc authored Jul 14, 2024
2 parents b600a42 + 517a247 commit cc79a3f
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 0 deletions.
47 changes: 47 additions & 0 deletions members/DeJeune/task3/.gitignore
Original file line number Diff line number Diff line change
@@ -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
._*
20 changes: 20 additions & 0 deletions members/DeJeune/task3/contracts/nft_exchange/DeJeuneNFT.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}
48 changes: 48 additions & 0 deletions members/DeJeune/task3/contracts/nft_exchange/NFTMarket.sol
Original file line number Diff line number Diff line change
@@ -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);

}
}
14 changes: 14 additions & 0 deletions members/DeJeune/task3/contracts/nft_exchange/OpenBuildToken.sol
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 4 additions & 0 deletions members/DeJeune/task3/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1. 上架NFT交易哈希
0x6a1d640e088a32db2b31edfbb5fea7102570382c7da0d153875ffaabc0317cf9
2. 购买NFT交易哈希
0x8bb6556a23791f9df7b829dc9deb6ef2248570f211f3a3d2558bd327e43df450

0 comments on commit cc79a3f

Please sign in to comment.