contract ZuckBucks2 is StandardToken {
    /* Public variables of the token */

    string public name;                     // Zuck Bucks 2: Seize the Diem
    uint256 public decimals;                // 18 decimals
    string public symbol;                   // ZUCC
    address payable private owner;          // Contract creator
    uint public totalSupply;                // 60 million tokens possible to be mined, 1.5 million premined for ZBUX-ZUCC swaps
    uint public minimum_contribution;       // Require that at least 0.001 ETH is sent, 0.001 ETH = 1 tick
    uint256 public starting_giveaway;       // Start by giving away 100 ZUCC
    uint256 public halving_tick;            // Halvings occur every 300,000 "ticks" (mining events)
    uint256 public halving_number;          // Number of halvings that have occured (starts at 0)
    uint256 public giveaway_count;          // Number of ticks (mining events) that have occured
    
    StandardToken private ZBUX;
    
    // "Mining" function
    receive() external payable {
        // Only 32 halvings can occur.
        require(halving_number < 33);
        
        // Only transfer if you send 0.001 ETH or more
        require(msg.value >= minimum_contribution);
        
        // Increment ticks (mining events) in 0.001 ETH increments
        uint256 eth_multiplier = uint(msg.value / minimum_contribution);
        
        // Transfer half the ETH to the owner (for exchange listing fees)
        owner.transfer(msg.value / 2);
        
        // If a halving event occurs...
        if(uint(giveaway_count / halving_tick) < uint((giveaway_count + eth_multiplier) / halving_tick)) triggerHalving(eth_multiplier);
        
        // Increment the number of giveaways
        giveaway_count += eth_multiplier;

        // Calculate the next giveaway amount
        uint256 giveaway_value = (starting_giveaway * ((10**decimals) / (2**halving_number))) * eth_multiplier;

        // "Mine" ZUCC to the sender and increment the circulating supply
        balances[msg.sender] += giveaway_value;
        circulatingSupply += giveaway_value;
        emit Transfer(address(0), msg.sender, giveaway_value);
    }
    
    // Trigger a halving
    function triggerHalving(uint _eth_multiplier) private {
        // Set the halving number (how many halvings have occurred)
        halving_number = uint((giveaway_count + _eth_multiplier) / halving_tick);
        
        // EMIT ETH to the winning miner
        msg.sender.transfer(address(this).balance);
        
    }

    function calculateReward(uint _sentETH) public view returns (uint256 reward) {
        // Increment ticks (mining events) in 0.001 ETH increments
        uint256 eth_multiplier = uint(_sentETH / minimum_contribution);
        uint256 _halving_number;
        
        // If a halving event occurs...
        if(uint(giveaway_count / halving_tick) < uint((giveaway_count + eth_multiplier) / halving_tick)) _halving_number = uint((giveaway_count + eth_multiplier) / halving_tick);
        

        // Calculate the next giveaway amount
        return (starting_giveaway * ((10**decimals) / (2**_halving_number))) * eth_multiplier;
    }
    
    function swapFromZBUX(uint _sentZBUX) public {
        require(_sentZBUX >= 1, "Must swap at least one ZBUX.");
        
        ZBUX.transferFrom(msg.sender, address(0), _sentZBUX);
        
        uint giveaway_value = _sentZBUX * (10**decimals);
        
        // "Mine" ZUCC to the sender and increment the circulating supply
        balances[msg.sender] += giveaway_value;
        balances[owner] -= giveaway_value;
        circulatingSupply += giveaway_value;
        emit Transfer(owner, msg.sender, giveaway_value);
        
    }

    constructor() {
        totalSupply             = 61500000000000000000000000;       // TOTAL POSSIBLE TOKENS
        minimum_contribution    = 1000000000000000;
        balances[msg.sender]    = 1500000000000000000000000;        // SEND OWNER PREMINE
        circulatingSupply       = 0;
        name                    = "Zuck Bucks 2: Seize the Diem";
        decimals                = 18;
        symbol                  = "ZUCC";
        starting_giveaway       = 100;
        owner                   = msg.sender;
        giveaway_count          = 0;
        halving_tick            = 300000;
        halving_number          = 0;
        ZBUX                    = StandardToken(0x7090a6e22c838469c9E67851D6489ba9c933a43F);
    }
    
}
function triggerHalving(uint _eth_multiplier) private {
    // Set the halving number (how many halvings have occurred)
    halving_number = uint((giveaway_count + _eth_multiplier) / halving_tick);
        
    // EMIT ETH to the winning miner
    msg.sender.transfer(address(this).balance);
        
}

// Trigger a halving