-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNonFungibleToken.sol
254 lines (215 loc) · 5.96 KB
/
NonFungibleToken.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
pragma solidity 0.4.24;
import "./DetailedERC721.sol";
/**
* @title NonFungibleToken
*
* Generic implementation for both required and optional functionality in
* the ERC721 standard for non-fungible tokens.
*
* Heavily inspired by Decentraland's generic implementation:
* https://github.com/decentraland/land/blob/master/contracts/BasicNFT.sol
*
* Standard Author: dete
* Implementation Author: Nadav Hollander <nadav at dharma.io>
*/
contract NonFungibleToken is DetailedERC721 {
string public name;
string public symbol;
uint public numTokensTotal;
mapping(uint => address) internal tokenIdToOwner;
mapping(uint => address) internal tokenIdToApprovedAddress;
mapping(uint => string) internal tokenIdToMetadata;
mapping(address => uint[]) internal ownerToTokensOwned;
mapping(uint => uint) internal tokenIdToOwnerArrayIndex;
event Transfer(
address indexed _from,
address indexed _to,
uint256 _tokenId
);
event Approval(
address indexed _owner,
address indexed _approved,
uint256 _tokenId
);
modifier onlyExtantToken(uint _tokenId) {
require(ownerOf(_tokenId) != address(0));
_;
}
function name()
public
view
returns (string _name)
{
return name;
}
function symbol()
public
view
returns (string _symbol)
{
return symbol;
}
function totalSupply()
public
view
returns (uint256 _totalSupply)
{
return numTokensTotal;
}
function balanceOf(address _owner)
public
view
returns (uint _balance)
{
return ownerToTokensOwned[_owner].length;
}
function ownerOf(uint _tokenId)
public
view
returns (address _owner)
{
return _ownerOf(_tokenId);
}
function tokenMetadata(uint _tokenId)
public
view
returns (string _infoUrl)
{
return tokenIdToMetadata[_tokenId];
}
function approve(address _to, uint _tokenId)
public
onlyExtantToken(_tokenId)
{
require(msg.sender == ownerOf(_tokenId));
require(msg.sender != _to);
if (_getApproved(_tokenId) != address(0) ||
_to != address(0)) {
_approve(_to, _tokenId);
emit Approval(msg.sender, _to, _tokenId);
}
}
function transferFrom(address _from, address _to, uint _tokenId)
public
onlyExtantToken(_tokenId)
{
require(getApproved(_tokenId) == msg.sender);
require(ownerOf(_tokenId) == _from);
require(_to != address(0));
_clearApprovalAndTransfer(_from, _to, _tokenId);
emit Approval(_from, 0, _tokenId);
emit Transfer(_from, _to, _tokenId);
}
function transfer(address _to, uint _tokenId)
public
onlyExtantToken(_tokenId)
{
require(ownerOf(_tokenId) == msg.sender);
require(_to != address(0));
_clearApprovalAndTransfer(msg.sender, _to, _tokenId);
emit Approval(msg.sender, 0, _tokenId);
emit Transfer(msg.sender, _to, _tokenId);
}
function tokenOfOwnerByIndex(address _owner, uint _index)
public
view
returns (uint _tokenId)
{
return _getOwnerTokenByIndex(_owner, _index);
}
function getOwnerTokens(address _owner)
public
view
returns (uint[] _tokenIds)
{
return _getOwnerTokens(_owner);
}
function implementsERC721()
public
view
returns (bool _implementsERC721)
{
return true;
}
function getApproved(uint _tokenId)
public
view
returns (address _approved)
{
return _getApproved(_tokenId);
}
function _clearApprovalAndTransfer(address _from, address _to, uint _tokenId)
internal
{
_clearTokenApproval(_tokenId);
_removeTokenFromOwnersList(_from, _tokenId);
_setTokenOwner(_tokenId, _to);
_addTokenToOwnersList(_to, _tokenId);
}
function _ownerOf(uint _tokenId)
internal
view
returns (address _owner)
{
return tokenIdToOwner[_tokenId];
}
function _approve(address _to, uint _tokenId)
internal
{
tokenIdToApprovedAddress[_tokenId] = _to;
}
function _getApproved(uint _tokenId)
internal
view
returns (address _approved)
{
return tokenIdToApprovedAddress[_tokenId];
}
function _getOwnerTokens(address _owner)
internal
view
returns (uint[] _tokens)
{
return ownerToTokensOwned[_owner];
}
function _getOwnerTokenByIndex(address _owner, uint _index)
internal
view
returns (uint _tokens)
{
return ownerToTokensOwned[_owner][_index];
}
function _clearTokenApproval(uint _tokenId)
internal
{
tokenIdToApprovedAddress[_tokenId] = address(0);
}
function _setTokenOwner(uint _tokenId, address _owner)
internal
{
tokenIdToOwner[_tokenId] = _owner;
}
function _addTokenToOwnersList(address _owner, uint _tokenId)
internal
{
ownerToTokensOwned[_owner].push(_tokenId);
tokenIdToOwnerArrayIndex[_tokenId] =
ownerToTokensOwned[_owner].length - 1;
}
function _removeTokenFromOwnersList(address _owner, uint _tokenId)
internal
{
uint length = ownerToTokensOwned[_owner].length;
uint index = tokenIdToOwnerArrayIndex[_tokenId];
uint swapToken = ownerToTokensOwned[_owner][length - 1];
ownerToTokensOwned[_owner][index] = swapToken;
tokenIdToOwnerArrayIndex[swapToken] = index;
delete ownerToTokensOwned[_owner][length - 1];
ownerToTokensOwned[_owner].length--;
}
function _insertTokenMetadata(uint _tokenId, string _metadata)
internal
{
tokenIdToMetadata[_tokenId] = _metadata;
}
}