-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNFT.sol
66 lines (52 loc) · 2.13 KB
/
NFT.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
contract PixellNFT is ERC721URIStorage {
using Counters for Counters.Counter;
event NFTBought(address _seller, address _buyer, uint256 _price);
mapping (uint256 => uint256) public tokenIdToPrice;
// keep track of total NFTs minted and assign unique IDs
Counters.Counter private _tokenIds;
constructor() public ERC721("PixellNFT", "NFT") {}
function mintNFT(string memory tokenURI)
public
returns (uint256)
{
// call this function to mint a pixel art NFT
_tokenIds.increment();
uint256 newTokenId = _tokenIds.current(); // new tokenId for the NFT to be minted
_mint(msg.sender, newTokenId);
_setTokenURI(newTokenId, tokenURI); // assigns a tokenId to the tokenURI
return newTokenId; // returns the tokenId of the NFT
}
function allowBuy(uint256 _tokenId, uint256 _price)
external
{
// call this function to place the NFT on sale in the marketplace
require(msg.sender == ownerOf(_tokenId), 'Not owner of this token');
require(_price > 0, 'Price cannot be 0');
tokenIdToPrice[_tokenId] = _price;
}
function disallowBuy(uint256 _tokenId)
external
{
// call this function to remove the NFT from the marketplace (after it is bought)
require(msg.sender == ownerOf(_tokenId), 'Not owner of this token');
tokenIdToPrice[_tokenId] = 0;
}
function buyNFT(uint256 _tokenId)
external payable
{
// call this function to buy an NFT
uint256 price = tokenIdToPrice[_tokenId];
require(price > 0, 'This token is not for sale');
require(msg.value == price, 'Incorrect value');
address seller = ownerOf(_tokenId);
require(msg.sender != seller, 'You already own this token');
_transfer(seller, msg.sender, _tokenId); // transfer ownership of token from seller to buyer
tokenIdToPrice[_tokenId] = 0; // remove from sale
payable(seller).transfer(msg.value); // transfer ETH to seller
emit NFTBought(seller, msg.sender, msg.value);
}
}