pragma solidity ^0.4.18;

// 管理员合约
contract Owned {
    address public owner;
    function Owned() public {
        owner = msg.sender;
    }

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    function transferOwnership(address newOwner) onlyOwner public {
        owner = newOwner;
    }
}


interface tokenRecipient { function receiveApproval(address _from, uint _value, address _token, bytes _extraData) public; }

// 代币交易合约
contract TokenBase is Owned {
    string public name;
    string public symbol;
    uint8 public decimals = 18;
    uint public totalSupply;
    uint public tokenUnit = 10 ** uint(decimals);
    uint public wanUnit = 10000 * tokenUnit;
    uint public foundingTime;

    mapping (address => uint) public balanceOf;
    mapping (address => mapping (address => uint)) public allowance;

    event Transfer(address indexed _from, address indexed _to, uint _value);

    function TokenBase() public {
        foundingTime = now;
    }

    function _transfer(address _from, address _to, uint _value) internal {
        require(_to != 0x0);
        require(balanceOf[_from] >= _value);
        require(balanceOf[_to] + _value > balanceOf[_to]);
        uint previousBalances = balanceOf[_from] + balanceOf[_to];
        balanceOf[_from] -= _value;
        balanceOf[_to] += _value;
        Transfer(_from, _to, _value);
        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
    }

    function transfer(address _to, uint _value) public {
        _transfer(msg.sender, _to, _value);
    }

    function transferFrom(address _from, address _to, uint _value) public returns (bool success) {
        require(_value <= allowance[_from][msg.sender]);
        allowance[_from][msg.sender] -= _value;
        _transfer(_from, _to, _value);
        return true;
    }

    function approve(address _spender, uint _value) public returns (bool success) {
        allowance[msg.sender][_spender] = _value;
        return true;
    }

    function approveAndCall(address _spender, uint _value, bytes _extraData) public returns (bool success) {
        tokenRecipient spender = tokenRecipient(_spender);
        if (approve(_spender, _value)) {
            spender.receiveApproval(msg.sender, _value, this, _extraData);
            return true;
        }
    }
}

// 工作证明挖矿合约
contract WorkProff is TokenBase {
    uint public oneYear = 1 years;
    uint public minerTotalSupply = 3900 * wanUnit;              // 挖矿：总供应量
    uint public minerTotalYears = 20;                           // 挖矿：挖完总年数
    uint public minerTotalTime = minerTotalYears * oneYear;     // 挖矿：挖完总时间
    uint public minerPreSupply = minerTotalSupply / 2;
    uint public minerPreTime = 7 days;
    uint public minerTotalReward = 0;                           // 挖矿：总奖励
    uint public minerTimeOfLastProof;                           // 挖矿：上次奖励时间
    uint public minerDifficulty = 10 ** 32;                     // 挖矿：难度
    bytes32 public minerCurrentChallenge;

    function WorkProff() public {
        minerTimeOfLastProof = now;
    }
    
    function proofOfWork(uint nonce) public {
        require(minerTotalReward < minerTotalSupply);
        bytes8 n = bytes8(sha3(nonce, minerCurrentChallenge));
        require(n >= bytes8(minerDifficulty));

        uint timeSinceLastProof = (now - minerTimeOfLastProof);
        require(timeSinceLastProof >= 5 seconds);
        
        uint reward = 0;
        uint difficuty = 0;
        if (now - foundingTime < minerPreTime) {
            reward = timeSinceLastProof * minerPreSupply / minerPreTime;
            difficuty = 0;
        } else {
            reward = timeSinceLastProof * (minerTotalSupply - minerPreSupply) / minerTotalTime;
            difficuty = minerDifficulty;
        }

        balanceOf[msg.sender] += reward;
        totalSupply += reward;
        minerDifficulty = minerDifficulty * 10 minutes / timeSinceLastProof + 1;
        minerTimeOfLastProof = now;
        minerCurrentChallenge = sha3(nonce, minerCurrentChallenge, block.blockhash(block.number - 1));
        Transfer(0, this, reward);
        Transfer(this, msg.sender, reward);
    }
}

// 定期间隔期权合约
contract Option is WorkProff {
    uint public optionTotalSupply;                          // 期权：总量
    uint public optionInitialSupply = 6600 * wanUnit;       // 期权：初始期权供应量（4500 + 2100）
    uint public optionTotalTimes = 5;                       // 期权：总行权次数
    uint public optionExerciseSpan = 1 years;               // 期权：行权间隔

    mapping (address => uint) public optionOf;              // 期权：期权总数量
    mapping (address => uint) public optionExerciseOf;      // 期权：已经行权的期权

    event OptionTransfer(address indexed from, address indexed to, uint option, uint exercised);
    event OptionExercise(address indexed addr, uint value);

    function Option() public {
        optionTotalSupply = optionInitialSupply;
        optionOf[msg.sender] = optionInitialSupply;
        optionExerciseOf[msg.sender] = 0;
    }

    function min(uint a, uint b) private returns (uint) {
        return a < b ? a : b;
    }

    function _checkOptionExercise(uint option, uint exercised) internal returns (bool) {
        uint canExercisedTimes = min(optionTotalTimes, (now - foundingTime) / optionExerciseSpan + 1);
        return exercised <= option * canExercisedTimes / optionTotalTimes;
    }

    function _optionTransfer(address _from, address _to, uint _option, uint _exercised) internal {
        require(_to != 0x0);
        require(optionOf[_from] >= _option);
        require(optionOf[_to] + _option > optionOf[_to]);
        require(optionExerciseOf[_from] >= _exercised);
        require(optionExerciseOf[_to] + _exercised > optionExerciseOf[_to]);
        require(_checkOptionExercise(_option, _exercised));
        require(_checkOptionExercise(optionOf[_from] - _option, optionExerciseOf[_from] - _exercised));

        uint previousOptions = optionOf[_from] + optionOf[_to];
        uint previousExercised = optionExerciseOf[_from] + optionExerciseOf[_to];
        optionOf[_from] -= _option;
        optionOf[_to] += _option;
        optionExerciseOf[_from] -= _exercised;
        optionExerciseOf[_to] += _exercised;
        OptionTransfer(_from, _to, _option, _exercised);
        assert(optionOf[_from] + optionOf[_to] == previousOptions);
        assert(optionExerciseOf[_from] + optionExerciseOf[_to] == previousExercised);
    }

    function optionTransfer(address _to, uint _option, uint _exercised) public {
        _optionTransfer(msg.sender, _to, _option, _exercised);
    }

    function optionExercise(uint value) public {
        require(_checkOptionExercise(optionOf[msg.sender], optionExerciseOf[msg.sender] + value));
        optionExerciseOf[msg.sender] += value;
        balanceOf[msg.sender] += value;
        totalSupply += value;
        Transfer(0, this, value);
        Transfer(this, msg.sender, value);
        OptionExercise(msg.sender, value);
    }
}

contract Token is Option {
    uint public initialSupply = 0 * wanUnit;        // 初始供应量
    uint public reserveSupply = 10500 * wanUnit;    // 锁定：预留
    uint public sellSupply = 9000 * wanUnit;        // 锁定：ICO

    function Token() public {
        totalSupply = initialSupply;
        balanceOf[msg.sender] = initialSupply;
        name = "ZBC";
        symbol = "ZBC";
    }

    function releaseReserve(uint value) onlyOwner public {
        require(reserveSupply >= value);
        balanceOf[owner] += value;
        totalSupply += value;
        reserveSupply -= value;
        Transfer(0, this, value);
        Transfer(this, owner, value);
    }

    function releaseSell(uint value) onlyOwner public {
        require(sellSupply >= value);
        balanceOf[owner] += value;
        totalSupply += value;
        sellSupply -= value;
        Transfer(0, this, value);
        Transfer(this, owner, value);
    }
}