-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFT.sol
55 lines (43 loc) · 1.49 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
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract NftMinter is ERC721, Ownable {
using Counters for Counters.Counter;
using Strings for uint256;
Counters.Counter _tokenIds;
mapping(uint256 => string) _tokenURIs;
struct RenderToken{
uint256 id;
string uri;
}
constructor() ERC721("Test", "TS") {}
function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal {
_tokenURIs[tokenId] = _tokenURI;
}
function tokenURI(uint256 tokenId) public view virtual override returns(string memory){
require(_exists(tokenId));
string memory _tokenURI =_tokenURIs[tokenId];
return _tokenURI;
}
function getAllTokens() public view returns(RenderToken[] memory) {
uint256 latestId = _tokenIds.current();
uint256 counter = 0;
RenderToken[] memory result = new RenderToken[](latestId); // latest id is the maximum size of the current tokens.
for(uint256 i = 0; i < latestId; i++){
if(_exists(counter)){
string memory uri = tokenURI(counter);
result[counter] = RenderToken(counter, uri);
}
counter++;
}
return result;
}
function mint(address recipient, string memory uri) public returns(uint256) {
uint256 newId = _tokenIds.current();
_mint(msg.sender, newId);
_setTokenURI(newId, uri);
_tokenIds.increment();
return newId;
}
}