Skip to content

Commit

Permalink
Merge pull request #753 from xiangnuans/task3
Browse files Browse the repository at this point in the history
Task3:xiangnuans
  • Loading branch information
xiangnuans authored Jul 4, 2024
2 parents 9b4635e + a8a87d1 commit 5e1d5e4
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 1 deletion.
2 changes: 1 addition & 1 deletion members/xiangnuans/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
- 6年前端开发
- 约1年的RN开发

https://github.com/xiangnuans?tab=repositories&type=source
https://github.com/xiangnuans

88 changes: 88 additions & 0 deletions members/xiangnuans/task3/NFTMarket.sol
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);
}
}
46 changes: 46 additions & 0 deletions members/xiangnuans/task3/README.md
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)
21 changes: 21 additions & 0 deletions members/xiangnuans/task3/myNFT.sol
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;
}
}
10 changes: 10 additions & 0 deletions members/xiangnuans/task3/myToken.sol
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()));
}
}

0 comments on commit 5e1d5e4

Please sign in to comment.