|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
library Math {
|
|
enum Rounding {
|
|
Down,
|
|
Up,
|
|
Zero
|
|
}
|
|
|
|
|
|
|
|
|
|
function max(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
return a > b ? a : b;
|
|
}
|
|
|
|
|
|
|
|
|
|
function min(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
return a < b ? a : b;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function average(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
|
|
return (a & b) + (a ^ b) / 2;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
|
|
return a == 0 ? 0 : (a - 1) / b + 1;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function mulDiv(
|
|
uint256 x,
|
|
uint256 y,
|
|
uint256 denominator
|
|
) internal pure returns (uint256 result) {
|
|
unchecked {
|
|
|
|
|
|
|
|
uint256 prod0;
|
|
uint256 prod1;
|
|
assembly {
|
|
let mm := mulmod(x, y, not(0))
|
|
prod0 := mul(x, y)
|
|
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
|
|
}
|
|
|
|
|
|
if (prod1 == 0) {
|
|
return prod0 / denominator;
|
|
}
|
|
|
|
|
|
require(denominator > prod1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint256 remainder;
|
|
assembly {
|
|
|
|
remainder := mulmod(x, y, denominator)
|
|
|
|
|
|
prod1 := sub(prod1, gt(remainder, prod0))
|
|
prod0 := sub(prod0, remainder)
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint256 twos = denominator & (~denominator + 1);
|
|
assembly {
|
|
|
|
denominator := div(denominator, twos)
|
|
|
|
|
|
prod0 := div(prod0, twos)
|
|
|
|
|
|
twos := add(div(sub(0, twos), twos), 1)
|
|
}
|
|
|
|
|
|
prod0 |= prod1 * twos;
|
|
|
|
|
|
|
|
|
|
uint256 inverse = (3 * denominator) ^ 2;
|
|
|
|
|
|
|
|
inverse *= 2 - denominator * inverse;
|
|
inverse *= 2 - denominator * inverse;
|
|
inverse *= 2 - denominator * inverse;
|
|
inverse *= 2 - denominator * inverse;
|
|
inverse *= 2 - denominator * inverse;
|
|
inverse *= 2 - denominator * inverse;
|
|
|
|
|
|
|
|
|
|
|
|
result = prod0 * inverse;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
function mulDiv(
|
|
uint256 x,
|
|
uint256 y,
|
|
uint256 denominator,
|
|
Rounding rounding
|
|
) internal pure returns (uint256) {
|
|
uint256 result = mulDiv(x, y, denominator);
|
|
if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
|
|
result += 1;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function sqrt(uint256 a) internal pure returns (uint256) {
|
|
if (a == 0) {
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint256 result = 1 << (log2(a) >> 1);
|
|
|
|
|
|
|
|
|
|
|
|
unchecked {
|
|
result = (result + a / result) >> 1;
|
|
result = (result + a / result) >> 1;
|
|
result = (result + a / result) >> 1;
|
|
result = (result + a / result) >> 1;
|
|
result = (result + a / result) >> 1;
|
|
result = (result + a / result) >> 1;
|
|
result = (result + a / result) >> 1;
|
|
return min(result, a / result);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
|
|
unchecked {
|
|
uint256 result = sqrt(a);
|
|
return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function log2(uint256 value) internal pure returns (uint256) {
|
|
uint256 result = 0;
|
|
unchecked {
|
|
if (value >> 128 > 0) {
|
|
value >>= 128;
|
|
result += 128;
|
|
}
|
|
if (value >> 64 > 0) {
|
|
value >>= 64;
|
|
result += 64;
|
|
}
|
|
if (value >> 32 > 0) {
|
|
value >>= 32;
|
|
result += 32;
|
|
}
|
|
if (value >> 16 > 0) {
|
|
value >>= 16;
|
|
result += 16;
|
|
}
|
|
if (value >> 8 > 0) {
|
|
value >>= 8;
|
|
result += 8;
|
|
}
|
|
if (value >> 4 > 0) {
|
|
value >>= 4;
|
|
result += 4;
|
|
}
|
|
if (value >> 2 > 0) {
|
|
value >>= 2;
|
|
result += 2;
|
|
}
|
|
if (value >> 1 > 0) {
|
|
result += 1;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
|
|
unchecked {
|
|
uint256 result = log2(value);
|
|
return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function log10(uint256 value) internal pure returns (uint256) {
|
|
uint256 result = 0;
|
|
unchecked {
|
|
if (value >= 10**64) {
|
|
value /= 10**64;
|
|
result += 64;
|
|
}
|
|
if (value >= 10**32) {
|
|
value /= 10**32;
|
|
result += 32;
|
|
}
|
|
if (value >= 10**16) {
|
|
value /= 10**16;
|
|
result += 16;
|
|
}
|
|
if (value >= 10**8) {
|
|
value /= 10**8;
|
|
result += 8;
|
|
}
|
|
if (value >= 10**4) {
|
|
value /= 10**4;
|
|
result += 4;
|
|
}
|
|
if (value >= 10**2) {
|
|
value /= 10**2;
|
|
result += 2;
|
|
}
|
|
if (value >= 10**1) {
|
|
result += 1;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
|
|
unchecked {
|
|
uint256 result = log10(value);
|
|
return result + (rounding == Rounding.Up && 10**result < value ? 1 : 0);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function log256(uint256 value) internal pure returns (uint256) {
|
|
uint256 result = 0;
|
|
unchecked {
|
|
if (value >> 128 > 0) {
|
|
value >>= 128;
|
|
result += 16;
|
|
}
|
|
if (value >> 64 > 0) {
|
|
value >>= 64;
|
|
result += 8;
|
|
}
|
|
if (value >> 32 > 0) {
|
|
value >>= 32;
|
|
result += 4;
|
|
}
|
|
if (value >> 16 > 0) {
|
|
value >>= 16;
|
|
result += 2;
|
|
}
|
|
if (value >> 8 > 0) {
|
|
result += 1;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
|
|
unchecked {
|
|
uint256 result = log256(value);
|
|
return result + (rounding == Rounding.Up && 1 << (result * 8) < value ? 1 : 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
library Strings {
|
|
bytes16 private constant _SYMBOLS = "0123456789abcdef";
|
|
uint8 private constant _ADDRESS_LENGTH = 20;
|
|
|
|
|
|
|
|
|
|
function toString(uint256 value) internal pure returns (string memory) {
|
|
unchecked {
|
|
uint256 length = Math.log10(value) + 1;
|
|
string memory buffer = new string(length);
|
|
uint256 ptr;
|
|
|
|
assembly {
|
|
ptr := add(buffer, add(32, length))
|
|
}
|
|
while (true) {
|
|
ptr--;
|
|
|
|
assembly {
|
|
mstore8(ptr, byte(mod(value, 10), _SYMBOLS))
|
|
}
|
|
value /= 10;
|
|
if (value == 0) break;
|
|
}
|
|
return buffer;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
function toHexString(uint256 value) internal pure returns (string memory) {
|
|
unchecked {
|
|
return toHexString(value, Math.log256(value) + 1);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
|
|
bytes memory buffer = new bytes(2 * length + 2);
|
|
buffer[0] = "0";
|
|
buffer[1] = "x";
|
|
for (uint256 i = 2 * length + 1; i > 1; --i) {
|
|
buffer[i] = _SYMBOLS[value & 0xf];
|
|
value >>= 4;
|
|
}
|
|
require(value == 0, "Strings: hex length insufficient");
|
|
return string(buffer);
|
|
}
|
|
|
|
|
|
|
|
|
|
function toHexString(address addr) internal pure returns (string memory) {
|
|
return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract Context {
|
|
function _msgSender() internal view virtual returns (address) {
|
|
return msg.sender;
|
|
}
|
|
|
|
function _msgData() internal view virtual returns (bytes calldata) {
|
|
return msg.data;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract Pausable is Context {
|
|
|
|
|
|
|
|
event Paused(address account);
|
|
|
|
|
|
|
|
|
|
event Unpaused(address account);
|
|
|
|
bool private _paused;
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
_paused = false;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
modifier whenNotPaused() {
|
|
_requireNotPaused();
|
|
_;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
modifier whenPaused() {
|
|
_requirePaused();
|
|
_;
|
|
}
|
|
|
|
|
|
|
|
|
|
function paused() public view virtual returns (bool) {
|
|
return _paused;
|
|
}
|
|
|
|
|
|
|
|
|
|
function _requireNotPaused() internal view virtual {
|
|
require(!paused(), "Pausable: paused");
|
|
}
|
|
|
|
|
|
|
|
|
|
function _requirePaused() internal view virtual {
|
|
require(paused(), "Pausable: not paused");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _pause() internal virtual whenNotPaused {
|
|
_paused = true;
|
|
emit Paused(_msgSender());
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _unpause() internal virtual whenPaused {
|
|
_paused = false;
|
|
emit Unpaused(_msgSender());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract Ownable is Context {
|
|
address private _owner;
|
|
|
|
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
_transferOwnership(_msgSender());
|
|
}
|
|
|
|
|
|
|
|
|
|
modifier onlyOwner() {
|
|
_checkOwner();
|
|
_;
|
|
}
|
|
|
|
|
|
|
|
|
|
function owner() public view virtual returns (address) {
|
|
return _owner;
|
|
}
|
|
|
|
|
|
|
|
|
|
function _checkOwner() internal view virtual {
|
|
require(owner() == _msgSender(), "Ownable: caller is not the owner");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function renounceOwnership() public virtual onlyOwner {
|
|
_transferOwnership(address(0));
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function transferOwnership(address newOwner) public virtual onlyOwner {
|
|
require(newOwner != address(0), "Ownable: new owner is the zero address");
|
|
_transferOwnership(newOwner);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function _transferOwnership(address newOwner) internal virtual {
|
|
address oldOwner = _owner;
|
|
_owner = newOwner;
|
|
emit OwnershipTransferred(oldOwner, newOwner);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.1;
|
|
|
|
|
|
|
|
|
|
library Address {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isContract(address account) internal view returns (bool) {
|
|
|
|
|
|
|
|
|
|
return account.code.length > 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function sendValue(address payable recipient, uint256 amount) internal {
|
|
require(address(this).balance >= amount, "Address: insufficient balance");
|
|
|
|
(bool success, ) = recipient.call{value: amount}("");
|
|
require(success, "Address: unable to send value, recipient may have reverted");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
|
|
return functionCallWithValue(target, data, 0, "Address: low-level call failed");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionCall(
|
|
address target,
|
|
bytes memory data,
|
|
string memory errorMessage
|
|
) internal returns (bytes memory) {
|
|
return functionCallWithValue(target, data, 0, errorMessage);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionCallWithValue(
|
|
address target,
|
|
bytes memory data,
|
|
uint256 value
|
|
) internal returns (bytes memory) {
|
|
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionCallWithValue(
|
|
address target,
|
|
bytes memory data,
|
|
uint256 value,
|
|
string memory errorMessage
|
|
) internal returns (bytes memory) {
|
|
require(address(this).balance >= value, "Address: insufficient balance for call");
|
|
(bool success, bytes memory returndata) = target.call{value: value}(data);
|
|
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
|
|
return functionStaticCall(target, data, "Address: low-level static call failed");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionStaticCall(
|
|
address target,
|
|
bytes memory data,
|
|
string memory errorMessage
|
|
) internal view returns (bytes memory) {
|
|
(bool success, bytes memory returndata) = target.staticcall(data);
|
|
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
|
|
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function functionDelegateCall(
|
|
address target,
|
|
bytes memory data,
|
|
string memory errorMessage
|
|
) internal returns (bytes memory) {
|
|
(bool success, bytes memory returndata) = target.delegatecall(data);
|
|
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function verifyCallResultFromTarget(
|
|
address target,
|
|
bool success,
|
|
bytes memory returndata,
|
|
string memory errorMessage
|
|
) internal view returns (bytes memory) {
|
|
if (success) {
|
|
if (returndata.length == 0) {
|
|
|
|
|
|
require(isContract(target), "Address: call to non-contract");
|
|
}
|
|
return returndata;
|
|
} else {
|
|
_revert(returndata, errorMessage);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function verifyCallResult(
|
|
bool success,
|
|
bytes memory returndata,
|
|
string memory errorMessage
|
|
) internal pure returns (bytes memory) {
|
|
if (success) {
|
|
return returndata;
|
|
} else {
|
|
_revert(returndata, errorMessage);
|
|
}
|
|
}
|
|
|
|
function _revert(bytes memory returndata, string memory errorMessage) private pure {
|
|
|
|
if (returndata.length > 0) {
|
|
|
|
|
|
assembly {
|
|
let returndata_size := mload(returndata)
|
|
revert(add(32, returndata), returndata_size)
|
|
}
|
|
} else {
|
|
revert(errorMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface IERC165 {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function supportsInterface(bytes4 interfaceId) external view returns (bool);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface IERC2981 is IERC165 {
|
|
|
|
|
|
|
|
|
|
function royaltyInfo(uint256 tokenId, uint256 salePrice)
|
|
external
|
|
view
|
|
returns (address receiver, uint256 royaltyAmount);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract ERC165 is IERC165 {
|
|
|
|
|
|
|
|
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
|
|
return interfaceId == type(IERC165).interfaceId;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
interface IERC1155Receiver is IERC165 {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function onERC1155Received(
|
|
address operator,
|
|
address from,
|
|
uint256 id,
|
|
uint256 value,
|
|
bytes calldata data
|
|
) external returns (bytes4);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function onERC1155BatchReceived(
|
|
address operator,
|
|
address from,
|
|
uint256[] calldata ids,
|
|
uint256[] calldata values,
|
|
bytes calldata data
|
|
) external returns (bytes4);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface IERC1155 is IERC165 {
|
|
|
|
|
|
|
|
event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);
|
|
|
|
|
|
|
|
|
|
|
|
event TransferBatch(
|
|
address indexed operator,
|
|
address indexed from,
|
|
address indexed to,
|
|
uint256[] ids,
|
|
uint256[] values
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
event ApprovalForAll(address indexed account, address indexed operator, bool approved);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
event URI(string value, uint256 indexed id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function balanceOf(address account, uint256 id) external view returns (uint256);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function balanceOfBatch(address[] calldata accounts, uint256[] calldata ids)
|
|
external
|
|
view
|
|
returns (uint256[] memory);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function setApprovalForAll(address operator, bool approved) external;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isApprovedForAll(address account, address operator) external view returns (bool);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function safeTransferFrom(
|
|
address from,
|
|
address to,
|
|
uint256 id,
|
|
uint256 amount,
|
|
bytes calldata data
|
|
) external;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function safeBatchTransferFrom(
|
|
address from,
|
|
address to,
|
|
uint256[] calldata ids,
|
|
uint256[] calldata amounts,
|
|
bytes calldata data
|
|
) external;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface IERC1155MetadataURI is IERC1155 {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function uri(uint256 id) external view returns (string memory);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
|
|
using Address for address;
|
|
|
|
|
|
mapping(uint256 => mapping(address => uint256)) private _balances;
|
|
|
|
|
|
mapping(address => mapping(address => bool)) private _operatorApprovals;
|
|
|
|
|
|
string private _uri;
|
|
|
|
|
|
|
|
|
|
constructor(string memory uri_) {
|
|
_setURI(uri_);
|
|
}
|
|
|
|
|
|
|
|
|
|
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
|
|
return
|
|
interfaceId == type(IERC1155).interfaceId ||
|
|
interfaceId == type(IERC1155MetadataURI).interfaceId ||
|
|
super.supportsInterface(interfaceId);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function uri(uint256) public view virtual override returns (string memory) {
|
|
return _uri;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function balanceOf(address account, uint256 id) public view virtual override returns (uint256) {
|
|
require(account != address(0), "ERC1155: address zero is not a valid owner");
|
|
return _balances[id][account];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function balanceOfBatch(address[] memory accounts, uint256[] memory ids)
|
|
public
|
|
view
|
|
virtual
|
|
override
|
|
returns (uint256[] memory)
|
|
{
|
|
require(accounts.length == ids.length, "ERC1155: accounts and ids length mismatch");
|
|
|
|
uint256[] memory batchBalances = new uint256[](accounts.length);
|
|
|
|
for (uint256 i = 0; i < accounts.length; ++i) {
|
|
batchBalances[i] = balanceOf(accounts[i], ids[i]);
|
|
}
|
|
|
|
return batchBalances;
|
|
}
|
|
|
|
|
|
|
|
|
|
function setApprovalForAll(address operator, bool approved) public virtual override {
|
|
_setApprovalForAll(_msgSender(), operator, approved);
|
|
}
|
|
|
|
|
|
|
|
|
|
function isApprovedForAll(address account, address operator) public view virtual override returns (bool) {
|
|
return _operatorApprovals[account][operator];
|
|
}
|
|
|
|
|
|
|
|
|
|
function safeTransferFrom(
|
|
address from,
|
|
address to,
|
|
uint256 id,
|
|
uint256 amount,
|
|
bytes memory data
|
|
) public virtual override {
|
|
require(
|
|
from == _msgSender() || isApprovedForAll(from, _msgSender()),
|
|
"ERC1155: caller is not token owner or approved"
|
|
);
|
|
_safeTransferFrom(from, to, id, amount, data);
|
|
}
|
|
|
|
|
|
|
|
|
|
function safeBatchTransferFrom(
|
|
address from,
|
|
address to,
|
|
uint256[] memory ids,
|
|
uint256[] memory amounts,
|
|
bytes memory data
|
|
) public virtual override {
|
|
require(
|
|
from == _msgSender() || isApprovedForAll(from, _msgSender()),
|
|
"ERC1155: caller is not token owner or approved"
|
|
);
|
|
_safeBatchTransferFrom(from, to, ids, amounts, data);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _safeTransferFrom(
|
|
address from,
|
|
address to,
|
|
uint256 id,
|
|
uint256 amount,
|
|
bytes memory data
|
|
) internal virtual {
|
|
require(to != address(0), "ERC1155: transfer to the zero address");
|
|
|
|
address operator = _msgSender();
|
|
uint256[] memory ids = _asSingletonArray(id);
|
|
uint256[] memory amounts = _asSingletonArray(amount);
|
|
|
|
_beforeTokenTransfer(operator, from, to, ids, amounts, data);
|
|
|
|
uint256 fromBalance = _balances[id][from];
|
|
require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
|
|
unchecked {
|
|
_balances[id][from] = fromBalance - amount;
|
|
}
|
|
_balances[id][to] += amount;
|
|
|
|
emit TransferSingle(operator, from, to, id, amount);
|
|
|
|
_afterTokenTransfer(operator, from, to, ids, amounts, data);
|
|
|
|
_doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _safeBatchTransferFrom(
|
|
address from,
|
|
address to,
|
|
uint256[] memory ids,
|
|
uint256[] memory amounts,
|
|
bytes memory data
|
|
) internal virtual {
|
|
require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
|
|
require(to != address(0), "ERC1155: transfer to the zero address");
|
|
|
|
address operator = _msgSender();
|
|
|
|
_beforeTokenTransfer(operator, from, to, ids, amounts, data);
|
|
|
|
for (uint256 i = 0; i < ids.length; ++i) {
|
|
uint256 id = ids[i];
|
|
uint256 amount = amounts[i];
|
|
|
|
uint256 fromBalance = _balances[id][from];
|
|
require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
|
|
unchecked {
|
|
_balances[id][from] = fromBalance - amount;
|
|
}
|
|
_balances[id][to] += amount;
|
|
}
|
|
|
|
emit TransferBatch(operator, from, to, ids, amounts);
|
|
|
|
_afterTokenTransfer(operator, from, to, ids, amounts, data);
|
|
|
|
_doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _setURI(string memory newuri) internal virtual {
|
|
_uri = newuri;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _mint(
|
|
address to,
|
|
uint256 id,
|
|
uint256 amount,
|
|
bytes memory data
|
|
) internal virtual {
|
|
require(to != address(0), "ERC1155: mint to the zero address");
|
|
|
|
address operator = _msgSender();
|
|
uint256[] memory ids = _asSingletonArray(id);
|
|
uint256[] memory amounts = _asSingletonArray(amount);
|
|
|
|
_beforeTokenTransfer(operator, address(0), to, ids, amounts, data);
|
|
|
|
_balances[id][to] += amount;
|
|
emit TransferSingle(operator, address(0), to, id, amount);
|
|
|
|
_afterTokenTransfer(operator, address(0), to, ids, amounts, data);
|
|
|
|
_doSafeTransferAcceptanceCheck(operator, address(0), to, id, amount, data);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _mintBatch(
|
|
address to,
|
|
uint256[] memory ids,
|
|
uint256[] memory amounts,
|
|
bytes memory data
|
|
) internal virtual {
|
|
require(to != address(0), "ERC1155: mint to the zero address");
|
|
require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
|
|
|
|
address operator = _msgSender();
|
|
|
|
_beforeTokenTransfer(operator, address(0), to, ids, amounts, data);
|
|
|
|
for (uint256 i = 0; i < ids.length; i++) {
|
|
_balances[ids[i]][to] += amounts[i];
|
|
}
|
|
|
|
emit TransferBatch(operator, address(0), to, ids, amounts);
|
|
|
|
_afterTokenTransfer(operator, address(0), to, ids, amounts, data);
|
|
|
|
_doSafeBatchTransferAcceptanceCheck(operator, address(0), to, ids, amounts, data);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _burn(
|
|
address from,
|
|
uint256 id,
|
|
uint256 amount
|
|
) internal virtual {
|
|
require(from != address(0), "ERC1155: burn from the zero address");
|
|
|
|
address operator = _msgSender();
|
|
uint256[] memory ids = _asSingletonArray(id);
|
|
uint256[] memory amounts = _asSingletonArray(amount);
|
|
|
|
_beforeTokenTransfer(operator, from, address(0), ids, amounts, "");
|
|
|
|
uint256 fromBalance = _balances[id][from];
|
|
require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");
|
|
unchecked {
|
|
_balances[id][from] = fromBalance - amount;
|
|
}
|
|
|
|
emit TransferSingle(operator, from, address(0), id, amount);
|
|
|
|
_afterTokenTransfer(operator, from, address(0), ids, amounts, "");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _burnBatch(
|
|
address from,
|
|
uint256[] memory ids,
|
|
uint256[] memory amounts
|
|
) internal virtual {
|
|
require(from != address(0), "ERC1155: burn from the zero address");
|
|
require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
|
|
|
|
address operator = _msgSender();
|
|
|
|
_beforeTokenTransfer(operator, from, address(0), ids, amounts, "");
|
|
|
|
for (uint256 i = 0; i < ids.length; i++) {
|
|
uint256 id = ids[i];
|
|
uint256 amount = amounts[i];
|
|
|
|
uint256 fromBalance = _balances[id][from];
|
|
require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");
|
|
unchecked {
|
|
_balances[id][from] = fromBalance - amount;
|
|
}
|
|
}
|
|
|
|
emit TransferBatch(operator, from, address(0), ids, amounts);
|
|
|
|
_afterTokenTransfer(operator, from, address(0), ids, amounts, "");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _setApprovalForAll(
|
|
address owner,
|
|
address operator,
|
|
bool approved
|
|
) internal virtual {
|
|
require(owner != operator, "ERC1155: setting approval status for self");
|
|
_operatorApprovals[owner][operator] = approved;
|
|
emit ApprovalForAll(owner, operator, approved);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _beforeTokenTransfer(
|
|
address operator,
|
|
address from,
|
|
address to,
|
|
uint256[] memory ids,
|
|
uint256[] memory amounts,
|
|
bytes memory data
|
|
) internal virtual {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _afterTokenTransfer(
|
|
address operator,
|
|
address from,
|
|
address to,
|
|
uint256[] memory ids,
|
|
uint256[] memory amounts,
|
|
bytes memory data
|
|
) internal virtual {}
|
|
|
|
function _doSafeTransferAcceptanceCheck(
|
|
address operator,
|
|
address from,
|
|
address to,
|
|
uint256 id,
|
|
uint256 amount,
|
|
bytes memory data
|
|
) private {
|
|
if (to.isContract()) {
|
|
try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
|
|
if (response != IERC1155Receiver.onERC1155Received.selector) {
|
|
revert("ERC1155: ERC1155Receiver rejected tokens");
|
|
}
|
|
} catch Error(string memory reason) {
|
|
revert(reason);
|
|
} catch {
|
|
revert("ERC1155: transfer to non-ERC1155Receiver implementer");
|
|
}
|
|
}
|
|
}
|
|
|
|
function _doSafeBatchTransferAcceptanceCheck(
|
|
address operator,
|
|
address from,
|
|
address to,
|
|
uint256[] memory ids,
|
|
uint256[] memory amounts,
|
|
bytes memory data
|
|
) private {
|
|
if (to.isContract()) {
|
|
try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
|
|
bytes4 response
|
|
) {
|
|
if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
|
|
revert("ERC1155: ERC1155Receiver rejected tokens");
|
|
}
|
|
} catch Error(string memory reason) {
|
|
revert(reason);
|
|
} catch {
|
|
revert("ERC1155: transfer to non-ERC1155Receiver implementer");
|
|
}
|
|
}
|
|
}
|
|
|
|
function _asSingletonArray(uint256 element) private pure returns (uint256[] memory) {
|
|
uint256[] memory array = new uint256[](1);
|
|
array[0] = element;
|
|
|
|
return array;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract ERC1155URIStorage is ERC1155 {
|
|
using Strings for uint256;
|
|
|
|
|
|
string private _baseURI = "";
|
|
|
|
|
|
mapping(uint256 => string) private _tokenURIs;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function uri(uint256 tokenId) public view virtual override returns (string memory) {
|
|
string memory tokenURI = _tokenURIs[tokenId];
|
|
|
|
|
|
return bytes(tokenURI).length > 0 ? string(abi.encodePacked(_baseURI, tokenURI)) : super.uri(tokenId);
|
|
}
|
|
|
|
|
|
|
|
|
|
function _setURI(uint256 tokenId, string memory tokenURI) internal virtual {
|
|
_tokenURIs[tokenId] = tokenURI;
|
|
emit URI(uri(tokenId), tokenId);
|
|
}
|
|
|
|
|
|
|
|
|
|
function _setBaseURI(string memory baseURI) internal virtual {
|
|
_baseURI = baseURI;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract ERC1155Supply is ERC1155 {
|
|
mapping(uint256 => uint256) private _totalSupply;
|
|
|
|
|
|
|
|
|
|
function totalSupply(uint256 id) public view virtual returns (uint256) {
|
|
return _totalSupply[id];
|
|
}
|
|
|
|
|
|
|
|
|
|
function exists(uint256 id) public view virtual returns (bool) {
|
|
return ERC1155Supply.totalSupply(id) > 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
function _beforeTokenTransfer(
|
|
address operator,
|
|
address from,
|
|
address to,
|
|
uint256[] memory ids,
|
|
uint256[] memory amounts,
|
|
bytes memory data
|
|
) internal virtual override {
|
|
super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
|
|
|
|
if (from == address(0)) {
|
|
for (uint256 i = 0; i < ids.length; ++i) {
|
|
_totalSupply[ids[i]] += amounts[i];
|
|
}
|
|
}
|
|
|
|
if (to == address(0)) {
|
|
for (uint256 i = 0; i < ids.length; ++i) {
|
|
uint256 id = ids[i];
|
|
uint256 amount = amounts[i];
|
|
uint256 supply = _totalSupply[id];
|
|
require(supply >= amount, "ERC1155: burn amount exceeds totalSupply");
|
|
unchecked {
|
|
_totalSupply[id] = supply - amount;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract ERC1155Burnable is ERC1155 {
|
|
function burn(
|
|
address account,
|
|
uint256 id,
|
|
uint256 value
|
|
) public virtual {
|
|
require(
|
|
account == _msgSender() || isApprovedForAll(account, _msgSender()),
|
|
"ERC1155: caller is not token owner or approved"
|
|
);
|
|
|
|
_burn(account, id, value);
|
|
}
|
|
|
|
function burnBatch(
|
|
address account,
|
|
uint256[] memory ids,
|
|
uint256[] memory values
|
|
) public virtual {
|
|
require(
|
|
account == _msgSender() || isApprovedForAll(account, _msgSender()),
|
|
"ERC1155: caller is not token owner or approved"
|
|
);
|
|
|
|
_burnBatch(account, ids, values);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface IOperatorFilterRegistry {
|
|
function isOperatorAllowed(address registrant, address operator)
|
|
external
|
|
view
|
|
returns (bool);
|
|
|
|
function register(address registrant) external;
|
|
|
|
function registerAndSubscribe(address registrant, address subscription)
|
|
external;
|
|
|
|
function registerAndCopyEntries(
|
|
address registrant,
|
|
address registrantToCopy
|
|
) external;
|
|
|
|
function unregister(address addr) external;
|
|
|
|
function updateOperator(
|
|
address registrant,
|
|
address operator,
|
|
bool filtered
|
|
) external;
|
|
|
|
function updateOperators(
|
|
address registrant,
|
|
address[] calldata operators,
|
|
bool filtered
|
|
) external;
|
|
|
|
function updateCodeHash(
|
|
address registrant,
|
|
bytes32 codehash,
|
|
bool filtered
|
|
) external;
|
|
|
|
function updateCodeHashes(
|
|
address registrant,
|
|
bytes32[] calldata codeHashes,
|
|
bool filtered
|
|
) external;
|
|
|
|
function subscribe(address registrant, address registrantToSubscribe)
|
|
external;
|
|
|
|
function unsubscribe(address registrant, bool copyExistingEntries) external;
|
|
|
|
function subscriptionOf(address addr) external returns (address registrant);
|
|
|
|
function subscribers(address registrant)
|
|
external
|
|
returns (address[] memory);
|
|
|
|
function subscriberAt(address registrant, uint256 index)
|
|
external
|
|
returns (address);
|
|
|
|
function copyEntriesOf(address registrant, address registrantToCopy)
|
|
external;
|
|
|
|
function isOperatorFiltered(address registrant, address operator)
|
|
external
|
|
returns (bool);
|
|
|
|
function isCodeHashOfFiltered(address registrant, address operatorWithCode)
|
|
external
|
|
returns (bool);
|
|
|
|
function isCodeHashFiltered(address registrant, bytes32 codeHash)
|
|
external
|
|
returns (bool);
|
|
|
|
function filteredOperators(address addr)
|
|
external
|
|
returns (address[] memory);
|
|
|
|
function filteredCodeHashes(address addr)
|
|
external
|
|
returns (bytes32[] memory);
|
|
|
|
function filteredOperatorAt(address registrant, uint256 index)
|
|
external
|
|
returns (address);
|
|
|
|
function filteredCodeHashAt(address registrant, uint256 index)
|
|
external
|
|
returns (bytes32);
|
|
|
|
function isRegistered(address addr) external returns (bool);
|
|
|
|
function codeHashOf(address addr) external returns (bytes32);
|
|
}
|
|
|
|
abstract contract OperatorFilterer {
|
|
error OperatorNotAllowed(address operator);
|
|
|
|
IOperatorFilterRegistry constant operatorFilterRegistry =
|
|
IOperatorFilterRegistry(0x000000000000AAeB6D7670E522A718067333cd4E);
|
|
|
|
constructor(address subscriptionOrRegistrantToCopy, bool subscribe) {
|
|
|
|
|
|
|
|
if (address(operatorFilterRegistry).code.length > 0) {
|
|
if (subscribe) {
|
|
operatorFilterRegistry.registerAndSubscribe(
|
|
address(this),
|
|
subscriptionOrRegistrantToCopy
|
|
);
|
|
} else {
|
|
if (subscriptionOrRegistrantToCopy != address(0)) {
|
|
operatorFilterRegistry.registerAndCopyEntries(
|
|
address(this),
|
|
subscriptionOrRegistrantToCopy
|
|
);
|
|
} else {
|
|
operatorFilterRegistry.register(address(this));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
modifier onlyAllowedOperator(address from) virtual {
|
|
|
|
if (address(operatorFilterRegistry).code.length > 0) {
|
|
|
|
|
|
|
|
if (from == msg.sender) {
|
|
_;
|
|
return;
|
|
}
|
|
if (
|
|
!(operatorFilterRegistry.isOperatorAllowed(
|
|
address(this),
|
|
msg.sender
|
|
) &&
|
|
operatorFilterRegistry.isOperatorAllowed(
|
|
address(this),
|
|
from
|
|
))
|
|
) {
|
|
revert OperatorNotAllowed(msg.sender);
|
|
}
|
|
}
|
|
_;
|
|
}
|
|
}
|
|
|
|
abstract contract DefaultOperatorFilterer is OperatorFilterer {
|
|
address constant DEFAULT_SUBSCRIPTION =
|
|
address(0x3cc6CddA760b79bAfa08dF41ECFA224f810dCeB6);
|
|
|
|
constructor() OperatorFilterer(DEFAULT_SUBSCRIPTION, true) {}
|
|
}
|
|
|
|
contract UMP is ERC1155, IERC2981, Ownable, Pausable, ERC1155Burnable, ERC1155Supply, ERC1155URIStorage, DefaultOperatorFilterer {
|
|
enum METAL { GOLD, PLATINUM, SILVER, COPPER }
|
|
|
|
string public name;
|
|
string public symbol;
|
|
address private _recipient;
|
|
uint256 private _royaltyPoint = 1000;
|
|
|
|
uint256 _totalSupply = 3333;
|
|
uint256 _goldSupply = 33;
|
|
uint256 _platinumSupply = 333;
|
|
uint256 _silverSupply = 999;
|
|
uint256 _copperSupply = 1968;
|
|
|
|
mapping(address => uint256[]) private _userTokens;
|
|
mapping(uint256 => METAL) private _metalType;
|
|
mapping(uint256 => bool) private _tokenSold;
|
|
|
|
event InitMetalType();
|
|
event InitialMint(uint256[], uint256[]);
|
|
event WithdrawAll(uint256);
|
|
event Deposit(uint256);
|
|
|
|
constructor() ERC1155("https://www.urbanminerproject.org/")
|
|
{
|
|
name = "Urban Miner Project";
|
|
symbol = "UMP";
|
|
_recipient = 0x9Bc9F795887eb3917CC52518818667B30A3DEF58;
|
|
ERC1155._setURI("ipfs://bafkreie5wtkzdncvipkinttycmdeardlyurrgyacr6laoazhzckkhaaseu");
|
|
}
|
|
|
|
function safeTransferFrom(
|
|
address from,
|
|
address to,
|
|
uint256 id,
|
|
uint256 amount,
|
|
bytes memory data
|
|
) public override onlyAllowedOperator(from) {
|
|
super.safeTransferFrom(from, to, id, amount, data);
|
|
}
|
|
|
|
function safeBatchTransferFrom(
|
|
address from,
|
|
address to,
|
|
uint256[] memory ids,
|
|
uint256[] memory amounts,
|
|
bytes memory data
|
|
) public override onlyAllowedOperator(from) {
|
|
super.safeBatchTransferFrom(from, to, ids, amounts, data);
|
|
}
|
|
|
|
|
|
function pause() public onlyOwner {
|
|
_pause();
|
|
}
|
|
|
|
|
|
function unpause() public onlyOwner {
|
|
_unpause();
|
|
}
|
|
|
|
|
|
function totalCount() external view returns (uint256) {
|
|
return _totalSupply;
|
|
}
|
|
|
|
|
|
function mint(address account, uint256 id, uint256 amount, bytes memory data)
|
|
public
|
|
onlyOwner
|
|
{
|
|
_mint(account, id, amount, data);
|
|
}
|
|
|
|
|
|
function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data)
|
|
public
|
|
onlyOwner
|
|
{
|
|
_mintBatch(to, ids, amounts, data);
|
|
}
|
|
|
|
|
|
function _beforeTokenTransfer(address operator, address from, address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data)
|
|
internal
|
|
whenNotPaused
|
|
override(ERC1155, ERC1155Supply)
|
|
{
|
|
super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
|
|
|
|
require(to == owner() || _userTokens[to].length + amounts.length <= 2, "Account balance could not be bigger than 2");
|
|
}
|
|
|
|
|
|
function _afterTokenTransfer(address operator, address from, address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data)
|
|
internal
|
|
whenNotPaused
|
|
override
|
|
{
|
|
uint256 fromUserBal = _userTokens[from].length;
|
|
|
|
|
|
for (uint256 i=0; i<ids.length; i++) {
|
|
for (uint256 j=0; j<_userTokens[from].length; j++) {
|
|
if (_userTokens[from][j] == ids[i]) {
|
|
_userTokens[from][j] = _userTokens[from][fromUserBal-1];
|
|
_userTokens[from].pop();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
for (uint256 i=0; i<ids.length; i++) {
|
|
_userTokens[to].push(ids[i]);
|
|
|
|
|
|
if (from == owner() && to != owner()) {
|
|
if (_getMetalType(ids[i]) == METAL.GOLD) {
|
|
_setURI(ids[i], "ipfs://bafkreidxufd5j3j4lrup5qv5ga2ilc2o64ikeswjlotftnilcskoavcv4e");
|
|
} else if (_getMetalType(ids[i]) == METAL.PLATINUM) {
|
|
_setURI(ids[i], "ipfs://bafkreie3qf7e6ugiocox2loylcwv24nl5gyqyz3ravc2q2iunmlewtbsrq");
|
|
} else if (_getMetalType(ids[i]) == METAL.SILVER) {
|
|
_setURI(ids[i], "ipfs://bafkreibncuathwbnykwb72hvv25kuaz2lztj4ikq4njewz6h2tmg3qdcxy");
|
|
} else {
|
|
_setURI(ids[i], "ipfs://bafkreic5hfunyqyojw3zdkplkjdvjkve72il2rh4occpxsj6nukralsv4u");
|
|
}
|
|
}
|
|
}
|
|
super._afterTokenTransfer(operator, from, to, ids, amounts, data);
|
|
}
|
|
|
|
|
|
function contractURI() public pure returns (string memory) {
|
|
return
|
|
"ipfs://bafkreidi7eh5weuayslarn72jgdmvqkngbzuoqb5s3kn7dgr7gap2k6rga";
|
|
}
|
|
|
|
|
|
function getRandomNumber(uint256 max, uint256 adder) internal view returns (uint256) {
|
|
uint256 randomHash = uint256(
|
|
keccak256(abi.encodePacked(block.timestamp, uint256(7789), adder))
|
|
);
|
|
return randomHash % max;
|
|
}
|
|
|
|
|
|
function _initMetalType(uint256 _initValue, uint256 goldSupply_, uint256 platinumSupply_, uint256 silverSupply_, uint256 copperSupply_) internal {
|
|
uint256 goldCount = 0;
|
|
uint256 platinumCount = 0;
|
|
uint256 silverCount = 0;
|
|
uint256 copperCount = 0;
|
|
|
|
uint256 totalSupply = _totalSupply / 11;
|
|
uint256 goldSupply = goldSupply_;
|
|
uint256 platinumSupply = platinumSupply_;
|
|
uint256 silverSupply = silverSupply_;
|
|
uint256 copperSupply = copperSupply_;
|
|
uint256 initValue = _initValue;
|
|
|
|
bool[] memory usedIds = new bool[](totalSupply);
|
|
uint256 tryCount = 0;
|
|
|
|
while (true) {
|
|
uint256 tmp = getRandomNumber(totalSupply, tryCount);
|
|
tryCount ++;
|
|
if (usedIds[tmp]) {
|
|
while (usedIds[tmp]) {
|
|
tmp ++;
|
|
if (tmp == totalSupply) tmp = 0;
|
|
}
|
|
}
|
|
|
|
|
|
usedIds[tmp] = true;
|
|
|
|
if (goldCount < goldSupply) {
|
|
_metalType[initValue + tmp] = METAL.GOLD;
|
|
goldCount ++;
|
|
} else if (platinumCount < platinumSupply) {
|
|
_metalType[initValue + tmp] = METAL.PLATINUM;
|
|
platinumCount ++;
|
|
} else if (silverCount < silverSupply) {
|
|
_metalType[initValue + tmp] = METAL.SILVER;
|
|
silverCount ++;
|
|
} else {
|
|
_metalType[initValue + tmp] = METAL.COPPER;
|
|
copperCount ++;
|
|
}
|
|
if (copperCount == copperSupply) break;
|
|
}
|
|
|
|
emit InitMetalType();
|
|
}
|
|
|
|
|
|
function initMetalType1() external onlyOwner {
|
|
_initMetalType(0, 3, 31, 91, 178);
|
|
}
|
|
|
|
|
|
function initMetalType2() external onlyOwner {
|
|
_initMetalType(0, 3, 31, 91, 178);
|
|
}
|
|
|
|
|
|
function initMetalType3() external onlyOwner {
|
|
_initMetalType(0, 3, 31, 91, 178);
|
|
}
|
|
|
|
|
|
function initMetalType4() external onlyOwner {
|
|
_initMetalType(0, 3, 30, 91, 179);
|
|
}
|
|
|
|
|
|
function initMetalType5() external onlyOwner {
|
|
_initMetalType(0, 3, 30, 91, 179);
|
|
}
|
|
|
|
|
|
function initMetalType6() external onlyOwner {
|
|
_initMetalType(0, 3, 30, 91, 179);
|
|
}
|
|
|
|
|
|
function initMetalType7() external onlyOwner {
|
|
_initMetalType(0, 3, 30, 91, 179);
|
|
}
|
|
|
|
|
|
function initMetalType8() external onlyOwner {
|
|
_initMetalType(0, 3, 30, 91, 179);
|
|
}
|
|
|
|
|
|
function initMetalType9() external onlyOwner {
|
|
_initMetalType(0, 3, 30, 91, 179);
|
|
}
|
|
|
|
|
|
function initMetalType10() external onlyOwner {
|
|
_initMetalType(0, 3, 30, 90, 180);
|
|
}
|
|
|
|
|
|
function initMetalType11() external onlyOwner {
|
|
_initMetalType(0, 3, 30, 90, 180);
|
|
}
|
|
|
|
|
|
function _getMetalType(uint256 tokenId) internal view returns (METAL) {
|
|
return _metalType[tokenId];
|
|
}
|
|
|
|
|
|
function getMetalType(uint256 tokenId) external view onlyOwner returns (METAL) {
|
|
return _metalType[tokenId];
|
|
}
|
|
|
|
|
|
function initialMint(uint256 fromId, uint256 toId) onlyOwner external {
|
|
require(toId > fromId, "You need mint at least 1 NFT");
|
|
uint256[] memory ids = new uint256[](toId - fromId + 1);
|
|
uint256[] memory amounts = new uint256[](toId - fromId + 1);
|
|
|
|
for (uint256 i=fromId; i<=toId; i++) {
|
|
ids[i] = i;
|
|
amounts[i] = 1;
|
|
}
|
|
|
|
mintBatch(owner(), ids, amounts, "0x");
|
|
_userTokens[owner()] = ids;
|
|
|
|
emit InitialMint(ids, amounts);
|
|
}
|
|
|
|
|
|
function getUserTokenIds(address user) external view returns (uint256[] memory) {
|
|
return _userTokens[user];
|
|
}
|
|
|
|
|
|
function getUserBalance(address user) external view returns (uint256, uint256, uint256, uint256) {
|
|
uint256 goldCount = 0;
|
|
uint256 platinumCount = 0;
|
|
uint256 silverCount = 0;
|
|
uint256 copperCount = 0;
|
|
|
|
for (uint256 i=0; i<_userTokens[user].length; i++) {
|
|
METAL type_ = _getMetalType(_userTokens[user][i]);
|
|
if (type_ == METAL.GOLD) goldCount++;
|
|
else if (type_ == METAL.PLATINUM) platinumCount++;
|
|
else if (type_ == METAL.SILVER) silverCount++;
|
|
else copperCount++;
|
|
}
|
|
|
|
return (goldCount, platinumCount, silverCount, copperCount);
|
|
}
|
|
|
|
|
|
function uri(uint256 tokenId) public override(ERC1155, ERC1155URIStorage) view returns (string memory) {
|
|
return ERC1155URIStorage.uri(tokenId);
|
|
}
|
|
|
|
|
|
function deposit() external payable {
|
|
emit Deposit(msg.value);
|
|
}
|
|
|
|
|
|
function withdrawAll() external onlyOwner {
|
|
uint256 balance = address(this).balance;
|
|
require(balance > 0);
|
|
_withdraw(owner(), address(this).balance);
|
|
|
|
emit WithdrawAll(balance);
|
|
}
|
|
|
|
|
|
function _withdraw(address _address, uint256 _amount) private {
|
|
(bool success, ) = _address.call{value: _amount}("");
|
|
require(success, "Transfer failed!");
|
|
}
|
|
|
|
|
|
|
|
|
|
function _setRoyalties(address newRecipient, uint256 royaltyPoint) internal {
|
|
require(newRecipient != address(0), "Royalties: new recipient is the zero address");
|
|
require(royaltyPoint >= 0 && royaltyPoint <10000, "Royalty point should be between 0 and 100");
|
|
_recipient = newRecipient;
|
|
_royaltyPoint = royaltyPoint;
|
|
}
|
|
|
|
function setRoyalties(address newRecipient, uint256 royaltyPoint) external onlyOwner {
|
|
_setRoyalties(newRecipient, royaltyPoint);
|
|
}
|
|
|
|
|
|
function royaltyInfo(uint256 _tokenId, uint256 _salePrice)
|
|
external
|
|
view
|
|
override
|
|
returns (address receiver, uint256 royaltyAmount)
|
|
{
|
|
require(_tokenId >= 0, "Token id should be bigger than 0");
|
|
return (_recipient, (_salePrice * _royaltyPoint) / 10000);
|
|
}
|
|
|
|
|
|
function supportsInterface(bytes4 interfaceId)
|
|
public
|
|
view
|
|
virtual
|
|
override(ERC1155, IERC165)
|
|
returns (bool)
|
|
{
|
|
return (interfaceId == type(IERC2981).interfaceId ||
|
|
super.supportsInterface(interfaceId));
|
|
}
|
|
} |