-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1226 from DeJeune/main
task3: DeJeune
- Loading branch information
Showing
6 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
members/DeJeune/task3/contracts/nft_exchange/DeJeuneNFT.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
members/DeJeune/task3/contracts/nft_exchange/NFTMarket.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
members/DeJeune/task3/contracts/nft_exchange/OpenBuildToken.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
members/DeJeune/task3/contracts/nft_exchange/interface/INFTMarket.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
1. 上架NFT交易哈希 | ||
0x6a1d640e088a32db2b31edfbb5fea7102570382c7da0d153875ffaabc0317cf9 | ||
2. 购买NFT交易哈希 | ||
0x8bb6556a23791f9df7b829dc9deb6ef2248570f211f3a3d2558bd327e43df450 |