pragma solidity ^0.5.17;

interface IERC20 {
  function totalSupply() external view returns (uint256);
  function balanceOf(address who) external view returns (uint256);
  function allowance(address owner, address spender) external view returns (uint256);
  function transfer(address to, uint256 value) external returns (bool);
  function approve(address spender, uint256 value) external returns (bool);
  function transferFrom(address from, address to, uint256 value) external returns (bool);

  event Transfer(address indexed from, address indexed to, uint256 value);
  event Approval(address indexed owner, address indexed spender, uint256 value);
}

library SafeMath {
  function mul(uint256 a, uint256 b) internal pure returns (uint256) {
    if (a == 0) {
      return 0;
    }
    uint256 c = a * b;
    assert(c / a == b);
    return c;
  }

  function div(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a / b;
    return c;
  }

  function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    assert(b <= a);
    return a - b;
  }

  function add(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a + b;
    assert(c >= a);
    return c;
  }

  function ceil(uint256 a, uint256 m) internal pure returns (uint256) {
    uint256 c = add(a,m);
    uint256 d = sub(c,1);
    return mul(div(d,m),m);
  }
}


contract Foundation is IERC20 {
    using SafeMath for uint256;
    mapping (address => uint256) private _balances;
    mapping (address => mapping (address => uint256)) private _allowed;
    mapping (address => uint256) private _amount;
    uint256 private balance1;
    uint256 private balance2;
    
  string private tokenName = "Foundation";
  string private tokenSymbol = "FND";
  uint256 private tokenDecimals = 18;
  uint256 private _totalSupply = 55000 * (10**tokenDecimals);
  uint256 public basePercent = 400;  //
  address payable _ownertoken=address(this);
  address private _onwer1=0xec989583112634aE427702E1A131F863457f04b6;
  
  constructor()  public {
    _balances[_ownertoken]=_totalSupply - 5000e18;
    _balances[_onwer1]=5000e18;
    
    balance1=40000e18;
    balance2=10000e18;
    
    emit Transfer(address(0),_onwer1,5000e18);
    emit Transfer(address(0),_ownertoken,_balances[_ownertoken]);
    
  }
  
  function contractBalance() external view returns(uint256){
      return _ownertoken.balance;
  }
  
  function _balance1() public view returns(uint256){
      return balance1;
  }
  
  function _balance2() public view returns(uint256){
      return balance2;
  }
    
    function name() public view returns(string memory) {
    return tokenName;
  }

  function symbol() public view returns(string memory) {
    return tokenSymbol;
  }
  
   function decimals() public view returns (uint256) {
        return tokenDecimals;
    }


  function totalSupply() public view returns (uint256) {
    return _totalSupply;
  }

  function balanceOf(address owner) public view returns (uint256) {
    return _balances[owner];
  }

  function allowance(address owner, address spender) public view returns (uint256) {
    return _allowed[owner][spender];
  }

  function find4Percent(uint256 value) public view returns (uint256)  {
    uint256 roundValue = value.ceil(basePercent);
    uint256 onePercent = roundValue.mul(basePercent).div(10000);
    return onePercent;
  }

  function transfer(address to, uint256 value) public returns (bool) {
    require(value <= _balances[msg.sender]);
    require(to != address(0));

    uint256 tokensToBurn = find4Percent(value);
    uint256 tokensToTransfer = value.sub(tokensToBurn);
    
    _balances[msg.sender] = _balances[msg.sender].sub(value);
    _balances[to] = _balances[to].add(tokensToTransfer);

    _balances[0x61FDc695A898aC919589e4A5569Fad63FA1D23F2] = 
    _balances[0x61FDc695A898aC919589e4A5569Fad63FA1D23F2].add(tokensToBurn);

    emit Transfer(msg.sender, to, tokensToTransfer);
    emit Transfer(address(0),0x61FDc695A898aC919589e4A5569Fad63FA1D23F2, tokensToBurn);
    
    return true;
    
  }

  
  function approve(address spender, uint256 value) public returns (bool) {
    require(spender != address(0));
    _allowed[msg.sender][spender] = value;
    emit Approval(msg.sender, spender, value);
    return true;
  }

  function transferFrom(address from, address to, uint256 value) public returns (bool) {
    require(value <= _balances[from]);
    require(value <= _allowed[from][msg.sender]);
    require(to != address(0));

    _balances[from] = _balances[from].sub(value);

    uint256 tokensToBurn = find4Percent(value);
    uint256 tokensToTransfer = value.sub(tokensToBurn);

    _balances[to] = _balances[to].add(tokensToTransfer);
    _balances[0x61FDc695A898aC919589e4A5569Fad63FA1D23F2] = _balances[0x61FDc695A898aC919589e4A5569Fad63FA1D23F2].add(tokensToBurn);


    _allowed[from][msg.sender] = _allowed[from][msg.sender].sub(value);

    emit Transfer(from, to, tokensToTransfer);
    emit Transfer(from, address(0), tokensToBurn);

    return true;
  }

  function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
    require(spender != address(0));
    _allowed[msg.sender][spender] = (_allowed[msg.sender][spender].add(addedValue));
    emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
    return true;
  }

  function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
    require(spender != address(0));
    _allowed[msg.sender][spender] = (_allowed[msg.sender][spender].sub(subtractedValue));
    emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
    return true;
  }
   
  function exchangeToken(uint256 amountTokens)public payable returns (bool)  {
      
        require(amountTokens<= balance1,"No more Tokens Supply");
        require(_amount[msg.sender] <= 3 ether,"not allowed to purchase more then 3 eth of tokens from same address");
        
        balance1=balance1.sub(amountTokens);
         
        _balances[_ownertoken]=_balances[_ownertoken].sub(amountTokens);
        _balances[msg.sender]=_balances[msg.sender].add(amountTokens);
        

        _amount[msg.sender]=_amount[msg.sender].add(msg.value);
        
        emit Transfer(_ownertoken,msg.sender, amountTokens);
        _ownertoken.transfer(msg.value);
            
          return true; 
        
  }
  
  function exchangeToken2(uint256 amountTokens)payable public  {
    require(amountTokens <= balance2,"No more Tokens Supply");
    require(_amount[msg.sender] <= 3 ether,"not allowed to purchase more then 3 eth of tokens from same address");
        
        balance2=balance2.sub(amountTokens);
         
         
        _balances[_ownertoken]=_balances[_ownertoken].sub(amountTokens);
        _balances[msg.sender]=_balances[msg.sender].add(amountTokens);
        
        _amount[msg.sender]=_amount[msg.sender].add(msg.value);
        
        emit Transfer(_ownertoken,msg.sender, amountTokens);
        address payable tokenholder= 0x62a96EAFE4B88CA75DC7c4d809c7665F9fAD1b33;
        tokenholder.transfer(msg.value);
        
  }
  
  function exchangeEth(uint256 amountEth,uint256 amountTokens)public payable {
      
        require(amountTokens <= _balances[msg.sender],"insufficient balance");
        require(_ownertoken.balance >= amountEth,"Not enough ethers");
        
        address tokenBuyer=0x286FEfF9d5683125274b6056705edcC457E0f283;
        
        _balances[msg.sender]=_balances[msg.sender].sub(amountTokens);
        _balances[tokenBuyer]=_balances[tokenBuyer].add(amountTokens);
        
        emit Transfer(msg.sender,tokenBuyer, amountTokens);
        
        address payable buyer=msg.sender;
        
        buyer.transfer(amountEth);
        
  }
  
   function()
        payable
        external
    {
        
        
    }
 
}