From ebdf700f4b2184b5e6421774e7ab9db0477218fc Mon Sep 17 00:00:00 2001 From: owen05 Date: Wed, 14 Apr 2021 22:42:12 +0800 Subject: [PATCH] update mysteryboxV1 --- README.md | 8 +-- .../{MysteryBox1.sol => MysteryBoxV1.sol} | 30 +++++---- contracts/external/ERC721/ERC721.sol | 12 +++- .../external/ERC721/ERC721URIStorage.sol | 67 +++++++++++++++++++ .../external/ERC721/InitializableERC721.sol | 4 +- 5 files changed, 101 insertions(+), 20 deletions(-) rename contracts/DODOMysteryBox/{MysteryBox1.sol => MysteryBoxV1.sol} (84%) create mode 100644 contracts/external/ERC721/ERC721URIStorage.sol diff --git a/README.md b/README.md index d63d40b..f7e0122 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,6 @@ - contracts/DODOFee/FeeDistributer.sol -- contracts/external/ERC721/ - - contracts/external/ERC1155/ - contracts/external/ERC20/InitializableERC20.sol @@ -20,6 +18,8 @@ ### DODO MysteryBox -- contracts/DODOMysteryBox/MysteryBox1.sol +- contracts/DODOMysteryBox/MysteryBoxV1.sol -- contracts/lib/RandomGenerator.sol \ No newline at end of file +- contracts/lib/RandomGenerator.sol + +- contracts/external/ERC721/ \ No newline at end of file diff --git a/contracts/DODOMysteryBox/MysteryBox1.sol b/contracts/DODOMysteryBox/MysteryBoxV1.sol similarity index 84% rename from contracts/DODOMysteryBox/MysteryBox1.sol rename to contracts/DODOMysteryBox/MysteryBoxV1.sol index 39631a9..eddbd1b 100644 --- a/contracts/DODOMysteryBox/MysteryBox1.sol +++ b/contracts/DODOMysteryBox/MysteryBoxV1.sol @@ -11,9 +11,9 @@ import {SafeMath} from "../lib/SafeMath.sol"; import {IRandomGenerator} from "../lib/RandomGenerator.sol"; import {InitializableOwnable} from "../lib/InitializableOwnable.sol"; import {Address} from "../external/utils/Address.sol"; -import {ERC721} from "../external/ERC721/ERC721.sol"; +import {ERC721URIStorage} from "../external/ERC721/ERC721URIStorage.sol"; -contract MysteryBox1 is ERC721, InitializableOwnable { +contract MysteryBoxV1 is ERC721URIStorage, InitializableOwnable { using SafeMath for uint256; using SafeERC20 for IERC20; using Address for address; @@ -28,6 +28,8 @@ contract MysteryBox1 is ERC721, InitializableOwnable { uint256 public _TICKET_UNIT_ = 1; // ticket consumed in a single lottery uint256[] public _TOKEN_IDS_; + uint256 public _ID_POINT_; + address public _RANDOM_GENERATOR_; bool public _REDEEM_ALLOWED_ = true; @@ -51,13 +53,13 @@ contract MysteryBox1 is ERC721, InitializableOwnable { function init( string memory name, string memory symbol, - string memory baseUrI, + string memory baseUri, address owner, address randomGenerator ) public { _name = name; _symbol = symbol; - _baseURI = baseUrI; + _baseUri = baseUri; initOwner(owner); _RANDOM_GENERATOR_ = randomGenerator; @@ -78,14 +80,16 @@ contract MysteryBox1 is ERC721, InitializableOwnable { emit BuyTicket(msg.sender, buyAmount - leftOver, tickets); } - function redeemPrize() external { + + function redeemPrize(uint256 ticketNum) external { require(_REDEEM_ALLOWED_, "REDEEM_CLOSED"); require(!address(msg.sender).isContract(), "ONLY_ALLOW_EOA"); - uint256 ticketNum = _USER_TICKETS_[msg.sender]; - require(ticketNum >= 1, "TICKET_NOT_ENOUGH"); + require(ticketNum >= 1 && ticketNum <= _USER_TICKETS_[msg.sender], "TICKET_NUM_INVALID"); for (uint256 i = 0; i < ticketNum; i++) { _redeemSinglePrize(msg.sender); } + _USER_TICKETS_[msg.sender] = _USER_TICKETS_[msg.sender].sub(ticketNum); + _TOTAL_TICKETS_ = _TOTAL_TICKETS_.sub(ticketNum); } // =============== Internal ================ @@ -139,11 +143,13 @@ contract MysteryBox1 is ERC721, InitializableOwnable { emit Withdraw(msg.sender, amount); } - function batchMint(uint256[] calldata tokenIds) external onlyOwner { - for(uint256 i = 0; i address) private _owners; @@ -87,12 +87,20 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata { function tokenURI(uint256 tokenId) public view virtual override returns (string memory) { require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token"); - string memory baseURI = _baseURI; + string memory baseURI = _baseURI(); return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : ''; } + /** + * @dev Base URI for computing {tokenURI}. Empty by default, can be overriden + * in child contracts. + */ + function _baseURI() internal view virtual returns (string memory) { + return _baseUri; + } + /** * @dev See {IERC721-approve}. */ diff --git a/contracts/external/ERC721/ERC721URIStorage.sol b/contracts/external/ERC721/ERC721URIStorage.sol new file mode 100644 index 0000000..893b895 --- /dev/null +++ b/contracts/external/ERC721/ERC721URIStorage.sol @@ -0,0 +1,67 @@ +// This is a file copied from https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/extensions/ERC721URIStorage.sol +// SPDX-License-Identifier: MIT + +pragma solidity 0.6.9; + +import "./ERC721.sol"; + +/** + * @dev ERC721 token with storage based token URI management. + */ +abstract contract ERC721URIStorage is ERC721 { + using Strings for uint256; + + // Optional mapping for token URIs + mapping (uint256 => string) private _tokenURIs; + + /** + * @dev See {IERC721Metadata-tokenURI}. + */ + function tokenURI(uint256 tokenId) public view virtual override returns (string memory) { + require(_exists(tokenId), "ERC721URIStorage: URI query for nonexistent token"); + + string memory _tokenURI = _tokenURIs[tokenId]; + string memory base = _baseURI(); + + // If there is no base URI, return the token URI. + if (bytes(base).length == 0) { + return _tokenURI; + } + // If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked). + if (bytes(_tokenURI).length > 0) { + return string(abi.encodePacked(base, _tokenURI)); + } + + return super.tokenURI(tokenId); + } + + /** + * @dev Sets `_tokenURI` as the tokenURI of `tokenId`. + * + * Requirements: + * + * - `tokenId` must exist. + */ + function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual { + require(_exists(tokenId), "ERC721URIStorage: URI set of nonexistent token"); + _tokenURIs[tokenId] = _tokenURI; + } + + /** + * @dev Destroys `tokenId`. + * The approval is cleared when the token is burned. + * + * Requirements: + * + * - `tokenId` must exist. + * + * Emits a {Transfer} event. + */ + function _burn(uint256 tokenId) internal virtual override { + super._burn(tokenId); + + if (bytes(_tokenURIs[tokenId]).length != 0) { + delete _tokenURIs[tokenId]; + } + } +} \ No newline at end of file diff --git a/contracts/external/ERC721/InitializableERC721.sol b/contracts/external/ERC721/InitializableERC721.sol index 5ddbff3..e0fcc27 100644 --- a/contracts/external/ERC721/InitializableERC721.sol +++ b/contracts/external/ERC721/InitializableERC721.sol @@ -14,11 +14,11 @@ contract InitializableERC721 is ERC721 { address creator, string memory name, string memory symbol, - string memory baseUrI + string memory baseUri ) public { _name = name; _symbol = symbol; - _baseURI = baseUrI; + _baseUri = baseUri; _mint(creator, 0); } } \ No newline at end of file