Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task3: veithly #787

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
58 changes: 58 additions & 0 deletions members/veithly/task3/NFTMarket.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

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) Ownable(msg.sender) {
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);
}


function getAllListings() external view returns (Listing[] memory) {
Listing[] memory allListings = new Listing[](listingCount);
for (uint256 i = 1; i <= listingCount; i++) {
allListings[i-1] = listings[i];
}
return allListings;
}
}
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: 0xf563a73569698f80a82eab21e4ba4eaf5c73db5e85a398f03e7fa5aef2ca5e35
Buy NFT: 0x7948cece92264265a0008d458f18f7f421036f95982082f1908ea712abf4a50d
Loading