Created
June 26, 2019 18:34
-
-
Save jin10086/4eb125e4d9452397f19dc02b87164487 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.1; | |
contract Ownable { | |
address private _owner; | |
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); | |
/** | |
* @dev Initializes the contract setting the deployer as the initial owner. | |
*/ | |
constructor () internal { | |
_owner = msg.sender; | |
emit OwnershipTransferred(address(0), _owner); | |
} | |
/** | |
* @dev Returns the address of the current owner. | |
*/ | |
function owner() public view returns (address) { | |
return _owner; | |
} | |
/** | |
* @dev Throws if called by any account other than the owner. | |
*/ | |
modifier onlyOwner() { | |
require(isOwner(), "Ownable: caller is not the owner"); | |
_; | |
} | |
/** | |
* @dev Returns true if the caller is the current owner. | |
*/ | |
function isOwner() public view returns (bool) { | |
return msg.sender == _owner; | |
} | |
/** | |
* @dev Transfers ownership of the contract to a new account (`newOwner`). | |
* Can only be called by the current owner. | |
*/ | |
function transferOwnership(address newOwner) public onlyOwner { | |
_transferOwnership(newOwner); | |
} | |
/** | |
* @dev Transfers ownership of the contract to a new account (`newOwner`). | |
*/ | |
function _transferOwnership(address newOwner) internal { | |
require(newOwner != address(0), "Ownable: new owner is the zero address"); | |
emit OwnershipTransferred(_owner, newOwner); | |
_owner = newOwner; | |
} | |
} | |
library SafeMath { | |
/** | |
* @dev Returns the addition of two unsigned integers, reverting on | |
* overflow. | |
* | |
* Counterpart to Solidity's `+` operator. | |
* | |
* Requirements: | |
* - Addition cannot overflow. | |
*/ | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
require(c >= a, "SafeMath: addition overflow"); | |
return c; | |
} | |
/** | |
* @dev Returns the subtraction of two unsigned integers, reverting on | |
* overflow (when the result is negative). | |
* | |
* Counterpart to Solidity's `-` operator. | |
* | |
* Requirements: | |
* - Subtraction cannot overflow. | |
*/ | |
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
require(b <= a, "SafeMath: subtraction overflow"); | |
uint256 c = a - b; | |
return c; | |
} | |
/** | |
* @dev Returns the multiplication of two unsigned integers, reverting on | |
* overflow. | |
* | |
* Counterpart to Solidity's `*` operator. | |
* | |
* Requirements: | |
* - Multiplication cannot overflow. | |
*/ | |
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the | |
// benefit is lost if 'b' is also tested. | |
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522 | |
if (a == 0) { | |
return 0; | |
} | |
uint256 c = a * b; | |
require(c / a == b, "SafeMath: multiplication overflow"); | |
return c; | |
} | |
/** | |
* @dev Returns the integer division of two unsigned integers. Reverts on | |
* division by zero. The result is rounded towards zero. | |
* | |
* Counterpart to Solidity's `/` operator. Note: this function uses a | |
* `revert` opcode (which leaves remaining gas untouched) while Solidity | |
* uses an invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* - The divisor cannot be zero. | |
*/ | |
function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
// Solidity only automatically asserts when dividing by 0 | |
require(b > 0, "SafeMath: division by zero"); | |
uint256 c = a / b; | |
// assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
return c; | |
} | |
/** | |
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), | |
* Reverts when dividing by zero. | |
* | |
* Counterpart to Solidity's `%` operator. This function uses a `revert` | |
* opcode (which leaves remaining gas untouched) while Solidity uses an | |
* invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* - The divisor cannot be zero. | |
*/ | |
function mod(uint256 a, uint256 b) internal pure returns (uint256) { | |
require(b != 0, "SafeMath: modulo by zero"); | |
return a % b; | |
} | |
} | |
interface IERC20 { | |
/** | |
* @dev Returns the amount of tokens in existence. | |
*/ | |
function totalSupply() external view returns (uint256); | |
/** | |
* @dev Returns the amount of tokens owned by `account`. | |
*/ | |
function balanceOf(address account) external view returns (uint256); | |
/** | |
* @dev Moves `amount` tokens from the caller's account to `recipient`. | |
* | |
* Returns a boolean value indicating whether the operation succeeded. | |
* | |
* Emits a `Transfer` event. | |
*/ | |
function transfer(address recipient, uint256 amount) external returns (bool); | |
function mint(address account, uint256 amount) external returns (bool); | |
/** | |
* @dev Returns the remaining number of tokens that `spender` will be | |
* allowed to spend on behalf of `owner` through `transferFrom`. This is | |
* zero by default. | |
* | |
* This value changes when `approve` or `transferFrom` are called. | |
*/ | |
function allowance(address owner, address spender) external view returns (uint256); | |
/** | |
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens. | |
* | |
* Returns a boolean value indicating whether the operation succeeded. | |
* | |
* > Beware that changing an allowance with this method brings the risk | |
* that someone may use both the old and the new allowance by unfortunate | |
* transaction ordering. One possible solution to mitigate this race | |
* condition is to first reduce the spender's allowance to 0 and set the | |
* desired value afterwards: | |
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 | |
* | |
* Emits an `Approval` event. | |
*/ | |
function approve(address spender, uint256 amount) external returns (bool); | |
/** | |
* @dev Moves `amount` tokens from `sender` to `recipient` using the | |
* allowance mechanism. `amount` is then deducted from the caller's | |
* allowance. | |
* | |
* Returns a boolean value indicating whether the operation succeeded. | |
* | |
* Emits a `Transfer` event. | |
*/ | |
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool); | |
/** | |
* @dev Emitted when `value` tokens are moved from one account (`from`) to | |
* another (`to`). | |
* | |
* Note that `value` may be zero. | |
*/ | |
event Transfer(address indexed from, address indexed to, uint256 value); | |
/** | |
* @dev Emitted when the allowance of a `spender` for an `owner` is set by | |
* a call to `approve`. `value` is the new allowance. | |
*/ | |
event Approval(address indexed owner, address indexed spender, uint256 value); | |
} | |
contract ApproveAndCallFallBack { | |
function receiveApproval(address from, uint256 _amount, address _token, uint8 a) public; | |
} | |
contract ERC20 is IERC20,Ownable { | |
using SafeMath for uint256; | |
mapping (address => uint256) private _balances; | |
mapping (address => mapping (address => uint256)) private _allowances; | |
uint256 private _totalSupply; | |
/** | |
* @dev See `IERC20.totalSupply`. | |
*/ | |
function totalSupply() public view returns (uint256) { | |
return _totalSupply; | |
} | |
/** | |
* @dev See `IERC20.balanceOf`. | |
*/ | |
function balanceOf(address account) public view returns (uint256) { | |
return _balances[account]; | |
} | |
/** | |
* @dev See `IERC20.transfer`. | |
* | |
* Requirements: | |
* | |
* - `recipient` cannot be the zero address. | |
* - the caller must have a balance of at least `amount`. | |
*/ | |
function transfer(address recipient, uint256 amount) public returns (bool) { | |
_transfer(msg.sender, recipient, amount); | |
return true; | |
} | |
/** | |
* @dev See `IERC20.allowance`. | |
*/ | |
function allowance(address owner, address spender) public view returns (uint256) { | |
return _allowances[owner][spender]; | |
} | |
/** | |
* @dev See `IERC20.approve`. | |
* | |
* Requirements: | |
* | |
* - `spender` cannot be the zero address. | |
*/ | |
function approve(address spender, uint256 value) public returns (bool) { | |
_approve(msg.sender, spender, value); | |
return true; | |
} | |
function approveAndCall(address _spender, uint256 _amount, uint8 _extraData | |
) public returns (bool success) { | |
if (!approve(_spender, _amount)) revert(); | |
ApproveAndCallFallBack(_spender).receiveApproval( | |
msg.sender, | |
_amount, | |
address(this), | |
_extraData | |
); | |
return true; | |
} | |
/** | |
* @dev See `IERC20.transferFrom`. | |
* | |
* Emits an `Approval` event indicating the updated allowance. This is not | |
* required by the EIP. See the note at the beginning of `ERC20`; | |
* | |
* Requirements: | |
* - `sender` and `recipient` cannot be the zero address. | |
* - `sender` must have a balance of at least `value`. | |
* - the caller must have allowance for `sender`'s tokens of at least | |
* `amount`. | |
*/ | |
function transferFrom(address sender, address recipient, uint256 amount) public returns (bool) { | |
_transfer(sender, recipient, amount); | |
_approve(sender, msg.sender, _allowances[sender][msg.sender].sub(amount)); | |
return true; | |
} | |
/** | |
* @dev Atomically increases the allowance granted to `spender` by the caller. | |
* | |
* This is an alternative to `approve` that can be used as a mitigation for | |
* problems described in `IERC20.approve`. | |
* | |
* Emits an `Approval` event indicating the updated allowance. | |
* | |
* Requirements: | |
* | |
* - `spender` cannot be the zero address. | |
*/ | |
function increaseAllowance(address spender, uint256 addedValue) public returns (bool) { | |
_approve(msg.sender, spender, _allowances[msg.sender][spender].add(addedValue)); | |
return true; | |
} | |
/** | |
* @dev Atomically decreases the allowance granted to `spender` by the caller. | |
* | |
* This is an alternative to `approve` that can be used as a mitigation for | |
* problems described in `IERC20.approve`. | |
* | |
* Emits an `Approval` event indicating the updated allowance. | |
* | |
* Requirements: | |
* | |
* - `spender` cannot be the zero address. | |
* - `spender` must have allowance for the caller of at least | |
* `subtractedValue`. | |
*/ | |
function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) { | |
_approve(msg.sender, spender, _allowances[msg.sender][spender].sub(subtractedValue)); | |
return true; | |
} | |
/** | |
* @dev Moves tokens `amount` from `sender` to `recipient`. | |
* | |
* This is internal function is equivalent to `transfer`, and can be used to | |
* e.g. implement automatic token fees, slashing mechanisms, etc. | |
* | |
* Emits a `Transfer` event. | |
* | |
* Requirements: | |
* | |
* - `sender` cannot be the zero address. | |
* - `recipient` cannot be the zero address. | |
* - `sender` must have a balance of at least `amount`. | |
*/ | |
function _transfer(address sender, address recipient, uint256 amount) internal { | |
require(sender != address(0), "ERC20: transfer from the zero address"); | |
require(recipient != address(0), "ERC20: transfer to the zero address"); | |
_balances[sender] = _balances[sender].sub(amount); | |
_balances[recipient] = _balances[recipient].add(amount); | |
emit Transfer(sender, recipient, amount); | |
} | |
/** @dev Creates `amount` tokens and assigns them to `account`, increasing | |
* the total supply. | |
* | |
* Emits a `Transfer` event with `from` set to the zero address. | |
* | |
* Requirements | |
* | |
* - `to` cannot be the zero address. | |
*/ | |
function mint(address account, uint256 amount) public onlyOwner returns (bool){ | |
require(account != address(0), "ERC20: mint to the zero address"); | |
_totalSupply = _totalSupply.add(amount); | |
_balances[account] = _balances[account].add(amount); | |
emit Transfer(address(0), account, amount); | |
return true; | |
} | |
/** | |
* @dev Destoys `amount` tokens from `account`, reducing the | |
* total supply. | |
* | |
* Emits a `Transfer` event with `to` set to the zero address. | |
* | |
* Requirements | |
* | |
* - `account` cannot be the zero address. | |
* - `account` must have at least `amount` tokens. | |
*/ | |
function _burn(address account, uint256 value) internal { | |
require(account != address(0), "ERC20: burn from the zero address"); | |
_totalSupply = _totalSupply.sub(value); | |
_balances[account] = _balances[account].sub(value); | |
emit Transfer(account, address(0), value); | |
} | |
/** | |
* @dev Sets `amount` as the allowance of `spender` over the `owner`s tokens. | |
* | |
* This is internal function is equivalent to `approve`, and can be used to | |
* e.g. set automatic allowances for certain subsystems, etc. | |
* | |
* Emits an `Approval` event. | |
* | |
* Requirements: | |
* | |
* - `owner` cannot be the zero address. | |
* - `spender` cannot be the zero address. | |
*/ | |
function _approve(address owner, address spender, uint256 value) internal { | |
require(owner != address(0), "ERC20: approve from the zero address"); | |
require(spender != address(0), "ERC20: approve to the zero address"); | |
_allowances[owner][spender] = value; | |
emit Approval(owner, spender, value); | |
} | |
/** | |
* @dev Destoys `amount` tokens from `account`.`amount` is then deducted | |
* from the caller's allowance. | |
* | |
* See `_burn` and `_approve`. | |
*/ | |
function _burnFrom(address account, uint256 amount) internal { | |
_burn(account, amount); | |
_approve(account, msg.sender, _allowances[account][msg.sender].sub(amount)); | |
} | |
} | |
contract ERC20Detailed is IERC20 { | |
string private _name; | |
string private _symbol; | |
uint8 private _decimals; | |
/** | |
* @dev Sets the values for `name`, `symbol`, and `decimals`. All three of | |
* these values are immutable: they can only be set once during | |
* construction. | |
*/ | |
constructor (string memory name, string memory symbol, uint8 decimals) public { | |
_name = name; | |
_symbol = symbol; | |
_decimals = decimals; | |
} | |
/** | |
* @dev Returns the name of the token. | |
*/ | |
function name() public view returns (string memory) { | |
return _name; | |
} | |
/** | |
* @dev Returns the symbol of the token, usually a shorter version of the | |
* name. | |
*/ | |
function symbol() public view returns (string memory) { | |
return _symbol; | |
} | |
/** | |
* @dev Returns the number of decimals used to get its user representation. | |
* For example, if `decimals` equals `2`, a balance of `505` tokens should | |
* be displayed to a user as `5,05` (`505 / 10 ** 2`). | |
* | |
* Tokens usually opt for a value of 18, imitating the relationship between | |
* Ether and Wei. | |
* | |
* > Note that this information is only used for _display_ purposes: it in | |
* no way affects any of the arithmetic of the contract, including | |
* `IERC20.balanceOf` and `IERC20.transfer`. | |
*/ | |
function decimals() public view returns (uint8) { | |
return _decimals; | |
} | |
} | |
contract SimpleToken is ERC20, ERC20Detailed { | |
/** | |
* @dev Constructor that gives msg.sender all of existing tokens. | |
*/ | |
constructor (string memory name, string memory symbol) public ERC20Detailed(name, symbol, 18) { | |
// mint(msg.sender, 10000 * (10 ** uint256(decimals()))); | |
} | |
} | |
contract Go is Ownable { | |
using SafeMath for *; | |
uint256 public starttime; | |
uint256 public yearGameend; | |
uint256 public endtime; | |
uint8 public gameStatus=1; | |
uint8 public winTeam = 1; | |
uint8 private isVitalik = 1; | |
//每个队一共有多少代币. | |
uint256 public teamA; | |
uint256 public teamB; | |
uint256 public teamC; | |
uint256 public totalToken; //合约一共有多少ETH | |
uint256 public DEVSendETH; //开发者送多少ETH | |
uint256 public DEVTeamETH; //开发者拿到的ETH | |
uint256 public FOMOETH; //最后一个改变游戏结果的人的ETH | |
uint256 public toStatus2ETH;//当TeamA 大于 这个值的时候,游戏进入到第二阶段 | |
uint256 public toVitalikETH;//激活彩蛋需要的ETH,为toStatus2ETH的10倍 | |
address public FOMOAddress; //fomo玩家地址 | |
address public evilAddress; //使游戏进入到第二阶段的玩家地址 | |
address public vitalikAddress; //彩蛋玩家地址 | |
SimpleToken public tokenAAddress; | |
SimpleToken public tokenBAddress; | |
SimpleToken public tokenCAddress; | |
uint256 public totalETH; //用户分红的ETH的总数 为totalToken + DEVSendETH | |
uint256 private rndInit = 24 hours; | |
event T(address, uint256 , address , bytes); | |
event T1(uint8); | |
//每个用户可以分多少钱为 | |
// 他有的token数/ teamA * totalETH (对应team) | |
constructor() public payable{ | |
DEVSendETH = msg.value; | |
toStatus2ETH = DEVSendETH.mul(20); | |
toVitalikETH = toStatus2ETH.mul(10); | |
} | |
function yearsGameEnd() public view returns(bool){ | |
if (now > yearGameend){ | |
return true; | |
} | |
return false; | |
} | |
function gameEnd() public view returns(bool){ | |
if (now > endtime){ | |
return true; | |
} | |
return false; | |
} | |
modifier checkGameTime(){ | |
require(!yearsGameEnd(),"1 years ago,game over"); | |
require(!gameEnd(),"game over"); | |
_; | |
} | |
modifier onlyStatusnot1(){ //不能是第一阶段. | |
require(gameStatus>1,"当前游戏在第一阶段."); | |
_; | |
} | |
modifier onlyVitalik(){ //不能是第一阶段. | |
require(gameStatus == 3,"请确保当前游戏在第三阶段."); | |
require(vitalikAddress != address(0),"彩蛋未激活."); | |
_; | |
} | |
modifier isWithinLimits(uint256 _eth) { | |
require(_eth >= 1000000000, "pocket lint: not a valid currency"); | |
_; | |
} | |
modifier isHuman() { | |
require(msg.sender == tx.origin, "Humans only"); | |
_; | |
} | |
function start() public onlyOwner{ | |
starttime = now; | |
yearGameend = starttime +365*24*60*60; | |
endtime = starttime + rndInit; | |
tokenAAddress = new SimpleToken("TokenA","TokenA"); | |
tokenBAddress = new SimpleToken("TokenB","TokenB"); | |
tokenCAddress = new SimpleToken("TokenC","TokenC"); | |
} | |
function _mint(SimpleToken tokenContract,uint256 value) private { | |
tokenContract.mint(msg.sender,value); | |
} | |
function calvalue(uint256 v) private returns(uint256){ | |
uint256 xx = v.mul(95).div(100); | |
totalToken = totalToken.add(xx); | |
if (gameStatus == 3){//在第三阶段.才会把钱丢到FOMO奖池 | |
FOMOETH = FOMOETH.add(v.mul(3).div(100)); //3%给FOMO奖池 | |
DEVTeamETH = DEVTeamETH.add(v.mul(2).div(100)); //2%给开发者 | |
}else{ | |
DEVTeamETH = DEVTeamETH.add(v.mul(5).div(100));//5%给开发者 | |
} | |
return xx; | |
} | |
function joinA()external payable isWithinLimits(msg.value) isHuman checkGameTime{ | |
uint256 xx = calvalue(msg.value); | |
teamA = teamA.add(xx); | |
if(teamA > toStatus2ETH){ | |
gameStatus = 2; | |
rndInit = 12 hours; | |
endtime = now.add(rndInit);//修改结束为12小时候后 | |
evilAddress = msg.sender; | |
} | |
check(); | |
_mint(tokenAAddress,xx); | |
} | |
function()external payable{ | |
require(1==2,"return"); | |
} | |
function joinB()external payable onlyStatusnot1 isWithinLimits(msg.value) isHuman checkGameTime{ | |
uint256 xx = calvalue(msg.value); | |
teamB = teamB.add(xx); | |
if (teamB > teamA ){ | |
gameStatus = 3; | |
FOMOAddress = msg.sender; | |
} | |
check(); | |
_mint(tokenBAddress,xx); | |
} | |
function joinC()external payable onlyVitalik isWithinLimits(msg.value) isHuman checkGameTime{ | |
uint256 xx = calvalue(msg.value); | |
teamC = teamC.add(xx); | |
check(); | |
_mint(tokenCAddress,xx); | |
} | |
function vitalik() external payable isHuman checkGameTime { | |
require(isVitalik==1, "彩蛋只能激活一次."); | |
isVitalik = 0; | |
require(msg.value>=toVitalikETH, "你发送的ETH不够."); | |
require(gameStatus == 3,"请确保当前游戏在第三阶段."); | |
vitalikAddress = msg.sender; | |
uint256 xx = calvalue(msg.value); | |
teamC = teamC.add(xx); | |
check(); | |
_mint(tokenCAddress,xx); | |
} | |
function check() private{ | |
if(gameStatus == 2){ | |
if (teamB < teamA){ | |
if (winTeam != 2){ | |
winTeam =2; | |
endtime = now.add(rndInit); | |
} | |
} | |
} | |
if (gameStatus == 3){ | |
if (vitalikAddress == address(0)){ //only 2 team | |
if (teamA > teamB){ | |
if (winTeam != 1){ | |
winTeam = 1; | |
endtime = now.add(rndInit); | |
FOMOAddress = msg.sender; | |
} | |
}else{ | |
if (winTeam != 2){ | |
winTeam = 2; | |
endtime = now.add(rndInit); | |
FOMOAddress = msg.sender; | |
} | |
} | |
}else{ //3 team | |
uint8 _winTeam = getWinTeam(); | |
if (_winTeam != winTeam){ | |
winTeam = _winTeam; | |
endtime = now.add(rndInit); | |
FOMOAddress = msg.sender; | |
} | |
} | |
} | |
} | |
function getWinTeam() view public returns(uint8){ | |
uint256 max; | |
if (teamA > teamB) { | |
max = teamA; | |
} else { | |
max = teamB; | |
} | |
if (max < teamC) { | |
max = teamC; | |
return 3; | |
}else{ | |
if (max == teamA){ | |
return 1; | |
}else{ | |
return 2; | |
} | |
} | |
} | |
function fomoWithdraw() public{ | |
require(msg.sender == FOMOAddress,"需要FOMO玩家才能提取"); | |
require(gameEnd(),"游戏还没有结束"); | |
msg.sender.transfer(FOMOETH); | |
FOMOETH = 0; | |
} | |
function receiveApproval(address payable from, uint256 _amount, SimpleToken tokenContract, uint8 a) public{ | |
require(tokenContract.transferFrom(from, address(this), _amount),"gogogo"); //先拿到代币 | |
if (a == 0){ | |
if (tokenContract == tokenAAddress || tokenContract ==tokenBAddress || tokenContract ==tokenCAddress){ | |
require(timeEndWithdraw(from,_amount),"go"); | |
} | |
} | |
if (a == 1){ | |
require(gameEndWithdraw(from,_amount,tokenContract),"go"); | |
} | |
} | |
function timeEndWithdraw(address payable from,uint256 amount) private returns(bool){// 1年后解锁Token | |
require(yearsGameEnd(),"时间到期解锁ETH"); | |
address(from).transfer(amount); | |
return true; | |
} | |
function gameEndWithdraw(address payable from,uint256 amount,SimpleToken tokenContract) private returns (bool){ //游戏结束解锁 | |
require(gameEnd(),"游戏还没有结束"); | |
uint256 teamx; | |
if (winTeam == 1){ | |
require(tokenContract == tokenAAddress,"你没有赢."); | |
teamx = teamA; | |
}else if (winTeam == 2){ | |
require(tokenContract == tokenBAddress,"你没有赢."); | |
teamx = teamB; | |
}else { | |
require(tokenContract == tokenCAddress,"你没有赢."); | |
teamx = teamC; | |
} | |
uint256 winAmount = amount.div(teamx).mul(totalToken.add(DEVSendETH)); | |
require(winAmount > 0,"go"); | |
address(from).transfer(winAmount); | |
return true; | |
} | |
function DEVwithdraw() public onlyOwner{ | |
msg.sender.transfer(DEVTeamETH); | |
DEVTeamETH = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment