forked from BuildBearLabs/lokiCode-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 15
/
NFT.sol
53 lines (42 loc) · 1.9 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract SimpleERC721 {
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
event TokenURIUpdated(uint256 indexed tokenId, string tokenURI);
mapping(uint256 => address) private _tokenOwner;
mapping(address => uint256) private _ownedTokensCount;
mapping(uint256 => string) private _tokenURIs; // Mapping for token URIs
function balanceOf(address owner) public view returns (uint256) {
require(owner != address(0), "Address is zero");
return _ownedTokensCount[owner];
}
function ownerOf(uint256 tokenId) public view returns (address) {
address owner = _tokenOwner[tokenId];
require(owner != address(0), "Token ID does not exist");
return owner;
}
function tokenURI(uint256 tokenId) public view returns (string memory) {
require(_exists(tokenId), "Token ID does not exist");
return _tokenURIs[tokenId];
}
function setTokenURI(uint256 tokenId, string memory uri) public {
require(_exists(tokenId), "Token ID does not exist");
require(ownerOf(tokenId) == msg.sender, "Only owner can set token URI"); // Ensuring the message sender is the owner
_tokenURIs[tokenId] = uri;
emit TokenURIUpdated(tokenId, uri);
}
function _exists(uint256 tokenId) internal view returns (bool) {
address owner = _tokenOwner[tokenId];
return owner != address(0);
}
function _mint(address to, uint256 tokenId) internal virtual {
require(to != address(0), "ERC721: minting to the zero address");
require(!_exists(tokenId), "ERC721: token already minted");
_tokenOwner[tokenId] = to;
_ownedTokensCount[to] += 1;
emit Transfer(address(0), to, tokenId);
}
function mintUniqueTokenTo(address to, uint256 tokenId) public {
_mint(to, tokenId);
}
}