-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(task3): add MyERC20Token and MyNFT contracts with NFTMarket inte…
…rface Add ERC20 and ERC721 smart contracts named `MyERC20Token` and `MyNFT`, respectively. Introduce the `NFTMarket` contract that facilitates buying and selling of NFTs, leveraging OpenZeppelin libraries for security and efficiency. README updated with deployed contract addresses for list and buy functionalities.
- Loading branch information
Showing
4 changed files
with
80 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,10 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
|
||
contract MyERC20Token is ERC20 { | ||
constructor(uint256 initialSupply) ERC20("MyToken", "MTK") { | ||
_mint(msg.sender, initialSupply); | ||
} | ||
} |
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/extensions/ERC721URIStorage.sol"; | ||
import "@openzeppelin/contracts/utils/Counters.sol"; | ||
|
||
contract MyNFT is ERC721URIStorage { | ||
using Counters for Counters.Counter; | ||
Counters.Counter private _tokenIds; | ||
|
||
constructor() ERC721("MyNFT", "MNFT") {} | ||
|
||
function mintNFT(address recipient, string memory tokenURI) public returns (uint256) { | ||
_tokenIds.increment(); | ||
uint256 newItemId = _tokenIds.current(); | ||
_mint(recipient, newItemId); | ||
_setTokenURI(newItemId, tokenURI); | ||
return newItemId; | ||
} | ||
} |
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 @@ | ||
pragma solidity ^0.8.0; | ||
|
||
import "@openzeppelin/contracts/token/ERC721/IERC721.sol"; | ||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
|
||
contract NFTMarket is Ownable { | ||
struct Listing { | ||
address seller; | ||
address nftContract; | ||
uint256 tokenId; | ||
uint256 price; | ||
} | ||
|
||
IERC20 public currencyToken; | ||
uint256 public listingCount; | ||
mapping(uint256 => Listing) public listings; | ||
|
||
event NFTListed(uint256 listingId, address seller, address nftContract, uint256 tokenId, uint256 price); | ||
event NFTBought(uint256 listingId, address buyer); | ||
|
||
constructor(address _currencyToken) { | ||
currencyToken = IERC20(_currencyToken); | ||
} | ||
|
||
function listNFT(address _nftContract, uint256 _tokenId, uint256 _price) external { | ||
IERC721 nftContract = IERC721(_nftContract); | ||
require(nftContract.ownerOf(_tokenId) == msg.sender, "You do not own this NFT"); | ||
require(nftContract.isApprovedForAll(msg.sender, address(this)), "Market contract is not approved"); | ||
|
||
listingCount++; | ||
listings[listingCount] = Listing(msg.sender, _nftContract, _tokenId, _price); | ||
|
||
emit NFTListed(listingCount, msg.sender, _nftContract, _tokenId, _price); | ||
} | ||
|
||
function buyNFT(uint256 _listingId) external { | ||
Listing memory listing = listings[_listingId]; | ||
require(listing.price > 0, "NFT is not listed"); | ||
|
||
currencyToken.transferFrom(msg.sender, listing.seller, listing.price); | ||
IERC721(listing.nftContract).transferFrom(listing.seller, msg.sender, listing.tokenId); | ||
|
||
delete listings[_listingId]; | ||
|
||
emit NFTBought(_listingId, msg.sender); | ||
} | ||
} |
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,2 @@ | ||
List NFT: 0x9a659e39b29263ef2817c26a359e299580c1027982a27658dc486546adfa1097 | ||
Buy NFT: 0x0b2f69f185a4936410509a1c16b8f5f5e5c0734ead93b871486cd00a7254d8a2 |