-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClixLogix_NFT_ERC721.sol
139 lines (105 loc) · 4.76 KB
/
ClixLogix_NFT_ERC721.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.0;
contract ERC721 {
event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);
event Approval(address indexed _owner, address indexed _approved, uint256 _tokenId);
event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);
mapping(address => uint256) internal _balances;
mapping(uint256 => address) internal _owners;
mapping(address => mapping(address => bool)) private _operatorApprovals;
mapping(uint256 => address) private _tokenApprovals;
// Returns the number of NFTs assigned to an owner
function balanceOf(address owner) public view returns(uint256) {
require(owner != address(0), "Address is zero");
return _balances[owner];
}
// Finds the owner of an NFT
function ownerOf(uint256 tokenId) public view returns(address) {
address owner = _owners[tokenId];
require(owner != address(0), "TokenID does not exist");
return owner;
}
// Enables or disables an operator to manage all of msg.senders assets.
function setApprovalForAll(address operator, bool approved) public {
_operatorApprovals[msg.sender][operator] = approved;
emit ApprovalForAll(msg.sender, operator, approved);
}
// Checks if an address is an operator for another address
function isApprovedForAll(address owner, address operator) public view returns(bool) {
return _operatorApprovals[owner][operator];
}
// Updates an approved address for an NFT
function approve(address to, uint256 tokenId) public {
address owner = ownerOf(tokenId);
require( msg.sender == owner || isApprovedForAll(owner, msg.sender), "Msg.sender is not the owner or an approved operator");
_tokenApprovals[tokenId] = to;
emit Approval(owner, to, tokenId);
}
// Gets the approved address for a single NFT
function getApproved(uint256 tokenId) public view returns(address) {
require(_owners[tokenId] != address(0), "Token ID does not exist");
return _tokenApprovals[tokenId];
}
// Transfers ownership of an NFT
function transferFrom(address from, address to, uint256 tokenId) public {
address owner = ownerOf(tokenId);
require(
msg.sender == owner ||
getApproved(tokenId) == msg.sender ||
isApprovedForAll(owner, msg.sender),
"Msg.sender is not the owner or approved for transfer"
);
require(owner == from, "From address is not the owner");
require(to != address(0), "Address is zero");
require(_owners[tokenId] != address(0), "TokenID does not exist");
approve(address(0), tokenId);
_balances[from] -= 1;
_balances[to] += 1;
_owners[tokenId] = to;
emit Transfer(from, to, tokenId);
}
// Standard transferFrom
// Checks if onERC721Received is implemented WHEN sending to smart contracts
// function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public {
// transferFrom(from, to, tokenId);
// require(_checkOnERC721Received(), "Receiver not implemented");
// }
// function safeTransferFrom(address from, address to, uint256 tokenId) public {
// safeTransferFrom(from, to, tokenId, "");
// }
// Oversimplified
function _checkOnERC721Received() private pure returns(bool) {
return true;
}
// EIP165 : Query if a contract implements another interface
// function supportsInterface(bytes4 interfaceId) public pure virtual returns(bool) {
// return interfaceId == 0x80ac58cd;
// }
}
pragma solidity ^0.8.0;
contract Clix721collection is ERC721 {
string public name; // ERC721Metadata
string public symbol; // ERC721Metadata
uint256 public tokenCount;
mapping(uint256 => string) private _tokenURIs;
constructor(string memory _name, string memory _symbol) {
name = _name;
symbol = _symbol;
}
// Returns a URL that points to the metadata
function tokenURI(uint256 tokenId) public view returns (string memory) { // ERC721Metadata
require(_owners[tokenId] != address(0), "TokenId does not exist");
return _tokenURIs[tokenId];
}
// Creates a new NFT inside our collection
function mint(string memory _tokenURI) public {
tokenCount += 1; // tokenId
_balances[msg.sender] += 1;
_owners[tokenCount] = msg.sender;
_tokenURIs[tokenCount] = _tokenURI;
emit Transfer(address(0), msg.sender, tokenCount);
}
// function supportsInterface(bytes4 interfaceId) public pure override returns(bool) {
// return interfaceId == 0x80ac58cd || interfaceId == 0x5b5e139f;
// }
}