-
Notifications
You must be signed in to change notification settings - Fork 0
/
NFTContract
77 lines (61 loc) · 2.81 KB
/
NFTContract
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
67
68
69
70
71
72
73
74
75
76
77
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
// TO DO: Explain the reason/advantadge to use ERC721URIStorage instead of ERC721 itself
contract NFT is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
address private marketplaceAddress;
mapping(uint256 => address) private _creators;
event TokenMinted(uint256 indexed tokenId, string tokenURI, address marketplaceAddress);
constructor(address _marketplaceAddress) ERC721("MarkKop", "MARK") {
marketplaceAddress = _marketplaceAddress;
}
function mintToken(string memory tokenURI) public returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(msg.sender, newItemId);
_creators[newItemId] = msg.sender;
_setTokenURI(newItemId, tokenURI);
// Give the marketplace approval to transact NFTs between users
setApprovalForAll(marketplaceAddress, true);
emit TokenMinted(newItemId, tokenURI, marketplaceAddress);
return newItemId;
}
function getTokensOwnedByMe() public view returns (uint256[] memory) {
uint256 numberOfExistingTokens = _tokenIds.current();
uint256 numberOfTokensOwned = balanceOf(msg.sender);
uint256[] memory ownedTokenIds = new uint256[](numberOfTokensOwned);
uint256 currentIndex = 0;
for (uint256 i = 0; i < numberOfExistingTokens; i++) {
uint256 tokenId = i + 1;
if (ownerOf(tokenId) != msg.sender) continue;
ownedTokenIds[currentIndex] = tokenId;
currentIndex += 1;
}
return ownedTokenIds;
}
function getTokenCreatorById(uint256 tokenId) public view returns (address) {
return _creators[tokenId];
}
function getTokensCreatedByMe() public view returns (uint256[] memory) {
uint256 numberOfExistingTokens = _tokenIds.current();
uint256 numberOfTokensCreated = 0;
for (uint256 i = 0; i < numberOfExistingTokens; i++) {
uint256 tokenId = i + 1;
if (_creators[tokenId] != msg.sender) continue;
numberOfTokensCreated += 1;
}
uint256[] memory createdTokenIds = new uint256[](numberOfTokensCreated);
uint256 currentIndex = 0;
for (uint256 i = 0; i < numberOfExistingTokens; i++) {
uint256 tokenId = i + 1;
if (_creators[tokenId] != msg.sender) continue;
createdTokenIds[currentIndex] = tokenId;
currentIndex += 1;
}
return createdTokenIds;
}
}