contract EDEX is StandardToken{

    // public variables of the token

    string public name = "Equadex";
    string public symbol = "EDEX";
    uint256 public decimals = 18;
    
    // reachable if max amount raised
    uint256 public maxSupply = 100000000e18;
    
    // ICO starting and ending blocks, can be changed as needed
    uint256 public icoStartBlock;
    // icoEndBlock = icoStartBlock + 345,600 blocks for 2 months ICO
    uint256 public icoEndBlock;

    // set the wallets with different levels of authority
    address public mainWallet;
    address public secondaryWallet;
    
    // time to wait between secondaryWallet price updates, mainWallet can update without restrictions
    uint256 public priceUpdateWaitingTime = 1 hours;

    uint256 public previousUpdateTime = 0;
    
    // strucure of price
    PriceEDEX public currentPrice;
    uint256 public minInvestment = 0.01 ether;
    
    // for tokens allocated to the team
    address public grantVestedEDEXContract;
    bool private grantVestedEDEXSet = false;
    
    // halt the crowdsale should any suspicious behavior of a third-party be identified
    // tokens will be locked for trading until they are listed on exchanges
    bool public haltICO = false;
    bool public setTrading = false;

    // maps investor address to a liquidation request
    mapping (address => Liquidation) public liquidations;
    // maps previousUpdateTime to the next price
    mapping (uint256 => PriceEDEX) public prices;
    // maps verified addresses
    mapping (address => bool) public verified;

    event Verification(address indexed investor);
    event LiquidationCall(address indexed investor, uint256 amountTokens);
    event Liquidations(address indexed investor, uint256 amountTokens, uint256 etherAmount);
    event Buy(address indexed investor, address indexed beneficiary, uint256 ethValue, uint256 amountTokens);
    event PrivateSale(address indexed investor, uint256 amountTokens);
    event PriceEDEXUpdate(uint256 topInteger, uint256 bottomInteger);
    event AddLiquidity(uint256 etherAmount);
    event RemoveLiquidity(uint256 etherAmount);
    
    // for price updates as a rational number
    struct PriceEDEX{
        uint256 topInteger;
        uint256 bottomInteger;
    }

    struct Liquidation{
        uint256 tokens;
        uint256 time;
    }

    // grantVestedEDEXContract and mainWallet can transfer to allow team allocations
    modifier isSetTrading{
        require(setTrading || msg.sender == mainWallet || msg.sender == grantVestedEDEXContract);
        _;
    }

    modifier onlyVerified{
        require(verified[msg.sender]);
        _;
    }

    modifier onlyMainWallet{
        require(msg.sender == mainWallet);
        _;
    }

    modifier onlyControllingWallets{
        require(msg.sender == secondaryWallet || msg.sender == mainWallet);
        _;
    }

    modifier only_if_secondaryWallet{
        if (msg.sender == secondaryWallet) _;
    }
    modifier require_waited{
        require(safeSub(now, priceUpdateWaitingTime) >= previousUpdateTime);
        _;
    }
    modifier only_if_increase (uint256 newTopInteger){
        if (newTopInteger > currentPrice.topInteger) _;
    }

    function EDEX(address secondaryWalletInput, uint256 priceTopIntegerInput, uint256 startBlockInput, uint256 endBlockInput){
        require(secondaryWalletInput != address(0));
        require(endBlockInput > startBlockInput);
        require(priceTopIntegerInput > 0);
        mainWallet = msg.sender;
        secondaryWallet = secondaryWalletInput;
        verified[mainWallet] = true;
        verified[secondaryWallet] = true;
        // priceTopIntegerInput = 800,000 for 1 ETH = 800 EDEX
        currentPrice = PriceEDEX(priceTopIntegerInput, 1000);
        // icoStartBlock should be around block number 5,709,200 = June 1st 2018
        icoStartBlock = startBlockInput;
        // icoEndBlock = icoStartBlock + 345,600 blocks
        icoEndBlock = endBlockInput;
        previousUpdateTime = now;
    }

    function setGrantVestedEDEXContract(address grantVestedEDEXContractInput) external onlyMainWallet{
        require(grantVestedEDEXContractInput != address(0));
        grantVestedEDEXContract = grantVestedEDEXContractInput;
        verified[grantVestedEDEXContract] = true;
        grantVestedEDEXSet = true;
    }

    function updatePriceEDEX(uint256 newTopInteger) external onlyControllingWallets{
        require(newTopInteger > 0);
        require_limited_change(newTopInteger);
        currentPrice.topInteger = newTopInteger;
        // maps time to new PriceEDEX
        prices[previousUpdateTime] = currentPrice;
        previousUpdateTime = now;
        PriceEDEXUpdate(newTopInteger, currentPrice.bottomInteger);
    }

    function require_limited_change (uint256 newTopInteger) private only_if_secondaryWallet require_waited only_if_increase(newTopInteger){
        uint256 percentage_diff = 0;
        percentage_diff = safeMul(newTopInteger, 100) / currentPrice.topInteger;
        percentage_diff = safeSub(percentage_diff, 100);
        // secondaryWallet can increase price by 20% maximum once every priceUpdateWaitingTime
        require(percentage_diff <= 20);
    }

    function updatePriceBottomInteger(uint256 newBottomInteger) external onlyMainWallet{
        require(block.number > icoEndBlock);
        require(newBottomInteger > 0);
        currentPrice.bottomInteger = newBottomInteger;
        // maps time to new Price
        prices[previousUpdateTime] = currentPrice;
        previousUpdateTime = now;
        PriceEDEXUpdate(currentPrice.topInteger, newBottomInteger);
    }

    function tokenAllocation(address investor, uint256 amountTokens) private{
        require(grantVestedEDEXSet);
        // the 15% allocated to the team
        uint256 teamAllocation = safeMul(amountTokens, 1764705882352941) / 1e16;
        uint256 newTokens = safeAdd(amountTokens, teamAllocation);
        require(safeAdd(totalSupply, newTokens) <= maxSupply);
        totalSupply = safeAdd(totalSupply, newTokens);
        balances[investor] = safeAdd(balances[investor], amountTokens);
        balances[grantVestedEDEXContract] = safeAdd(balances[grantVestedEDEXContract], teamAllocation);
    }

    function privateSaleTokens(address investor, uint amountTokens) external onlyMainWallet{
        require(block.number < icoEndBlock);
        require(investor != address(0));
        verified[investor] = true;
        tokenAllocation(investor, amountTokens);
        Verification(investor);
        PrivateSale(investor, amountTokens);
    }

    function verifyInvestor(address investor) external onlyControllingWallets{
        verified[investor] = true;
        Verification(investor);
    }
    
    // blacklists bot addresses using ICO whitelisted addresses
    function removeVerifiedInvestor(address investor) external onlyControllingWallets{
        verified[investor] = false;
        Verification(investor);
    }

    function buy() external payable{
        buyTo(msg.sender);
    }

    function buyTo(address investor) public payable onlyVerified{
        require(!haltICO);
        require(investor != address(0));
        require(msg.value >= minInvestment);
        require(block.number >= icoStartBlock && block.number < icoEndBlock);
        uint256 icoBottomInteger = icoBottomIntegerPrice();
        uint256 tokensToBuy = safeMul(msg.value, currentPrice.topInteger) / icoBottomInteger;
        tokenAllocation(investor, tokensToBuy);
        // send ether to mainWallet
        mainWallet.transfer(msg.value);
        Buy(msg.sender, investor, msg.value, tokensToBuy);
    }

    // bonus scheme during ICO, 1 ETH = 800 EDEX for 1st 20 days, 1 ETH = 727 EDEX for 2nd 20 days, 1 ETH = 667 EDEX for 3rd 20 days
    function icoBottomIntegerPrice() public constant returns (uint256){
        uint256 icoDuration = safeSub(block.number, icoStartBlock);
        uint256 bottomInteger;
        // icoDuration < 115,200 blocks = 20 days
        if (icoDuration < 115200){
            return currentPrice.bottomInteger;
        }
        // icoDuration < 230,400 blocks = 40 days
        else if (icoDuration < 230400 ){
            bottomInteger = safeMul(currentPrice.bottomInteger, 110) / 100;
            return bottomInteger;
        }
        else{
            bottomInteger = safeMul(currentPrice.bottomInteger, 120) / 100;
            return bottomInteger;
        }
    }

    // change ICO starting date if more time needed for preparation
    function changeIcoStartBlock(uint256 newIcoStartBlock) external onlyMainWallet{
        require(block.number < icoStartBlock);
        require(block.number < newIcoStartBlock);
        icoStartBlock = newIcoStartBlock;
    }

    function changeIcoEndBlock(uint256 newIcoEndBlock) external onlyMainWallet{
        require(block.number < icoEndBlock);
        require(block.number < newIcoEndBlock);
        icoEndBlock = newIcoEndBlock;
    }

    function changePriceUpdateWaitingTime(uint256 newPriceUpdateWaitingTime) external onlyMainWallet{
        priceUpdateWaitingTime = newPriceUpdateWaitingTime;
    }

    function requestLiquidation(uint256 amountTokensToLiquidate) external isSetTrading onlyVerified{
        require(block.number > icoEndBlock);
        require(amountTokensToLiquidate > 0);
        address investor = msg.sender;
        require(balanceOf(investor) >= amountTokensToLiquidate);
        require(liquidations[investor].tokens == 0);
        balances[investor] = safeSub(balances[investor], amountTokensToLiquidate);
        liquidations[investor] = Liquidation({tokens: amountTokensToLiquidate, time: previousUpdateTime});
        LiquidationCall(investor, amountTokensToLiquidate);
    }

    function liquidate() external{
        address investor = msg.sender;
        uint256 tokens = liquidations[investor].tokens;
        require(tokens > 0);
        uint256 requestTime = liquidations[investor].time;
        // obtain the next price that was set after the request
        PriceEDEX storage price = prices[requestTime];
        require(price.topInteger > 0);
        uint256 liquidationValue = safeMul(tokens, price.bottomInteger) / price.topInteger;
        // if there is enough ether on the contract, proceed. Otherwise, send back tokens
        liquidations[investor].tokens = 0;
        if (this.balance >= liquidationValue)
            enact_liquidation_greater_equal(investor, liquidationValue, tokens);
        else
            enact_liquidation_less(investor, liquidationValue, tokens);
    }

    function enact_liquidation_greater_equal(address investor, uint256 liquidationValue, uint256 tokens) private{
        assert(this.balance >= liquidationValue);
        balances[mainWallet] = safeAdd(balances[mainWallet], tokens);
        investor.transfer(liquidationValue);
        Liquidations(investor, tokens, liquidationValue);
    }
    
    function enact_liquidation_less(address investor, uint256 liquidationValue, uint256 tokens) private{
        assert(this.balance < liquidationValue);
        balances[investor] = safeAdd(balances[investor], tokens);
        Liquidations(investor, tokens, 0);
    }

    function checkLiquidationValue(uint256 amountTokensToLiquidate) constant returns (uint256 etherValue){
        require(amountTokensToLiquidate > 0);
        require(balanceOf(msg.sender) >= amountTokensToLiquidate);
        uint256 liquidationValue = safeMul(amountTokensToLiquidate, currentPrice.bottomInteger) / currentPrice.topInteger;
        require(this.balance >= liquidationValue);
        return liquidationValue;
    }

    // add liquidity to contract for investor liquidation
    function addLiquidity() external onlyControllingWallets payable{
        require(msg.value > 0);
        AddLiquidity(msg.value);
    }

    // remove liquidity from contract
    function removeLiquidity(uint256 amount) external onlyControllingWallets{
        require(amount <= this.balance);
        mainWallet.transfer(amount);
        RemoveLiquidity(amount);
    }

    function changeMainWallet(address newMainWallet) external onlyMainWallet{
        require(newMainWallet != address(0));
        mainWallet = newMainWallet;
    }

    function changeSecondaryWallet(address newSecondaryWallet) external onlyMainWallet{
        require(newSecondaryWallet != address(0));
        secondaryWallet = newSecondaryWallet;
    }

    function enableTrading() external onlyMainWallet{
        require(block.number > icoEndBlock);
        setTrading = true;
    }

    function claimEDEX(address _token) external onlyMainWallet{
        require(_token != address(0));
        Token token = Token(_token);
        uint256 balance = token.balanceOf(this);
        token.transfer(mainWallet, balance);
     }

    // disable transfers and allow them once token is tradeable
    function transfer(address _to, uint256 _value) isSetTrading returns (bool success){
        return super.transfer(_to, _value);
    }
    function transferFrom(address _from, address _to, uint256 _value) isSetTrading returns (bool success){
        return super.transferFrom(_from, _to, _value);
    }

    function haltICO() external onlyMainWallet{
        haltICO = true;
    }
    
    function unhaltICO() external onlyMainWallet{
        haltICO = false;
    }
    
    // fallback function
    function() payable{
        require(tx.origin == msg.sender);
        buyTo(msg.sender);
    }
}
function icoBottomIntegerPrice() public constant returns (uint256){
    uint256 icoDuration = safeSub(block.number, icoStartBlock);
    uint256 bottomInteger;
    // icoDuration < 115,200 blocks = 20 days
    if (icoDuration < 115200){
        return currentPrice.bottomInteger;
    }
    // icoDuration < 230,400 blocks = 40 days
    else if (icoDuration < 230400 ){
        bottomInteger = safeMul(currentPrice.bottomInteger, 110) / 100;
        return bottomInteger;
    }
    else{
        bottomInteger = safeMul(currentPrice.bottomInteger, 120) / 100;
        return bottomInteger;
    }
}

// bonus scheme during ICO, 1 ETH = 800 EDEX for 1st 20 days, 1 ETH = 727 EDEX for 2nd 20 days, 1 ETH = 667 EDEX for 3rd 20 days