Skip to content

Instantly share code, notes, and snippets.

@lychees
Last active May 31, 2018 17:35
Show Gist options
  • Save lychees/190a27ef1e99e5b8211497e62e0516db to your computer and use it in GitHub Desktop.
Save lychees/190a27ef1e99e5b8211497e62e0516db to your computer and use it in GitHub Desktop.
idoldraw.sol
pragma solidity ^0.4.24;
contract IdolDraw {
event Buy(address indexed buyerAddr);
event RollDice(address indexed playerAddr, address indexed prizeIssuer, uint prizeId);
address public mainContractAddr;
uint256 public sigmaType = 6;
address private owner;
mapping (address => bool) private admins;
mapping (uint256 => uint256) public types;
constructor() public {
owner = msg.sender;
admins[owner] = true;
}
/* Modifiers */
modifier onlyOwner() {
require(owner == msg.sender);
_;
}
modifier onlyAdmins() {
require(admins[msg.sender]);
_;
}
/* Owner */
function setOwner (address _owner) onlyOwner() public {
owner = _owner;
}
function addAdmin (address _admin) onlyOwner() public {
admins[_admin] = true;
}
function removeAdmin (address _admin) onlyOwner() public {
delete admins[_admin];
}
/* Withdraw */
/*
NOTICE: These functions withdraw the developer's cut which is left
in the contract by `buy`. User funds are immediately sent to the old
owner in `buy`, no user funds are left in the contract.
*/
function withdrawAll () onlyAdmins() public {
msg.sender.transfer(address(this).balance);
}
function withdrawAmount (uint256 _amount) onlyAdmins() public {
msg.sender.transfer(_amount);
}
/* ERC721 */
function typesOf (uint256 _tokenId) public view returns (uint256 _type) {
return types[_tokenId];
}
/* Read */
function isAdmin (address _admin) public view returns (bool _isAdmin) {
return admins[_admin];
}
/* Util */
function isContract(address addr) internal view returns (bool) {
uint size;
assembly { size := extcodesize(addr) } // solium-disable-line
return size > 0;
}
function buy(address _referrer) public payable {
require(msg.value >= 0.001 ether);
if (_referrer != address(0)) _referrer.transfer(msg.value * 97 / 100); // 3% cut off for contract
uint256 count = msg.value / 0.001 ether;
for (uint i = 0; i < count; ++i){
Issuer issuer = Issuer(mainContractAddr);
uint256 id = issuer.totalSupply();
issuer.issueTokenAndTransfer(msg.sender);
types[id] = rollDice();
}
}
function bonusIssue(address to, uint256 count, uint256 _type) public onlyAdmins {
for (uint i = 0; i < count; ++i) {
Issuer issuer = Issuer(mainContractAddr);
uint256 id = issuer.totalSupply();
issuer.issueTokenAndTransfer(to);
types[id] = _type;
}
}
function rollDice() public view returns (uint256 _result) {
return uint(keccak256(abi.encodePacked(block.timestamp, block.difficulty))) % sigmaType;
}
}
interface Issuer {
function ownerOf(uint256 _tokenId) external view returns (address _owner);
function issueTokenAndTransfer(address _to) external;
function totalSupply() external returns (uint256 _totalSupply);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment