forked from Perpetual-Altruism-Ltd/myNFT-Bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImplTestERC721.sol
272 lines (213 loc) · 11.7 KB
/
ImplTestERC721.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.9;
//This contract only exist in order to run migration test.
//It is a genric fgreely mintable ERC-721 standard.
import "../ERC721.sol";
contract ImplTestERC721 is ERC721 {
address public owner; //Address of the smart contract creator
mapping(address => uint256) internal balanceOfToken; //A counter tracking each owner token balance without having to loop.
mapping(uint256 => address) internal tokenOwners; //The mapping of the token to their owner
// Mapping associating owner with their operators
mapping(address => mapping(address => bool)) internal ownerOperators; // owner => operator => isOperator.
// Mapping associating tokens with an operator
mapping(uint256 => address) internal tokenOperator; // tokenId => operator
mapping(uint256 => address) internal preminters; //Each token preminter
// Total number of minted token
uint256 public mintedTokens;
//Set the owner as the smart contract creator
constructor(){
owner = msg.sender;
}
/// @notice Mint a token for msg.sender and return the tokenId of this token
/// @return the newly minted tokenId
function mint() external returns(uint256){
mintedTokens = mintedTokens + 1;
tokenOwners[mintedTokens] = msg.sender;
balanceOfToken[msg.sender] = balanceOfToken[msg.sender] + 1;
emit Transfer(address(0x0), msg.sender, mintedTokens);
return mintedTokens;
}
/// @notice Mint a token reservation, allowing the preminter to send the non-existing token from address 0
/// @return the future minted tokenId
function premintFor(address _preminter) external returns(uint256){
mintedTokens = mintedTokens + 1;
preminters[mintedTokens] = _preminter;
return mintedTokens;
}
/// @notice Transfers the ownership of an NFT from one address to another address
/// @dev Throws unless `msg.sender` is the current owner, an authorized
/// operator, or the approved address for this NFT. Throws if `_from` is
/// not the current owner. Throws if `_to` is the zero address. Throws if
/// `_tokenId` is not a valid NFT. When transfer is complete, this function
/// checks if `_to` is a smart contract (code size > 0). If so, it calls
/// `onERC721Received` on `_to` and throws if the return value is not
/// `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`.
/// @param _from The current owner of the NFT
/// @param _to The new owner
/// @param _tokenId The NFT to transfer
/// @param _data Additional data with no specified format, sent in call to `_to`
function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes calldata _data) external payable override {
safeTransferInternal(_from, _to, _tokenId, _data);
}
/// @notice Transfers the ownership of an NFT from one address to another address
/// @dev This works identically to the other function with an extra data parameter,
/// except this function just sets data to ""
/// @param _from The current owner of the NFT
/// @param _to The new owner
/// @param _tokenId The NFT to transfer
function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable override {
safeTransferInternal(_from, _to, _tokenId, bytes(""));
}
/// @notice Transfer ownership of an NFT -- THE CALLER IS RESPONSIBLE
/// TO CONFIRM THAT `_to` IS CAPABLE OF RECEIVING NFTS OR ELSE
/// THEY MAY BE PERMANENTLY LOST
/// @dev Throws unless `msg.sender` is the current owner, an authorized
/// operator, or the approved address for this NFT. Throws if `_from` is
/// not the current owner. Throws if `_to` is the zero address. Throws if
/// `_tokenId` is not a valid NFT.
/// @param _from The current owner of the NFT
/// @param _to The new owner
/// @param _tokenId The NFT to transfer
function transferFrom(address _from, address _to, uint256 _tokenId) external payable override{
transferInternal(_from, _to, _tokenId);
}
/// @notice Set or reaffirm the approved address for an NFT
/// @dev The zero address indicates there is no approved address.
/// @dev Throws unless `msg.sender` is the current NFT owner, or an authorized
/// operator of the current owner.
/// @param _approved The new approved NFT controller
/// @param _tokenId The NFT to approve
function approve(address _approved, uint256 _tokenId) external payable override{
address _owner = tokenOwners[_tokenId];
//Operator verification
require(
msg.sender == _owner || // the current owner
ownerOperators[_owner][msg.sender], // an authorized operqtor
"msg.sender is not allowed to approve an address for the NFT"
);
tokenOperator[_tokenId] = _approved;
emit Approval(_owner, _approved, _tokenId);
}
/// @notice Enable or disable approval for a third party ("operator") to manage
/// all of `msg.sender`'s assets.
/// @dev Emits the ApprovalForAll event. The contract MUST allow
/// multiple operators per owner.
/// @param _operator Address to add to the set of authorized operators.
/// @param _approved True if the operator is approved, false to revoke approval
function setApprovalForAll(address _operator, bool _approved) external override{
ownerOperators[msg.sender][_operator] = _approved;
emit ApprovalForAll(msg.sender, _operator, _approved);
}
/// @notice Count all NFTs assigned to an owner
/// @dev NFTs assigned to the zero address are considered invalid, and this
/// function throws for queries about the zero address.
/// @param _owner An address for whom to query the balance
/// @return The number of NFTs owned by `_owner`, possibly zero
function balanceOf(address _owner) external view override returns (uint256){
require(_owner != address(0x0), "0x0 is an invalid owner address");
return(balanceOfToken[_owner]);
}
/// @notice Find the owner of an NFT
/// @dev NFTs assigned to zero address are considered invalid, and queries
/// about them do throw.
/// @param _tokenId The identifier for an NFT
/// @return The address of the owner of the NFT
function ownerOf(uint256 _tokenId) external view override returns (address){
address retour = tokenOwners[_tokenId];
require(retour != address(0x0), "0x0 is an invalid owner address");
return retour;
}
/// @notice Get the approved address for a single NFT
/// @dev Throws if `_tokenId` is not a valid NFT
/// @param _tokenId The NFT to find the approved address for
/// @return The approved address for this NFT, or the zero address if there is none
function getApproved(uint256 _tokenId) external view override returns (address) {
require(tokenOwners[_tokenId] != address(0x0), "_tokenId is not a valid NFT tokenID");
return tokenOperator[_tokenId];
}
/// @notice Query if an address is an authorized operator for another address
/// @param _owner The address that owns the NFTs
/// @param _operator The address that acts on behalf of the owner
/// @return True if `_operator` is an approved operator for `_owner`, false otherwise
function isApprovedForAll(address _owner, address _operator) external view override returns (bool){
return ownerOperators[_owner][_operator];
}
function isContract( address _addr ) internal view returns (bool addressCheck)
{
// This method relies in extcodesize, which returns 0 for contracts in
// construction, since the code is only stored at the end of the
// constructor execution.
// According to EIP-1052, 0x0 is the value returned for not-yet created accounts
// and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
// for accounts without code, i.e. `keccak256('')`
bytes32 codehash;
bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
assembly { codehash := extcodehash(_addr) } // solhint-disable-line
addressCheck = (codehash != 0x0 && codehash != accountHash);
}
function safeTransferInternal(address _from, address _to, uint256 _tokenId, bytes memory _data) internal {
transferInternal(_from, _to, _tokenId);
if (isContract(_to))
{
bytes4 retval = ERC721TokenReceiver(_to).onERC721Received(msg.sender, _from, _tokenId, _data);
// bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")) === 0x150b7a02
require(retval == 0x150b7a02, "The NFT was not received properly by the contract");
}
}
/// @notice Transfer ownership of an NFT
/// @dev Throws unless `msg.sender` is the current owner, an authorized
/// operator, or the approved address for this NFT. Throws if `_from` is
/// not the current owner. Throws if `_to` is the zero address. Throws if
/// `_tokenId` is not a valid NFT.
/// @param _from The current owner of the NFT
/// @param _to The new owner
/// @param _tokenId The NFT to transfer
function transferInternal(address _from, address _to, uint256 _tokenId) internal {
if(tokenOwners[_tokenId] != address(0x0)){ //If already minted
//Ownership verification
require( tokenOwners[_tokenId] == _from, "The specified _from does not match the current token owner");
//Valid nft <=> owner != 0x0
require(_from != address(0x0), "_tokenId is not a valid NFT");
} else { //If requiring minting
require(_from == address(0x0), "_tokenId doesn't exist yet and neet to be minted");
require(msg.sender == preminters[_tokenId], "_tokenId has not be approved for minting by msg.sender");
}
//Prevent 0x0 burns
require(_to != address(0x0), "_to cannot be the address 0");
//Operator verification
require(
msg.sender == _from || // the current owner
ownerOperators[_from][msg.sender] || // an authorized operqtor
msg.sender == tokenOperator[_tokenId], // the approved address for this NFT
"msg.sender is not allowed to transfer this NFT"
);
//Transfer the token ownership record
tokenOwners[_tokenId] = _to;
//Clean the token approved address
tokenOperator[_tokenId] == address(0x0);
balanceOfToken[_from] = balanceOfToken[_from] - 1;
balanceOfToken[_to] = balanceOfToken[_to] + 1;
//Emit the transfer event
emit Transfer(_from, _to, _tokenId);
}
/// @notice A distinct Uniform Resource Identifier (URI) for a given asset.
/// @dev Throws if `_tokenId` is not a valid NFT. URIs are defined in RFC
/// 3986. The URI may point to a JSON file that conforms to the "ERC721
/// Metadata JSON Schema".
function tokenURI(uint256 _tokenId) external view returns(string memory){
require(tokenOwners[_tokenId] != address(0), "This token is not minted");
return string(abi.encodePacked("https://cryptograph.co/tokenuri/0x2449835e86a539ab33f5773729c0db42e89016ff"));
}
/// @notice Query if a contract implements an interface
/// @param interfaceID The interface identifier, as specified in ERC-165
/// @dev Interface identification is specified in ERC-165. This function
/// uses less than 30,000 gas.
/// @return `true` if the contract implements `interfaceID` and
/// `interfaceID` is not 0xffffffff, `false` otherwise
function supportsInterface(bytes4 interfaceID) external pure returns(bool) {
return (
interfaceID == 0x80ac58cd || //ERC721
interfaceID == 0x01ffc9a7 //ERC165
);
}
}