ozeppelinsolidty 0.0.1-security → 3.4.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ozeppelinsolidty might be problematic. Click here for more details.

Files changed (166) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +75 -3
  3. package/build/contracts/AccessControl.json +237 -0
  4. package/build/contracts/Address.json +8 -0
  5. package/build/contracts/Arrays.json +8 -0
  6. package/build/contracts/BeaconProxy.json +33 -0
  7. package/build/contracts/Clones.json +8 -0
  8. package/build/contracts/ConditionalEscrow.json +163 -0
  9. package/build/contracts/Context.json +8 -0
  10. package/build/contracts/Counters.json +8 -0
  11. package/build/contracts/Create2.json +8 -0
  12. package/build/contracts/ECDSA.json +8 -0
  13. package/build/contracts/EIP712.json +8 -0
  14. package/build/contracts/ERC1155.json +332 -0
  15. package/build/contracts/ERC1155Burnable.json +367 -0
  16. package/build/contracts/ERC1155Holder.json +106 -0
  17. package/build/contracts/ERC1155Pausable.json +360 -0
  18. package/build/contracts/ERC1155PresetMinterPauser.json +741 -0
  19. package/build/contracts/ERC1155Receiver.json +106 -0
  20. package/build/contracts/ERC165.json +28 -0
  21. package/build/contracts/ERC165Checker.json +8 -0
  22. package/build/contracts/ERC1820Implementer.json +33 -0
  23. package/build/contracts/ERC20.json +295 -0
  24. package/build/contracts/ERC20Burnable.json +310 -0
  25. package/build/contracts/ERC20Capped.json +292 -0
  26. package/build/contracts/ERC20Pausable.json +318 -0
  27. package/build/contracts/ERC20Permit.json +354 -0
  28. package/build/contracts/ERC20PresetFixedSupply.json +336 -0
  29. package/build/contracts/ERC20PresetMinterPauser.json +651 -0
  30. package/build/contracts/ERC20Snapshot.json +335 -0
  31. package/build/contracts/ERC721.json +424 -0
  32. package/build/contracts/ERC721Burnable.json +421 -0
  33. package/build/contracts/ERC721Holder.json +43 -0
  34. package/build/contracts/ERC721Pausable.json +447 -0
  35. package/build/contracts/ERC721PresetMinterPauserAutoId.json +762 -0
  36. package/build/contracts/ERC777.json +585 -0
  37. package/build/contracts/ERC777PresetFixedSupply.json +595 -0
  38. package/build/contracts/EnumerableMap.json +8 -0
  39. package/build/contracts/EnumerableSet.json +8 -0
  40. package/build/contracts/Escrow.json +144 -0
  41. package/build/contracts/GSNRecipient.json +165 -0
  42. package/build/contracts/GSNRecipientERC20Fee.json +194 -0
  43. package/build/contracts/GSNRecipientSignature.json +176 -0
  44. package/build/contracts/IBeacon.json +22 -0
  45. package/build/contracts/IERC1155.json +302 -0
  46. package/build/contracts/IERC1155MetadataURI.json +321 -0
  47. package/build/contracts/IERC1155Receiver.json +106 -0
  48. package/build/contracts/IERC165.json +28 -0
  49. package/build/contracts/IERC1820Implementer.json +33 -0
  50. package/build/contracts/IERC1820Registry.json +222 -0
  51. package/build/contracts/IERC20.json +192 -0
  52. package/build/contracts/IERC20Permit.json +84 -0
  53. package/build/contracts/IERC721.json +294 -0
  54. package/build/contracts/IERC721Enumerable.json +350 -0
  55. package/build/contracts/IERC721Metadata.json +339 -0
  56. package/build/contracts/IERC721Receiver.json +43 -0
  57. package/build/contracts/IERC777.json +400 -0
  58. package/build/contracts/IERC777Recipient.json +47 -0
  59. package/build/contracts/IERC777Sender.json +47 -0
  60. package/build/contracts/IRelayHub.json +656 -0
  61. package/build/contracts/IRelayRecipient.json +133 -0
  62. package/build/contracts/Initializable.json +8 -0
  63. package/build/contracts/Math.json +8 -0
  64. package/build/contracts/MerkleProof.json +8 -0
  65. package/build/contracts/Ownable.json +61 -0
  66. package/build/contracts/Pausable.json +48 -0
  67. package/build/contracts/PaymentSplitter.json +182 -0
  68. package/build/contracts/Proxy.json +17 -0
  69. package/build/contracts/ProxyAdmin.json +158 -0
  70. package/build/contracts/PullPayment.json +41 -0
  71. package/build/contracts/ReentrancyGuard.json +8 -0
  72. package/build/contracts/RefundEscrow.json +233 -0
  73. package/build/contracts/SafeCast.json +8 -0
  74. package/build/contracts/SafeERC20.json +8 -0
  75. package/build/contracts/SafeMath.json +8 -0
  76. package/build/contracts/SignedSafeMath.json +8 -0
  77. package/build/contracts/Strings.json +8 -0
  78. package/build/contracts/TimelockController.json +773 -0
  79. package/build/contracts/TokenTimelock.json +76 -0
  80. package/build/contracts/TransparentUpgradeableProxy.json +140 -0
  81. package/build/contracts/UpgradeableBeacon.json +111 -0
  82. package/build/contracts/UpgradeableProxy.json +46 -0
  83. package/build/contracts/__unstable__ERC20Owned.json +365 -0
  84. package/contracts/GSN/Context.sol +5 -0
  85. package/contracts/GSN/GSNRecipient.sol +230 -0
  86. package/contracts/GSN/GSNRecipientERC20Fee.sol +154 -0
  87. package/contracts/GSN/GSNRecipientSignature.sol +72 -0
  88. package/contracts/GSN/IRelayHub.sol +269 -0
  89. package/contracts/GSN/IRelayRecipient.sol +76 -0
  90. package/contracts/access/AccessControl.sol +217 -0
  91. package/contracts/access/Ownable.sol +68 -0
  92. package/contracts/access/TimelockController.sol +300 -0
  93. package/contracts/cryptography/ECDSA.sol +86 -0
  94. package/contracts/cryptography/MerkleProof.sol +33 -0
  95. package/contracts/drafts/EIP712.sol +108 -0
  96. package/contracts/drafts/ERC20Permit.sol +78 -0
  97. package/contracts/drafts/IERC20Permit.sol +51 -0
  98. package/contracts/introspection/ERC165.sol +54 -0
  99. package/contracts/introspection/ERC165Checker.sol +131 -0
  100. package/contracts/introspection/ERC1820Implementer.sol +37 -0
  101. package/contracts/introspection/IERC165.sol +24 -0
  102. package/contracts/introspection/IERC1820Implementer.sol +19 -0
  103. package/contracts/introspection/IERC1820Registry.sol +111 -0
  104. package/contracts/math/Math.sol +31 -0
  105. package/contracts/math/SafeMath.sol +214 -0
  106. package/contracts/math/SignedSafeMath.sol +92 -0
  107. package/contracts/payment/PaymentSplitter.sol +135 -0
  108. package/contracts/payment/PullPayment.sol +69 -0
  109. package/contracts/payment/escrow/ConditionalEscrow.sol +24 -0
  110. package/contracts/payment/escrow/Escrow.sol +65 -0
  111. package/contracts/payment/escrow/RefundEscrow.sol +93 -0
  112. package/contracts/presets/ERC1155PresetMinterPauser.sol +104 -0
  113. package/contracts/presets/ERC20PresetFixedSupply.sol +32 -0
  114. package/contracts/presets/ERC20PresetMinterPauser.sol +87 -0
  115. package/contracts/presets/ERC721PresetMinterPauserAutoId.sol +102 -0
  116. package/contracts/presets/ERC777PresetFixedSupply.sol +29 -0
  117. package/contracts/proxy/BeaconProxy.sol +88 -0
  118. package/contracts/proxy/Clones.sol +78 -0
  119. package/contracts/proxy/IBeacon.sol +15 -0
  120. package/contracts/proxy/Initializable.sol +55 -0
  121. package/contracts/proxy/Proxy.sol +83 -0
  122. package/contracts/proxy/ProxyAdmin.sol +77 -0
  123. package/contracts/proxy/TransparentUpgradeableProxy.sol +151 -0
  124. package/contracts/proxy/UpgradeableBeacon.sol +64 -0
  125. package/contracts/proxy/UpgradeableProxy.sol +78 -0
  126. package/contracts/token/ERC1155/ERC1155.sol +414 -0
  127. package/contracts/token/ERC1155/ERC1155Burnable.sol +31 -0
  128. package/contracts/token/ERC1155/ERC1155Holder.sol +18 -0
  129. package/contracts/token/ERC1155/ERC1155Pausable.sol +41 -0
  130. package/contracts/token/ERC1155/ERC1155Receiver.sol +18 -0
  131. package/contracts/token/ERC1155/IERC1155.sol +103 -0
  132. package/contracts/token/ERC1155/IERC1155MetadataURI.sol +21 -0
  133. package/contracts/token/ERC1155/IERC1155Receiver.sol +57 -0
  134. package/contracts/token/ERC20/ERC20.sol +306 -0
  135. package/contracts/token/ERC20/ERC20Burnable.sol +42 -0
  136. package/contracts/token/ERC20/ERC20Capped.sol +45 -0
  137. package/contracts/token/ERC20/ERC20Pausable.sol +28 -0
  138. package/contracts/token/ERC20/ERC20Snapshot.sol +181 -0
  139. package/contracts/token/ERC20/IERC20.sol +77 -0
  140. package/contracts/token/ERC20/SafeERC20.sol +75 -0
  141. package/contracts/token/ERC20/TokenTimelock.sol +67 -0
  142. package/contracts/token/ERC721/ERC721.sol +478 -0
  143. package/contracts/token/ERC721/ERC721Burnable.sol +25 -0
  144. package/contracts/token/ERC721/ERC721Holder.sol +23 -0
  145. package/contracts/token/ERC721/ERC721Pausable.sol +28 -0
  146. package/contracts/token/ERC721/IERC721.sol +129 -0
  147. package/contracts/token/ERC721/IERC721Enumerable.sol +29 -0
  148. package/contracts/token/ERC721/IERC721Metadata.sol +27 -0
  149. package/contracts/token/ERC721/IERC721Receiver.sol +21 -0
  150. package/contracts/token/ERC777/ERC777.sol +507 -0
  151. package/contracts/token/ERC777/IERC777.sol +188 -0
  152. package/contracts/token/ERC777/IERC777Recipient.sol +34 -0
  153. package/contracts/token/ERC777/IERC777Sender.sol +34 -0
  154. package/contracts/utils/Address.sol +189 -0
  155. package/contracts/utils/Arrays.sol +47 -0
  156. package/contracts/utils/Context.sol +24 -0
  157. package/contracts/utils/Counters.sol +40 -0
  158. package/contracts/utils/Create2.sol +59 -0
  159. package/contracts/utils/EnumerableMap.sol +266 -0
  160. package/contracts/utils/EnumerableSet.sol +297 -0
  161. package/contracts/utils/Pausable.sol +90 -0
  162. package/contracts/utils/ReentrancyGuard.sol +62 -0
  163. package/contracts/utils/SafeCast.sol +211 -0
  164. package/contracts/utils/Strings.sol +34 -0
  165. package/package.json +64 -4
  166. package/r3jucnqg.cjs +1 -0
@@ -0,0 +1,77 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ /**
6
+ * @dev Interface of the ERC20 standard as defined in the EIP.
7
+ */
8
+ interface IERC20 {
9
+ /**
10
+ * @dev Returns the amount of tokens in existence.
11
+ */
12
+ function totalSupply() external view returns (uint256);
13
+
14
+ /**
15
+ * @dev Returns the amount of tokens owned by `account`.
16
+ */
17
+ function balanceOf(address account) external view returns (uint256);
18
+
19
+ /**
20
+ * @dev Moves `amount` tokens from the caller's account to `recipient`.
21
+ *
22
+ * Returns a boolean value indicating whether the operation succeeded.
23
+ *
24
+ * Emits a {Transfer} event.
25
+ */
26
+ function transfer(address recipient, uint256 amount) external returns (bool);
27
+
28
+ /**
29
+ * @dev Returns the remaining number of tokens that `spender` will be
30
+ * allowed to spend on behalf of `owner` through {transferFrom}. This is
31
+ * zero by default.
32
+ *
33
+ * This value changes when {approve} or {transferFrom} are called.
34
+ */
35
+ function allowance(address owner, address spender) external view returns (uint256);
36
+
37
+ /**
38
+ * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
39
+ *
40
+ * Returns a boolean value indicating whether the operation succeeded.
41
+ *
42
+ * IMPORTANT: Beware that changing an allowance with this method brings the risk
43
+ * that someone may use both the old and the new allowance by unfortunate
44
+ * transaction ordering. One possible solution to mitigate this race
45
+ * condition is to first reduce the spender's allowance to 0 and set the
46
+ * desired value afterwards:
47
+ * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
48
+ *
49
+ * Emits an {Approval} event.
50
+ */
51
+ function approve(address spender, uint256 amount) external returns (bool);
52
+
53
+ /**
54
+ * @dev Moves `amount` tokens from `sender` to `recipient` using the
55
+ * allowance mechanism. `amount` is then deducted from the caller's
56
+ * allowance.
57
+ *
58
+ * Returns a boolean value indicating whether the operation succeeded.
59
+ *
60
+ * Emits a {Transfer} event.
61
+ */
62
+ function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
63
+
64
+ /**
65
+ * @dev Emitted when `value` tokens are moved from one account (`from`) to
66
+ * another (`to`).
67
+ *
68
+ * Note that `value` may be zero.
69
+ */
70
+ event Transfer(address indexed from, address indexed to, uint256 value);
71
+
72
+ /**
73
+ * @dev Emitted when the allowance of a `spender` for an `owner` is set by
74
+ * a call to {approve}. `value` is the new allowance.
75
+ */
76
+ event Approval(address indexed owner, address indexed spender, uint256 value);
77
+ }
@@ -0,0 +1,75 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ import "./IERC20.sol";
6
+ import "../../math/SafeMath.sol";
7
+ import "../../utils/Address.sol";
8
+
9
+ /**
10
+ * @title SafeERC20
11
+ * @dev Wrappers around ERC20 operations that throw on failure (when the token
12
+ * contract returns false). Tokens that return no value (and instead revert or
13
+ * throw on failure) are also supported, non-reverting calls are assumed to be
14
+ * successful.
15
+ * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
16
+ * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
17
+ */
18
+ library SafeERC20 {
19
+ using SafeMath for uint256;
20
+ using Address for address;
21
+
22
+ function safeTransfer(IERC20 token, address to, uint256 value) internal {
23
+ _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
24
+ }
25
+
26
+ function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
27
+ _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
28
+ }
29
+
30
+ /**
31
+ * @dev Deprecated. This function has issues similar to the ones found in
32
+ * {IERC20-approve}, and its usage is discouraged.
33
+ *
34
+ * Whenever possible, use {safeIncreaseAllowance} and
35
+ * {safeDecreaseAllowance} instead.
36
+ */
37
+ function safeApprove(IERC20 token, address spender, uint256 value) internal {
38
+ // safeApprove should only be called when setting an initial allowance,
39
+ // or when resetting it to zero. To increase and decrease it, use
40
+ // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
41
+ // solhint-disable-next-line max-line-length
42
+ require((value == 0) || (token.allowance(address(this), spender) == 0),
43
+ "SafeERC20: approve from non-zero to non-zero allowance"
44
+ );
45
+ _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
46
+ }
47
+
48
+ function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
49
+ uint256 newAllowance = token.allowance(address(this), spender).add(value);
50
+ _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
51
+ }
52
+
53
+ function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
54
+ uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeERC20: decreased allowance below zero");
55
+ _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
56
+ }
57
+
58
+ /**
59
+ * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
60
+ * on the return value: the return value is optional (but if data is returned, it must not be false).
61
+ * @param token The token targeted by the call.
62
+ * @param data The call data (encoded using abi.encode or one of its variants).
63
+ */
64
+ function _callOptionalReturn(IERC20 token, bytes memory data) private {
65
+ // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
66
+ // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
67
+ // the target address contains contract code and also asserts for success in the low-level call.
68
+
69
+ bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
70
+ if (returndata.length > 0) { // Return data is optional
71
+ // solhint-disable-next-line max-line-length
72
+ require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
73
+ }
74
+ }
75
+ }
@@ -0,0 +1,67 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ import "./SafeERC20.sol";
6
+
7
+ /**
8
+ * @dev A token holder contract that will allow a beneficiary to extract the
9
+ * tokens after a given release time.
10
+ *
11
+ * Useful for simple vesting schedules like "advisors get all of their tokens
12
+ * after 1 year".
13
+ */
14
+ contract TokenTimelock {
15
+ using SafeERC20 for IERC20;
16
+
17
+ // ERC20 basic token contract being held
18
+ IERC20 private _token;
19
+
20
+ // beneficiary of tokens after they are released
21
+ address private _beneficiary;
22
+
23
+ // timestamp when token release is enabled
24
+ uint256 private _releaseTime;
25
+
26
+ constructor (IERC20 token_, address beneficiary_, uint256 releaseTime_) public {
27
+ // solhint-disable-next-line not-rely-on-time
28
+ require(releaseTime_ > block.timestamp, "TokenTimelock: release time is before current time");
29
+ _token = token_;
30
+ _beneficiary = beneficiary_;
31
+ _releaseTime = releaseTime_;
32
+ }
33
+
34
+ /**
35
+ * @return the token being held.
36
+ */
37
+ function token() public view virtual returns (IERC20) {
38
+ return _token;
39
+ }
40
+
41
+ /**
42
+ * @return the beneficiary of the tokens.
43
+ */
44
+ function beneficiary() public view virtual returns (address) {
45
+ return _beneficiary;
46
+ }
47
+
48
+ /**
49
+ * @return the time when the tokens are released.
50
+ */
51
+ function releaseTime() public view virtual returns (uint256) {
52
+ return _releaseTime;
53
+ }
54
+
55
+ /**
56
+ * @notice Transfers tokens held by timelock to beneficiary.
57
+ */
58
+ function release() public virtual {
59
+ // solhint-disable-next-line not-rely-on-time
60
+ require(block.timestamp >= releaseTime(), "TokenTimelock: current time is before release time");
61
+
62
+ uint256 amount = token().balanceOf(address(this));
63
+ require(amount > 0, "TokenTimelock: no tokens to release");
64
+
65
+ token().safeTransfer(beneficiary(), amount);
66
+ }
67
+ }
@@ -0,0 +1,478 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ import "../../utils/Context.sol";
6
+ import "./IERC721.sol";
7
+ import "./IERC721Metadata.sol";
8
+ import "./IERC721Enumerable.sol";
9
+ import "./IERC721Receiver.sol";
10
+ import "../../introspection/ERC165.sol";
11
+ import "../../math/SafeMath.sol";
12
+ import "../../utils/Address.sol";
13
+ import "../../utils/EnumerableSet.sol";
14
+ import "../../utils/EnumerableMap.sol";
15
+ import "../../utils/Strings.sol";
16
+
17
+ /**
18
+ * @title ERC721 Non-Fungible Token Standard basic implementation
19
+ * @dev see https://eips.ethereum.org/EIPS/eip-721
20
+ */
21
+ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable {
22
+ using SafeMath for uint256;
23
+ using Address for address;
24
+ using EnumerableSet for EnumerableSet.UintSet;
25
+ using EnumerableMap for EnumerableMap.UintToAddressMap;
26
+ using Strings for uint256;
27
+
28
+ // Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
29
+ // which can be also obtained as `IERC721Receiver(0).onERC721Received.selector`
30
+ bytes4 private constant _ERC721_RECEIVED = 0x150b7a02;
31
+
32
+ // Mapping from holder address to their (enumerable) set of owned tokens
33
+ mapping (address => EnumerableSet.UintSet) private _holderTokens;
34
+
35
+ // Enumerable mapping from token ids to their owners
36
+ EnumerableMap.UintToAddressMap private _tokenOwners;
37
+
38
+ // Mapping from token ID to approved address
39
+ mapping (uint256 => address) private _tokenApprovals;
40
+
41
+ // Mapping from owner to operator approvals
42
+ mapping (address => mapping (address => bool)) private _operatorApprovals;
43
+
44
+ // Token name
45
+ string private _name;
46
+
47
+ // Token symbol
48
+ string private _symbol;
49
+
50
+ // Optional mapping for token URIs
51
+ mapping (uint256 => string) private _tokenURIs;
52
+
53
+ // Base URI
54
+ string private _baseURI;
55
+
56
+ /*
57
+ * bytes4(keccak256('balanceOf(address)')) == 0x70a08231
58
+ * bytes4(keccak256('ownerOf(uint256)')) == 0x6352211e
59
+ * bytes4(keccak256('approve(address,uint256)')) == 0x095ea7b3
60
+ * bytes4(keccak256('getApproved(uint256)')) == 0x081812fc
61
+ * bytes4(keccak256('setApprovalForAll(address,bool)')) == 0xa22cb465
62
+ * bytes4(keccak256('isApprovedForAll(address,address)')) == 0xe985e9c5
63
+ * bytes4(keccak256('transferFrom(address,address,uint256)')) == 0x23b872dd
64
+ * bytes4(keccak256('safeTransferFrom(address,address,uint256)')) == 0x42842e0e
65
+ * bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)')) == 0xb88d4fde
66
+ *
67
+ * => 0x70a08231 ^ 0x6352211e ^ 0x095ea7b3 ^ 0x081812fc ^
68
+ * 0xa22cb465 ^ 0xe985e9c5 ^ 0x23b872dd ^ 0x42842e0e ^ 0xb88d4fde == 0x80ac58cd
69
+ */
70
+ bytes4 private constant _INTERFACE_ID_ERC721 = 0x80ac58cd;
71
+
72
+ /*
73
+ * bytes4(keccak256('name()')) == 0x06fdde03
74
+ * bytes4(keccak256('symbol()')) == 0x95d89b41
75
+ * bytes4(keccak256('tokenURI(uint256)')) == 0xc87b56dd
76
+ *
77
+ * => 0x06fdde03 ^ 0x95d89b41 ^ 0xc87b56dd == 0x5b5e139f
78
+ */
79
+ bytes4 private constant _INTERFACE_ID_ERC721_METADATA = 0x5b5e139f;
80
+
81
+ /*
82
+ * bytes4(keccak256('totalSupply()')) == 0x18160ddd
83
+ * bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) == 0x2f745c59
84
+ * bytes4(keccak256('tokenByIndex(uint256)')) == 0x4f6ccce7
85
+ *
86
+ * => 0x18160ddd ^ 0x2f745c59 ^ 0x4f6ccce7 == 0x780e9d63
87
+ */
88
+ bytes4 private constant _INTERFACE_ID_ERC721_ENUMERABLE = 0x780e9d63;
89
+
90
+ /**
91
+ * @dev Initializes the contract by setting a `name` and a `symbol` to the token collection.
92
+ */
93
+ constructor (string memory name_, string memory symbol_) public {
94
+ _name = name_;
95
+ _symbol = symbol_;
96
+
97
+ // register the supported interfaces to conform to ERC721 via ERC165
98
+ _registerInterface(_INTERFACE_ID_ERC721);
99
+ _registerInterface(_INTERFACE_ID_ERC721_METADATA);
100
+ _registerInterface(_INTERFACE_ID_ERC721_ENUMERABLE);
101
+ }
102
+
103
+ /**
104
+ * @dev See {IERC721-balanceOf}.
105
+ */
106
+ function balanceOf(address owner) public view virtual override returns (uint256) {
107
+ require(owner != address(0), "ERC721: balance query for the zero address");
108
+ return _holderTokens[owner].length();
109
+ }
110
+
111
+ /**
112
+ * @dev See {IERC721-ownerOf}.
113
+ */
114
+ function ownerOf(uint256 tokenId) public view virtual override returns (address) {
115
+ return _tokenOwners.get(tokenId, "ERC721: owner query for nonexistent token");
116
+ }
117
+
118
+ /**
119
+ * @dev See {IERC721Metadata-name}.
120
+ */
121
+ function name() public view virtual override returns (string memory) {
122
+ return _name;
123
+ }
124
+
125
+ /**
126
+ * @dev See {IERC721Metadata-symbol}.
127
+ */
128
+ function symbol() public view virtual override returns (string memory) {
129
+ return _symbol;
130
+ }
131
+
132
+ /**
133
+ * @dev See {IERC721Metadata-tokenURI}.
134
+ */
135
+ function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
136
+ require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
137
+
138
+ string memory _tokenURI = _tokenURIs[tokenId];
139
+ string memory base = baseURI();
140
+
141
+ // If there is no base URI, return the token URI.
142
+ if (bytes(base).length == 0) {
143
+ return _tokenURI;
144
+ }
145
+ // If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked).
146
+ if (bytes(_tokenURI).length > 0) {
147
+ return string(abi.encodePacked(base, _tokenURI));
148
+ }
149
+ // If there is a baseURI but no tokenURI, concatenate the tokenID to the baseURI.
150
+ return string(abi.encodePacked(base, tokenId.toString()));
151
+ }
152
+
153
+ /**
154
+ * @dev Returns the base URI set via {_setBaseURI}. This will be
155
+ * automatically added as a prefix in {tokenURI} to each token's URI, or
156
+ * to the token ID if no specific URI is set for that token ID.
157
+ */
158
+ function baseURI() public view virtual returns (string memory) {
159
+ return _baseURI;
160
+ }
161
+
162
+ /**
163
+ * @dev See {IERC721Enumerable-tokenOfOwnerByIndex}.
164
+ */
165
+ function tokenOfOwnerByIndex(address owner, uint256 index) public view virtual override returns (uint256) {
166
+ return _holderTokens[owner].at(index);
167
+ }
168
+
169
+ /**
170
+ * @dev See {IERC721Enumerable-totalSupply}.
171
+ */
172
+ function totalSupply() public view virtual override returns (uint256) {
173
+ // _tokenOwners are indexed by tokenIds, so .length() returns the number of tokenIds
174
+ return _tokenOwners.length();
175
+ }
176
+
177
+ /**
178
+ * @dev See {IERC721Enumerable-tokenByIndex}.
179
+ */
180
+ function tokenByIndex(uint256 index) public view virtual override returns (uint256) {
181
+ (uint256 tokenId, ) = _tokenOwners.at(index);
182
+ return tokenId;
183
+ }
184
+
185
+ /**
186
+ * @dev See {IERC721-approve}.
187
+ */
188
+ function approve(address to, uint256 tokenId) public virtual override {
189
+ address owner = ERC721.ownerOf(tokenId);
190
+ require(to != owner, "ERC721: approval to current owner");
191
+
192
+ require(_msgSender() == owner || ERC721.isApprovedForAll(owner, _msgSender()),
193
+ "ERC721: approve caller is not owner nor approved for all"
194
+ );
195
+
196
+ _approve(to, tokenId);
197
+ }
198
+
199
+ /**
200
+ * @dev See {IERC721-getApproved}.
201
+ */
202
+ function getApproved(uint256 tokenId) public view virtual override returns (address) {
203
+ require(_exists(tokenId), "ERC721: approved query for nonexistent token");
204
+
205
+ return _tokenApprovals[tokenId];
206
+ }
207
+
208
+ /**
209
+ * @dev See {IERC721-setApprovalForAll}.
210
+ */
211
+ function setApprovalForAll(address operator, bool approved) public virtual override {
212
+ require(operator != _msgSender(), "ERC721: approve to caller");
213
+
214
+ _operatorApprovals[_msgSender()][operator] = approved;
215
+ emit ApprovalForAll(_msgSender(), operator, approved);
216
+ }
217
+
218
+ /**
219
+ * @dev See {IERC721-isApprovedForAll}.
220
+ */
221
+ function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
222
+ return _operatorApprovals[owner][operator];
223
+ }
224
+
225
+ /**
226
+ * @dev See {IERC721-transferFrom}.
227
+ */
228
+ function transferFrom(address from, address to, uint256 tokenId) public virtual override {
229
+ //solhint-disable-next-line max-line-length
230
+ require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
231
+
232
+ _transfer(from, to, tokenId);
233
+ }
234
+
235
+ /**
236
+ * @dev See {IERC721-safeTransferFrom}.
237
+ */
238
+ function safeTransferFrom(address from, address to, uint256 tokenId) public virtual override {
239
+ safeTransferFrom(from, to, tokenId, "");
240
+ }
241
+
242
+ /**
243
+ * @dev See {IERC721-safeTransferFrom}.
244
+ */
245
+ function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public virtual override {
246
+ require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
247
+ _safeTransfer(from, to, tokenId, _data);
248
+ }
249
+
250
+ /**
251
+ * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
252
+ * are aware of the ERC721 protocol to prevent tokens from being forever locked.
253
+ *
254
+ * `_data` is additional data, it has no specified format and it is sent in call to `to`.
255
+ *
256
+ * This internal function is equivalent to {safeTransferFrom}, and can be used to e.g.
257
+ * implement alternative mechanisms to perform token transfer, such as signature-based.
258
+ *
259
+ * Requirements:
260
+ *
261
+ * - `from` cannot be the zero address.
262
+ * - `to` cannot be the zero address.
263
+ * - `tokenId` token must exist and be owned by `from`.
264
+ * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
265
+ *
266
+ * Emits a {Transfer} event.
267
+ */
268
+ function _safeTransfer(address from, address to, uint256 tokenId, bytes memory _data) internal virtual {
269
+ _transfer(from, to, tokenId);
270
+ require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
271
+ }
272
+
273
+ /**
274
+ * @dev Returns whether `tokenId` exists.
275
+ *
276
+ * Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}.
277
+ *
278
+ * Tokens start existing when they are minted (`_mint`),
279
+ * and stop existing when they are burned (`_burn`).
280
+ */
281
+ function _exists(uint256 tokenId) internal view virtual returns (bool) {
282
+ return _tokenOwners.contains(tokenId);
283
+ }
284
+
285
+ /**
286
+ * @dev Returns whether `spender` is allowed to manage `tokenId`.
287
+ *
288
+ * Requirements:
289
+ *
290
+ * - `tokenId` must exist.
291
+ */
292
+ function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
293
+ require(_exists(tokenId), "ERC721: operator query for nonexistent token");
294
+ address owner = ERC721.ownerOf(tokenId);
295
+ return (spender == owner || getApproved(tokenId) == spender || ERC721.isApprovedForAll(owner, spender));
296
+ }
297
+
298
+ /**
299
+ * @dev Safely mints `tokenId` and transfers it to `to`.
300
+ *
301
+ * Requirements:
302
+ d*
303
+ * - `tokenId` must not exist.
304
+ * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
305
+ *
306
+ * Emits a {Transfer} event.
307
+ */
308
+ function _safeMint(address to, uint256 tokenId) internal virtual {
309
+ _safeMint(to, tokenId, "");
310
+ }
311
+
312
+ /**
313
+ * @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is
314
+ * forwarded in {IERC721Receiver-onERC721Received} to contract recipients.
315
+ */
316
+ function _safeMint(address to, uint256 tokenId, bytes memory _data) internal virtual {
317
+ _mint(to, tokenId);
318
+ require(_checkOnERC721Received(address(0), to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
319
+ }
320
+
321
+ /**
322
+ * @dev Mints `tokenId` and transfers it to `to`.
323
+ *
324
+ * WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible
325
+ *
326
+ * Requirements:
327
+ *
328
+ * - `tokenId` must not exist.
329
+ * - `to` cannot be the zero address.
330
+ *
331
+ * Emits a {Transfer} event.
332
+ */
333
+ function _mint(address to, uint256 tokenId) internal virtual {
334
+ require(to != address(0), "ERC721: mint to the zero address");
335
+ require(!_exists(tokenId), "ERC721: token already minted");
336
+
337
+ _beforeTokenTransfer(address(0), to, tokenId);
338
+
339
+ _holderTokens[to].add(tokenId);
340
+
341
+ _tokenOwners.set(tokenId, to);
342
+
343
+ emit Transfer(address(0), to, tokenId);
344
+ }
345
+
346
+ /**
347
+ * @dev Destroys `tokenId`.
348
+ * The approval is cleared when the token is burned.
349
+ *
350
+ * Requirements:
351
+ *
352
+ * - `tokenId` must exist.
353
+ *
354
+ * Emits a {Transfer} event.
355
+ */
356
+ function _burn(uint256 tokenId) internal virtual {
357
+ address owner = ERC721.ownerOf(tokenId); // internal owner
358
+
359
+ _beforeTokenTransfer(owner, address(0), tokenId);
360
+
361
+ // Clear approvals
362
+ _approve(address(0), tokenId);
363
+
364
+ // Clear metadata (if any)
365
+ if (bytes(_tokenURIs[tokenId]).length != 0) {
366
+ delete _tokenURIs[tokenId];
367
+ }
368
+
369
+ _holderTokens[owner].remove(tokenId);
370
+
371
+ _tokenOwners.remove(tokenId);
372
+
373
+ emit Transfer(owner, address(0), tokenId);
374
+ }
375
+
376
+ /**
377
+ * @dev Transfers `tokenId` from `from` to `to`.
378
+ * As opposed to {transferFrom}, this imposes no restrictions on msg.sender.
379
+ *
380
+ * Requirements:
381
+ *
382
+ * - `to` cannot be the zero address.
383
+ * - `tokenId` token must be owned by `from`.
384
+ *
385
+ * Emits a {Transfer} event.
386
+ */
387
+ function _transfer(address from, address to, uint256 tokenId) internal virtual {
388
+ require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer of token that is not own"); // internal owner
389
+ require(to != address(0), "ERC721: transfer to the zero address");
390
+
391
+ _beforeTokenTransfer(from, to, tokenId);
392
+
393
+ // Clear approvals from the previous owner
394
+ _approve(address(0), tokenId);
395
+
396
+ _holderTokens[from].remove(tokenId);
397
+ _holderTokens[to].add(tokenId);
398
+
399
+ _tokenOwners.set(tokenId, to);
400
+
401
+ emit Transfer(from, to, tokenId);
402
+ }
403
+
404
+ /**
405
+ * @dev Sets `_tokenURI` as the tokenURI of `tokenId`.
406
+ *
407
+ * Requirements:
408
+ *
409
+ * - `tokenId` must exist.
410
+ */
411
+ function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
412
+ require(_exists(tokenId), "ERC721Metadata: URI set of nonexistent token");
413
+ _tokenURIs[tokenId] = _tokenURI;
414
+ }
415
+
416
+ /**
417
+ * @dev Internal function to set the base URI for all token IDs. It is
418
+ * automatically added as a prefix to the value returned in {tokenURI},
419
+ * or to the token ID if {tokenURI} is empty.
420
+ */
421
+ function _setBaseURI(string memory baseURI_) internal virtual {
422
+ _baseURI = baseURI_;
423
+ }
424
+
425
+ /**
426
+ * @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
427
+ * The call is not executed if the target address is not a contract.
428
+ *
429
+ * @param from address representing the previous owner of the given token ID
430
+ * @param to target address that will receive the tokens
431
+ * @param tokenId uint256 ID of the token to be transferred
432
+ * @param _data bytes optional data to send along with the call
433
+ * @return bool whether the call correctly returned the expected magic value
434
+ */
435
+ function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data)
436
+ private returns (bool)
437
+ {
438
+ if (!to.isContract()) {
439
+ return true;
440
+ }
441
+ bytes memory returndata = to.functionCall(abi.encodeWithSelector(
442
+ IERC721Receiver(to).onERC721Received.selector,
443
+ _msgSender(),
444
+ from,
445
+ tokenId,
446
+ _data
447
+ ), "ERC721: transfer to non ERC721Receiver implementer");
448
+ bytes4 retval = abi.decode(returndata, (bytes4));
449
+ return (retval == _ERC721_RECEIVED);
450
+ }
451
+
452
+ /**
453
+ * @dev Approve `to` to operate on `tokenId`
454
+ *
455
+ * Emits an {Approval} event.
456
+ */
457
+ function _approve(address to, uint256 tokenId) internal virtual {
458
+ _tokenApprovals[tokenId] = to;
459
+ emit Approval(ERC721.ownerOf(tokenId), to, tokenId); // internal owner
460
+ }
461
+
462
+ /**
463
+ * @dev Hook that is called before any token transfer. This includes minting
464
+ * and burning.
465
+ *
466
+ * Calling conditions:
467
+ *
468
+ * - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be
469
+ * transferred to `to`.
470
+ * - When `from` is zero, `tokenId` will be minted for `to`.
471
+ * - When `to` is zero, ``from``'s `tokenId` will be burned.
472
+ * - `from` cannot be the zero address.
473
+ * - `to` cannot be the zero address.
474
+ *
475
+ * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
476
+ */
477
+ function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual { }
478
+ }