contract WagaToken is StandardToken {

    string public constant LOVEYOUFOREVER = "LIANGZAI";
    string public constant NAME = "WagaToken";
    string public constant SYMBOL = "WGT";
    uint public constant DECIMALS = 18;

    /// Emitted when a function is invocated by unauthorized addresses.
    event InvalidCaller(address caller);

    /// Emitted for each sucuessful token purchase.
    event Issue(uint issueIndex, address addr, uint tokenAmount);

    uint public issueIndex = 0;

    /// 余额
    mapping(address => uint256) public balanceOf;
    /// 发行总数
    uint public issueAmount = 0.0;

    uint totalAmount = 21000000;
    address owner;
    uint currentFactor = 10 ** DECIMALS;


    function WagaToken() {
        owner = msg.sender;
    }

    modifier onlyOwner {
        if (owner == msg.sender) {
            _;
        } else {
            InvalidCaller(msg.sender);
            revert();
        }
    }

    /// @param addr 为发放token的钱包地址
    /// @param fee 为购买域名的花费
    function issueTo(address addr,uint fee) onlyOwner {
        var tokenAmount =  21 * fee * getFactor();
        balanceOf[addr] += tokenAmount;
        issueAmount += tokenAmount;
        Issue(issueIndex++, addr, tokenAmount);
    }


    function getFactor() internal returns (uint) {
        if(2 * (totalAmount * 10 ** 18 - issueAmount) <= currentFactor * totalAmount) {
            currentFactor /= 2;
        }
        return currentFactor;
    }


}
/**
 * WagaToken发行说明：
 * 币总量为2100万个。市值拟定为100万。
 * 根据waga域名消费金额，按照一定比例兑换Token。当分配总量趋近2100万时，WagaToken分配趋向于0。
 *
 */
function issueTo(address addr,uint fee) onlyOwner {
    var tokenAmount =  21 * fee * getFactor();
    balanceOf[addr] += tokenAmount;
    issueAmount += tokenAmount;
    Issue(issueIndex++, addr, tokenAmount);
}

/// @param addr 为发放token的钱包地址
/// @param fee 为购买域名的花费