Skip to content

Commit

Permalink
Merge pull request #1078 from warku123/task3
Browse files Browse the repository at this point in the history
task3: warku123
  • Loading branch information
linghuccc authored Jul 5, 2024
2 parents 969cbc5 + f5d9acc commit 0571b20
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
20 changes: 20 additions & 0 deletions members/warku123/task3/MyNFT.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract MyNFT is ERC721 {
using Counters for Counters.Counter;
Counters.Counter private _tokenIdCounter;

constructor() ERC721("PiGNFT", "PFT") {
_tokenIdCounter.increment();
}

function mint(address to) public {
uint256 tokenId = _tokenIdCounter.current();
_safeMint(to, tokenId);
_tokenIdCounter.increment();
}
}
15 changes: 15 additions & 0 deletions members/warku123/task3/MyToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {
constructor() ERC20("PiGToken", "PGK")
{
_mint(msg.sender, 10000000000*10**6);
}

function decimals() public view virtual override returns (uint8) {
return 6;
}
}
45 changes: 45 additions & 0 deletions members/warku123/task3/NFTMarket.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";

contract NFTMarket is ReentrancyGuard {
struct Listing {
address seller;
address nftContract;
uint256 tokenId;
uint256 price;
}

IERC20 public paymentToken;
mapping(uint256 => Listing) public listings;
uint256 public nextListingId;
event NFTListed(uint256 listingId, address seller, address nftContract, uint256 tokenId, uint256 price);
event NFTPurchased(uint256 listingId, address buyer);

constructor(address _paymentToken) {
paymentToken = IERC20(_paymentToken);
}

function listNFT(address nftContract, uint256 tokenId, uint256 price) external nonReentrant {
require(price > 0, "Price must be greater than zero");

IERC721(nftContract).transferFrom(msg.sender, address(this), tokenId);
listings[nextListingId] = Listing(msg.sender, nftContract, tokenId, price);
emit NFTListed(nextListingId, msg.sender, nftContract, tokenId, price);
nextListingId++;
}

function buyNFT(uint256 listingId) external nonReentrant {
Listing storage listing = listings[listingId];
require(listing.price > 0, "NFT not listed for sale");

paymentToken.transferFrom(msg.sender, listing.seller, listing.price);
IERC721(listing.nftContract).transferFrom(address(this), msg.sender, listing.tokenId);

emit NFTPurchased(listingId, msg.sender);
delete listings[listingId];
}
}
8 changes: 8 additions & 0 deletions members/warku123/task3/task3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
| 说明 | 地址 |
| ---------- | ------------------------------------------------------------ |
| PGT(token) | 0xcEDC4aE58aE7748453fF31a4db1F264E198586d9 |
| PFT(NFT) | 0x6C98BeB91Afa81817E7248fCaf7603d947938983 |
| NFTMarket | 0x1E850f5010E94B70de98085Afe2DD434322DdF40 |
| 上架 NFT | 0xdcd7f2f88f40be37fd7912dc9aed407e85962005c55106958c500ec13060cc4e |
| 购买 NFT | 0x3947f2e5514f426d02c666bdab2436fc5845168752f39f2eec37a04b3574c5c4 |

0 comments on commit 0571b20

Please sign in to comment.