Skip to content

Commit

Permalink
feat(task3): add MyERC20Token and MyNFT contracts with NFTMarket inte…
Browse files Browse the repository at this point in the history
…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
veithly committed Jun 20, 2024
1 parent 303a6cc commit 41fc624
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
10 changes: 10 additions & 0 deletions members/veithly/task3/MyERC20Token.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 MyERC20Token is ERC20 {
constructor(uint256 initialSupply) ERC20("MyToken", "MTK") {
_mint(msg.sender, initialSupply);
}
}
20 changes: 20 additions & 0 deletions members/veithly/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/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;
}
}
48 changes: 48 additions & 0 deletions members/veithly/task3/NFTMarket.sol
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);
}
}
2 changes: 2 additions & 0 deletions members/veithly/task3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
List NFT: 0x9a659e39b29263ef2817c26a359e299580c1027982a27658dc486546adfa1097
Buy NFT: 0x0b2f69f185a4936410509a1c16b8f5f5e5c0734ead93b871486cd00a7254d8a2

0 comments on commit 41fc624

Please sign in to comment.