-
Notifications
You must be signed in to change notification settings - Fork 350
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 #753 from xiangnuans/task3
Task3:xiangnuans
- Loading branch information
Showing
5 changed files
with
166 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -17,5 +17,5 @@ | |
- 6年前端开发 | ||
- 约1年的RN开发 | ||
|
||
https://github.com/xiangnuans?tab=repositories&type=source | ||
https://github.com/xiangnuans | ||
|
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,88 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import "@openzeppelin/contracts/token/ERC721/IERC721.sol"; | ||
|
||
contract NFTMarket { | ||
struct Listing { | ||
address seller; | ||
address nftContract; | ||
uint256 tokenId; | ||
uint256 price; | ||
bool isActive; | ||
} | ||
|
||
mapping(address => mapping(uint256 => Listing)) public listings; | ||
IERC20 public paymentToken; | ||
|
||
event NFTListed( | ||
address indexed seller, | ||
address indexed nftContract, | ||
uint256 indexed tokenId, | ||
uint256 price | ||
); | ||
event NFTBought( | ||
address indexed buyer, | ||
address indexed nftContract, | ||
uint256 indexed tokenId, | ||
uint256 price | ||
); | ||
|
||
constructor(IERC20 _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" | ||
// ); | ||
require( | ||
nft.getApproved(_tokenId) == address(this), | ||
"Contract not approved" | ||
); | ||
|
||
listings[_nftContract][_tokenId] = Listing( | ||
msg.sender, | ||
_nftContract, | ||
_tokenId, | ||
_price, | ||
true | ||
); | ||
emit NFTListed(msg.sender, _nftContract, _tokenId, _price); | ||
} | ||
|
||
function getAllListings() external view returns (Listing[] memory) { | ||
return listings; | ||
} | ||
|
||
function buyNFT(uint256 index) external { | ||
Listing storage listing = listings[index]; | ||
require(listing.price > 0, "NFT not 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.isActive = false; | ||
|
||
emit NFTBought(msg.sender, _nftContract, _tokenId, listing.price); | ||
} | ||
} |
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,46 @@ | ||
# Sample Hardhat Project | ||
|
||
This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, and a Hardhat Ignition module that deploys that contract. | ||
|
||
Try running some of the following tasks: | ||
|
||
```shell | ||
npx hardhat help | ||
npx hardhat test | ||
REPORT_GAS=true npx hardhat test | ||
npx hardhat node | ||
npx hardhat ignition deploy ./ignition/modules/Lock.ts | ||
``` | ||
|
||
|
||
# 编写 NFTMarket 智能合约 | ||
|
||
## 任务目标 | ||
编写一个智能合约,实现一个简单的NFT市场功能,允许用户上架NFT并使用自己部署的ERC20代币进行购买。 | ||
## 任务要求 | ||
### 1.部署ERC20代币 | ||
需要部署一个ERC20代币,用于NFT的购买。 | ||
|
||
### 2.部署NFT | ||
需要部署NFT用于买卖 | ||
|
||
### 3.上架NFT | ||
- 用户可以将自己的NFT上架到市场。 | ||
- 上架时需要指定NFT的合约地址、Token ID以及价格(使用ERC20代币)。 | ||
- 声明上架NFT事件 | ||
|
||
### 4.购买NFT | ||
- 用户可以使用自己部署的ERC20代币购买上架的NFT。 | ||
- 购买成功后,NFT转移给买家,卖家收到ERC20代币。 | ||
- 声明购买NFT事件 | ||
|
||
## 提交要求 | ||
- 部署在Sepolia测试网上 | ||
- 提交全部合约文件(ERC20、ERC721、NFTMarket) | ||
- 提交上架NFT、购买NFT的交易哈希 | ||
|
||
|
||
- Contract MyToken deployed successfully: [0xC2F6FE1FCb070abE231CA0f9c2d148183740E4a8](https://sepolia.etherscan.io/address/0xC2F6FE1FCb070abE231CA0f9c2d148183740E4a8) | ||
- Contract MyNFT deployed successfully: [0x4ECEd2DEAfC38a647675F650c8ee0abbc52Db26e](https://sepolia.etherscan.io/address/0x4ECEd2DEAfC38a647675F650c8ee0abbc52Db26e) | ||
- Contract NFTMarket deployed successfully: [0x53b72dE55e80B4569EfDA5C96393b26FBDd27eF8](https://sepolia.etherscan.io/address/0x53b72dE55e80B4569EfDA5C96393b26FBDd27eF8) | ||
- Purchase NFT's transaction hash: [0x60ee944f5619c5f7d1315f2d7e0cafb0f354f4a8dd3cf295709232415da08085](https://sepolia.etherscan.io/tx/0x60ee944f5619c5f7d1315f2d7e0cafb0f354f4a8dd3cf295709232415da08085) |
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.0; | ||
|
||
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; | ||
|
||
contract MyNFT is ERC721URIStorage { | ||
uint256 private _tokenIds; | ||
|
||
constructor() ERC721("MyNFT", "MNFT") {} | ||
|
||
function mint( | ||
address recipient, | ||
string memory tokenURI | ||
) public returns (uint256) { | ||
_tokenIds++; | ||
uint256 newTokenId = _tokenIds; | ||
_mint(recipient, newTokenId); | ||
_setTokenURI(newTokenId, tokenURI); | ||
return newTokenId; | ||
} | ||
} |
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,10 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
|
||
contract MyToken is ERC20 { | ||
constructor(uint256 initialSupply) ERC20("MyToken", "MTK") { | ||
_mint(msg.sender, initialSupply * 10 ** uint(decimals())); | ||
} | ||
} |