-
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 #1078 from warku123/task3
task3: warku123
- Loading branch information
Showing
4 changed files
with
88 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,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(); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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]; | ||
} | ||
} |
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,8 @@ | ||
| 说明 | 地址 | | ||
| ---------- | ------------------------------------------------------------ | | ||
| PGT(token) | 0xcEDC4aE58aE7748453fF31a4db1F264E198586d9 | | ||
| PFT(NFT) | 0x6C98BeB91Afa81817E7248fCaf7603d947938983 | | ||
| NFTMarket | 0x1E850f5010E94B70de98085Afe2DD434322DdF40 | | ||
| 上架 NFT | 0xdcd7f2f88f40be37fd7912dc9aed407e85962005c55106958c500ec13060cc4e | | ||
| 购买 NFT | 0x3947f2e5514f426d02c666bdab2436fc5845168752f39f2eec37a04b3574c5c4 | | ||
|