mysterybox1
This commit is contained in:
@@ -21,74 +21,100 @@ contract MysteryBox1 is ERC721, InitializableOwnable {
|
||||
// ============ Storage ============
|
||||
|
||||
mapping(address => uint256) _USER_TICKETS_;
|
||||
|
||||
uint256 public _TOTAL_TICKETS_;
|
||||
|
||||
uint256 public _CUR_SELLING_TICKETS_;
|
||||
uint256 public _CUR_PRCIE_;
|
||||
uint256 public _TICKET_UNIT_ = 1; // ticket consumed in a single lottery
|
||||
|
||||
|
||||
mapping(uint256 => bool) _TOKEN_ID_FLAG_;
|
||||
|
||||
uint256[] public _TOKEN_IDS_;
|
||||
address public _RANDOM_GENERATOR_;
|
||||
|
||||
uint256 constant _TOTAL_NFTs_ = 3000;
|
||||
bool public _REDEEM_ALLOWED_ = true;
|
||||
|
||||
|
||||
// ============ Event =============
|
||||
event ChangeRandomGenerator(address randomGenerator);
|
||||
event ChangeTicketUnit(uint256 newTicketUnit);
|
||||
event ChangeSellingInfo(uint256 curSellingTickets, uint256 curPrice);
|
||||
event Withdraw(address account, uint256 amount);
|
||||
event BatchMint(uint256 mintAmount);
|
||||
event BuyTicket(address account, uint256 value, uint256 tickets);
|
||||
event RedeemPrize(address account, uint256 tokenId);
|
||||
event DisableRedeem();
|
||||
event EnableRedeem();
|
||||
|
||||
fallback() external payable {}
|
||||
|
||||
receive() external payable {}
|
||||
|
||||
function init(
|
||||
string memory name,
|
||||
string memory symbol,
|
||||
string memory baseUrI,
|
||||
address owner,
|
||||
string memory baseUri,
|
||||
address randomGenerator
|
||||
) public {
|
||||
_name = name;
|
||||
_symbol = symbol;
|
||||
_baseURI = baseUrI;
|
||||
|
||||
initOwner(owner);
|
||||
_setURI(baseUri);
|
||||
_RANDOM_GENERATOR_ = randomGenerator;
|
||||
}
|
||||
|
||||
function buyTicket() payable external {
|
||||
require(msg.value >= _CUR_PRCIE_, "BNB_NOT_ENOUGH");
|
||||
uint256 buyAmount = msg.value;
|
||||
require(buyAmount >= _CUR_PRCIE_, "BNB_NOT_ENOUGH");
|
||||
uint256 tickets = buyAmount.div(_CUR_PRCIE_);
|
||||
require(tickets <= _CUR_SELLING_TICKETS_, "TICKETS_NOT_ENOUGH");
|
||||
_USER_TICKETS_[msg.sender] = _USER_TICKETS_[msg.sender].add(tickets);
|
||||
_TOTAL_TICKETS_ = _TOTAL_TICKETS_.add(tickets);
|
||||
_CUR_SELLING_TICKETS_ = _CUR_SELLING_TICKETS_.sub(tickets);
|
||||
|
||||
uint256 leftOver = msg.value - tickets.mul(_CUR_PRCIE_);
|
||||
if(leftOver > 0)
|
||||
msg.sender.transfer(leftOver);
|
||||
emit BuyTicket(msg.sender, buyAmount - leftOver, tickets);
|
||||
}
|
||||
|
||||
function redeemPrize(address to) external {
|
||||
|
||||
// uint256 ticketNum = ticketInput.div(_TICKET_UNIT_);
|
||||
// require(ticketNum >= 1, "DODOMysteryBox: TICKET_NOT_ENOUGH");
|
||||
// for (uint256 i = 0; i < ticketNum; i++) {
|
||||
// _redeemSinglePrize(to);
|
||||
// }
|
||||
// emit RedeemPrize(to, ticketInput, ticketNum);
|
||||
function redeemPrize() 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");
|
||||
for (uint256 i = 0; i < ticketNum; i++) {
|
||||
_redeemSinglePrize(msg.sender);
|
||||
}
|
||||
}
|
||||
|
||||
// =============== View ================
|
||||
|
||||
// =============== Internal ================
|
||||
|
||||
function _redeemSinglePrize(address to) internal {
|
||||
// uint256 range = _PROB_INTERVAL_[_PROB_INTERVAL_.length - 1];
|
||||
// uint256 random = IRandomGenerator(_RANDOM_GENERATOR_).random(gasleft()) % range;
|
||||
// uint256 i;
|
||||
// for (i = 0; i < _PROB_INTERVAL_.length; i++) {
|
||||
// if (random <= _PROB_INTERVAL_[i]) {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// require(_PRIZE_SET_[i].length > 0, "EMPTY_PRIZE_SET");
|
||||
// uint256 prize = _PRIZE_SET_[i][random % _PRIZE_SET_[i].length];
|
||||
// _mint(to, prize, 1, "");
|
||||
uint256 range = _TOKEN_IDS_.length;
|
||||
uint256 random = IRandomGenerator(_RANDOM_GENERATOR_).random(gasleft()) % range;
|
||||
uint256 prizeId = _TOKEN_IDS_[random];
|
||||
|
||||
if(random != range - 1) {
|
||||
_TOKEN_IDS_[random] = _TOKEN_IDS_[range - 1];
|
||||
}
|
||||
_TOKEN_IDS_.pop();
|
||||
safeTransferFrom(address(this), to, prizeId, "");
|
||||
emit RedeemPrize(to, prizeId);
|
||||
}
|
||||
|
||||
// ================= Owner ===================
|
||||
|
||||
function disableRedeemPrize() external onlyOwner {
|
||||
_REDEEM_ALLOWED_ = false;
|
||||
emit DisableRedeem();
|
||||
}
|
||||
|
||||
function enableRedeemPrize() external onlyOwner {
|
||||
_REDEEM_ALLOWED_ = true;
|
||||
emit EnableRedeem();
|
||||
}
|
||||
|
||||
function updateRandomGenerator(address newRandomGenerator) external onlyOwner {
|
||||
require(newRandomGenerator != address(0));
|
||||
_RANDOM_GENERATOR_ = newRandomGenerator;
|
||||
@@ -112,4 +138,12 @@ contract MysteryBox1 is ERC721, InitializableOwnable {
|
||||
msg.sender.transfer(amount);
|
||||
emit Withdraw(msg.sender, amount);
|
||||
}
|
||||
|
||||
function batchMint(uint256[] calldata tokenIds) external onlyOwner {
|
||||
for(uint256 i = 0; i<tokenIds.length; i++) {
|
||||
_mint(address(this), tokenIds[i]);
|
||||
_TOKEN_IDS_.push(tokenIds[i]);
|
||||
}
|
||||
emit BatchMint(tokenIds.length);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import {IWETH} from "../../intf/IWETH.sol";
|
||||
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
import {SafeERC20} from "../../lib/SafeERC20.sol";
|
||||
import {ReentrancyGuard} from "../../lib/ReentrancyGuard.sol";
|
||||
import {Address} from "../../lib/Address.sol";
|
||||
import {Address} from "../../external/utils/Address.sol";
|
||||
|
||||
interface IDODOMysteryBox {
|
||||
function _TICKET_() external view returns (address);
|
||||
|
||||
9
contracts/external/ERC1155/ERC1155.sol
vendored
9
contracts/external/ERC1155/ERC1155.sol
vendored
@@ -4,6 +4,7 @@
|
||||
pragma solidity 0.6.9;
|
||||
|
||||
import {IERC1155} from "../../intf/IERC1155.sol";
|
||||
import {IERC165} from "../../intf/IERC165.sol";
|
||||
import {IERC1155Receiver} from "../../intf/IERC1155Receiver.sol";
|
||||
import {IERC1155MetadataURI} from "../../intf/IERC1155MetadataURI.sol";
|
||||
import {ERC165} from "../utils/ERC165.sol";
|
||||
@@ -31,13 +32,6 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
|
||||
// Used as the URI for all token types by relying on ID substitution, e.g. https://token-cdn-domain/{id}.json
|
||||
string private _uri;
|
||||
|
||||
/**
|
||||
* @dev See {_setURI}.
|
||||
*/
|
||||
constructor (string memory uri_) {
|
||||
_setURI(uri_);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC165-supportsInterface}.
|
||||
*/
|
||||
@@ -399,4 +393,3 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
17
contracts/external/ERC721/ERC721.sol
vendored
17
contracts/external/ERC721/ERC721.sol
vendored
@@ -4,6 +4,7 @@
|
||||
pragma solidity 0.6.9;
|
||||
|
||||
import {IERC721} from "../../intf/IERC721.sol";
|
||||
import {IERC165} from "../../intf/IERC165.sol";
|
||||
import {IERC721Receiver} from "../../intf/IERC721Receiver.sol";
|
||||
import {IERC721Metadata} from "../../intf/IERC721Metadata.sol";
|
||||
import {ERC165} from "../utils/ERC165.sol";
|
||||
@@ -21,10 +22,12 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {
|
||||
using Strings for uint256;
|
||||
|
||||
// Token name
|
||||
string private _name;
|
||||
string internal _name;
|
||||
|
||||
// Token symbol
|
||||
string private _symbol;
|
||||
string internal _symbol;
|
||||
|
||||
string internal _baseURI = "";
|
||||
|
||||
// Mapping from token ID to owner address
|
||||
mapping (uint256 => address) private _owners;
|
||||
@@ -84,20 +87,12 @@ 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 "";
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev See {IERC721-approve}.
|
||||
*/
|
||||
|
||||
@@ -9,7 +9,7 @@ pragma solidity 0.6.9;
|
||||
|
||||
import {ERC721} from "./ERC721.sol";
|
||||
|
||||
contract InitializableERC1155 is ERC721 {
|
||||
contract InitializableERC721 is ERC721 {
|
||||
function init(
|
||||
address creator,
|
||||
string memory name,
|
||||
|
||||
2
contracts/external/utils/ERC165.sol
vendored
2
contracts/external/utils/ERC165.sol
vendored
@@ -3,7 +3,7 @@
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
|
||||
import "./IERC165.sol";
|
||||
import "../../intf/IERC165.sol";
|
||||
|
||||
/**
|
||||
* @dev Implementation of the {IERC165} interface.
|
||||
|
||||
Reference in New Issue
Block a user